1414
1515class AgentServer (asyncio .Protocol ):
1616 """
17- Class used for serving the agents when conneting to the game run by th GameCoordinator.
17+ Class used for serving the agents when connecting to the game run by the GameCoordinator.
18+
19+ Attributes:
20+ actions_queue (asyncio.Queue): Queue for actions from agents.
21+ answers_queues (dict): Mapping of agent addresses to their response queues.
22+ max_connections (int): Maximum allowed concurrent agent connections.
23+ current_connections (int): Current number of connected agents.
24+ logger (logging.Logger): Logger for the AgentServer.
1825 """
1926 def __init__ (self , actions_queue , agent_response_queues , max_connections ):
27+ """
28+ Initialize the AgentServer.
29+
30+ Args:
31+ actions_queue (asyncio.Queue): Queue for actions from agents.
32+ agent_response_queues (dict): Mapping of agent addresses to their response queues.
33+ max_connections (int): Maximum allowed concurrent agent connections.
34+ """
2035 self .actions_queue = actions_queue
2136 self .answers_queues = agent_response_queues
2237 self .max_connections = max_connections
@@ -26,6 +41,9 @@ def __init__(self, actions_queue, agent_response_queues, max_connections):
2641 async def handle_agent_quit (self , peername :tuple ):
2742 """
2843 Helper function to handle agent disconnection.
44+
45+ Args:
46+ peername (tuple): The address of the disconnecting agent.
2947 """
3048 # Send a quit message to the Coordinator
3149 self .logger .info (f"\t Handling agent quit for { peername } ." )
@@ -35,6 +53,10 @@ async def handle_agent_quit(self, peername:tuple):
3553 async def handle_new_agent (self , reader , writer ):
3654 """
3755 Handle a new agent connection.
56+
57+ Args:
58+ reader (asyncio.StreamReader): Stream reader for the agent.
59+ writer (asyncio.StreamWriter): Stream writer for the agent.
3860 """
3961 # get the peername of the writer
4062 peername = writer .get_extra_info ("peername" )
@@ -102,6 +124,13 @@ async def handle_new_agent(self, reader, writer):
102124 # swallow exceptions on close to avoid crash on cleanup
103125 pass
104126 async def __call__ (self , reader , writer ):
127+ """
128+ Allow the server instance to be called as a coroutine.
129+
130+ Args:
131+ reader (asyncio.StreamReader): Stream reader for the agent.
132+ writer (asyncio.StreamWriter): Stream writer for the agent.
133+ """
105134 await self .handle_new_agent (reader , writer )
106135
107136class GameCoordinator :
0 commit comments