예제 #1
0
    def run(self):
        # Try to connect to an incomming tcp socket using its socket descriptor
        tcpSocket = QTcpSocket()
        if not tcpSocket.setSocketDescriptor(self.socketDescriptor):
            FreeCAD.Console.PrintError("Socket not accepted.\n")
            return

        # Wait for an incomming message
        if not tcpSocket.waitForReadyRead(msecs=WAIT_TIME_MS):
            FreeCAD.Console.PrintError("No request send.\n")
            return

        # Make an input data stream
        instr = QDataStream(tcpSocket)
        instr.setVersion(QDataStream.Qt_4_0)

        # Try to read the message size
        if self.blockSize == 0:
            if tcpSocket.bytesAvailable() < 2:
                return
            self.blockSize = instr.readUInt16()

        # Check message is sent complete
        if tcpSocket.bytesAvailable() < self.blockSize:
            return

        # Read message and inform about it
        instr = instr.readString()
        FreeCAD.Console.PrintLog("CommandServer received> " + str(instr) +
                                 "\n")

        # Try to execute the message string and prepare  a response
        try:
            exec(str(instr))
        except Exception as e:
            FreeCAD.Console.PrintError("Executing external command failed:" +
                                       str(e) + "\n")
            message = "Command failed - " + str(e)
        else:
            FreeCAD.Console.PrintLog("Executing external command succeded!\n")
            message = COMMAND_EXECUTED_CONFIRMATION_MESSAGE

        # Prepare the data block to send back and inform about it
        FreeCAD.Console.PrintLog("CommandServer sending> " + message + " \n")
        block = QByteArray()
        outstr = QDataStream(block, QIODevice.WriteOnly)
        outstr.setVersion(QDataStream.Qt_4_0)
        outstr.writeUInt16(0)
        outstr.writeQString(message)
        outstr.device().seek(0)
        outstr.writeUInt16(block.size() - 2)

        # Send the block, disconnect from the socket and terminate the QThread
        tcpSocket.write(block)
        tcpSocket.disconnectFromHost()
        tcpSocket.waitForDisconnected()
예제 #2
0
 def startDrag(self, dropActions):
     item = self.currentItem()
     # icon = item.icon()
     data = QByteArray()
     stream = QDataStream(data, QIODevice.WriteOnly)
     stream.writeQString(item.text())
     stream.writeInt16(item.data(1))
     # stream << icon
     mimeData = QMimeData()
     mimeData.setData("application/x-lista-item", data)
     drag = QDrag(self)
     drag.setMimeData(mimeData)
     if drag.exec_(Qt.MoveAction) == Qt.MoveAction:
         self.takeItem(self.row(item))
    def run(self):
        self.mutex.lock()
        serverName = self.hostName
        serverPort = self.port
        self.mutex.unlock()

        while not self.quit:
            Timeout = 5 * 1000

            socket = QTcpSocket()
            socket.connectToHost(serverName, serverPort)

            if not socket.waitForConnected(Timeout):
                self.error.emit(socket.error(), socket.errorString())
                return

            while socket.bytesAvailable() < 2:
                if not socket.waitForReadyRead(Timeout):
                    self.error.emit(socket.error(), socket.errorString())
                    return

            instr = QDataStream(socket)
            instr.setVersion(QDataStream.Qt_4_0)
            blockSize = instr.readUInt16()

            block = QByteArray()
            outstr = QDataStream(block, QIODevice.WriteOnly)
            outstr.setVersion(QDataStream.Qt_4_0)

            

            while socket.bytesAvailable() < blockSize:
                if not socket.waitForReadyRead(Timeout):
                    self.error.emit(socket.error(), socket.errorString())
                    return

            self.mutex.lock()
            outstr.writeUInt16(0)
            outstr.writeQString("Message to Server")
            outstr.device().seek(0)
            outstr.writeUInt16(block.size() - 2)
            socket.write(block)
