Esempio n. 1
0
def test_get_from_multiplexer_when_empty():
    """Test that getting an envelope from the multiplexer when the input queue is empty raises an exception."""
    connection = DummyConnection(connection_id="dummy")
    multiplexer = Multiplexer([connection])

    with pytest.raises(aea.mail.base.Empty):
        multiplexer.get()
Esempio n. 2
0
def test_multiple_connection():
    """Test that we can send a message with two different connections."""
    with LocalNode() as node:
        address_1 = "address_1"
        address_2 = "address_2"
        connection_1_id = PublicId.from_str("author/local_1:0.1.0")
        connection_2_id = PublicId.from_str("author/local_2:0.1.0")

        connection_1 = OEFLocalConnection(node,
                                          address=address_1,
                                          connection_id=connection_1_id)

        connection_2 = OEFLocalConnection(node,
                                          address=address_2,
                                          connection_id=connection_2_id)

        multiplexer = Multiplexer([connection_1, connection_2])

        assert not connection_1.connection_status.is_connected
        assert not connection_2.connection_status.is_connected

        multiplexer.connect()

        assert connection_1.connection_status.is_connected
        assert connection_2.connection_status.is_connected

        message = DefaultMessage(
            dialogue_reference=("", ""),
            message_id=1,
            target=0,
            performative=DefaultMessage.Performative.BYTES,
            content=b"hello",
        )
        envelope_from_1_to_2 = Envelope(
            to=address_2,
            sender=address_1,
            protocol_id=DefaultMessage.protocol_id,
            message=DefaultSerializer().encode(message),
            context=EnvelopeContext(connection_id=connection_1_id),
        )
        multiplexer.put(envelope_from_1_to_2)

        actual_envelope = multiplexer.get(block=True, timeout=2.0)
        assert envelope_from_1_to_2 == actual_envelope

        envelope_from_2_to_1 = Envelope(
            to=address_1,
            sender=address_2,
            protocol_id=DefaultMessage.protocol_id,
            message=DefaultSerializer().encode(message),
            context=EnvelopeContext(connection_id=connection_2_id),
        )
        multiplexer.put(envelope_from_2_to_1)

        actual_envelope = multiplexer.get(block=True, timeout=2.0)
        assert envelope_from_2_to_1 == actual_envelope

        multiplexer.disconnect()
def run():
    # Ensure the input and output files do not exist initially
    if os.path.isfile(INPUT_FILE):
        os.remove(INPUT_FILE)
    if os.path.isfile(OUTPUT_FILE):
        os.remove(OUTPUT_FILE)

    # create the connection and multiplexer objects
    stub_connection = StubConnection(input_file_path=INPUT_FILE,
                                     output_file_path=OUTPUT_FILE)
    multiplexer = Multiplexer([stub_connection])
    try:
        # Set the multiplexer running in a different thread
        t = Thread(target=multiplexer.connect)
        t.start()

        # Wait for everything to start up
        time.sleep(3)

        # Create a message inside an envelope and get the stub connection to pass it into the multiplexer
        message_text = (
            "multiplexer,some_agent,fetchai/default:0.1.0,\x08\x01*\x07\n\x05hello,"
        )
        with open(INPUT_FILE, "w") as f:
            f.write(message_text)

        # Wait for the envelope to get processed
        time.sleep(2)

        # get the envelope
        envelope = multiplexer.get()  # type: Optional[Envelope]
        assert envelope is not None

        # Inspect its contents
        print(
            "Envelope received by Multiplexer: sender={}, to={}, protocol_id={}, message={}"
            .format(envelope.sender, envelope.to, envelope.protocol_id,
                    envelope.message))

        # Create a mirrored response envelope
        response_envelope = copy(envelope)
        response_envelope.to = envelope.sender
        response_envelope.sender = envelope.to

        # Send the envelope back
        multiplexer.put(response_envelope)

        # Read the output envelope generated by the multiplexer
        with open(OUTPUT_FILE, "r") as f:
            print("Envelope received from Multiplexer: " + f.readline())
    finally:
        # Shut down the multiplexer
        multiplexer.disconnect()
        t.join()
