Skip to content

Commit

Permalink
sync_send_to implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sjrct committed Aug 1, 2017
1 parent 8cc4afa commit d6a7355
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
11 changes: 8 additions & 3 deletions halibot/halmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
class HalModule(HalObject):

def reply(self, msg0=None, **kwargs):
# Create the reply message
body = kwargs.get('body', msg0.body)
mtype = kwargs.get('type', msg0.type)
author = kwargs.get('author', msg0.author)
origin = kwargs.get('origin', msg0.origin)

msg = Message(body=body, type=mtype, author=author, origin=origin)

# For deprecation, remove for 1.0
if 'context' in kwargs:
msg.context = kwargs['context']

self.send_to(msg, [ msg.origin ])
msg = Message(body=body, type=mtype, author=author, origin=origin)

# Synchronous reply?
if msg0.sync:
self.sync_replies[msg0.uuid].append(msg)
else:
self.send_to(msg, [ msg.origin ])

def hasPermission(self, msg, perm):
return self._hal.auth.hasPermission(msg.origin, msg.identity, perm)
19 changes: 19 additions & 0 deletions halibot/halobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
import copy
from threading import Thread
from collections import defaultdict
from .halconfigurer import HalConfigurer

class HalObject():
Expand All @@ -12,6 +13,10 @@ def __init__(self, hal, conf={}):
self.config = conf
self.log = logging.getLogger(self.__class__.__name__) # TODO: Put instantiated name in here too

# Only used in HalModule.reply right now, but accessed on potentially any
# HalObject, so it exists on every HalObject to avoid attribute errors
self.sync_replies = defaultdict(lambda: []) # UUID -> [Message, ...]

self.eventloop = asyncio.SelectorEventLoop()
self._thread = Thread(target=self.eventloop.run_forever)
self._thread.start()
Expand Down Expand Up @@ -48,6 +53,20 @@ def send_to(self, msg, dests):
self.log.warning('Unknown module/agent: ' + str(name))
return ret

def sync_send_to(self, msg, dests):
msg.sync = True

futs = self.send_to(msg, dests)

r = {}
for name, fut in futs.items():
fut.result()
to = self._hal.objects.get(name)
if to and msg.uuid in to.sync_replies:
# Assure that the module was not removed in the interim
r[name] = to.sync_replies.pop(msg.uuid)
return r

async def _receive(self, msg):
self.receive(msg)

Expand Down
3 changes: 3 additions & 0 deletions halibot/message.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import uuid
from .jsdict import jsdict

# Deprecated, remove at 1.0
Expand Down Expand Up @@ -28,6 +29,8 @@ class Message():

def __init__(self, **kwargs):
self.log = logging.getLogger(self.__class__.__name__)
self.uuid = uuid.uuid4()
self.sync = False

self.body = kwargs.get('body', None)
self.type = kwargs.get('type', 'simple')
Expand Down

0 comments on commit d6a7355

Please sign in to comment.