21from enum
import IntEnum
34 ON_SENDING_TIMEOUT = 20
35 ON_RECEIVING_TIMEOUT = 21
65 self.
_mPort = 21319
if not "MeVisLabTestCenterPort" in os.environ
else int(os.environ[
"MeVisLabTestCenterPort"])
97 def send(self, data, timeout):
100 self.
_handleError(Error.NOT_CONNECTED,
"Not connected.")
108 if not self.
_send(str(length).ljust(16), 16, timeout):
112 return self.
_send(data, length, 1)
121 def _send(self, data, length, timeout):
122 if not isinstance(data, bytes):
123 data = data.encode(
"UTF-8")
128 (rlist, wlist, xlist) = select.select([], [self.
_csocket], [], timeout)
130 self.
_handleError(Error.ON_SENDING_TIMEOUT,
"Connection error (sending).")
132 except select.error
as msg:
133 self.
_handleError(Error.ON_SENDING_1,
"Error sending: %s" % (msg))
141 self.
_handleError(Error.SENDING_BLOCKED,
"Error sending: couldn't send.")
144 except OSError
as msg:
145 self.
_handleError(Error.ON_SENDING_2,
"Error sending: %s" % (msg))
158 self.
_handleError(Error.NOT_CONNECTED,
"Not connected.")
162 data = self.
_recv(16, timeout)
163 if data
in (b
"",
None):
168 data = self.
_recv(length, 1)
169 if isinstance(data, bytes):
170 data = data.decode(
"UTF-8")
171 if data ==
"##[empty]":
185 while len(data) < length:
188 (rlist, wlist, xlist) = select.select([self.
_csocket], [], [], timeout)
190 self.
_handleError(Error.ON_RECEIVING_TIMEOUT,
"Connection error (receiving).")
192 except OSError
as msg:
193 self.
_handleError(Error.ON_RECEIVING_1,
"Error receiving: %s" % (msg))
199 if tdata
in (b
"",
None):
200 self.
_handleError(Error.EMPTY_MESSAGE,
"Error receiving: empty message received")
203 except OSError
as msg:
204 self.
_handleError(Error.ON_RECEIVING_2,
"Error receiving: %s" % (msg))
261 print(f
"Port {self._mPort} was blocked, using {port} instead.")
267 if e.errno
in (errno.EADDRINUSE, errno.EADDRNOTAVAIL, 10013):
272 except OSError
as msg:
273 self.
_handleError(Error.ON_INIT,
"Socket error (server): %s " % (msg))
299 except OSError
as msg:
301 self.
_handleError(Error.ON_CONNECTING,
"Error connecting (server): %s" % (msg), silent=
True)
315 except OSError
as msg:
316 self.
_handleError(Error.ON_DISCONNECTING,
"Error disconnecting: %s" % (msg), silent=
True)
323 """Returns the timeout used by the Master while establishing the connection to the slave."""
340 except OSError
as msg:
341 self.
_handleError(Error.ON_INIT,
"Socket error (client): %s" % (msg))
360 except OSError
as msg:
361 if sys.platform.startswith(
"win")
or msg.errno != errno.EINTR:
365 while poll.poll(-1) == -1:
366 if msg.errno != errno.EINTR:
367 print(
"poll() failed", file=sys.stderr)
370 error = self.
_csocket_csocket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
372 print(
"getsockopt() failed %s" % (error), file=sys.stderr)
378 except OSError
as msg:
379 self.
_handleError(Error.ON_CONNECTING,
"Error connecting (client): %s" % (msg))
392 except OSError
as msg:
393 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.