Esempio n. 4
0
def test_multiple_connection():
    """Test that we can send a message with two different connections."""
    with LocalNode() as node:
        public_key_1 = "public_key_1"
        public_key_2 = "public_key_2"
        connection_1_id = "local_1"
        connection_2_id = "local_2"

        connection_1 = OEFLocalConnection(public_key_1, node, connection_id=connection_1_id)
        connection_2 = OEFLocalConnection(public_key_2, node, connection_id=connection_2_id)
        multiplexer = Multiplexer([connection_1, connection_2])

        assert not connection_1.connection_status.is_connected
        assert not connection_2.connection_status.is_connected

        multiplexer.connect()

        assert connection_1.connection_status.is_connected
        assert connection_2.connection_status.is_connected

        message = DefaultMessage(type=DefaultMessage.Type.BYTES, content=b"hello")
        envelope_from_1_to_2 = Envelope(to=public_key_2, sender=public_key_1, protocol_id=DefaultMessage.protocol_id,
                                        message=DefaultSerializer().encode(message),
                                        context=EnvelopeContext(connection_id=connection_1_id))
        multiplexer.put(envelope_from_1_to_2)

        actual_envelope = multiplexer.get(block=True, timeout=2.0)
        assert envelope_from_1_to_2 == actual_envelope

        envelope_from_2_to_1 = Envelope(to=public_key_1, sender=public_key_2, protocol_id=DefaultMessage.protocol_id,
                                        message=DefaultSerializer().encode(message),
                                        context=EnvelopeContext(connection_id=connection_2_id))
        multiplexer.put(envelope_from_2_to_1)

        actual_envelope = multiplexer.get(block=True, timeout=2.0)
        assert envelope_from_2_to_1 == actual_envelope

        multiplexer.disconnect()
Esempio n. 5
0
def test_communication():
    """Test that two multiplexer can communicate through the node."""
    with LocalNode() as node:

        multiplexer1 = Multiplexer([OEFLocalConnection("multiplexer1", node)])
        multiplexer2 = Multiplexer([OEFLocalConnection("multiplexer2", node)])

        multiplexer1.connect()
        multiplexer2.connect()

        msg = DefaultMessage(type=DefaultMessage.Type.BYTES, content=b"hello")
        msg_bytes = DefaultSerializer().encode(msg)
        envelope = Envelope(to="multiplexer2",
                            sender="multiplexer1",
                            protocol_id=DefaultMessage.protocol_id,
                            message=msg_bytes)
        multiplexer1.put(envelope)

        msg = FIPAMessage((str(0), ''),
                          0,
                          0,
                          FIPAMessage.Performative.CFP,
                          query=None)
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(to="multiplexer2",
                            sender="multiplexer1",
                            protocol_id=FIPAMessage.protocol_id,
                            message=msg_bytes)
        multiplexer1.put(envelope)

        msg = FIPAMessage((str(0), str(1)),
                          0,
                          0,
                          FIPAMessage.Performative.PROPOSE,
                          proposal=[])
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(to="multiplexer2",
                            sender="multiplexer1",
                            protocol_id=FIPAMessage.protocol_id,
                            message=msg_bytes)
        multiplexer1.put(envelope)

        msg = FIPAMessage((str(0), str(1)), 0, 0,
                          FIPAMessage.Performative.ACCEPT)
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(to="multiplexer2",
                            sender="multiplexer1",
                            protocol_id=FIPAMessage.protocol_id,
                            message=msg_bytes)
        multiplexer1.put(envelope)

        msg = FIPAMessage((str(0), str(1)), 0, 0,
                          FIPAMessage.Performative.DECLINE)
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(to="multiplexer2",
                            sender="multiplexer1",
                            protocol_id=FIPAMessage.protocol_id,
                            message=msg_bytes)
        multiplexer1.put(envelope)

        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = DefaultSerializer().decode(envelope.message)
        assert envelope.protocol_id == "default"
        assert msg.get("content") == b"hello"
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FIPASerializer().decode(envelope.message)
        assert envelope.protocol_id == "fipa"
        assert msg.get("performative") == FIPAMessage.Performative.CFP
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FIPASerializer().decode(envelope.message)
        assert envelope.protocol_id == "fipa"
        assert msg.get("performative") == FIPAMessage.Performative.PROPOSE
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FIPASerializer().decode(envelope.message)
        assert envelope.protocol_id == "fipa"
        assert msg.get("performative") == FIPAMessage.Performative.ACCEPT
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FIPASerializer().decode(envelope.message)
        assert envelope.protocol_id == "fipa"
        assert msg.get("performative") == FIPAMessage.Performative.DECLINE
        multiplexer1.disconnect()
        multiplexer2.disconnect()
