def test_reception_b(self): """Test that the connection receives what has been enqueued in the input file.""" # a message containing delimiters and newline characters msg = b"\x08\x02\x12\x011\x1a\x011 \x01:,\n*0x32468d\n,\nB8Ab795\n\n49B49C88DC991990E7910891,,dbd\n" protocol_specification_id = PublicId.from_str( "some_author/some_name:0.1.0") encoded_envelope = "{}{}{}{}{}{}{}{}".format( "any", SEPARATOR, "any", SEPARATOR, protocol_specification_id, SEPARATOR, msg.decode("utf-8"), SEPARATOR, ) encoded_envelope = encoded_envelope.encode("utf-8") with open(self.input_file_path, "ab+") as f: write_with_lock(f, encoded_envelope) actual_envelope = self.multiplexer.get(block=True, timeout=3.0) assert "any" == actual_envelope.to assert "any" == actual_envelope.sender assert protocol_specification_id == actual_envelope.protocol_specification_id assert msg == actual_envelope.message
def run(): """Run demo.""" # 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 an addresses identity: identity = Identity(name="my_agent", address="some_address") # Set up the stub connection configuration = ConnectionConfig( input_file_path=INPUT_FILE, output_file_path=OUTPUT_FILE, connection_id=StubConnection.connection_id, ) stub_connection = StubConnection(configuration=configuration, data_dir=".", identity=identity) # Create our Agent my_agent = MyAgent(identity, [stub_connection]) # Set the agent running in a different thread try: t = Thread(target=my_agent.start) 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 agent message_text = b"my_agent,other_agent,fetchai/default:0.1.0,\x12\r\x08\x01*\t*\x07\n\x05hello," with open(INPUT_FILE, "wb") as f: write_with_lock(f, message_text) # Wait for the envelope to get processed time.sleep(2) # Read the output envelope generated by the agent with open(OUTPUT_FILE, "rb") as f: print("output message: " + f.readline().decode("utf-8")) finally: # Shut down the agent my_agent.stop() t.join()
def test_reception_c(self): """Test that the connection receives what has been enqueued in the input file.""" encoded_envelope = b"0x5E22777dD831A459535AA4306AceC9cb22eC4cB5,default_oef,fetchai/oef_search:0.10.0,\x08\x02\x12\x011\x1a\x011 \x01:,\n*0x32468dB8Ab79549B49C88DC991990E7910891dbd," expected_envelope = Envelope( to="0x5E22777dD831A459535AA4306AceC9cb22eC4cB5", sender="default_oef", protocol_id=OefSearchMessage.protocol_id, message= b"\x08\x02\x12\x011\x1a\x011 \x01:,\n*0x32468dB8Ab79549B49C88DC991990E7910891dbd", ) with open(self.input_file_path, "ab+") as f: write_with_lock(f, encoded_envelope) actual_envelope = self.multiplexer.get(block=True, timeout=3.0) assert expected_envelope == actual_envelope
def run(): """Run demo.""" # Create a private key create_private_key(FetchAICrypto.identifier, FETCHAI_PRIVATE_KEY_FILE) # 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) # Instantiate the builder and build the AEA # By default, the default protocol, error skill and stub connection are added builder = AEABuilder() builder.set_name("my_aea") builder.add_private_key(FetchAICrypto.identifier, FETCHAI_PRIVATE_KEY_FILE) # Add the echo skill (assuming it is present in the local directory 'packages') builder.add_skill("./packages/fetchai/skills/echo") # create skill and handler manually from aea.protocols.base import Message from aea.skills.base import Handler from packages.fetchai.protocols.default.message import DefaultMessage class DummyHandler(Handler): """Dummy handler to handle messages.""" SUPPORTED_PROTOCOL = DefaultMessage.protocol_id def setup(self) -> None: """Noop setup.""" def teardown(self) -> None: """Noop teardown.""" def handle(self, message: Message) -> None: """Handle incoming message.""" self.context.logger.info("You got a message: {}".format(str(message))) config = SkillConfig(name="test_skill", author="fetchai") skill = Skill(configuration=config) dummy_handler = DummyHandler( name="dummy_handler", skill_context=skill.skill_context ) skill.handlers.update({dummy_handler.name: dummy_handler}) builder.add_component_instance(skill) # Create our AEA my_aea = builder.build() # Set the AEA running in a different thread try: t = Thread(target=my_aea.start) t.start() # Wait for everything to start up time.sleep(4) # Create a message inside an envelope and get the stub connection to pass it on to the echo skill message_text = b"my_aea,other_agent,fetchai/default:0.9.0,\x12\x10\x08\x01\x12\x011*\t*\x07\n\x05hello," with open(INPUT_FILE, "wb") as f: write_with_lock(f, message_text) print(b"input message: " + message_text) # Wait for the envelope to get processed time.sleep(4) # Read the output envelope generated by the echo skill with open(OUTPUT_FILE, "rb") as f: print(b"output message: " + f.readline()) finally: # Shut down the AEA my_aea.stop() t.join() t = None
def run(): """Run demo.""" # 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 configuration = ConnectionConfig( input_file=INPUT_FILE, output_file=OUTPUT_FILE, connection_id=StubConnection.connection_id, ) stub_connection = StubConnection( configuration=configuration, data_dir=".", identity=Identity("some_agent", "some_address"), ) multiplexer = Multiplexer([stub_connection], protocols=[DefaultMessage]) 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: write_with_lock(f, 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_specification_id={}, message={}" .format( envelope.sender, envelope.to, envelope.protocol_specification_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()