コード例 #1
0
    def test_messaging_single_channel(self) -> None:
        """
        Basic messaging test.
        """

        receiver_public, receiver_private = generate_contact_key_pair()
        sender_public, sender_private = generate_contact_key_pair()

        sender_contact = Contact(id="sender",
                                 public_key=sender_public,
                                 host=self.localhost,
                                 port=self.port_range_min)

        receiver_contact = Contact(id="receiver",
                                   public_key=receiver_public,
                                   host=self.localhost,
                                   port=self.port_range_min)

        channel = "channel1"

        sender = MessageSender(receiver_contact)

        consumer = DebugConsumer()

        receiver = MessageReceiver(port=self.port_range_min,
                                   private_key=receiver_private,
                                   contacts=[sender_contact],
                                   notify_interval=self.notify_interval)
        receiver.register_consumer(channel, consumer)

        message = Message(channel, "test command", "test data")

        sender.send_message(message=message,
                            sender_contact_id=sender_contact.id,
                            private_key=sender_private)

        time.sleep(self.notify_interval * 2)

        def assertions():

            assert len(consumer.messages) == 1

            received_message_sender_id, received_message = consumer.messages[0]

            assert received_message_sender_id == sender_contact.id
            assert received_message == message

        self.assert_and_kill_receiver(assertions, receiver)
コード例 #2
0
    def new_node(self,
                 port: int,
                 parent_ab: AddressBook = None) -> AddressBook:

        new_node_pub, new_node_priv = generate_contact_key_pair()

        parent_id = "" if parent_ab is None else parent_ab.self_contact.id
        contacts = [] if parent_ab is None else parent_ab.contacts + [
            parent_ab.self_contact
        ]

        new_node_contact = Contact(host="127.0.0.1",
                                   id=generate_contact_id(parent_id),
                                   port=port,
                                   public_key=new_node_pub)

        new_node_ab = AddressBook(
            self_contact=new_node_contact,
            private_key=new_node_priv,
            contacts=contacts,
            receiver_notify_interval=self.receiver_notify_interval,
            contact_restore_timeout=self.contact_restore_timeout,
            inactive_nodes_ping_interval=self.inactive_nodes_ping_interval,
        )

        if parent_ab is not None:

            parent_ab.create_new_distributed_contact(new_node_contact)

        return new_node_ab
コード例 #3
0
    def test_wrong_receiver_public(self):
        """
        Tests that sending message with wrong key results in MessageDeliveryError.
        """

        receiver_public, receiver_private = generate_contact_key_pair()
        sender_public, sender_private = generate_contact_key_pair()

        wrong_public, wrong_private = generate_contact_key_pair()

        sender_contact = Contact(id="sender",
                                 public_key=sender_public,
                                 host=self.localhost,
                                 port=self.port_range_min)

        receiver_contact = Contact(id="receiver",
                                   public_key=wrong_public,
                                   host=self.localhost,
                                   port=self.port_range_min)

        channel = "channel1"

        sender = MessageSender(receiver_contact)

        consumer = DebugConsumer()

        receiver = MessageReceiver(port=self.port_range_min,
                                   private_key=receiver_private,
                                   contacts=[sender_contact],
                                   notify_interval=self.notify_interval)
        receiver.register_consumer(channel, consumer)

        message = Message(channel, "test command", "test data")

        sender.send_message(message=message,
                            sender_contact_id=sender_contact.id,
                            private_key=sender_private)

        time.sleep(self.notify_interval * 2)

        def assertions():

            assert len(consumer.messages) == 0

        self.assert_and_kill_receiver(assertions, receiver)
コード例 #4
0
ファイル: address_book.py プロジェクト: lamazzone/Dollynator
def _demo():  # pragma: no cover
    id_counter = 1
    port_counter = 8001

    ping_interval = 1
    restore_timeout = 0
    receiver_notify_interval = 0.5

    root_pub, root_priv = generate_contact_key_pair()
    root_contact = Contact(id=str(id_counter),
                           host="127.0.0.1",
                           port=port_counter,
                           public_key=root_pub)

    root = AddressBook(self_contact=root_contact,
                       private_key=root_priv,
                       contact_restore_timeout=restore_timeout,
                       inactive_nodes_ping_interval=ping_interval,
                       receiver_notify_interval=receiver_notify_interval)

    nodes = [root]

    while True:

        port_counter += 1
        id_counter += 1

        replicating_node = random.choice(nodes)

        print("Node " + replicating_node.self_contact.id + " replicates: \n")

        new_node_contact_list = replicating_node.contacts.copy()
        new_node_contact_list.append(replicating_node.self_contact)

        pub, priv = generate_contact_key_pair()

        new_node_contact = Contact(id=str(id_counter),
                                   host='127.0.0.1',
                                   port=port_counter,
                                   public_key=pub)

        new_node = AddressBook(
            self_contact=new_node_contact,
            private_key=priv,
            contacts=new_node_contact_list,
            contact_restore_timeout=restore_timeout,
            inactive_nodes_ping_interval=ping_interval,
            receiver_notify_interval=receiver_notify_interval)

        nodes.append(new_node)

        replicating_node.create_new_distributed_contact(new_node_contact)

        time.sleep(1)

        for node in nodes:

            contacts = ""

            for contact in node.contacts:
                contacts += contact.id + ", "

            print("Node " + node.self_contact.id + " has " +
                  str(len(node.contacts)) + " contacts: " + contacts)

        time.sleep(1)

        for i in range(int(len(nodes) / 3)):
            node_to_remove = random.choice(nodes)
            print("Killing node " + node_to_remove.self_contact.id)
            node_to_remove.receiver.kill()
            nodes.remove(node_to_remove)

        time.sleep(3)

        print("")
コード例 #5
0
    def test_messaging_multiple_channels(self):
        """
        Messaging test with multiple channels.
        """

        receiver_public, receiver_private = generate_contact_key_pair()
        sender_public, sender_private = generate_contact_key_pair()

        sender_contact = Contact(id="sender",
                                 public_key=sender_public,
                                 host=self.localhost,
                                 port=self.port_range_min)

        receiver_contact = Contact(id="receiver",
                                   public_key=receiver_public,
                                   host=self.localhost,
                                   port=self.port_range_min)

        channel1 = "channel1"
        channel2 = "channel2"

        sender = MessageSender(receiver_contact)

        consumer1 = DebugConsumer()
        consumer2 = DebugConsumer()

        receiver = MessageReceiver(port=self.port_range_min,
                                   private_key=receiver_private,
                                   contacts=[sender_contact],
                                   notify_interval=self.notify_interval)
        receiver.register_consumer(channel1, consumer1)
        receiver.register_consumer(channel2, consumer2)

        message1 = Message(channel1, "test command", "message 1")
        message2 = Message(channel2, "test command", "message 2")

        sender.send_message(message=message1,
                            sender_contact_id=sender_contact.id,
                            private_key=sender_private)

        sender.send_message(message=message2,
                            sender_contact_id=sender_contact.id,
                            private_key=sender_private)

        time.sleep(self.notify_interval * 2)

        def assertions():

            assert len(consumer1.messages) == 1
            assert len(consumer2.messages) == 1

            received_message_sender_id1, received_message1 = consumer1.messages[
                0]
            received_message_sender_id2, received_message2 = consumer2.messages[
                0]

            assert received_message_sender_id1 == sender_contact.id
            assert received_message_sender_id2 == sender_contact.id
            assert received_message1 == message1
            assert received_message2 == message2

        self.assert_and_kill_receiver(assertions, receiver)