db
This commit is contained in:
parent
db2e6c4f15
commit
f583352e42
10 changed files with 47 additions and 12 deletions
Binary file not shown.
BIN
__pycache__/db.cpython-38.pyc
Normal file
BIN
__pycache__/db.cpython-38.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4
agent.py
4
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)
|
||||
|
||||
|
|
|
|||
26
db.py
Normal file
26
db.py
Normal file
|
|
@ -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
|
||||
8
mail.py
8
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
19
session.py
19
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):
|
||||
|
|
|
|||
Loading…
Reference in a new issue