Exemplo n.º 1
0
def config(request, setget, name, *values):
    setget = setget.lower()
    if setget == 'get':
        if len(values) > 0:
            raise CommandError('"config get" accept only one parameter')
        return request.actor.cfg.get(name)
    elif setget == 'set':
        if len(values) > 1:
            raise CommandError('"config get" accept only two parameters')
        request.actor.cfg.set(name, values[0])
    else:
        raise CommandError('config must be followed by set or get')
Exemplo n.º 2
0
 def _on_message(self, message):
     actor = get_actor()
     command = message.get('command')
     ack = message.get('ack')
     if command == 'callback':
         if not ack:
             raise ProtocolError('A callback without id')
         try:
             pending = self._pending_responses.pop(ack)
         except KeyError:
             raise KeyError('Callback %s not in pending callbacks' % ack)
         pending.set_result(message.get('result'))
     else:
         try:
             target = actor.get_actor(message['target'])
             if target is None:
                 raise CommandError('cannot execute "%s", unknown actor '
                                    '"%s"' % (command, message['target']))
             # Get the caller proxy without throwing
             caller = get_proxy(actor.get_actor(message['sender']),
                                safe=True)
             if isinstance(target, ActorProxy):
                 # route the message to the actor proxy
                 if caller is None:
                     raise CommandError(
                         "'%s' got message from unknown '%s'"
                         % (actor, message['sender']))
                 result = yield actor.send(target, command,
                                           *message['args'],
                                           **message['kwargs'])
             else:
                 actor = target
                 cmd = get_command(command)
                 req = CommandRequest(target, caller, self)
                 if actor.cfg.debug:
                     actor.logger.debug('Executing command %s from %s',
                                        command, caller or 'monitor')
                 result = yield cmd(req, message['args'], message['kwargs'])
         except CommandError as exc:
             self.logger.warning('Command error: %s' % exc)
             result = None
         except Exception as exc:
             self.logger.exception('Unhandled exception')
             result = None
         if ack:
             self._start(Message.callback(result, ack))
Exemplo n.º 3
0
def command_in_context(command, caller, target, args, kwargs, connection=None):
    cmnd = get_command(command)
    if not cmnd:
        raise CommandError('unknown %s' % command)
    request = CommandRequest(target, caller, connection)
    result = cmnd(request, args, kwargs)
    if is_async(result):
        result = yield from result
    return result
Exemplo n.º 4
0
 def _responde(self, message):
     actor = get_actor()
     command = message.get('command')
     #actor.logger.debug('handling %s', command)
     if command == 'callback':
         # this is a callback
         self._callback(message.get('ack'), message.get('result'))
     else:
         try:
             target = actor.get_actor(message['target'])
             if target is None:
                 raise CommandError('Cannot execute "%s" in %s. Unknown '
                                    'actor %s' % (command, actor,
                                                  message['target']))
             # Get the caller proxy without throwing
             caller = get_proxy(actor.get_actor(message['sender']),
                                safe=True)
             if isinstance(target, ActorProxy):
                 # route the message to the actor proxy
                 if caller is None:
                     raise CommandError(
                         "'%s' got message from unknown '%s'"
                         % (actor, message['sender']))
                 result = yield actor.send(target, command,
                                           *message['args'],
                                           **message['kwargs'])
             else:
                 actor = target
                 command = get_command(command)
                 req = CommandRequest(target, caller, self.connection)
                 result = yield command(req, message['args'],
                                        message['kwargs'])
         except Exception:
             result = Failure(sys.exc_info())
         if message.get('ack'):
             req = Message.callback(result, message['ack'])
             self.start_request(req)
Exemplo n.º 5
0
 def _send(self, target, action, *args, **kwargs):
     target = self.monitor if target == 'monitor' else target
     mailbox = self.mailbox
     if isinstance(target, ActorProxyMonitor):
         mailbox = target.mailbox
     else:
         actor = self.get_actor(target)
         if isinstance(actor, Actor):
             # this occur when sending a message from arbiter to monitors or
             # viceversa.
             return command_in_context(action, self, actor, args, kwargs)
         elif isinstance(actor, ActorProxyMonitor):
             mailbox = actor.mailbox
     if hasattr(mailbox, 'request'):
         #if not mailbox.closed:
         return mailbox.request(action, self, target, args, kwargs)
     else:
         raise CommandError('Cannot execute "%s" in %s. Unknown actor %s.' %
                            (action, self, target))
Exemplo n.º 6
0
 def send(self, target, action, *args, **kwargs):
     '''Send a message to ``target`` to perform ``action`` with given
     positional ``args`` and key-valued ``kwargs``.
     Always return a :class:`~asyncio.Future`.'''
     target = self.monitor if target == 'monitor' else target
     mailbox = self.mailbox
     if isinstance(target, ActorProxyMonitor):
         mailbox = target.mailbox
     else:
         actor = self.get_actor(target)
         if isinstance(actor, Actor):
             # this occur when sending a message from arbiter to monitors or
             # vice-versa.
             return command_in_context(action, self, actor, args, kwargs)
         elif isinstance(actor, ActorProxyMonitor):
             mailbox = actor.mailbox
     if hasattr(mailbox, 'request'):
         # if not mailbox.closed:
         return mailbox.request(action, self, target, args, kwargs)
     else:
         raise CommandError('Cannot execute "%s" in %s. Unknown actor %s.'
                            % (action, self, target))
Exemplo n.º 7
0
def command_in_context(command, caller, actor, args, kwargs):
    cmnd = get_command(command)
    if not cmnd:
        raise CommandError('unknown %s' % command)
    request = CommandRequest(actor, caller, None)
    return cmnd(request, args, kwargs)