def test_from_valid_json(self):
        json = {"identity": "a", "command": "b", "arguments": "c"}
        message = InternalMessage.from_json(json)
        self.assertEqual(message.get_identity(), "a")
        self.assertEqual(message.get_command(), "b")
        self.assertEqual(message.get_arguments(), "c")

        json = {"identity": "a", "command": "b"}
        message = InternalMessage.from_json(json)
        self.assertEqual(message.get_identity(), "a")
        self.assertEqual(message.get_command(), "b")
        self.assertEqual(message.get_arguments(), "")
Exemplo n.º 2
0
    def run(self):
        poller = zmq.Poller()
        poller.register(self.puller, zmq.POLLIN)
        poller.register(self.sub, zmq.POLLIN)

        while True:
            sockets = dict(poller.poll())

            if self.puller in sockets and sockets[self.puller] == zmq.POLLIN:
                json = self.puller.recv_json()
                internal = InternalMessage.from_json(json)
                self.from_client(internal)
            if self.sub in sockets and sockets[self.sub] == zmq.POLLIN:
                topic, message = self.sub.recv_multipart()
                self.from_broadcast(topic.decode(), message.decode())
Exemplo n.º 3
0
 def receive_from_internal(self, json):
     internal = InternalMessage.from_json(json)
     if not internal.is_valid():
         return
     if internal.get_command() in self.commands.keys():
         self.commands[internal.get_command()](self, internal)