class StompMessageProcessor(stomper.Engine): def __init__(self): super(StompMessageProcessor, self).__init__() self.username = getattr(settings, "RPC_USER", "pygowave_server") self.password = getattr(settings, "RPC_PASSWORD", "pygowave_server") self.pygo_mp = PyGoWaveClientMessageProcessor() def connect(self): """Generate the STOMP connect command to get a session.""" self.pygo_mp.logger.info("=> PyGoWave RPC Server starting <=") return stomper.connect(self.username, self.password) def connected(self, msg): """Once I've connected I want to subscribe to my the message queue.""" super(StompMessageProcessor, self).connected(msg) self.pygo_mp.logger.info("=> PyGoWave RPC Server ready <=") f = stomper.Frame() f.unpack(stomper.subscribe("wavelet_rpc_singlethread")) f.headers["exchange"] = "wavelet.topic" f.headers["routing_key"] = "#.#.clientop" f.headers["exchange_type"] = "topic" return f.pack() def ack(self, message): rkey = message["headers"]["destination"] self.pygo_mp.logger.info("Got "+message["body"]) message_data = anyjson.deserialize(message["body"]) msg_dict = self.pygo_mp.process(rkey, message_data) out_frames = "" for out_rkey, messages in msg_dict.iteritems(): out_frames += self.send(out_rkey, messages) return super(StompMessageProcessor, self).ack(message) + out_frames def send(self, routing_key, message_data): """Convert a routing_key and data dictionary into a STOMP message.""" f = stomper.Frame() f.unpack(stomper.send(routing_key, anyjson.serialize(message_data))) f.headers["exchange"] = "wavelet.direct" f.headers["content-type"] = "application/json" return f.pack().encode("utf-8") def purge_connections(self): """Forwards to PyGoWaveClientMessageProcessor""" self.pygo_mp.purge_connections()
class AmqpMessageProcessor(object): purge_every = datetime.timedelta(minutes=10) conn_lifetime = datetime.timedelta(minutes=getattr(settings, "ACCESS_KEY_TIMEOUT_MINUTES", 2)) def __init__(self, connection): self.pygo_mp = PyGoWaveClientMessageProcessor() self.consumer = Consumer( connection, queue="wavelet_rpc_singlethread", exchange="wavelet.topic", routing_key="#.#.clientop", exchange_type="topic", serializer="json", auto_ack=True, ) self.consumer.register_callback(self.receive) self.publisher = Publisher( connection, exchange="wavelet.direct", exchange_type="direct", delivery_mode=1, serializer="json" ) self.pygo_mp.purge_connections() self.next_purge = datetime.datetime.now() + self.purge_every def wait(self, limit=None): self.consumer.wait(limit) def send(self, routing_key, message_data): self.publisher.send(message_data, routing_key=routing_key, delivery_mode=1) def receive(self, message_data, message): routing_key = message.amqp_message.routing_key msg_dict = self.pygo_mp.process(routing_key, message_data) for out_rkey, messages in msg_dict.iteritems(): self.send(out_rkey, messages) # Cleanup time? if datetime.datetime.now() > self.next_purge: self.pygo_mp.purge_connections() self.next_purge = datetime.datetime.now() + self.purge_every