communication.CommandThread Class Reference

QThread class used to receive commands, try to execute and respond to them. More...

Inheritance diagram for communication.CommandThread:

Public Member Functions

def __init__ (self, socketDescriptor, parent)
 Initialization method for CommandThread. More...
 
def run (self)
 Thread's functionality method. More...
 

Public Attributes

 socketDescriptor
 A Qt's qintptr socket descriptor to initialize tcpSocket. More...
 
 blockSize
 An int representing size of incoming tcp message. More...
 

Detailed Description

QThread class used to receive commands, try to execute and respond to them.

This class describes a QThread used to receive a command string from a QTcpSocket, try to execute received string and send a repspondse whether the execution was successful or not.

Definition at line 97 of file communication.py.

Constructor & Destructor Documentation

◆ __init__()

def communication.CommandThread.__init__ (   self,
  socketDescriptor,
  parent 
)

Initialization method for CommandThread.

A class instance is created, socketDescriptor and blockSize are initializated.

Parameters
socketDescriptorA Qt's qintptr socket descriptor to initialize tcpSocket.
parentA reference to an instance which will take thread's ownership.

Definition at line 115 of file communication.py.

115  def __init__(self, socketDescriptor, parent):
116  super(CommandThread, self).__init__(parent)
117  self.socketDescriptor = socketDescriptor
118  self.blockSize = 0
119 

Member Function Documentation

◆ run()

def communication.CommandThread.run (   self)

Thread's functionality method.

The starting point for the thread. After calling start(), the newly created thread calls this function. This function then tries to make QTcpSocket. It waits WAIT_TIME_MS for an incoming message. If message is received it checks its a whole message using blockSize sent in the first word as an UINT16 number. If a whole message is received, the thread tries to execute the message string and sends back an appropriate response. The response is Command failed - "error string" if the execution failed, or Command executed successfully otherwise. Then the thread is terminated.

Definition at line 132 of file communication.py.

132  def run(self):
133  # Try to connect to an incoming tcp socket using its socket descriptor
134  tcpSocket = QTcpSocket()
135  if not tcpSocket.setSocketDescriptor(self.socketDescriptor):
136  FreeCAD.Console.PrintError("Socket not accepted.\n")
137  return
138  FreeCAD.Console.PrintLog("Socket accepted.\n")
139 
140  # Wait for an incoming message
141  if not tcpSocket.waitForReadyRead(msecs=WAIT_TIME_MS):
142  FreeCAD.Console.PrintError("No request send.\n")
143  return
144 
145  # Make an input data stream
146  instr = QDataStream(tcpSocket)
147  instr.setVersion(QDataStream.Qt_4_0)
148 
149  # Try to read the message size
150  if self.blockSize == 0:
151  if tcpSocket.bytesAvailable() < 2:
152  FreeCAD.Console.PrintError("Received message "
153  + "has too few bytes.\n")
154  return
155  self.blockSize = instr.readUInt16()
156 
157  # Check message is sent complete
158  if tcpSocket.bytesAvailable() < self.blockSize:
159  FreeCAD.Console.PrintError("Received message has less bytes "
160  + "then it's supposed to.\n")
161  return
162 
163  # Read message and inform about it
164  cmd = instr.readRawData(self.blockSize).decode("UTF-8")
165  FreeCAD.Console.PrintLog("CommandServer received> "
166  + cmd + "\n")
167 
168  # Try to execute the message string and prepare a response
169  try:
170  exec(cmd)
171  except Exception as e:
172  FreeCAD.Console.PrintError("Executing external command failed:"
173  + str(e) + "\n")
174  message = "Command failed - " + str(e)
175  else:
176  FreeCAD.Console.PrintLog("Executing external command succeeded!\n")
177  message = COMMAND_EXECUTED_CONFIRMATION_MESSAGE
178 
179  # Prepare the data block to send back and inform about it
180  FreeCAD.Console.PrintLog("CommandServer sending> " + message + " \n")
181  block = QByteArray(
182  len(message.encode("UTF-8")).to_bytes(2, byteorder='big')
183  + message.encode("UTF-8"))
184  outstr = QDataStream(block, QIODevice.WriteOnly)
185  outstr.setVersion(QDataStream.Qt_4_0)
186 
187  # Send the block, disconnect from the socket and terminate the QThread
188  tcpSocket.write(block)
189  tcpSocket.disconnectFromHost()
190  tcpSocket.waitForDisconnected()
191 
192 

Member Data Documentation

◆ blockSize

communication.CommandThread.blockSize

An int representing size of incoming tcp message.

Definition at line 118 of file communication.py.

◆ socketDescriptor

communication.CommandThread.socketDescriptor

A Qt's qintptr socket descriptor to initialize tcpSocket.

Definition at line 117 of file communication.py.


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