Esempio n. 6
0
def test_communication():
    """Test that two multiplexer can communicate through the node."""
    with LocalNode() as node:

        multiplexer1 = Multiplexer([_make_local_connection("multiplexer1", node)])
        multiplexer2 = Multiplexer([_make_local_connection("multiplexer2", node)])

        multiplexer1.connect()
        multiplexer2.connect()

        msg = DefaultMessage(
            dialogue_reference=("", ""),
            message_id=1,
            target=0,
            performative=DefaultMessage.Performative.BYTES,
            content=b"hello",
        )
        msg_bytes = DefaultSerializer().encode(msg)
        envelope = Envelope(
            to="multiplexer2",
            sender="multiplexer1",
            protocol_id=DefaultMessage.protocol_id,
            message=msg_bytes,
        )
        multiplexer1.put(envelope)

        msg = FipaMessage(
            performative=FipaMessage.Performative.CFP,
            dialogue_reference=(str(0), ""),
            message_id=1,
            target=0,
            query=Query([Constraint("something", ConstraintType(">", 1))]),
        )
        msg_bytes = FipaSerializer().encode(msg)
        envelope = Envelope(
            to="multiplexer2",
            sender="multiplexer1",
            protocol_id=FipaMessage.protocol_id,
            message=msg_bytes,
        )
        multiplexer1.put(envelope)

        msg = FipaMessage(
            performative=FipaMessage.Performative.PROPOSE,
            dialogue_reference=(str(0), ""),
            message_id=2,
            target=1,
            proposal=Description({}),
        )

        msg_bytes = FipaSerializer().encode(msg)
        envelope = Envelope(
            to="multiplexer2",
            sender="multiplexer1",
            protocol_id=FipaMessage.protocol_id,
            message=msg_bytes,
        )
        multiplexer1.put(envelope)

        msg = FipaMessage(
            performative=FipaMessage.Performative.ACCEPT,
            dialogue_reference=(str(0), ""),
            message_id=1,
            target=0,
        )
        msg_bytes = FipaSerializer().encode(msg)
        envelope = Envelope(
            to="multiplexer2",
            sender="multiplexer1",
            protocol_id=FipaMessage.protocol_id,
            message=msg_bytes,
        )
        multiplexer1.put(envelope)

        msg = FipaMessage(
            performative=FipaMessage.Performative.DECLINE,
            dialogue_reference=(str(0), ""),
            message_id=1,
            target=0,
        )
        msg_bytes = FipaSerializer().encode(msg)
        envelope = Envelope(
            to="multiplexer2",
            sender="multiplexer1",
            protocol_id=FipaMessage.protocol_id,
            message=msg_bytes,
        )
        multiplexer1.put(envelope)

        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = DefaultSerializer().decode(envelope.message)
        assert envelope.protocol_id == DefaultMessage.protocol_id
        assert msg.content == b"hello"
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FipaSerializer().decode(envelope.message)
        assert envelope.protocol_id == FipaMessage.protocol_id
        assert msg.performative == FipaMessage.Performative.CFP
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FipaSerializer().decode(envelope.message)
        assert envelope.protocol_id == FipaMessage.protocol_id
        assert msg.performative == FipaMessage.Performative.PROPOSE
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FipaSerializer().decode(envelope.message)
        assert envelope.protocol_id == FipaMessage.protocol_id
        assert msg.performative == FipaMessage.Performative.ACCEPT
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FipaSerializer().decode(envelope.message)
        assert envelope.protocol_id == FipaMessage.protocol_id
        assert msg.performative == FipaMessage.Performative.DECLINE
        multiplexer1.disconnect()
        multiplexer2.disconnect()
Esempio n. 7
0
def test_communication():
    """Test that two multiplexer can communicate through the node."""
    with LocalNode() as node:

        local_public_id = PublicId("fetchai", "local", "0.1.0")
        multiplexer1 = Multiplexer([
            OEFLocalConnection("multiplexer1",
                               node,
                               connection_id=local_public_id)
        ])
        multiplexer2 = Multiplexer([
            OEFLocalConnection("multiplexer2",
                               node,
                               connection_id=local_public_id)
        ])

        multiplexer1.connect()
        multiplexer2.connect()

        msg = DefaultMessage(
            dialogue_reference=("", ""),
            message_id=1,
            target=0,
            performative=DefaultMessage.Performative.BYTES,
            content=b"hello",
        )
        msg_bytes = DefaultSerializer().encode(msg)
        envelope = Envelope(
            to="multiplexer2",
            sender="multiplexer1",
            protocol_id=DefaultMessage.protocol_id,
            message=msg_bytes,
        )
        multiplexer1.put(envelope)

        msg = FIPAMessage((str(0), ""),
                          0,
                          0,
                          FIPAMessage.Performative.CFP,
                          query=None)
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(
            to="multiplexer2",
            sender="multiplexer1",
            protocol_id=FIPAMessage.protocol_id,
            message=msg_bytes,
        )
        multiplexer1.put(envelope)

        msg = FIPAMessage(
            (str(0), str(1)),
            0,
            0,
            FIPAMessage.Performative.PROPOSE,
            proposal=[],
        )
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(
            to="multiplexer2",
            sender="multiplexer1",
            protocol_id=FIPAMessage.protocol_id,
            message=msg_bytes,
        )
        multiplexer1.put(envelope)

        msg = FIPAMessage((str(0), str(1)), 0, 0,
                          FIPAMessage.Performative.ACCEPT)
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(
            to="multiplexer2",
            sender="multiplexer1",
            protocol_id=FIPAMessage.protocol_id,
            message=msg_bytes,
        )
        multiplexer1.put(envelope)

        msg = FIPAMessage((str(0), str(1)), 0, 0,
                          FIPAMessage.Performative.DECLINE)
        msg_bytes = FIPASerializer().encode(msg)
        envelope = Envelope(
            to="multiplexer2",
            sender="multiplexer1",
            protocol_id=FIPAMessage.protocol_id,
            message=msg_bytes,
        )
        multiplexer1.put(envelope)

        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = DefaultSerializer().decode(envelope.message)
        assert envelope.protocol_id == DefaultMessage.protocol_id
        assert msg.get("content") == b"hello"
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FIPASerializer().decode(envelope.message)
        assert envelope.protocol_id == FIPAMessage.protocol_id
        assert msg.get("performative") == FIPAMessage.Performative.CFP
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FIPASerializer().decode(envelope.message)
        assert envelope.protocol_id == FIPAMessage.protocol_id
        assert msg.get("performative") == FIPAMessage.Performative.PROPOSE
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FIPASerializer().decode(envelope.message)
        assert envelope.protocol_id == FIPAMessage.protocol_id
        assert msg.get("performative") == FIPAMessage.Performative.ACCEPT
        envelope = multiplexer2.get(block=True, timeout=1.0)
        msg = FIPASerializer().decode(envelope.message)
        assert envelope.protocol_id == FIPAMessage.protocol_id
        assert msg.get("performative") == FIPAMessage.Performative.DECLINE
        multiplexer1.disconnect()
        multiplexer2.disconnect()
