minor improvments

This commit is contained in:
Andrew Gundersen 2020-12-15 15:30:42 -06:00
commit db2e6c4f15
14 changed files with 39 additions and 120 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -16,52 +16,22 @@ class Agent:
self.service = None self.service = None
self.client_connection = connection 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): async def recv_agent_intent(self):
package = await self.client_connection.recv() package = await self.client_connection.recv()
intent = self.__decode(package) jpackage = self.__decode(package)
return intent return jpackage
async def send_agent_intent(self, intent): async def send_agent_intent(self, jpackage):
package = self.__encode(intent) package = self.__encode(jpackage)
await self.client_connection.send(package) await self.client_connection.send(package)
def __encode(self, intent: typs.Intent): def __encode(self, jpackage):
package = json.dumps(intent.__dict__) package = json.dumps(jpackage)
return package return package
def __decode(self, package) -> typs.Intent: def __decode(self, package):
jpackage = json.loads(package) jpackage = json.loads(package)
name = jpackage.get("name") return jpackage
params = jpackage.get("params")
intent = typs.Intent(name, params)
return intent

27
mail.py
View file

@ -10,8 +10,8 @@ class Mail:
Two main IO methods. Will translate intents to mail and vice versa. Two main IO methods. Will translate intents to mail and vice versa.
Should be a parent class of Session. Should be a parent class of Session.
""" """
def __init__(self): def __init__(self, crimata_id):
self.crimata_id = False self.crimata_id = crimata_id
# Receive intent. # Receive intent.
async def mailbox_recv(self): async def mailbox_recv(self):
@ -20,27 +20,26 @@ class Mail:
return intent return intent
# Send intent. # Send intent.
def mailbox_send(self, intent): def mailbox_send(self, jpackage):
mail = self.__encode(intent) mail = self.__encode(jpackage)
print(f"Putting message into outbox {self.crimata_id}") print(f"Putting message into outbox {self.crimata_id}")
mailroom.put_mail(self.crimata_id, mail) mailroom.put_mail(self.crimata_id, mail)
def __encode(self, intent: typs.Intent) -> typs.Mail: def __encode(self, jpackage) -> typs.Mail:
p = intent.params j = jpackage
owner = self.crimata_id owner = self.crimata_id
target = p.get("target") target = j.get("target")
text = p.get("text") print(target)
audio = p.get("audio") text = j.get("text")
audio = j.get("audio")
mail = typs.Mail(owner, target, text, audio) mail = typs.Mail(owner, target, text, audio)
return mail return mail
@staticmethod @staticmethod
def __decode(mail: typs.Mail) -> typs.Intent: def __decode(mail: typs.Mail):
name = "message" jpackage = {
params = {
"owner": mail.owner, "owner": mail.owner,
"text": mail.text, #dev only "text": mail.text, #dev only
"audio": mail.audio "audio": mail.audio
} }
intent = typs.Intent(name, params) return jpackage
return intent

View file

@ -22,6 +22,10 @@ async def messenger():
while not mailbox.outbox.empty(): while not mailbox.outbox.empty():
message = mailbox.outbox.get() 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. # Get mailbox of target and put mail there.
target_mailbox = mailroom.get_mailbox(message.target) target_mailbox = mailroom.get_mailbox(message.target)
print(f"{mailbox.name} -> {target_mailbox.name}: {message.text}") print(f"{mailbox.name} -> {target_mailbox.name}: {message.text}")

View file

@ -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)

View file

@ -11,24 +11,11 @@ import session
HOST = "localhost" HOST = "localhost"
PORT = 8764 PORT = 8764
token_refs = {}
# Makes instance of a Messenger session for every client that connects. # 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. # Create the session.
s = session.Session(connection) s = session.Session(connection, crimata_id)
# 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. # Run core loops.
await asyncio.gather( 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.") print("Ended session.")
async def main(connection, path): async def main(connection, path):
token = await connection.recv() crimata_id = await connection.recv()
# Create Session ID if None.
if token not in token_refs.keys():
token = str(uuid.uuid4())
# Start session with token. # 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. # Run run for every new connection.
async def lift(): async def lift():

View file

@ -6,30 +6,29 @@ import websockets
import mail import mail
import agent import agent
import schemes
class Session(schemes.Schemes, agent.Agent, mail.Mail): class Session(agent.Agent, mail.Mail):
"""Launch instance for every client connection. """Launch instance for every client connection.
Will recv messages from client and push them to mailbox. Will recv messages from client and push them to mailbox.
Also, will pull messages from mailbox and send to client. 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) agent.Agent.__init__(self, connection)
mail.Mail.__init__(self)
self.on = True self.on = True
print("Launched sesssion.") print(f"Launched sesssion for {crimata_id}.")
# Recv intents run desired endpoint. # Recv intents run desired endpoint.
async def do_agent_intents(self): async def do_agent_intents(self):
while self.on: while self.on:
try: try:
intent = await asyncio.wait_for( jpackage = await asyncio.wait_for(
self.recv_agent_intent(), timeout=0.2) self.recv_agent_intent(), timeout=0.2)
except asyncio.TimeoutError: except asyncio.TimeoutError:
continue continue
@ -38,7 +37,7 @@ class Session(schemes.Schemes, agent.Agent, mail.Mail):
continue continue
# Call corresponding endpoint. # Call corresponding endpoint.
self.handle_intent(intent) self.mailbox_send(jpackage)
# Send mail back to agent. # Send mail back to agent.
#! Pulling message from backend. #! Pulling message from backend.
@ -51,14 +50,14 @@ class Session(schemes.Schemes, agent.Agent, mail.Mail):
# Try recv. # Try recv.
try: try:
intent = await asyncio.wait_for( jpackage = await asyncio.wait_for(
self.mailbox_recv(), timeout=0.2) self.mailbox_recv(), timeout=0.2)
except asyncio.TimeoutError: except asyncio.TimeoutError:
continue continue
# Send message to Agent. # Send message to Agent.
print("Sending message") print("Sending message")
await self.send_agent_intent(intent) await self.send_agent_intent(jpackage)
# Shutdown protocol. # Shutdown protocol.
async def shutdown(self): async def shutdown(self):

View file

@ -6,7 +6,7 @@ import asyncio
class Mail: class Mail:
def __init__(self, owner, target, text): def __init__(self, owner, target, text, audio):
self.owner = owner self.owner = owner
self.target = target self.target = target
self.text = text self.text = text