21 from enum
import IntEnum
32 ON_SENDING_TIMEOUT = 20
33 ON_RECEIVING_TIMEOUT = 21
59 self.
_mHost_mHost =
'localhost'
61 self.
_mPort_mPort = 20000
if not 'MeVisLabTestCenterPort' in os.environ
else int(os.environ[
'MeVisLabTestCenterPort'])
91 def send (self, data, timeout):
94 self.
_handleError_handleError(Error.NOT_CONNECTED,
"Not connected.")
102 if not self.
_send_send(str(length).ljust(16), 16, timeout):
106 return self.
_send_send(data, length, 1)
114 def _send (self, data, length, timeout):
115 if not isinstance(data, bytes):
116 data = data.encode(
'UTF-8')
121 (rlist, wlist, xlist) = select.select([], [self.
_csocket_csocket], [], timeout)
122 if self.
_csocket_csocket
not in wlist:
123 self.
_handleError_handleError(Error.ON_SENDING_TIMEOUT,
"Connection error (sending).")
125 except select.error
as msg:
126 self.
_handleError_handleError(Error.ON_SENDING_1,
"Error sending: %s" % (msg))
134 self.
_handleError_handleError(Error.SENDING_BLOCKED,
"Error sending: couldn't send.")
137 except OSError
as msg:
138 self.
_handleError_handleError(Error.ON_SENDING_2,
"Error sending: %s" % (msg))
150 self.
_handleError_handleError(Error.NOT_CONNECTED,
"Not connected.")
154 data = self.
_recv_recv(16, timeout)
155 if data
in (b
'',
None):
160 data = self.
_recv_recv(length, 1)
161 if isinstance(data, bytes):
162 data = data.decode(
'UTF-8')
163 if data ==
"##[empty]":
174 def _recv (self, length, timeout):
176 while len(data) < length:
179 (rlist, wlist, xlist) = select.select([self.
_csocket_csocket], [], [], timeout)
180 if self.
_csocket_csocket
not in rlist:
181 self.
_handleError_handleError(Error.ON_RECEIVING_TIMEOUT,
"Connection error (receiving).")
183 except OSError
as msg:
184 self.
_handleError_handleError(Error.ON_RECEIVING_1,
"Error receiving: %s" % (msg))
190 if tdata
in (b
'',
None):
191 self.
_handleError_handleError(Error.EMPTY_MESSAGE,
"Error receiving: empty message received")
194 except OSError
as msg:
195 self.
_handleError_handleError(Error.ON_RECEIVING_2,
"Error receiving: %s" % (msg))
218 def _handleError (self, errorCode, errorMsg, silent=False):
237 self.
_msocket_msocket_msocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
238 self.
_msocket_msocket_msocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
245 print(f
"Port {self._mPort} was blocked, using {port} instead.")
251 if e.errno
in (errno.EADDRINUSE, errno.EADDRNOTAVAIL, 10013):
256 except OSError
as msg:
257 self.
_handleError_handleError(Error.ON_INIT,
"Socket error (server): %s " % (msg))
279 self.
_csocket_csocket.settimeout(timeout)
281 except OSError
as msg:
283 self.
_handleError_handleError(Error.ON_CONNECTING,
u"Error connecting (server): %s" % (msg), silent=
True)
296 except OSError
as msg:
297 self.
_handleError_handleError(Error.ON_DISCONNECTING,
"Error disconnecting: %s" % (msg), silent=
True)
303 '''Returns the timeout used by the Master while establishing the connection to the slave.'''
316 self.
_csocket_csocket_csocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
317 except OSError
as msg:
318 self.
_handleError_handleError(Error.ON_INIT,
"Socket error (client): %s" % (msg))
335 except OSError
as msg:
336 if sys.platform.startswith(
"win")
or msg.errno != errno.EINTR:
340 while poll.poll(-1) == -1:
341 if msg.errno != errno.EINTR:
342 print(
"poll() failed", file=sys.stderr)
345 error = self.
_csocket_csocket_csocket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
347 print(
"getsockopt() failed %s" % (error), file=sys.stderr)
351 self.
_csocket_csocket_csocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
353 except OSError
as msg:
354 self.
_handleError_handleError(Error.ON_CONNECTING,
"Error connecting (client): %s" % (msg))
366 except OSError
as msg:
367 self.
_handleError_handleError(Error.ON_DISCONNECTING,
"Error disconnecting: %s" % (msg), silent=
True)
def getConnectTimeout(self)
def __init__(self, connectTimeout=10)
Constructor initializing the master socket.
def connect(self, timeout=2)
Wait for a slave to connect and configure the connection.
def disconnect(self)
Unlink connection to the slave.
def __del__(self)
Destructor making sure the socket is deleted.
def disconnect(self)
Disconnect from master.
def __init__(self)
The slave's constructor.
def connect(self)
Connect to master and configure connection.
def __del__(self)
The slave's destructor.
The Communicator is the superclass for the communicating entities.
def send(self, data, timeout)
Send the data via the socket using the given timeout.
def getLastErrorWithMessage(self)
Get a tuple describing the last error.
def _recv(self, length, timeout)
def getLastError(self)
Get a tuple describing the last error.
def recv(self, timeout)
Receive data via the socket using the given timeout.
def isConnected(self)
Get the connection status of the IPC client.
def getPort(self)
Get the port used for connections.
def _handleError(self, errorCode, errorMsg, silent=False)
def _send(self, data, length, timeout)