communication.CommandClient Class Reference

Class to be used for sending commands. More...

Public Member Functions

def __init__ (self, host, port)
 Initialization method for CommandClient. More...
 
def sendCommand (self, cmd)
 Method used to send commands from client to CommandServer. More...
 
def displayError (self, socketError)
 Qt's slot method to print out received tcpSocket's error. More...
 

Public Attributes

 host
 A QtHostAddress to the CommandServer. More...
 
 port
 An int of port at which CommandServer is listening. More...
 
 tcpSocket
 A QTcpSocket used to contact CommandSErver
 
 blockSize
 An int representing size of incoming tcp message. More...
 

Detailed Description

Class to be used for sending commands.

This class can be used in FreeCAD's or regular python console to send commands to a CommandServer using sendCommand(). The class prints logs as it moves along.

To send a commands do:

client = CommandClient("127.0.0.1",54321)
client.sendCommand('FreeCAD.Console.PrintError("Hello World\\n")\n')
client.sendCommand('FreeCAD.Console.PrintError("Bye Bye\\n")\n')

Definition at line 311 of file communication.py.

Constructor & Destructor Documentation

◆ __init__()

def communication.CommandClient.__init__ (   self,
  host,
  port 
)

Initialization method for CommandClient.

A class instance is created and its attributes are initialized.

Parameters
hostA QtHostAddress to the CommandServer.
portAn int of port at which CommandServer is listening.

Definition at line 334 of file communication.py.

334  def __init__(self, host, port):
335  self.host = host
336  self.port = port
337  self.tcpSocket = QTcpSocket()
338  self.blockSize = 0
339 

Member Function Documentation

◆ displayError()

def communication.CommandClient.displayError (   self,
  socketError 
)

Qt's slot method to print out received tcpSocket's error.

QAbstractSocket.RemoteHostClosedError is not printed, because it occurs naturally when the tcpSocket closes after a transaction is over. Except that all errors are printed.

Parameters
socketErrorA QAbstractSocket::SocketError enum describing occurred error.

Definition at line 446 of file communication.py.

446  def displayError(self, socketError):
447  if socketError != QAbstractSocket.RemoteHostClosedError:
448  if "FreeCAD" in sys.modules:
449  FreeCAD.Console.PrintError("CommandClient error occurred> %s."
450  % self.tcpSocket.errorString()
451  + "\n")
452  else:
453  print("CommandClient error occurred> %s."
454  % self.tcpSocket.errorString() + "\n")
455 
456 

◆ sendCommand()

def communication.CommandClient.sendCommand (   self,
  cmd 
)

Method used to send commands from client to CommandServer.

This method tries to connect to a specified host CommandServer via tcpSocket. If connection was successful, the command cmd is sent. Then the response is expected. If the response is equal to COMMAND_EXECUTED_CONFIRMATION_MESSAGE, then the execution was successful. The progress and result of sendCommand can be obtained from printed logs and return value.

Parameters
cmdA str command to be executed.
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 361 of file communication.py.

361  def sendCommand(self, cmd):
362 
363  # connect a Qt slot to receive and print errors
364  self.tcpSocket.error.connect(self.displayError)
365 
366  # Try to connect to a host server
367  self.tcpSocket.connectToHost(self.host, self.port, QIODevice.ReadWrite)
368  if not self.tcpSocket.waitForConnected(msecs=WAIT_TIME_MS):
369  if "FreeCAD" in sys.modules:
370  FreeCAD.Console.PrintError("CommandClient.sendCommand error: "
371  + "No connection\n")
372  else:
373  print("CommandClient.sendCommand error: No connection\n")
374  return CLIENT_ERROR_NO_CONNECTION
375 
376  # Prepare a command message to be sent
377  block = QByteArray(
378  len(cmd.encode("UTF-8")).to_bytes(2, byteorder='big')
379  + cmd.encode("UTF-8"))
380  outstr = QDataStream(block, QIODevice.WriteOnly)
381  outstr.setVersion(QDataStream.Qt_4_0)
382 
383  # Try to send the message
384  if "FreeCAD" in sys.modules:
385  FreeCAD.Console.PrintMessage("CommandClient sending> "
386  + cmd + "\n")
387  else:
388  print("CommandClient sending> " + cmd + "\n")
389  self.tcpSocket.write(block)
390  if not self.tcpSocket.waitForBytesWritten(msecs=WAIT_TIME_MS):
391  if "FreeCAD" in sys.modules:
392  FreeCAD.Console.PrintError("CommandClient.sendCommand error: "
393  + "Block not written\n")
394  else:
395  print("CommandClient.sendCommand error: Block not written\n")
396  return CLIENT_ERROR_BLOCK_NOT_WRITTEN
397 
398  # Wait for a response from the host server
399  if not self.tcpSocket.waitForReadyRead(msecs=WAIT_TIME_MS):
400  if "FreeCAD" in sys.modules:
401  FreeCAD.Console.PrintError("CommandClient.sendCommand error: "
402  + "No response received.\n")
403  else:
404  print("CommandClient.sendCommand error: "
405  + "No response received.\n")
406  return CLIENT_ERROR_NO_RESPONSE
407 
408  # Try to read the response
409  instr = QDataStream(self.tcpSocket)
410  instr.setVersion(QDataStream.Qt_4_0)
411  if self.blockSize == 0:
412  if self.tcpSocket.bytesAvailable() < 2:
413  return CLIENT_ERROR_RESPONSE_NOT_COMPLETE
414  self.blockSize = instr.readUInt16()
415 
416  if self.tcpSocket.bytesAvailable() < self.blockSize:
417  return CLIENT_ERROR_RESPONSE_NOT_COMPLETE
418  response = instr.readRawData(self.blockSize).decode("UTF-8")
419  if "FreeCAD" in sys.modules:
420  FreeCAD.Console.PrintMessage("CommandClient received> "
421  + response + "\n")
422  else:
423  print("CommandClient received> " + response + "\n")
424 
425  # Wait until the host server terminates the connection
426  self.tcpSocket.waitForDisconnected()
427  # Reset blockSize to prepare for sending next command
428  self.blockSize = 0
429 
430  # Return value representing a command execution status
431  if response == COMMAND_EXECUTED_CONFIRMATION_MESSAGE:
432  return CLIENT_COMMAND_EXECUTED
433  else:
434  return CLIENT_COMMAND_FAILED
435 

Member Data Documentation

◆ blockSize

communication.CommandClient.blockSize

An int representing size of incoming tcp message.

Definition at line 338 of file communication.py.

◆ host

communication.CommandClient.host

A QtHostAddress to the CommandServer.

Definition at line 335 of file communication.py.

◆ port

communication.CommandClient.port

An int of port at which CommandServer is listening.

Definition at line 336 of file communication.py.


The documentation for this class was generated from the following file: