1 """Deals with creating the interface between the wrapper and the socket.
3 Copyright (C) 2013, Joshua More and Michele Ceriotti
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http.//www.gnu.org/licenses/>.
20 InputInterface: Deals with creating the Interface object from a file, and
21 writing the checkpoints.
24 __all__
= [ 'InputInterfaceSocket' ]
26 import socket
, select
, threading
, signal
, string
, os
, time
28 from ipi
.utils
.messages
import verbosity
, warning
29 from ipi
.utils
.inputvalue
import *
30 from ipi
.interfaces
.sockets
import *
33 class InputInterfaceSocket(Input
):
34 """Interface input class.
36 Handles generating the apporopriate interface class from the xml
37 input file, and generating the xml checkpoin tags and data from an
38 instance of the object.
41 mode: A string giving the type of socket used.
42 pbc: A boolean giving whether the atom positions will be folded back
43 into the unit cell before being sent through the socket or not.
46 address: A string giving the host name.
47 port: An integer giving the port used by the socket.
48 slots: An integer giving the maximum allowed backlog of queued clients.
49 latency: A float giving the number of seconds that the interface waits
50 before updating the client list.
51 timeout: A float giving a number of seconds after which a calculation core
52 is considered dead. Defaults to zero, i.e. no timeout.
55 fields
= {"address": (InputValue
, {"dtype" : str,
56 "default" : "localhost",
57 "help" : "This gives the server address that the socket will run on." } ),
58 "port": (InputValue
, {"dtype" : int,
60 "help" : "This gives the port number that defines the socket."} ),
61 "slots": (InputValue
, {"dtype" : int,
63 "help" : "This gives the number of client codes that can queue at any one time."} ),
64 "latency": (InputValue
, {"dtype" : float,
66 "help" : "This gives the number of seconds between each check for new clients."} ),
67 "timeout": (InputValue
, {"dtype" : float,
69 "help" : "This gives the number of seconds before assuming a calculation has died. If 0 there is no timeout." } )}
70 attribs
= { "mode": (InputAttribute
, {"dtype" : str,
71 "options" : [ "unix", "inet" ],
73 "help" : "Specifies whether the driver interface will listen onto a internet socket [inet] or onto a unix socket [unix]." } ),
74 "pbc": ( InputAttribute
, { "dtype" : bool,
76 "help" : "Applies periodic boundary conditions to the atoms coordinates before passing them on to the driver code." })
79 default_help
= "Specifies the parameters for the socket interface."
80 default_label
= "INTERFACE"
82 def store(self
, iface
):
83 """Takes an Interface instance and stores a minimal representation of it.
86 iface: An interface object.
89 super(InputInterfaceSocket
,self
).store(iface
)
90 self
.latency
.store(iface
.latency
)
91 self
.mode
.store(iface
.mode
)
92 self
.address
.store(iface
.address
)
93 self
.port
.store(iface
.port
)
94 self
.slots
.store(iface
.slots
)
95 self
.timeout
.store(iface
.timeout
)
96 self
.pbc
.store(iface
.dopbc
)
99 """Creates an InterfaceSocket object.
102 An interface object with the appropriate socket given the attributes
103 of the InputInterfaceSocket object.
106 super(InputInterfaceSocket
,self
).fetch()
107 return InterfaceSocket(address
=self
.address
.fetch(), port
=self
.port
.fetch(),
108 slots
=self
.slots
.fetch(), mode
=self
.mode
.fetch(),
109 latency
=self
.latency
.fetch(), timeout
=self
.timeout
.fetch(), dopbc
=self
.pbc
.fetch())
112 """Function that deals with optional arguments."""
114 super(InputInterfaceSocket
,self
).check()
115 if self
.port
.fetch() < 1 or self
.port
.fetch() > 65535:
116 raise ValueError("Port number " + str(self
.port
.fetch()) + " out of acceptable range.")
117 elif self
.port
.fetch() < 1025:
118 warning("Low port number being used, this may interrupt important system processes.", verbosity
.low
)
120 if self
.slots
.fetch() < 1 or self
.slots
.fetch() > 5:
121 raise ValueError("Slot number " + str(self
.slots
.fetch()) + " out of acceptable range.")
122 if self
.latency
.fetch() < 0:
123 raise ValueError("Negative latency parameter specified.")
124 if self
.timeout
.fetch() < 0.0:
125 raise ValueError("Negative timeout parameter specified.")