def decode(self, msg): if isinstance(msg, bytes): msg = msg.decode('utf-8') try: return json.loads(msg) except Exception: raise ProtocolError('Invalid JSON') from None
def _callback(self, ack, result): 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.callback(result)
def data_received(self, data): # Feed data into the parser msg = self._parser.decode(data) while msg: try: message = pickle.loads(msg.body) except Exception as e: raise ProtocolError('Could not decode message body: %s' % e) ensure_future(self._on_message(message), loop=self._loop) msg = self._parser.decode()
def data_received(self, data): # Feed data into the parser msg = self._parser.decode(data) while msg: try: message = pickle.loads(msg.body) except Exception as e: raise ProtocolError('Could not decode message body: %s' % e) maybe_async(self._responde(message), event_loop=self.event_loop) msg = self._parser.decode()
async def on_message(self, message): """Handle a new message in the websocket """ try: # SockJS sends a string as a single element of an array. # Therefore JSON is double-encoded! msg = json.loads(message) if not isinstance(msg, list): raise ProtocolError('Malformed message; expected list, ' 'got %s' % type(msg).__name__) data = self.protocol.decode(msg[0]) if not isinstance(data, dict): raise ProtocolError('Malformed data; expected dict, ' 'got %s' % type(data).__name__) await self.rpc(data) except ProtocolError as exc: self.logger.error('Protocol error: %s', str(exc)) except Exception: self.logger.exception('While loading websocket message') self.transport.connection.close()
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))
def data_received(self, data): '''Implements the :meth:`Protocol.data_received` method. Delegates handling of data to the :attr:`current_consumer`. Once done set a timeout for idle connctions (when a :attr:`timeout` is given). ''' self._cancel_timeout() while data: consumer = self._current_consumer if consumer is None: # New consumer. consumer = self._consumer_factory() self.set_consumer(consumer) consumer.start() # Call the consumer _data_received method data = consumer._data_received(data) if data and self._current_consumer: # if data is returned from the response feed method and the # response has not done yet raise a Protocol Error raise ProtocolError('current consumer not done.') self._add_idle_timeout()
def decode(self, msg): try: return json.loads(to_string(msg)) except Exception as exc: raise ProtocolError('Invalid JSON') from exc
def decode(self, msg): try: return json.loads(msg.decode('utf-8')) except Exception as exc: raise ProtocolError('Invalid JSON') from exc