]> Repos - abc-messenger/commitdiff
minor improvments
authorAndrew Gundersen <gundersena@xavier.edu>
Tue, 15 Dec 2020 21:30:42 +0000 (15:30 -0600)
committerAndrew Gundersen <gundersena@xavier.edu>
Tue, 15 Dec 2020 21:30:42 +0000 (15:30 -0600)
14 files changed:
__pycache__/agent.cpython-38.pyc
__pycache__/mail.cpython-38.pyc
__pycache__/mailroom.cpython-38.pyc
__pycache__/message.cpython-38.pyc
__pycache__/server.cpython-38.pyc
__pycache__/session.cpython-38.pyc
__pycache__/typs.cpython-38.pyc
agent.py
mail.py
message.py
schemes.py [deleted file]
server.py
session.py
typs.py

index e02fcf36c48affe73aacdc1f047fdfe6102c204e..e0a1799db4ca3f40d59a7aee0d7f3d0449e448bf 100644 (file)
Binary files a/__pycache__/agent.cpython-38.pyc and b/__pycache__/agent.cpython-38.pyc differ
index 26b40b87b2197258fe7ef0bb57b9ff99c6b7b730..8c36803d8715be1039d470665db7d04a08ce119a 100644 (file)
Binary files a/__pycache__/mail.cpython-38.pyc and b/__pycache__/mail.cpython-38.pyc differ
index ab55b25b7de20e26f01bb9316b0defcbc0e2afbc..dced2a86ee35b235828e07b86f53408a58afb80e 100644 (file)
Binary files a/__pycache__/mailroom.cpython-38.pyc and b/__pycache__/mailroom.cpython-38.pyc differ
index 20e50b36d79678f3901e207dcbbb37911e68d5d8..2380ee2de6a5b1526a1ccf1dd99ae6ba803b93ac 100644 (file)
Binary files a/__pycache__/message.cpython-38.pyc and b/__pycache__/message.cpython-38.pyc differ
index dbfe3b22defb2fbe898d25402cab3f9ebc364f7a..66aea7c203572f509ff0fe32d54f8605cbb53049 100644 (file)
Binary files a/__pycache__/server.cpython-38.pyc and b/__pycache__/server.cpython-38.pyc differ
index 550c1e780337ac9dc80a5d1d3b963477c84b4bbb..61f4152d83645ed2d05f4ffa34366514e9903f9f 100644 (file)
Binary files a/__pycache__/session.cpython-38.pyc and b/__pycache__/session.cpython-38.pyc differ
index 8f56c5d53f7e3ca7aa8bfbc739b4855581b87972..20d7d8bef25ca0e6303c2f234bda35f1dd2d7a93 100644 (file)
Binary files a/__pycache__/typs.cpython-38.pyc and b/__pycache__/typs.cpython-38.pyc differ
index 4d654b38d7ee5db33e0b0e9a279f0536031b3628..2d0fbc34fd289d78700536326a1eaaaef92c7197 100644 (file)
--- 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 1f6cbbbc2b9a1e20b8856f5d4a7b34faa1d3c4cd..7ca46d73107cc43fe244499d43a5ad4ae9f63876 100644 (file)
--- 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
index ef5959bf479567cd7a00c26d98e9a400b2b65926..9f803b55a9dc41ad0109be012b9ef9c9c3ba394d 100644 (file)
@@ -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 (file)
index b8ecb26..0000000
+++ /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
index c7bbdc085281c57b2648364faf937997d0d7d291..c7e4121d74c6cf95b9ec710f89231af5c06b931b 100644 (file)
--- 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():
index 9c1d648c311e2a10e67023846512a2bbcf049267..2e1f806314d8af2f0c8696b0a9086553f3cd7c0e 100644 (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):
diff --git a/typs.py b/typs.py
index 2368957ae498171f9143a1d84cb85b819e37f8d9..5af12a41b4452b399e2653be6664f1c202e5d5bf 100644 (file)
--- 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