#            socket.write(block)
            fortune = instr.readQString()
            self.newFortune.emit(fortune)

            self.cond.wait(self.mutex)
            serverName = self.hostName
            serverPort = self.port
            self.mutex.unlock()
    def run(self):
        tcpSocket = QTcpSocket()
        if not tcpSocket.setSocketDescriptor(self.socketDescriptor):
            self.error.emit(tcpSocket.error())
            return

        block = QByteArray()
        outstr = QDataStream(block, QIODevice.WriteOnly)
        outstr.setVersion(QDataStream.Qt_4_0)
        outstr.writeUInt16(0)
        outstr.writeQString(self.text)
        outstr.device().seek(0)
        outstr.writeUInt16(block.size() - 2)

        tcpSocket.write(block)
        tcpSocket.disconnectFromHost()
        tcpSocket.waitForDisconnected()
    def run(self):
        tcpSocket = QTcpSocket()
        if not tcpSocket.setSocketDescriptor(self.socketDescriptor):
            self.error.emit(tcpSocket.error())
            return

        block = QByteArray()
        outstr = QDataStream(block, QIODevice.WriteOnly)
        outstr.setVersion(QDataStream.Qt_4_0)
        outstr.writeUInt16(0)
        outstr.writeQString(self.text)
        outstr.device().seek(0)
        outstr.writeUInt16(block.size() - 2)

        tcpSocket.write(block)
        tcpSocket.disconnectFromHost()
        tcpSocket.waitForDisconnected()
예제 #6
0
 def startDrag(self):
     data = QByteArray()
     stream = QDataStream(data, QIODevice.WriteOnly)
     stream.writeQString(self._player_name)
     stream.writeInt16(self._player_id)
     mimedata = QMimeData()
     mimedata.setData("application/x-lista-item", data)
     drag = QDrag(self)
     drag.setMimeData(mimedata)
     if drag.exec_(Qt.MoveAction) == Qt.MoveAction:
         self._player_name = ""
         self._player_id = 0
         for i in range(self.sorok_szama):
             self.parent.eredmenyek[self._csoport_number][
                 self._csoport_sor][i]._set_p1_id(0)
             self.parent.eredmenyek[self._csoport_number][i][
                 self._csoport_sor]._set_p2_id(0)
         self.update()
예제 #7
0
def sendClientCommand(host, port, cmd, wait_time=WAIT_TIME_MS):
    # Try to connect to a host server
    tcpSocket = QTcpSocket()
    tcpSocket.connectToHost(host, port, QIODevice.ReadWrite)
    if not tcpSocket.waitForConnected(msecs=wait_time):
        return CLIENT_ERROR_NO_CONNECTION

    # Prepare a command message to be sent
    block = QByteArray()
    outstr = QDataStream(block, QIODevice.WriteOnly)
    outstr.setVersion(QDataStream.Qt_4_0)
    outstr.writeUInt16(0)
    outstr.writeQString(cmd)
    outstr.device().seek(0)
    outstr.writeUInt16(block.size() - 2)
    tcpSocket.write(block)

    # Try to send the message
    if not tcpSocket.waitForBytesWritten(msecs=wait_time):
        return CLIENT_ERROR_BLOCK_NOT_WRITTEN

    # Wait for a response from the host server
    if not tcpSocket.waitForReadyRead(msecs=10000):
        return CLIENT_ERROR_NO_RESPONSE

    # Try to read the response
    instr = QDataStream(tcpSocket)
    instr.setVersion(QDataStream.Qt_4_0)
    blockSize = 0
    if blockSize == 0:
        if tcpSocket.bytesAvailable() < 2:
            return CLIENT_ERROR_RESPONSE_NOT_COMPLETE
        blockSize = instr.readUInt16()
    if tcpSocket.bytesAvailable() < blockSize:
        return CLIENT_ERROR_RESPONSE_NOT_COMPLETE

    # Wait until the host server terminates the connection
    tcpSocket.waitForDisconnected()

    # Return value representing a command execution status
    if instr.readString() == COMMAND_EXECUTED_CONFIRMATION_MESSAGE:
        return CLIENT_COMMAND_EXECUTED
    else:
        return CLIENT_COMMAND_FAILED
