async def test_load_from_dir(): """Test stub connection can be loaded from dir.""" StubConnection.from_dir( ROOT_DIR + "/packages/fetchai/connections/stub", Identity("name", "address"), CryptoStore(), )
def _run_interaction_channel(): loader = ConfigLoader.from_configuration_type(PackageType.AGENT) agent_configuration = loader.load(Path(DEFAULT_AEA_CONFIG_FILE).open()) agent_name = agent_configuration.name identity_stub = Identity(agent_name + "_interact", "interact") _load_packages(identity_stub) # load agent configuration file from packages.fetchai.connections.stub.connection import ( # noqa: F811 # pylint: disable=import-outside-toplevel DEFAULT_INPUT_FILE_NAME, DEFAULT_OUTPUT_FILE_NAME, StubConnection, ) from packages.fetchai.protocols.default.dialogues import ( # noqa: F811 # pylint: disable=import-outside-toplevel DefaultDialogue, DefaultDialogues, ) from packages.fetchai.protocols.default.message import ( # noqa: F811 # pylint: disable=import-outside-toplevel DefaultMessage, ) # load stub connection configuration = ConnectionConfig( input_file=DEFAULT_OUTPUT_FILE_NAME, output_file=DEFAULT_INPUT_FILE_NAME, connection_id=StubConnection.connection_id, ) stub_connection = StubConnection( configuration=configuration, identity=identity_stub ) multiplexer = Multiplexer([stub_connection]) inbox = InBox(multiplexer) outbox = OutBox(multiplexer) def role_from_first_message( # pylint: disable=unused-argument message: Message, receiver_address: Address ) -> BaseDialogue.Role: """Infer the role of the agent from an incoming/outgoing first message :param message: an incoming/outgoing first message :param receiver_address: the address of the receiving agent :return: The role of the agent """ return DefaultDialogue.Role.AGENT dialogues = DefaultDialogues(identity_stub.name, role_from_first_message) try: multiplexer.connect() while True: # pragma: no cover _process_envelopes(agent_name, inbox, outbox, dialogues, DefaultMessage) except KeyboardInterrupt: click.echo("Interaction interrupted!") except BaseException as e: # pylint: disable=broad-except # pragma: no cover click.echo(e) finally: multiplexer.disconnect()
def _make_stub_connection(input_file_path: str, output_file_path: str): configuration = ConnectionConfig( input_file=input_file_path, output_file=output_file_path, connection_id=StubConnection.connection_id, ) connection = StubConnection(configuration=configuration) return connection
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 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()