From: Andrew Gundersen Date: Mon, 21 Dec 2020 18:00:32 +0000 (-0600) Subject: db X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=f583352e426ef114865e72832a0d28b8cf33827c;p=abc-messenger db --- diff --git a/__pycache__/agent.cpython-38.pyc b/__pycache__/agent.cpython-38.pyc index e0a1799..8fdd51c 100644 Binary files a/__pycache__/agent.cpython-38.pyc and b/__pycache__/agent.cpython-38.pyc differ diff --git a/__pycache__/db.cpython-38.pyc b/__pycache__/db.cpython-38.pyc new file mode 100644 index 0000000..0050954 Binary files /dev/null and b/__pycache__/db.cpython-38.pyc differ diff --git a/__pycache__/mail.cpython-38.pyc b/__pycache__/mail.cpython-38.pyc index 8c36803..3915983 100644 Binary files a/__pycache__/mail.cpython-38.pyc and b/__pycache__/mail.cpython-38.pyc differ diff --git a/__pycache__/message.cpython-38.pyc b/__pycache__/message.cpython-38.pyc index 2380ee2..c458305 100644 Binary files a/__pycache__/message.cpython-38.pyc and b/__pycache__/message.cpython-38.pyc differ diff --git a/__pycache__/session.cpython-38.pyc b/__pycache__/session.cpython-38.pyc index 61f4152..bf6a650 100644 Binary files a/__pycache__/session.cpython-38.pyc and b/__pycache__/session.cpython-38.pyc differ diff --git a/agent.py b/agent.py index 2d0fbc3..1ff4d86 100644 --- a/agent.py +++ b/agent.py @@ -16,12 +16,12 @@ class Agent: self.service = None self.client_connection = connection - async def recv_agent_intent(self): + async def recv(self): package = await self.client_connection.recv() jpackage = self.__decode(package) return jpackage - async def send_agent_intent(self, jpackage): + async def send(self, jpackage): package = self.__encode(jpackage) await self.client_connection.send(package) diff --git a/db.py b/db.py new file mode 100644 index 0000000..9b72d8e --- /dev/null +++ b/db.py @@ -0,0 +1,26 @@ +# db.py + +import mysql.connector + +# database settings +DBUSER = "remoteAccess" +DBPASSWORD = "remoteAccess2020" +DBHOST = "75.86.178.105" + +# People Schema +# ------------- + +people_cnx = mysql.connector.connect(user=DBUSER, password=DBPASSWORD, host=DBHOST, database="People") +people_cursor = people_cnx.cursor(buffered=True) + +# Verify that an email is a valid crimata_id. +def verify(crimata_id, get=False): + query = (f"SELECT * FROM People.users WHERE crimata_id='{crimata_id}';") + + people_cursor.execute(query) + + verified = False + for (_, crimata_id, _) in people_cursor: + verified = True + + return verified \ No newline at end of file diff --git a/mail.py b/mail.py index 7ca46d7..cb5b6f9 100644 --- a/mail.py +++ b/mail.py @@ -20,25 +20,25 @@ class Mail: return intent # Send intent. - def mailbox_send(self, jpackage): + async 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) + await self.send(1) # agent confirmation. def __encode(self, jpackage) -> typs.Mail: j = jpackage owner = self.crimata_id 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): + def __decode(self, mail: typs.Mail): jpackage = { "owner": mail.owner, + "target": self.crimata_id, "text": mail.text, #dev only "audio": mail.audio } diff --git a/message.py b/message.py index 9f803b5..73fc394 100644 --- a/message.py +++ b/message.py @@ -33,7 +33,7 @@ async def messenger(): # Put the mailbox back mailroom.que.put(mailbox) - await asyncio.sleep(1) + await asyncio.sleep(0.1) async def run(): await messenger() \ No newline at end of file diff --git a/session.py b/session.py index 2e1f806..ec4f0b9 100644 --- a/session.py +++ b/session.py @@ -4,6 +4,7 @@ import time import asyncio import websockets +import db import mail import agent @@ -29,15 +30,23 @@ class Session(agent.Agent, mail.Mail): try: jpackage = await asyncio.wait_for( - self.recv_agent_intent(), timeout=0.2) + self.recv(), timeout=0.1) except asyncio.TimeoutError: continue except websockets.exceptions.ConnectionClosed: await self.shutdown() - continue + continue + + # Check if target is verified. + verified = db.verify(jpackage.get("target")) + + # Send error code back to agent. + if not verified: + await self.send(2) + continue # Call corresponding endpoint. - self.mailbox_send(jpackage) + await self.mailbox_send(jpackage) # Send mail back to agent. #! Pulling message from backend. @@ -51,13 +60,13 @@ class Session(agent.Agent, mail.Mail): # Try recv. try: jpackage = await asyncio.wait_for( - self.mailbox_recv(), timeout=0.2) + self.mailbox_recv(), timeout=0.1) except asyncio.TimeoutError: continue # Send message to Agent. print("Sending message") - await self.send_agent_intent(jpackage) + await self.send(jpackage) # Shutdown protocol. async def shutdown(self):