Skip to content

Commit

Permalink
added a container class to speedup local sends.
Browse files Browse the repository at this point in the history
  • Loading branch information
javipalanca committed Sep 27, 2018
1 parent 01e82ca commit 4bae3ba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
10 changes: 9 additions & 1 deletion spade/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def __init__(self, jid, password, verify_security=False, loop=None):
self.behaviours = []
self._values = {}

self.container = None

self.traces = TraceStore(size=1000)

if loop:
Expand All @@ -63,6 +65,9 @@ def __init__(self, jid, password, verify_security=False, loop=None):
# Web service
self.web = WebApp(agent=self)

def set_container(self, container):
self.container = container

def start(self, auto_register=True):
"""
Starts the agent. This fires some actions:
Expand Down Expand Up @@ -302,9 +307,12 @@ def _message_received(self, msg):
list(asyncio.Future): a list of futures of the append of the message at each matched behaviour.
"""
logger.debug(f"Got message: {msg}")

msg = Message.from_node(msg)
return self.dispatch(msg)

def dispatch(self, msg):
logger.debug(f"Got message: {msg}")
futures = []
matched = False
for behaviour in (x for x in self.behaviours if x.match(msg)):
Expand Down
10 changes: 8 additions & 2 deletions spade/behaviour.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,17 @@ async def send(self, msg):
if not msg.sender:
msg.sender = str(self.agent.jid)
logger.debug(f"Adding agent's jid as sender to message: {msg}")
aioxmpp_msg = msg.prepare()
await self.agent.client.send(aioxmpp_msg)
if self.agent.container:
self.agent.container.send(msg, self)
else:
await self._xmpp_send(msg)
msg.sent = True
self.agent.traces.append(msg, category=str(self))

async def _xmpp_send(self, msg):
aioxmpp_msg = msg.prepare()
await self.agent.client.send(aioxmpp_msg)

async def receive(self, timeout=None):
"""
Receives a message for this behaviour.
Expand Down
15 changes: 15 additions & 0 deletions spade/container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

class Container(object):
def __init__(self):
self.__agents = {}

def register(self, agent):
self.__agents[str(agent.jid)] = agent
agent.set_container(self)

def send(self, msg, behaviour):
to = str(msg.to)
if to in self.__agents:
self.__agents[to].dispatch(msg)
else:
behaviour._xmpp_send(msg)

0 comments on commit 4bae3ba

Please sign in to comment.