21from enum
import IntEnum
32 ON_SENDING_TIMEOUT = 20
33 ON_RECEIVING_TIMEOUT = 21
61 self.
_mPort = 20000
if not 'MeVisLabTestCenterPort' in os.environ
else int(os.environ[
'MeVisLabTestCenterPort'])
91 def send (self, data, timeout):
102 if not self.
_send(str(length).ljust(16), 16, timeout):
106 return self.
_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], [], timeout)
123 self.
_handleError(Error.ON_SENDING_TIMEOUT,
"Connection error (sending).")
125 except select.error
as msg:
126 self.
_handleError(Error.ON_SENDING_1,
"Error sending: %s" % (msg))
134 self.
_handleError(Error.SENDING_BLOCKED,
"Error sending: couldn't send.")
137 except OSError
as msg:
138 self.
_handleError(Error.ON_SENDING_2,
"Error sending: %s" % (msg))
150 self.
_handleError(Error.NOT_CONNECTED,
"Not connected.")
154 data = self.
_recv(16, timeout)
155 if data
in (b
'',
None):
160 data = self.
_recv(length, 1)
161 if isinstance(data, bytes):
162 data = data.decode(
'UTF-8')
163 if data ==
"##[empty]":
176 while len(data) < length:
179 (rlist, wlist, xlist) = select.select([self.
_csocket], [], [], timeout)
181 self.
_handleError(Error.ON_RECEIVING_TIMEOUT,
"Connection error (receiving).")
183 except OSError
as msg:
184 self.
_handleError(Error.ON_RECEIVING_1,
"Error receiving: %s" % (msg))
190 if tdata
in (b
'',
None):
191 self.
_handleError(Error.EMPTY_MESSAGE,
"Error receiving: empty message received")
194 except OSError
as msg:
195 self.
_handleError(Error.ON_RECEIVING_2,
"Error receiving: %s" % (msg))
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(Error.ON_INIT,
"Socket error (server): %s " % (msg))
281 except OSError
as msg:
283 self.
_handleError(Error.ON_CONNECTING,
u"Error connecting (server): %s" % (msg), silent=
True)
296 except OSError
as msg:
297 self.
_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.'''
317 except OSError
as msg:
318 self.
_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.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
347 print(
"getsockopt() failed %s" % (error), file=sys.stderr)
353 except OSError
as msg:
354 self.
_handleError(Error.ON_CONNECTING,
"Error connecting (client): %s" % (msg))
366 except OSError
as msg:
367 self.
_handleError(Error.ON_DISCONNECTING,
"Error disconnecting: %s" % (msg), silent=
True)
__init__(self, connectTimeout=10)
Constructor initializing the master socket.
__del__(self)
Destructor making sure the socket is deleted.
connect(self, timeout=2)
Wait for a slave to connect and configure the connection.
disconnect(self)
Unlink connection to the slave.
connect(self)
Connect to master and configure connection.
__del__(self)
The slave's destructor.
disconnect(self)
Disconnect from master.
__init__(self)
The slave's constructor.
The Communicator is the superclass for the communicating entities.
int _mMsgLength
Length of the messages sent between the two entities.
_handleError(self, errorCode, errorMsg, silent=False)
Set the error code and message and disconnect from network.
_mConnected
Status of the connection.
_csocket
Connection socket.
_mErrorCode
Internal error code used to track the last error.
getLastError(self)
Get a tuple describing the last error.
getLastErrorWithMessage(self)
Get a tuple describing the last error.
_recv(self, length, timeout)
Receive's little helper.
_msocket
Main socket of the server.
recv(self, timeout)
Receive data via the socket using the given timeout.
getPort(self)
Get the port used for connections.
isConnected(self)
Get the connection status of the IPC client.
_send(self, data, length, timeout)
Send's little helper.
send(self, data, timeout)
Send the data via the socket using the given timeout.
_mPort
Port to use for the connection.
_mErrorMsg
Internal error messages.