communication Namespace Reference

Classes related to interprocess communications. More...

Classes

class  CommandClient
 Class to be used for sending commands. More...
 
class  CommandServer
 QTcpServer class used to receive commands and execute them. More...
 
class  CommandThread
 QThread class used to receive commands, try to execute and respond to them. More...
 

Functions

def checkIPIsValid (ip)
 Method used to check a selected IP is possible to use with a Qt's QHostAddress. More...
 
def startServer (addr, port)
 Method used to try to start a CommandServer at a valid IP address and port. More...
 
def sendClientCommand (host, port, cmd, wait_time=WAIT_TIME_MS)
 Method to be used for sending commands. More...
 

Variables

int SIZEOF_UINT16 = 2
 Size of uint16 in bytes used to leave space at the beginning of each message to specify tcp message length (maximal length is 65535 bytes). More...
 
int SERVER_ERROR_INVALID_ADDRESS = 1
 Error code used in startServer() when trying to connect CommandServer to an invalid address. More...
 
int SERVER_ERROR_PORT_OCCUPIED = 2
 Error code used in startServer() when trying to connect CommandServer to an occupied port. More...
 
int WAIT_TIME_MS = 30000
 Time to wait in milliseconds - used to wait for incoming message etc. More...
 
string COMMAND_EXECUTED_CONFIRMATION_MESSAGE = "Command executed successfully"
 Message send from a CommandServer to a CommandClient.sendCommand() or sendClientCommand() after successful execution of a command. More...
 
int CLIENT_COMMAND_EXECUTED = 0
 CommandClient.sendCommand() or sendClientCommand() return value after confirmation of successful command execution. More...
 
int CLIENT_COMMAND_FAILED = 1
 CommandClient.sendCommand() or sendClientCommand() return value if command execution failed (invalid command string). More...
 
int CLIENT_ERROR_RESPONSE_NOT_COMPLETE = 2
 CommandClient.sendCommand() or sendClientCommand() error code to signal that response was no received complete. More...
 
int CLIENT_ERROR_NO_RESPONSE = 3
 CommandClient.sendCommand() or sendClientCommand() error code to signal that no response from CommandServer was received. More...
 
int CLIENT_ERROR_BLOCK_NOT_WRITTEN = 4
 CommandClient.sendCommand() or sendClientCommand() error code to signal that message block was not written to a TCP socket. More...
 
int CLIENT_ERROR_NO_CONNECTION = 5
 CommandClient.sendCommand() or sendClientCommand() error code to signal that connection a host CommandServer was not established. More...
 

Detailed Description

Classes related to interprocess communications.

The Classes in this module...

Function Documentation

◆ checkIPIsValid()

def communication.checkIPIsValid (   ip)

Method used to check a selected IP is possible to use with a Qt's QHostAddress.

The IP can be either IPv4 address or *"localhost"* string with possible capital letters anywhere. IPv6 is not supported for now.

Parameters
ipA str with an IP address selected.

Definition at line 244 of file communication.py.

244 def checkIPIsValid(ip):
245  if ip.upper() == "LOCALHOST":
246  return True
247 
248  numbers = ip.split(".")
249  if len(numbers) == 4:
250  if all([(0 <= int(num) <= 255) for num in numbers]):
251  return True
252 
253  return False
254 
255 

◆ sendClientCommand()

def communication.sendClientCommand (   host,
  port,
  cmd,
  wait_time = WAIT_TIME_MS 
)

Method to be used for sending commands.

This method is an alternative to using CommandClient. It does not print any logs, just returns a value saying how the execution went.

To send a command using this method do: sendClientCommand("127.0.0.1",54333, "FreeCAD.Console.PrintWarning('Hello World\\n')")

Parameters
cmdA str command to be executed.
hostA QtHostAddress to the CommandServer.
portAn int of port at which CommandServer is listening.
wait_timeAn int setting milliseconds to wait for connection or message.
Returns
CLIENT_COMMAND_EXECUTED if all went great and command was executed. CLIENT_COMMAND_FAILED if cmd execution failed. CLIENT_ERROR_RESPONSE_NOT_COMPLETE if a response received was incomplete. CLIENT_ERROR_NO_RESPONSE if there was no response within WAIT_TIME_MS. CLIENT_ERROR_BLOCK_NOT_WRITTEN if communication failed during sending. CLIENT_ERROR_NO_CONNECTION if no connection to a host was established.

Definition at line 483 of file communication.py.

