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)
--- /dev/null
+# 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
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
}
# 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
import asyncio
import websockets
+import db
import mail
import agent
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.
# 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):