예제 #8
0
class QDataStreamShift(unittest.TestCase):
    '''Test case for << and >> operators'''
    def setUp(self):
        self.ba = QByteArray()
        self.stream = QDataStream(self.ba, QIODevice.WriteOnly)
        self.read_stream = QDataStream(self.ba, QIODevice.ReadOnly)

    def testQCharValid(self):
        '''QDataStream <<>> QChar - valid'''
        self.stream.writeQChar(42)

        res = self.read_stream.readQChar()
        self.assertEqual(res, py3k.unichr(42))

    def testQCharNull(self):
        '''QDataStream <<>> QChar - null'''
        self.stream.writeQChar(None)

        res = self.read_stream.readQChar()
        self.assertEqual(res, py3k.unicode_('\x00'))

    def testQByteArrayValid(self):
        '''QDataStream <<>> QByteArray - valid'''
        self.stream << QByteArray("hello")

        res = QByteArray()

        self.read_stream >> res
        self.assertEqual(res, QByteArray("hello"))

    def testQByteArrayEmpty(self):
        '''QDataStream <<>> QByteArray - empty'''
        self.stream << QByteArray("")

        res = QByteArray()

        self.read_stream >> res
        self.assertEqual(res, QByteArray(""))
        self.assertTrue(res.isEmpty())
        self.assertFalse(res.isNull())

    def testQByteArrayNull(self):
        '''QDataStream <<>> QByteArray - null'''
        self.stream << QByteArray()

        res = QByteArray()

        self.read_stream >> res
        self.assertEqual(res, QByteArray())
        self.assertTrue(res.isEmpty())
        self.assertTrue(res.isNull())

    def testQStringValid(self):
        '''QDataStream <<>> QString - valid'''
        self.stream.writeQString('Ka-boom')

        res = self.read_stream.readQString()
        self.assertEqual(res, py3k.unicode_('Ka-boom'))

    def testQStringEmpty(self):
        '''QDataStream <<>> QString - empty'''
        self.stream.writeQString('')

        res = self.read_stream.readQString()
        self.assertEqual(res, py3k.unicode_(''))

    def testQStringNull(self):
        '''QDataStream <<>> QString - null'''
        self.stream.writeQString(None)

        res = self.read_stream.readQString()
        self.assertEqual(res, py3k.unicode_(''))

    def testQBitArrayNull(self):
        '''QDataStream <<>> QBitArray - null'''
        self.stream << QBitArray()

        res = QBitArray()

        self.read_stream >> res
        self.assertEqual(res, QBitArray())

    def testQBitArrayValid(self):
        '''QDataStream <<>> QBitArray - valid'''
        self.stream << create_bitarray('01010101')

        res = QBitArray()

        self.read_stream >> res
        self.assertEqual(res, create_bitarray('01010101'))

    def testQDateNull(self):
        '''QDataStream <<>> QDate - null'''
        self.stream << QDate()

        res = QDate()

        self.read_stream >> res
        self.assertEqual(res, QDate())
        self.assertFalse(res.isValid())
        self.assertTrue(res.isNull())

    def testQDateValid(self):
        '''QDataStream <<>> QDate - valid'''
        self.stream << QDate(2012, 12, 21)

        res = QDate()

        self.read_stream >> res
        self.assertEqual(res, QDate(2012, 12, 21))
        self.assertTrue(res.isValid())
        self.assertFalse(res.isNull())

    def testQTimeNull(self):
        '''QDataStream <<>> QTime - null'''
        self.stream << QTime()

        res = QTime()

        self.read_stream >> res
        self.assertEqual(res, QTime())
        self.assertFalse(res.isValid())
        self.assertTrue(res.isNull())

    def testQTimeValid(self):
        '''QDataStream <<>> QTime - valid'''
        self.stream << QTime(12, 12, 21)

        res = QTime()

        self.read_stream >> res
        self.assertEqual(res, QTime(12, 12, 21))
        self.assertTrue(res.isValid())
        self.assertFalse(res.isNull())

    def testQDateTimeNull(self):
        '''QDataStream <<>> QDateTime - null'''

        self.stream << QDateTime()

        res = QDateTime()

        self.read_stream >> res
        self.assertEqual(res, QDateTime())
        self.assertFalse(res.isValid())
        self.assertTrue(res.isNull())

    def testQDateTimeValid(self):
        '''QDataStream <<>> QDateTime - valid'''
        time = QTime(23, 23, 23)
        date = QDate(2009, 1, 1)

        self.stream << QDateTime(date, time)

        res = QDateTime()

        self.read_stream >> res
        self.assertEqual(res, QDateTime(date, time))
        self.assertTrue(res.isValid())
        self.assertFalse(res.isNull())
