]> Repos - abc-messenger/commitdiff
messenger v0.9
authorAndrew Gundersen <gundersena@xavier.edu>
Sun, 29 Nov 2020 14:48:56 +0000 (08:48 -0600)
committerAndrew Gundersen <gundersena@xavier.edu>
Sun, 29 Nov 2020 14:48:56 +0000 (08:48 -0600)
13 files changed:
__pycache__/agent.cpython-38.pyc
__pycache__/mail.cpython-38.pyc
__pycache__/mailroom.cpython-38.pyc
__pycache__/message.cpython-38.pyc
__pycache__/schemes.cpython-38.pyc
__pycache__/server.cpython-38.pyc
__pycache__/session.cpython-38.pyc
agent.py
mail.py
message.py
schemes.py
server.py
session.py

index 2fc5689b5cc0d30cf855f8f967b0b4c297a01ed6..e02fcf36c48affe73aacdc1f047fdfe6102c204e 100644 (file)
Binary files a/__pycache__/agent.cpython-38.pyc and b/__pycache__/agent.cpython-38.pyc differ
index 6a79f0ad1e61fc959517db1db5396963092b1808..67d45850e4dc9d4560aa372e5c7b7a68af339f60 100644 (file)
Binary files a/__pycache__/mail.cpython-38.pyc and b/__pycache__/mail.cpython-38.pyc differ
index cba747dd28fdfe6f6432b3f4f81071a1886a5894..cf55692f201a934d6bf5f349f54fdf9a75010839 100644 (file)
Binary files a/__pycache__/mailroom.cpython-38.pyc and b/__pycache__/mailroom.cpython-38.pyc differ
index 77079d0b612704e5531ab0b8b26223cbb1873b52..20e50b36d79678f3901e207dcbbb37911e68d5d8 100644 (file)
Binary files a/__pycache__/message.cpython-38.pyc and b/__pycache__/message.cpython-38.pyc differ
index 05076b90382fa6061f82b193b77aef461e6597d5..183122980357c28883452a890c1fb23081ea928c 100644 (file)
Binary files a/__pycache__/schemes.cpython-38.pyc and b/__pycache__/schemes.cpython-38.pyc differ
index ff30f0d620cc8f4ccfa5e5949a0e36dc6e4633d5..41fac4eebd5c91ed9d2f744607a7db83a8acfc05 100644 (file)
Binary files a/__pycache__/server.cpython-38.pyc and b/__pycache__/server.cpython-38.pyc differ
index 5c968ecba9b57b5726bd3bc12ed691978a30856f..db3107c1b111a315f8fb415bbbf9712e469a6f4d 100644 (file)
Binary files a/__pycache__/session.cpython-38.pyc and b/__pycache__/session.cpython-38.pyc differ
index 3d9c28305ee3df865bda30f9c33c5ba38e09c1e2..4d654b38d7ee5db33e0b0e9a279f0536031b3628 100644 (file)
--- a/agent.py
+++ b/agent.py
@@ -13,74 +13,44 @@ class Agent:
 
     """
     def __init__(self, connection):
-        self.connection = connection
+        self.service = None
+        self.client_connection = connection
 
-    # Submit a request for info, return response.
-    # Must disclose what service it's for.
-    async def fetch(self, entities, service):
-
-        # Assert list.
-        if type(entities) == str:
-            entities = [entities]
-
-        print(f"Fetching {[e for e in entities]} for {service}")
-
-        # Build intent.
+    def fetch(self, entity, invocation):
+        # Create the intent.
         params = {
-            "service": service,
-            "entities": entities
-        }; intent = typs.Intent("fetch", params)
-
-        # Send intent to agent, wait for response.
-        await self.send_agent_intent(intent)
-        response = await self.recv_agent_intent()
-
-        # Parse response and return entities.
-        entities = response.params.get("found")
-        print(f"Fetch response: {entities}")
-        return entities
+            "entity": entity, 
+            "service": self.service,
+            "invocation": invocation
+        }
+        intent = typs.Intent(name="fetch", params=params)
 
-    # Text the client something. Also, let agent know
-    #   when your service is complete via the complete
-    #   attribute.
-    # Must disclose what service it's for.
-    async def notify(self, text, end=False):
+        self.send_agent_intent(intent)
+        intent = self.recv_agent_intent()
 
-        print(f"Notifying: {text} for {self.service}")
+        response = intent.params.get("response")
+        return response
 
+    def notify(self, text, end=False):
+        # Create the intent.
         params = {
-            "service": self.service,
-            "text": text,
+            "text": text, 
+            "service": "message", 
             "end": end
         }
-
-        # Build the intent
-        intent = typs.Intent("notify", params)
-        
-        # Send intent to agent.
-        await self.send_agent_intent(intent)
-
-    # Get user info.
-    async def sync(self):
-
-        intent = typs.Intent("sync", params={})
+        intent = typs.Intent(name="notify", params=params)
 
         # Send intent to agent.
-        await self.send_agent_intent(intent)
-
-        # Recv and return profile.
-        intent = await self.recv_agent_intent()
-        profile = intent.params.get("profile")
-        return profile
+        self.send_agent_intent(intent)
 
     async def recv_agent_intent(self):
-        package = await self.connection.recv()
+        package = await self.client_connection.recv()
         intent = self.__decode(package)
         return intent
 
     async def send_agent_intent(self, intent):
         package = self.__encode(intent)
-        await self.connection.send(package)
+        await self.client_connection.send(package)
 
     def __encode(self, intent: typs.Intent):
         package = json.dumps(intent.__dict__)
diff --git a/mail.py b/mail.py
index 489b2a03d2b8dede4d6af514da97f3e044fb5553..c2a61a556d229087057291fbe552d6db3636d5d0 100644 (file)
--- a/mail.py
+++ b/mail.py
@@ -8,9 +8,10 @@ class Mail:
        """Session interface for mailroom.
 
        Two main IO methods. Will translate intents to mail and vice versa.
