From: Andrew Gundersen Date: Tue, 15 Dec 2020 21:30:42 +0000 (-0600) Subject: minor improvments X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=db2e6c4f15812f23ced99b0dfffdb019f634d947;p=abc-messenger minor improvments --- diff --git a/__pycache__/agent.cpython-38.pyc b/__pycache__/agent.cpython-38.pyc index e02fcf3..e0a1799 100644 Binary files a/__pycache__/agent.cpython-38.pyc and b/__pycache__/agent.cpython-38.pyc differ diff --git a/__pycache__/mail.cpython-38.pyc b/__pycache__/mail.cpython-38.pyc index 26b40b8..8c36803 100644 Binary files a/__pycache__/mail.cpython-38.pyc and b/__pycache__/mail.cpython-38.pyc differ diff --git a/__pycache__/mailroom.cpython-38.pyc b/__pycache__/mailroom.cpython-38.pyc index ab55b25..dced2a8 100644 Binary files a/__pycache__/mailroom.cpython-38.pyc and b/__pycache__/mailroom.cpython-38.pyc differ diff --git a/__pycache__/message.cpython-38.pyc b/__pycache__/message.cpython-38.pyc index 20e50b3..2380ee2 100644 Binary files a/__pycache__/message.cpython-38.pyc and b/__pycache__/message.cpython-38.pyc differ diff --git a/__pycache__/server.cpython-38.pyc b/__pycache__/server.cpython-38.pyc index dbfe3b2..66aea7c 100644 Binary files a/__pycache__/server.cpython-38.pyc and b/__pycache__/server.cpython-38.pyc differ diff --git a/__pycache__/session.cpython-38.pyc b/__pycache__/session.cpython-38.pyc index 550c1e7..61f4152 100644 Binary files a/__pycache__/session.cpython-38.pyc and b/__pycache__/session.cpython-38.pyc differ diff --git a/__pycache__/typs.cpython-38.pyc b/__pycache__/typs.cpython-38.pyc index 8f56c5d..20d7d8b 100644 Binary files a/__pycache__/typs.cpython-38.pyc and b/__pycache__/typs.cpython-38.pyc differ diff --git a/agent.py b/agent.py index 4d654b3..2d0fbc3 100644 --- a/agent.py +++ b/agent.py @@ -16,52 +16,22 @@ class Agent: self.service = None self.client_connection = connection - def fetch(self, entity, invocation): - # Create the intent. - params = { - "entity": entity, - "service": self.service, - "invocation": invocation - } - intent = typs.Intent(name="fetch", params=params) - - self.send_agent_intent(intent) - intent = self.recv_agent_intent() - - response = intent.params.get("response") - return response - - def notify(self, text, end=False): - # Create the intent. - params = { - "text": text, - "service": "message", - "end": end - } - intent = typs.Intent(name="notify", params=params) - - # Send intent to agent. - self.send_agent_intent(intent) - async def recv_agent_intent(self): package = await self.client_connection.recv() - intent = self.__decode(package) - return intent + jpackage = self.__decode(package) + return jpackage - async def send_agent_intent(self, intent): - package = self.__encode(intent) + async def send_agent_intent(self, jpackage): + package = self.__encode(jpackage) await self.client_connection.send(package) - def __encode(self, intent: typs.Intent): - package = json.dumps(intent.__dict__) + def __encode(self, jpackage): + package = json.dumps(jpackage) return package - def __decode(self, package) -> typs.Intent: + def __decode(self, package): jpackage = json.loads(package) - name = jpackage.get("name") - params = jpackage.get("params") - intent = typs.Intent(name, params) - return intent + return jpackage diff --git a/mail.py b/mail.py index 1f6cbbb..7ca46d7 100644 --- a/mail.py +++ b/mail.py @@ -10,8 +10,8 @@ class Mail: Two main IO methods. Will translate intents to mail and vice versa. Should be a parent class of Session. """ - def __init__(self): - self.crimata_id = False + def __init__(self, crimata_id): + self.crimata_id = crimata_id # Receive intent. async def mailbox_recv(self): @@ -20,27 +20,26 @@ class Mail: return intent # Send intent. - def mailbox_send(self, intent): - mail = self.__encode(intent) + def mailbox_send(self, jpackage): + mail = self.__encode(jpackage) print(f"Putting message into outbox {self.crimata_id}") mailroom.put_mail(self.crimata_id, mail) - def __encode(self, intent: typs.Intent) -> typs.Mail: - p = intent.params + def __encode(self, jpackage) -> typs.Mail: + j = jpackage owner = self.crimata_id - target = p.get("target") - text = p.get("text") - audio = p.get("audio") + target = j.get("target") + print(target) + text = j.get("text") + audio = j.get("audio") mail = typs.Mail(owner, target, text, audio) return mail @staticmethod - def __decode(mail: typs.Mail) -> typs.Intent: - name = "message" - params = { + def __decode(mail: typs.Mail): + jpackage = { "owner": mail.owner, "text": mail.text, #dev only "audio": mail.audio } - intent = typs.Intent(name, params) - return intent \ No newline at end of file + return jpackage \ No newline at end of file diff --git a/message.py b/message.py index ef5959b..9f803b5 100644 --- a/message.py +++ b/message.py @@ -22,6 +22,10 @@ async def messenger(): while not mailbox.outbox.empty(): message = mailbox.outbox.get() + # Assert mailbox for target. + if message.target not in mailroom.subs: + mailroom.create_mailbox(message.target) + # Get mailbox of target and put mail there. target_mailbox = mailroom.get_mailbox(message.target) print(f"{mailbox.name} -> {target_mailbox.name}: {message.text}") diff --git a/schemes.py b/schemes.py deleted file mode 100644 index b8ecb26..0000000 --- a/schemes.py +++ /dev/null @@ -1,33 +0,0 @@ -# schemes.py - - -class Schemes: - - def handle_intent(self, intent): - print(f"Handling {intent.name}.") - - # Set the service. - self.service = intent.name - - # Run intent endpoint. - if intent.name == "init": - self.init(intent) - - if intent.name == "message": - self.message(intent) - - # Init session by setting Crimata ID. - def init(self, intent): - self.crimata_id = intent.params.get("crimata_id") - print(f"Initalized for {self.crimata_id}") - - # Push message to anoter Crimata ID. - #! Push message to backend. - def message(self, intent): - text = intent.params.get("text") - target = intent.params.get("target") #crimata_id - - print(f"Messaging {target}: '{text}'") - - # Uses mail.Mail interface. - self.mailbox_send(intent) \ No newline at end of file diff --git a/server.py b/server.py index c7bbdc0..c7e4121 100644 --- a/server.py +++ b/server.py @@ -11,24 +11,11 @@ import session HOST = "localhost" PORT = 8764 -token_refs = {} - # Makes instance of a Messenger session for every client that connects. -async def launch_session(token, connection): +async def launch_session(crimata_id, connection): # Create the session. - s = session.Session(connection) - - # Login if token. - if token in token_refs.keys(): - crimata_id = token_refs.get(token) - s.crimata_id = crimata_id - - print(f"Reconnected: {crimata_id}") - - # Update client's login token. - intent = typs.Intent("token", params={"text": token}) - await s.send_agent_intent(intent) + s = session.Session(connection, crimata_id) # Run core loops. await asyncio.gather( @@ -39,20 +26,13 @@ async def launch_session(token, connection): ) - # Update state before end. - token_refs.update({token: s.crimata_id}) - print("Ended session.") async def main(connection, path): - token = await connection.recv() - - # Create Session ID if None. - if token not in token_refs.keys(): - token = str(uuid.uuid4()) + crimata_id = await connection.recv() # Start session with token. - await launch_session(token, connection) #! Not stopping on disconnect + await launch_session(crimata_id, connection) #! Not stopping on disconnect # Run run for every new connection. async def lift(): diff --git a/session.py b/session.py index 9c1d648..2e1f806 100644 --- a/session.py +++ b/session.py @@ -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): diff --git a/typs.py b/typs.py index 2368957..5af12a4 100644 --- a/typs.py +++ b/typs.py @@ -6,7 +6,7 @@ import asyncio class Mail: - def __init__(self, owner, target, text): + def __init__(self, owner, target, text, audio): self.owner = owner self.target = target self.text = text