예제 #9
0
    def sendCommand(self, cmd):

        # connect a Qt slot to receive and print errors
        self.tcpSocket.error.connect(self.displayError)

        # Try to connect to a host server
        self.tcpSocket.connectToHost(self.host, self.port, QIODevice.ReadWrite)
        if not self.tcpSocket.waitForConnected(msecs=WAIT_TIME_MS):
            if "FreeCAD" in sys.modules:
                FreeCAD.Console.PrintError(
                    "CommandClient.sendCommand error: " + "No connection\n")
            else:
                print("CommandClient.sendCommand error: No connection\n")
            return CLIENT_ERROR_NO_CONNECTION

        # Prepare a command message to be sent
        block = QByteArray()
        outstr = QDataStream(block, QIODevice.WriteOnly)
        outstr.setVersion(QDataStream.Qt_4_0)
        outstr.writeUInt16(0)
        outstr.writeQString(cmd)
        outstr.device().seek(0)
        outstr.writeUInt16(block.size() - 2)

        # Try to send the message
        if "FreeCAD" in sys.modules:
            FreeCAD.Console.PrintMessage("CommandClient sending> " + cmd +
                                         "\n")
        else:
            print("CommandClient sending> " + cmd + "\n")
        self.tcpSocket.write(block)
        if not self.tcpSocket.waitForBytesWritten(msecs=WAIT_TIME_MS):
            if "FreeCAD" in sys.modules:
                FreeCAD.Console.PrintError(
                    "CommandClient.sendCommand error: " +
                    "Block not written\n")
            else:
                print("CommandClient.sendCommand error: Block not written\n")
            return CLIENT_ERROR_BLOCK_NOT_WRITTEN

        # Wait for a response from the host server
        if not self.tcpSocket.waitForReadyRead(msecs=WAIT_TIME_MS):
            if "FreeCAD" in sys.modules:
                FreeCAD.Console.PrintError(
                    "CommandClient.sendCommand error: " +
                    "No response received.\n")
            else:
                print("CommandClient.sendCommand error: " +
                      "No response received.\n")
            return CLIENT_ERROR_NO_RESPONSE

        # Try to read the response
        instr = QDataStream(self.tcpSocket)
        instr.setVersion(QDataStream.Qt_4_0)
        if self.blockSize == 0:
            if self.tcpSocket.bytesAvailable() < 2:
                return CLIENT_ERROR_RESPONSE_NOT_COMPLETE
            self.blockSize = instr.readUInt16()

        if self.tcpSocket.bytesAvailable() < self.blockSize:
            return CLIENT_ERROR_RESPONSE_NOT_COMPLETE
        response = instr.readString()
        if "FreeCAD" in sys.modules:
            FreeCAD.Console.PrintMessage("CommandClient received> " +
                                         response + "\n")
        else:
            print("CommandClient received> " + response + "\n")

        # Wait until the host server terminates the connection
        self.tcpSocket.waitForDisconnected()
        # Reset blockSize to prepare for sending next command
        self.blockSize = 0

        # Return value representing a command execution status
        if response == COMMAND_EXECUTED_CONFIRMATION_MESSAGE:
            return CLIENT_COMMAND_EXECUTED
        else:
            return CLIENT_COMMAND_FAILED