483 def sendClientCommand(host, port, cmd, wait_time=WAIT_TIME_MS):
484  # Try to connect to a host server
485  tcpSocket = QTcpSocket()
486  tcpSocket.connectToHost(host, port, QIODevice.ReadWrite)
487  if not tcpSocket.waitForConnected(msecs=wait_time):
488  return CLIENT_ERROR_NO_CONNECTION
489 
490  # Prepare a command message to be sent
491  block = QByteArray(
492  len(cmd.encode("UTF-8")).to_bytes(2, byteorder='big')
493  + cmd.encode("UTF-8"))
494  outstr = QDataStream(block, QIODevice.WriteOnly)
495  outstr.setVersion(QDataStream.Qt_4_0)
496  tcpSocket.write(block)
497 
498  # Try to send the message
499  if not tcpSocket.waitForBytesWritten(msecs=wait_time):
500  return CLIENT_ERROR_BLOCK_NOT_WRITTEN
501 
502  # Wait for a response from the host server
503  if not tcpSocket.waitForReadyRead(msecs=wait_time):
504  return CLIENT_ERROR_NO_RESPONSE
505 
506  # Try to read the response
507  instr = QDataStream(tcpSocket)
508  instr.setVersion(QDataStream.Qt_4_0)
509  blockSize = 0
510  if blockSize == 0:
511  if tcpSocket.bytesAvailable() < 2:
512  return CLIENT_ERROR_RESPONSE_NOT_COMPLETE
513  blockSize = instr.readUInt16()
514  if tcpSocket.bytesAvailable() < blockSize:
515  return CLIENT_ERROR_RESPONSE_NOT_COMPLETE
516 
517  # Wait until the host server terminates the connection
518  tcpSocket.waitForDisconnected()
519 
520  # Return value representing a command execution status
521  if instr.readRawData(blockSize).decode("UTF-8") \
522  == COMMAND_EXECUTED_CONFIRMATION_MESSAGE:
523  return CLIENT_COMMAND_EXECUTED
524  else:
525  return CLIENT_COMMAND_FAILED

◆ startServer()

def communication.startServer (   addr,
  port 
)

Method used to try to start a CommandServer at a valid IP address and port.

This method checks that a chosen addr is valid IP address to be used to create Qt's QHostAddress using checkIPIsValid(). If the IP address is valid, then a CommandServer instance is created and tried to be made listen at selected addr and port. If it fails, then the used port had to be occupied.

Parameters
addrA str with a valid IPv4 Address or *"localhost"*.
portAn int selecting a port to be used for the CommandServer.
Returns
An integer error code signifying that and error occurred(either ERROR_INVALID ADDRESS or ERROR_PORT_OCCUPIED) or a CommnadServer instance if everything went hunky-dory.

Definition at line 273 of file communication.py.

273 def startServer(addr, port):
274 
275  if not checkIPIsValid(addr):
276  return SERVER_ERROR_INVALID_ADDRESS
277 
278  if addr.upper() == "LOCALHOST":
279  addr = QHostAddress(QHostAddress.LocalHost)
280  else:
281  addr = QHostAddress(addr)
282 
283  server = CommandServer()
284  if not server.listen(addr, port):
285  FreeCAD.Console.PrintLog("Unable to start the server: %s.\n"
286  % server.errorString())
287  return SERVER_ERROR_PORT_OCCUPIED
288 
289  else:
290  FreeCAD.Console.PrintLog("The server is running on address %s"
291  % server.serverAddress().toString()
292  + " and port %d.\n" % server.serverPort())
293  return server
294 
295 

Variable Documentation

◆ CLIENT_COMMAND_EXECUTED

int communication.CLIENT_COMMAND_EXECUTED = 0

CommandClient.sendCommand() or sendClientCommand() return value after confirmation of successful command execution.

Definition at line 65 of file communication.py.

◆ CLIENT_COMMAND_FAILED

int communication.CLIENT_COMMAND_FAILED = 1

CommandClient.sendCommand() or sendClientCommand() return value if command execution failed (invalid command string).

Definition at line 69 of file communication.py.

◆ CLIENT_ERROR_BLOCK_NOT_WRITTEN

int communication.CLIENT_ERROR_BLOCK_NOT_WRITTEN = 4

CommandClient.sendCommand() or sendClientCommand() error code to signal that message block was not written to a TCP socket.

Definition at line 81 of file communication.py.

◆ CLIENT_ERROR_NO_CONNECTION

int communication.CLIENT_ERROR_NO_CONNECTION = 5

CommandClient.sendCommand() or sendClientCommand() error code to signal that connection a host CommandServer was not established.

Definition at line 85 of file communication.py.

◆ CLIENT_ERROR_NO_RESPONSE

int communication.CLIENT_ERROR_NO_RESPONSE = 3

CommandClient.sendCommand() or sendClientCommand() error code to signal that no response from CommandServer was received.

Definition at line 77 of file communication.py.

◆ CLIENT_ERROR_RESPONSE_NOT_COMPLETE

int communication.CLIENT_ERROR_RESPONSE_NOT_COMPLETE = 2

CommandClient.sendCommand() or sendClientCommand() error code to signal that response was no received complete.

Definition at line 73 of file communication.py.

◆ COMMAND_EXECUTED_CONFIRMATION_MESSAGE

string communication.COMMAND_EXECUTED_CONFIRMATION_MESSAGE = "Command executed successfully"

Message send from a CommandServer to a CommandClient.sendCommand() or sendClientCommand() after successful execution of a command.

Definition at line 61 of file communication.py.

◆ SERVER_ERROR_INVALID_ADDRESS

int communication.SERVER_ERROR_INVALID_ADDRESS = 1

Error code used in startServer() when trying to connect CommandServer to an invalid address.

Definition at line 50 of file communication.py.

◆ SERVER_ERROR_PORT_OCCUPIED

int communication.SERVER_ERROR_PORT_OCCUPIED = 2

Error code used in startServer() when trying to connect CommandServer to an occupied port.

Definition at line 54 of file communication.py.

◆ SIZEOF_UINT16

int communication.SIZEOF_UINT16 = 2

Size of uint16 in bytes used to leave space at the beginning of each message to specify tcp message length (maximal length is 65535 bytes).

Definition at line 46 of file communication.py.

◆ WAIT_TIME_MS

int communication.WAIT_TIME_MS = 30000

Time to wait in milliseconds - used to wait for incoming message etc.

Definition at line 57 of file communication.py.