This agent builds on RecvAgent_0 by introducing a validation hook using the Summoner SDK's @client.hook feature. It receives messages from the Summoner server and stores them in a local SQLite database via the async ORM in db_sdk.py (copied from the db_sdk_(use_me)/ folder — see its README for full instructions).
(Click to expand) The agent goes through these steps:
- On startup, the agent ensures the
messagestable (defined indb_models.py) exists inRecvAgent_1.db. - When a message is received:
- A hook (
@client.hook) checks that the message is a dictionary with"remote_addr"and"content".- If validation fails, the hook logs:
and the message is not processed further.
[hook:recv] missing address/content - If validation passes, the hook logs:
[hook:recv] 127.0.0.1:64790 passed validation
- If validation fails, the hook logs:
- The validated message is then passed to the
@client.receivehandler, which:- Logs a receipt line:
INFO - Received message from Client @(SocketAddress=127.0.0.1:64790). - Stores the
(addr, content)pair in the database. - Queries how many messages have been stored for that address and logs:
INFO - Client @(SocketAddress=127.0.0.1:64790) has now 2 messages stored.
- Logs a receipt line:
- A hook (
- The agent runs until you stop it (e.g. Ctrl+C).
While it is running, you can inspect the live data with the provided db_check.py script (see How to Run).
| Feature | Description |
|---|---|
SummonerClient(name=...) |
Instantiates and manages the agent |
@client.hook(direction=RECEIVE) |
Validates or transforms messages before they reach the receive handler |
@client.receive(route=...) |
Registers an async handler for validated messages |
client.logger |
Logs runtime events and debugging information |
client.loop.run_until_complete(...) |
Executes a coroutine on the client's internal asyncio loop (e.g., table creation) |
Database(db_path) |
Single async connection to SQLite |
Message.create_table(db) |
Ensures the messages table exists (async ORM from db_sdk) |
Message.insert(db, ...) |
Inserts a new row into messages |
Message.find(db, ...) |
Fetches stored rows matching a filter (e.g. by address) |
client.run(...) |
Connects to the server and starts the asyncio event loop |
-
Start the Summoner server (in one terminal):
python server.py
-
Run the receiver agent (in another terminal):
python agents/agent_RecvAgent_1/agent.py
-
Inspect the database live (in a third terminal):
python agents/agent_RecvAgent_1/db_check.py
You will see a menu like:
Available Addresses: [0] 127.0.0.1:64790 [1] 127.0.0.1:64788 Enter the index of the address to view messages:Selecting an index displays the stored messages for that client address:
1. {"message":"Hello Server!","from":"..."} 2. {"message":"Hello Server!","from":"..."}
Try running multiple senders and this receiver together:
# Terminal 1 (server)
python server.py
# Terminal 2 (sender 0 with no UUID)
python agents/agent_SendAgent_0/agent.py
# Terminal 3 (sender 1 with UUID in message)
python agents/agent_SendAgent_1/agent.py
# Terminal 4 (receiver with hook)
python agents/agent_RecvAgent_1/agent.py
# Terminal 5 (db inspector)
python agents/agent_RecvAgent_1/db_check.pyYou will observe the hook validation step in action, with only well-formed messages being passed along to the receive handler and stored in the database.