예제 #10
0
class QDataStreamShift(unittest.TestCase):
    '''Test case for << and >> operators'''

    def setUp(self):
        self.ba = QByteArray()
        self.stream = QDataStream(self.ba, QIODevice.WriteOnly)
        self.read_stream = QDataStream(self.ba, QIODevice.ReadOnly)

    def testQCharValid(self):
        '''QDataStream <<>> QChar - valid'''
        self.stream.writeQChar(42)

        res = self.read_stream.readQChar()
        self.assertEqual(res, py3k.unichr(42))

    def testQCharNull(self):
        '''QDataStream <<>> QChar - null'''
        self.stream.writeQChar(None)

        res = self.read_stream.readQChar()
        self.assertEqual(res, py3k.unicode_('\x00'))

    def testQByteArrayValid(self):
        '''QDataStream <<>> QByteArray - valid'''
        self.stream << QByteArray("hello")

        res = QByteArray()

        self.read_stream >> res
        self.assertEqual(res, QByteArray("hello"))

    def testQByteArrayEmpty(self):
        '''QDataStream <<>> QByteArray - empty'''
        self.stream << QByteArray("")

        res = QByteArray()

        self.read_stream >> res
        self.assertEqual(res, QByteArray(""))
        self.assertTrue(res.isEmpty())
        self.assertFalse(res.isNull())

    def testQByteArrayNull(self):
        '''QDataStream <<>> QByteArray - null'''
        self.stream << QByteArray()

        res = QByteArray()

        self.read_stream >> res
        self.assertEqual(res, QByteArray())
        self.assertTrue(res.isEmpty())
        self.assertTrue(res.isNull())

    def testQStringValid(self):
        '''QDataStream <<>> QString - valid'''
        self.stream.writeQString('Ka-boom')

        res = self.read_stream.readQString()
        self.assertEqual(res, py3k.unicode_('Ka-boom'))

    def testQStringEmpty(self):
        '''QDataStream <<>> QString - empty'''
        self.stream.writeQString('')

        res = self.read_stream.readQString()
        self.assertEqual(res, py3k.unicode_(''))

    def testQStringNull(self):
        '''QDataStream <<>> QString - null'''
        self.stream.writeQString(None)

        res = self.read_stream.readQString()
        self.assertEqual(res, py3k.unicode_(''))

    def testQBitArrayNull(self):
        '''QDataStream <<>> QBitArray - null'''
        self.stream << QBitArray()

        res = QBitArray()

        self.read_stream >> res
        self.assertEqual(res, QBitArray())

    def testQBitArrayValid(self):
        '''QDataStream <<>> QBitArray - valid'''
        self.stream << create_bitarray('01010101')

        res = QBitArray()

        self.read_stream >> res
        self.assertEqual(res, create_bitarray('01010101'))

    def testQDateNull(self):
        '''QDataStream <<>> QDate - null'''
        self.stream << QDate()

        res = QDate()

        self.read_stream >> res
        self.assertEqual(res, QDate())
        self.assertFalse(res.isValid())
        self.assertTrue(res.isNull())

    def testQDateValid(self):
        '''QDataStream <<>> QDate - valid'''
        self.stream << QDate(2012, 12, 21)

        res = QDate()

        self.read_stream >> res
        self.assertEqual(res, QDate(2012, 12, 21))
        self.assertTrue(res.isValid())
        self.assertFalse(res.isNull())


    def testQTimeNull(self):
        '''QDataStream <<>> QTime - null'''
        self.stream << QTime()

        res = QTime()

        self.read_stream >> res
        self.assertEqual(res, QTime())
        self.assertFalse(res.isValid())
        self.assertTrue(res.isNull())

    def testQTimeValid(self):
        '''QDataStream <<>> QTime - valid'''
        self.stream << QTime(12, 12, 21)

        res = QTime()

        self.read_stream >> res
        self.assertEqual(res, QTime(12, 12, 21))
        self.assertTrue(res.isValid())
        self.assertFalse(res.isNull())

    def testQDateTimeNull(self):
        '''QDataStream <<>> QDateTime - null'''

        self.stream << QDateTime()

        res = QDateTime()

        self.read_stream >> res
        self.assertEqual(res, QDateTime())
        self.assertFalse(res.isValid())
        self.assertTrue(res.isNull())

    def testQDateTimeValid(self):
        '''QDataStream <<>> QDateTime - valid'''
        time = QTime(23, 23, 23)
        date = QDate(2009, 1, 1)

        self.stream << QDateTime(date, time)

        res = QDateTime()

        self.read_stream >> res
        self.assertEqual(res, QDateTime(date, time))
        self.assertTrue(res.isValid())
        self.assertFalse(res.isNull())