+       Should be a parent class of Session.
        """
        def __init__(self):
-               pass
+               self.crimata_id = False
 
        # Receive intent.
        async def mailbox_recv(self):
@@ -21,6 +22,7 @@ class Mail:
        # Send intent.
        def mailbox_send(self, intent):
                mail = self.__encode(intent)
+               print(f"Putting message into outbox {self.crimata_id}")
                mailroom.put_mail(self.crimata_id, mail)
 
        def __encode(self, intent: typs.Intent) -> typs.Mail:
@@ -33,10 +35,11 @@ class Mail:
 
        @staticmethod
        def __decode(mail: typs.Mail) -> typs.Intent:
-               name = "notify"
+               name = "message"
                params = {
+                       "owner": mail.owner,
                        "text": mail.text #dev only
                }
                intent = typs.Intent(name, params)
                return intent
-       
\ No newline at end of file
+               
\ No newline at end of file
index 76a1c025f9ebd60e8873326f256f28ff9cc445b9..ef5959bf479567cd7a00c26d98e9a400b2b65926 100644 (file)
@@ -6,9 +6,7 @@ import mailroom
 
 
 # Independent loop that updates mailboxes.
-async def messenger():   
-
-    print("Running Messenger")   
+async def messenger():    
 
     while True:
 
@@ -19,7 +17,6 @@ async def messenger():
 
         # Grab a mailbox.
         mailbox = mailroom.que.get()
-        print(f"Handling mailbox: {mailbox.name}.")
 
         # Handle every message in outbox.
         while not mailbox.outbox.empty():
@@ -27,6 +24,7 @@ async def messenger():
 
             # Get mailbox of target and put mail there.
             target_mailbox = mailroom.get_mailbox(message.target)
+            print(f"{mailbox.name} -> {target_mailbox.name}: {message.text}")
             await target_mailbox.inbox.put(message)
 
         # Put the mailbox back
index 1703a86976719cefb7d5ae46148d9f3b47a32720..9a85986860f0aaf4f8a8dc0d3b366100f7ddaefc 100644 (file)
@@ -3,27 +3,29 @@
 
 class Schemes:
 
-       def __init__(self):
-               self.service = None
+       def handle_intent(self, intent):
+               print(f"Handling {intent.name}.")  
 
-       async def handle_intent(self, intent):
-               print(f"Handling intent {intent.name}.")  
-
-               # Set the service
+               # Set the service.
                self.service = intent.name
                 
+               # Run intent endpoint.
                if intent.name == "init":
-                       await self.init(intent)
+                       self.init(intent)
+
                if intent.name == "message":
                        self.message(intent)
 
-       async def init(self, intent):
+       # Init session by setting Crimata ID.
+       def init(self, intent):
                self.crimata_id = intent.params.get("crimata_id")
-               await self.notify("Messaging is live.")
+               print(f"Initalized for {self.crimata_id}")
 
+       # Push message to anoter Crimata ID.
        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.
index 58ce7b8a1f4d2509b4ae57a13daac8c67d4643bf..c7bbdc085281c57b2648364faf937997d0d7d291 100644 (file)
--- a/server.py
+++ b/server.py
@@ -1,27 +1,60 @@
 # server.py
 
+import json
+import uuid
 import asyncio
 import websockets
 
+import typs
 import session
 
 HOST = "localhost"
 PORT = 8764
 
+token_refs = {}
 
 # Makes instance of a Messenger session for every client that connects.
-async def launch_session(connection, path):
+async def launch_session(token, 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)
+
+    # Run core loops.
     await asyncio.gather(
 
-        s.do_agent_intents(), 
+        s.do_agent_intents(),
 
         s.deliver_mail()
 
     )
 
+    # 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())
+
+    # Start session with token.
+    await launch_session(token, connection) #! Not stopping on disconnect
+
 # Run run for every new connection.
 async def lift():
     print(f"Listening for connections on {HOST}:{PORT}")
-    await websockets.serve(launch_session, HOST, PORT)
\ No newline at end of file
+    await websockets.serve(main, HOST, PORT)
\ No newline at end of file
index 29a1d3756b3bd327414c3e4d928029839672124f..9aa152f6f99b0633a8e792a20e41731ea6fc33d6 100644 (file)
@@ -2,6 +2,7 @@
 
 import time
 import asyncio
+import websockets
 
 import mail
 import agent
@@ -17,65 +18,48 @@ class Session(schemes.Schemes, agent.Agent, mail.Mail):
     """
     def __init__(self, connection):
         agent.Agent.__init__(self, connection)