Esempio n. 8
0
def test_soef():

    # First run OEF in a separate terminal: python scripts/oef/launch.py -c ./scripts/oef/launch_config.json
    crypto = FetchAICrypto()

    # create the connection and multiplexer objects
    soef_connection = SOEFConnection(
        api_key="TwiCIriSl0mLahw17pyqoA",
        soef_addr="soef.fetch.ai",
        soef_port=9002,
        address=crypto.address,
    )
    multiplexer = Multiplexer([soef_connection])
    try:
        # Set the multiplexer running in a different thread
        t = Thread(target=multiplexer.connect)
        t.start()

        time.sleep(3.0)

        # register a service with location
        attr_service_name = Attribute("service_name", str, True,
                                      "The name of the service.")
        attr_location = Attribute(
            "location", Location, True,
            "The location where the service is provided.")
        service_location_model = DataModel(
            "location_service",
            [attr_service_name, attr_location],
            "A data model to describe location of a service.",
        )
        service_name = "train"
        service_location = Location(52.2057092, 2.1183431)
        service_instance = {
            "service_name": service_name,
            "location": service_location
        }
        service_description = Description(service_instance,
                                          data_model=service_location_model)
        message = OefSearchMessage(
            performative=OefSearchMessage.Performative.REGISTER_SERVICE,
            service_description=service_description,
        )
        message_b = OefSearchSerializer().encode(message)
        envelope = Envelope(
            to="soef",
            sender=crypto.address,
            protocol_id=ProtocolId.from_str("fetchai/oef_search:0.1.0"),
            message=message_b,
        )
        logger.info(
            "Registering service={} at location=({},{}) by agent={}".format(
                service_name,
                service_location.latitude,
                service_location.longitude,
                crypto.address,
            ))
        multiplexer.put(envelope)

        # find agents near the previously registered service
        radius = 0.1
        matches_my_service_name = Constraint(
            "service_name", ConstraintType("==", service_name))
        close_to_my_service = Constraint(
            "location", ConstraintType("distance", (service_location, radius)))
        closeness_query = Query([matches_my_service_name, close_to_my_service],
                                model=service_location_model)
        message = OefSearchMessage(
            performative=OefSearchMessage.Performative.SEARCH_SERVICES,
            query=closeness_query,
        )
        message_b = OefSearchSerializer().encode(message)
        envelope = Envelope(
            to="soef",
            sender=crypto.address,
            protocol_id=ProtocolId.from_str("fetchai/oef_search:0.1.0"),
            message=message_b,
        )
        logger.info(
            "Searching for agents in radius={} of service={} at location=({},{})"
            .format(
                radius,
                service_name,
                service_location.latitude,
                service_location.longitude,
            ))
        multiplexer.put(envelope)
        time.sleep(4.0)

        # check for search results
        envelope = multiplexer.get()
        message = OefSearchSerializer().decode(envelope.message)
        assert len(message.agents) >= 0

    finally:
        # Shut down the multiplexer
        multiplexer.disconnect()
        t.join()