예제 #11
0
def 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.

Args:
    cmd: A str command to be executed.
    host: A QtHostAddress to the `CommandServer`.
    port: An int of port at which `CommandServer` is listening.

Kwargs:
    wait_time: An int setting miliseconds 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.
    """
    # Try to connect to a host server
    tcpSocket = QTcpSocket()
    tcpSocket.connectToHost(host, port, QIODevice.ReadWrite)
    if not tcpSocket.waitForConnected(msecs=wait_time):
        return CLIENT_ERROR_NO_CONNECTION

    # Prepare a command message to be sent
    block = QByteArray()
    outstr = QDataStream(block, QIODevice.WriteOnly)
    outstr.setVersion(QDataStream.Qt_4_0)
    outstr.writeUInt16(0)
    outstr.writeQString(cmd)
    outstr.device().seek(0)
    outstr.writeUInt16(block.size() - 2)
    tcpSocket.write(block)

    # Try to send the message
    if not tcpSocket.waitForBytesWritten(msecs=wait_time):
        return CLIENT_ERROR_BLOCK_NOT_WRITTEN

    # Wait for a response from the host server
    if not tcpSocket.waitForReadyRead(msecs=10000):
        return CLIENT_ERROR_NO_RESPONSE

    # Try to read the response
    instr = QDataStream(tcpSocket)
    instr.setVersion(QDataStream.Qt_4_0)
    blockSize = 0
    if blockSize == 0:
        if tcpSocket.bytesAvailable() < 2:
            return CLIENT_ERROR_RESPONSE_NOT_COMPLETE
        blockSize = instr.readUInt16()
    if tcpSocket.bytesAvailable() < blockSize:
        return CLIENT_ERROR_RESPONSE_NOT_COMPLETE

    # Wait until the host server terminates the connection
    tcpSocket.waitForDisconnected()

    # Return value representing a command execution status
    if instr.readString() == COMMAND_EXECUTED_CONFIRMATION_MESSAGE:
        return CLIENT_COMMAND_EXECUTED
    else:
        return CLIENT_COMMAND_FAILED
예제 #12
0
    def 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 successfull, the command `cmd` is sent.
Then the response is expected. If the response is equal to
COMMAND_EXECUTED_CONFIRMATION_MESSAGE, then the execution was successfull.
The progress and result of `sendCommand` can be obtained from printed logs and
return value.

Args:
    cmd: A 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.
        """

        # connect a Qt slot to receive and print errors
        self.tcpSocket.error.connect(self.displayError)

        # Try to connect to a host server
        self.tcpSocket.connectToHost(self.host, self.port, QIODevice.ReadWrite)
        if not self.tcpSocket.waitForConnected(msecs=WAIT_TIME_MS):
            if "FreeCAD" in sys.modules:
                FreeCAD.Console.PrintError(
                    "CommandClient.sendCommand error: " + "No connection\n")
            else:
                print("CommandClient.sendCommand error: No connection\n")
            return CLIENT_ERROR_NO_CONNECTION

        # Prepare a command message to be sent
        block = QByteArray()
        outstr = QDataStream(block, QIODevice.WriteOnly)
        outstr.setVersion(QDataStream.Qt_4_0)
        outstr.writeUInt16(0)
        outstr.writeQString(cmd)
        outstr.device().seek(0)
        outstr.writeUInt16(block.size() - 2)

        # Try to send the message
        if "FreeCAD" in sys.modules:
            FreeCAD.Console.PrintMessage("CommandClient sending> " + cmd +
                                         "\n")
        else:
            print("CommandClient sending> " + cmd + "\n")
        self.tcpSocket.write(block)
        if not self.tcpSocket.waitForBytesWritten(msecs=WAIT_TIME_MS):
            if "FreeCAD" in sys.modules:
                FreeCAD.Console.PrintError(
                    "CommandClient.sendCommand error: " +
                    "Block not written\n")
            else:
                print("CommandClient.sendCommand error: Block not written\n")
            return CLIENT_ERROR_BLOCK_NOT_WRITTEN

        # Wait for a response from the host server
        if not self.tcpSocket.waitForReadyRead(msecs=WAIT_TIME_MS):
            if "FreeCAD" in sys.modules:
                FreeCAD.Console.PrintError(
                    "CommandClient.sendCommand error: " +
                    "No response received.\n")
            else:
                print("CommandClient.sendCommand error: " +
                      "No response received.\n")
            return CLIENT_ERROR_NO_RESPONSE

        # Try to read the response
        instr = QDataStream(self.tcpSocket)
        instr.setVersion(QDataStream.Qt_4_0)
        if self.blockSize == 0:
            if self.tcpSocket.bytesAvailable() < 2:
                return CLIENT_ERROR_RESPONSE_NOT_COMPLETE
            self.blockSize = instr.readUInt16()

        if self.tcpSocket.bytesAvailable() < self.blockSize:
            return CLIENT_ERROR_RESPONSE_NOT_COMPLETE
        response = instr.readString()
        if "FreeCAD" in sys.modules:
            FreeCAD.Console.PrintMessage("CommandClient received> " +
                                         response + "\n")
        else:
            print("CommandClient received> " + response + "\n")

        # Wait until the host server terminates the connection
        self.tcpSocket.waitForDisconnected()
        # Reset blockSize to prepare for sending next command
        self.blockSize = 0

        # Return value representing a command execution status
        if response == COMMAND_EXECUTED_CONFIRMATION_MESSAGE:
            return CLIENT_COMMAND_EXECUTED
        else:
            return CLIENT_COMMAND_FAILED
