Beispiel #1
0
 def test_json_ndarray(self):
     # Arrange
     m = 10
     expected = np.arange(m)
     
     # Act
     dumped = dumps(expected)
     result = loads(dumped)
     
     # Assert
     self.assertEqual(expected.shape, result.shape, "Shape mismatch")
     self.assertEqual(expected.dtype, result.dtype, "Type mismatch %s != %s" % (expected.dtype, result.dtype))
     self.assertTrue(np.allclose(expected, result), "Value mismatch")
Beispiel #2
0
 def test_json_ndarray_in_list(self):
     # Arrange
     m = 10
     expected = [np.arange(i) for i in range(m)]
     
     # Act
     dumped = dumps(expected)
     result = loads(dumped)
     
     # Assert
     for i, expected_array in enumerate(expected):
         result_array = result[i]
         self.assertEqual(expected_array.shape, result_array.shape, "Shape mismatch")
         self.assertEqual(expected_array.dtype, result_array.dtype, "Type mismatch %s != %s" % (expected_array.dtype, result_array.dtype))
         self.assertTrue(np.allclose(expected_array, result_array), "Value mismatch")
Beispiel #3
0
    def test_json_ndarray_in_dict(self):
        # Arrange
        m = 10
        expected = {'0':np.arange(0),
                    '10':np.arange(10)}
        
        # Act
        dumped = dumps(expected)
        result = loads(dumped)

        # Assert
        for i, expected_array in expected.iteritems():
            result_array = result[i]
            self.assertEqual(expected_array.shape, result_array.shape, "Shape mismatch")
            self.assertEqual(expected_array.dtype, result_array.dtype, "Type mismatch %s != %s" % (expected_array.dtype, result_array.dtype))
            self.assertTrue(np.allclose(expected_array, result_array), "Value mismatch")
Beispiel #4
0
    def _handleNewPacket(self, dataRaw):
        try:
            data = jsonEncoder.loads(dataRaw)

            #commands needing 1 argument
            if len(data) < 2:
                raise Exception("packet with insufficient number of args")

            if data[0] == TYPE_NAK:
                raise Exception("server reported: %s" % data[1])

            #command needing 2 arguments
            if len(data) < 3:
                raise Exception("packet with insufficient number of args")

            if data[0] == TYPE_PUBLISH:
                self.handleEvent(data[1], data[2])
                return

            if data[0] == TYPE_INFO:
                self.receivedInfo.emit(data[1], data[2])
                return

            if data[0] == TYPE_RPC_REQUEST:
                self._handleRPCRequest(data[1], data[2])
                return

            if data[0] == TYPE_RPC_REPLY:
                if 'id' in data[2] and data[2]['id'] in self.rpcPendingRequests:
                    if self.rpcPendingRequests[data[2]['id']] != None:
                        self.rpcPendingRequests[data[2]['id']](data[1],data[2])
                    del(self.rpcPendingRequests[data[2]['id']])
                return

        except Exception as e:
            errorstr = type(e).__name__ + ", " + str(e)
            sys.stderr.write(errorstr + "\n")
Beispiel #5
0
    def _handleNewPacket(self, dataRaw):
        try:
            #TODO: decoding the whole data on server side should not be necessary.
            data = jsonEncoder.loads(dataRaw)

            if len(data) < 2:
                raise Exception("packet with insufficient number of args")

            # add the second arg to the list of rpcFunctions
            if data[0] == TYPE_RPC_REGISTER:
                if len(data) < 3:
                    raise Exception("packet with insufficient number of args")
                if not 'argList' in data[2]:
                    print("Could not register RPC call %s since argList was missing in registration packet" % (data[1]))
                    return
                if not 'retCount' in data[2]:
                    print("Could not register RPC call %s since retCount was missing in registration packet" % (data[1]))
                    return
                self.rpcFunctions.update({data[1]: data[2]})
                return

            # remove the second arg to the list of rpcFunctions
            if data[0] == TYPE_RPC_UNREGISTER:
                if data[1] in self.rpcFunctions:
                    del(self.rpcFunctions[data[1]])
                return

            # handle info
            if data[0] == TYPE_INFO:
                if len(data) < 2:
                    raise Exception("packet with insufficient number of args")
                if data[1] == INFO_RPC_LIST:
                    self.infoRequested.emit(data[1], self)
                return

            # handle RPC request
            if data[0] == TYPE_RPC_REQUEST:
                if len(data) < 3:
                    raise Exception("packet with insufficient number of args")
                self.rpcRequested.emit(data[1], data[2], self)
                return

            # handle RPC reply
            if data[0] == TYPE_RPC_REPLY:
                if len(data) < 3:
                    raise Exception("packet with insufficient number of args")
                if not data[1] in self.rpcFunctions:
                    print("Sending reply for unregistered Function %s" % (data[1]))
                    return
                if 'id' in data[2] and data[2]['id'] in self.rpcPendingRequests:
                    self.rpcPendingRequests[data[2]['id']].forwardEvent(data[1], data[2], pkgType=TYPE_RPC_REPLY)
                self.rpcReplied.emit(data[1], data[2])
                return

            # add the second arg to the list of subscriptions
            if data[0] == TYPE_SUBSCRIBE:
                self.subscriptions.add(data[1])
                return

            # remove the second arg to the list of subscriptions
            if data[0] == TYPE_UNSUBSCRIBE:
                self.subscriptions.remove(data[1])
                return

            # publish packet to the server
            if data[0] == TYPE_PUBLISH:
                if len(data) < 3:
                    raise Exception("packet with insufficient number of args")
                self.eventPublished.emit(data[1], data[2])
                return

            # packet not recognized
            raise Exception("unrecognized instruction in packet")

        except Exception as e:
            errorstr = type(e).__name__ + ", " + str(e)
            # notify the client about the error
            self._sendPacket([TYPE_NAK, errorstr])
            # print the error server side
            print("error reading packet:", errorstr)