Example #1
0
    def send_message_to_node(self, instruction, content, nodeToSend):
        """
        @function_name      :   send_message_to_node
        @parameters         :   instruction -> str, content -> str, nodeToSend -> str
        @return             :   result := stub.send(msg)
        @description        :   sends a message to a node given a instruction, content, and node_id
        """

        print("\"" + instruction + "\" request from", content[0], "to", nodeToSend)
        '''set the channel and create the stub'''

        if nodeToSend in self.channels:
            channel = self.channels[nodeToSend]
        else:
            address = nodeToSend + ':50051'
            channel = grpc.insecure_channel(address)
            self.channels[nodeToSend] = channel
        stub = messenger_pb2_grpc.messengerStub(channel)

        msg = messenger_pb2.Msg()
        msg.instruction = instruction

        msg.content[:] = content

        # response = stub.send(request=msg)
        response_future = stub.send.future(request=msg)
        response = response_future.result()
Example #2
0
    def initial_wake(self):
        '''set the channel and create the stub'''
        address = 'node0:50051'
        channel = grpc.insecure_channel(address)

        stub = messenger_pb2_grpc.messengerStub(channel)

        msg = messenger_pb2.Msg()
        msg.instruction = 'wakeup'

        msg.content[:] = []

        response = stub.send(request=msg)
Example #3
0
    def disperse_neighbors(self):
        """
        @function_name      :   disperse_neighbors
        @parameters         :   none
        @return             :   result := void
        @description        :   creates a channel and sends a message containing the node_id, neighbors[] and weights[]
        """
        for info in self.node_info:
            print(info)
            '''set the channel and create the stub'''
            address = info[0] + ':50051'
            channel = grpc.insecure_channel(address)

            stub = messenger_pb2_grpc.messengerStub(channel)

            msg = messenger_pb2.Msg()
            msg.instruction = 'neighbors'

            msg.content[:] = [info[0], info[1], info[2]]

            response = stub.send(request=msg)
    def send_message(self):

        n1 = chat.WarningMsg
        try:
            while True:
                msg = input("[" + self.name + "] > ")
                message = "[" + self.name + "] " + msg
                n = chat.Msg()
                n.name = self.name
                n.othername = self.othername

                encrypted_msg = self.encrypt_message(message)
                n.nonce = encrypted_msg[0]
                n.ciphertext = encrypted_msg[1]
                n.tag = encrypted_msg[2]

                n1 = self.conn.SendMessage(n)
                for w in n1:
                    if(w.warning != ""):
                        print(w.warning)
        except KeyboardInterrupt:
            print("\nBye {}".format(name))
Example #5
0
    def ChatStream(self, request: chat.User, context):

        n = chat.Msg()
        users = [request.name, request.othername]
        sorted_users = sorted(users)
        my_key = sorted_users[0] + '-' + sorted_users[1]
        key2 = users[0] + '-' + users[1]
        n.name = users[0]
        n.othername = users[1]

        sender = users[0]
        receiver = users[1]

        #using lru cache to stream new messages

        while True:

            if self.unreadMessages[sender].get(receiver) == None:
                self.unreadMessages[sender][receiver] = 0
            if self.unreadMessages[receiver].get(sender) == None:
                self.unreadMessages[receiver][sender] = 0

            if(my_key in self.messages):
                new_messages = self.messages[my_key].show()

                while self.unreadMessages[sender][receiver] > 0:
                    index = len(new_messages) - self.unreadMessages[sender][receiver]
                    try:
                        n = new_messages[index]
                    except KeyError:
                        continue

                    if self.unreadMessages[sender][receiver] > 0:
                        self.unreadMessages[sender][receiver] -= 1
                    yield n
            time.sleep(0.1)