예제 #13
0
    def 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 incomming 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 *Commmand
executed successfully* otherwise. Then the thread is terminated.
        """
        # Try to connect to an incomming tcp socket using its socket descriptor
        tcpSocket = QTcpSocket()
        if not tcpSocket.setSocketDescriptor(self.socketDescriptor):
            FreeCAD.Console.PrintError("Socket not accepted.\n")
            return

        # Wait for an incomming message
        if not tcpSocket.waitForReadyRead(msecs=WAIT_TIME_MS):
            FreeCAD.Console.PrintError("No request send.\n")
            return

        # Make an input data stream
        instr = QDataStream(tcpSocket)
        instr.setVersion(QDataStream.Qt_4_0)

        # Try to read the message size
        if self.blockSize == 0:
            if tcpSocket.bytesAvailable() < 2:
                return
            self.blockSize = instr.readUInt16()

        # Check message is sent complete
        if tcpSocket.bytesAvailable() < self.blockSize:
            return

        # Read message and inform about it
        instr = instr.readString()
        FreeCAD.Console.PrintLog("CommandServer received> " + str(instr) +
                                 "\n")

        # Try to execute the message string and prepare  a response
        try:
            exec(str(instr))
        except Exception as e:
            FreeCAD.Console.PrintError("Executing external command failed:" +
                                       str(e) + "\n")
            message = "Command failed - " + str(e)
        else:
            FreeCAD.Console.PrintLog("Executing external command succeded!\n")
            message = COMMAND_EXECUTED_CONFIRMATION_MESSAGE

        # Prepare the data block to send back and inform about it
        FreeCAD.Console.PrintLog("CommandServer sending> " + message + " \n")
        block = QByteArray()
        outstr = QDataStream(block, QIODevice.WriteOnly)
        outstr.setVersion(QDataStream.Qt_4_0)
        outstr.writeUInt16(0)
        outstr.writeQString(message)
        outstr.device().seek(0)
        outstr.writeUInt16(block.size() - 2)

        # Send the block, disconnect from the socket and terminate the QThread
        tcpSocket.write(block)
        tcpSocket.disconnectFromHost()
        tcpSocket.waitForDisconnected()