+        mail.Mail.__init__(self)
 
-        self.crimata_id = False
+        self.on = True
 
-        print("Launched new session")
+        print("Launched sesssion.")
 
     # Recv intents run desired endpoint.
     async def do_agent_intents(self):
-        while True:
+        while self.on:
 
-            # Recv intent from agent.
-            intent = await self.recv_agent_intent()
-            print(f"Intent: {intent.name}")
+            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.
-            await self.handle_intent(intent)
-
-            await asyncio.sleep(0.1)
+            self.handle_intent(intent)
 
     # Send mail back to agent.
     async def deliver_mail(self):
-        while True:
+        while self.on:
 
-            # Sleep until Crimata ID.
             if not self.crimata_id:
-                await asyncio.sleep(1)
+                await asyncio.sleep(0.1)
                 continue
 
-            intent = await self.mailbox_recv()
-            await self.send_agent_intent(intent)
-
-            await asyncio.sleep(0.1)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-    # async def hello(self):
-
-    #     intent = await self.recv_agent_intent()
-
-    #     response = await self.fetch("crimata_id", service="message")
-    #     self.profile = response.get("profile")
-
-    #     target = intent.params.get("target")
-    #     await self.notify(f"Messenger APP: Received Message Intent", 
-    #         service="message", complete=True)
+            # Try recv.
+            try:
+                intent = await asyncio.wait_for(
+                    self.mailbox_recv(), timeout=0.2)
+            except asyncio.TimeoutError:
+                continue
 
-    #     print("Shutting down in 10s")
+            # Send message to Agent.
+            print("Sending message")
+            await self.send_agent_intent(intent)
 
-    #     await asyncio.sleep(10)
+    # Shutdown protocol.
+    async def shutdown(self):
+        print("Shutting down session.")
+        self.on = False
\ No newline at end of file