예제 #1
0
 def _notify(self, msg):
     # print("_notify: " + msg)
     msg_params = Message()
     msg_params.init_from_json_string(str(msg))
     msg_type = msg_params.get_type()
     for observer in self._observers:
         observer.receive_message(msg_type, msg_params)
예제 #2
0
    def send_message(self, msg: Message):
        payload = msg.to_json()

        receiver_id = msg.get_receiver_id()

        if receiver_id == 0:
            channel_url = '{}:{}'.format('67.199.133.186',
                                         str(50000 + receiver_id))
        elif receiver_id == 1:
            channel_url = '{}:{}'.format('67.199.133.186',
                                         str(50000 + receiver_id))
        elif receiver_id == 2:
            channel_url = '{}:{}'.format('117.161.90.229',
                                         str(50000 + receiver_id))

        channel = grpc.insecure_channel(channel_url)
        stub = grpc_comm_manager_pb2_grpc.gRPCCommManagerStub(channel)

        request = grpc_comm_manager_pb2.CommRequest()
        logging.info("sending message to port " +
                     str(50000 + int(msg.get_receiver_id())))

        request.client_id = self.client_id

        request.message = payload

        stub.sendMessage(request)
        logging.info("sent")
예제 #3
0
 def message_handling_subroutine(self):
     while True:
         if self.grpc_servicer.message_q.qsize() > 0:
             lock.acquire()
             msg_params_string = self.grpc_servicer.message_q.get()
             msg_params = Message()
             msg_params.init_from_json_string(msg_params_string)
             msg_type = msg_params.get_type()
             for observer in self._observers:
                 observer.receive_message(msg_type, msg_params)
             lock.release()
예제 #4
0
    def send_message(self, msg: Message):
        payload = msg.to_json()

        receiver_id = msg.get_receiver_id()

        # lookup ip of receiver from self.ip_config table
        receiver_ip = self.ip_config[str(receiver_id)]
        channel_url = '{}:{}'.format(receiver_ip, str(50000 + receiver_id))

        channel = grpc.insecure_channel(channel_url, options=self.opts)
        stub = grpc_comm_manager_pb2_grpc.gRPCCommManagerStub(channel)

        request = grpc_comm_manager_pb2.CommRequest()
        logging.info("sending message to port " + str(50000 + receiver_id))

        request.client_id = self.client_id

        request.message = payload

        stub.sendMessage(request)
        logging.info("sent")
        channel.close()
예제 #5
0
    def send_message(self, msg: Message):
        """
            [server]
            sending message topic (publish): serverID_clientID
            receiving message topic (subscribe): clientID

            [client]
            sending message topic (publish): clientID
            receiving message topic (subscribe): serverID_clientID

        """
        if self.client_id == 0:
            # server
            receiver_id = msg.get_receiver_id()
            topic = self._topic + str(0) + "_" + str(receiver_id)
            logging.info("topic = %s" % str(topic))
            payload = msg.to_json()
            self._client.publish(topic, payload=payload)
            logging.info("sent")
        else:
            # client
            self._client.publish(self._topic + str(self.client_id),
                                 payload=msg.to_json())
예제 #6
0
 def notify(self, message: Message):
     msg_type = message.get_type()
     for observer in self._observers:
         observer.receive_message(msg_type, message)
예제 #7
0
        else:
            # client
            self._client.publish(self._topic + str(self.client_id),
                                 payload=msg.to_json())

    def handle_receive_message(self):
        pass

    def stop_receive_message(self):
        pass


if __name__ == '__main__':

    class Obs(Observer):
        def receive_message(self, msg_type, msg_params) -> None:
            print("receive_message(%s, %s)" %
                  (msg_type, msg_params.to_string()))

    client = MqttCommManager("127.0.0.1", 1883)
    client.add_observer(Obs())
    time.sleep(3)
    print('client ID:%s' % client.client_id)

    message = Message(0, 1, 2)
    message.add_params("key1", 1)
    client.send_message(message)

    time.sleep(10)
    print("client, send Fin...")