abc-messenger/session.py
Andrew Gundersen 2287ddd6f1 stable remote app
2020-12-11 15:23:59 -06:00

66 lines
No EOL
1.7 KiB
Python

# session.py
import time
import asyncio
import websockets
import mail
import agent
import schemes
class Session(schemes.Schemes, 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):
agent.Agent.__init__(self, connection)
mail.Mail.__init__(self)
self.on = True
print("Launched sesssion.")
# Recv intents run desired endpoint.
async def do_agent_intents(self):
while self.on:
try:
intent = await asyncio.wait_for(
self.recv_agent_intent(), timeout=0.2)
except asyncio.TimeoutError:
continue
except websockets.exceptions.ConnectionClosed:
await self.shutdown()
continue
# Call corresponding endpoint.
self.handle_intent(intent)
# Send mail back to agent.
#! Pulling message from backend.
async def deliver_mail(self):
while self.on:
if not self.crimata_id:
await asyncio.sleep(0.1)
continue
# Try recv.
try:
intent = 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)
# Shutdown protocol.
async def shutdown(self):
print("Shutting down session.")
self.on = False