minor improvments

This commit is contained in:
Andrew Gundersen 2020-12-15 15:30:42 -06:00
commit db2e6c4f15
14 changed files with 39 additions and 120 deletions

View file

@ -6,39 +6,38 @@ import websockets
import mail
import agent
import schemes
class Session(schemes.Schemes, agent.Agent, mail.Mail):
class Session(agent.Agent, mail.Mail):
"""Launch instance for every client connection.
Will recv messages from client and push them to mailbox.
Also, will pull messages from mailbox and send to client.
"""
def __init__(self, connection):
def __init__(self, connection, crimata_id):
mail.Mail.__init__(self, crimata_id)
agent.Agent.__init__(self, connection)
mail.Mail.__init__(self)
self.on = True
print("Launched sesssion.")
print(f"Launched sesssion for {crimata_id}.")
# Recv intents run desired endpoint.
async def do_agent_intents(self):
while self.on:
try:
intent = await asyncio.wait_for(
jpackage = await asyncio.wait_for(
self.recv_agent_intent(), timeout=0.2)
except asyncio.TimeoutError:
continue
except websockets.exceptions.ConnectionClosed:
await self.shutdown()
continue
continue
# Call corresponding endpoint.
self.handle_intent(intent)
self.mailbox_send(jpackage)
# Send mail back to agent.
#! Pulling message from backend.
@ -51,14 +50,14 @@ class Session(schemes.Schemes, agent.Agent, mail.Mail):
# Try recv.
try:
intent = await asyncio.wait_for(
jpackage = await asyncio.wait_for(
self.mailbox_recv(), timeout=0.2)
except asyncio.TimeoutError:
continue
# Send message to Agent.
print("Sending message")
await self.send_agent_intent(intent)
await self.send_agent_intent(jpackage)
# Shutdown protocol.
async def shutdown(self):