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
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):
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
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}")
+++ /dev/null
-# 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
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(
)
- # 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():
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.
# 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):
class Mail:
- def __init__(self, owner, target, text):
+ def __init__(self, owner, target, text, audio):
self.owner = owner
self.target = target
self.text = text