def init_adversary(): global __username, __my_type, __connection, __global_exchange, __producer, __backchannel, __backch_producer, __adv_exchange, __adv_queue #, __my_queue __username = '******' __my_type = 'adversary' #adversary has broadcast-only access to the regular exchange __connection = Connection('amqp://') __connection.connect() __global_exchange = Exchange('broadcast', type='fanout', durable=False, delivery_mode=1) __global_exchange.maybe_bind(__connection) __producer = __connection.Producer(__connection) __backchannel = Connection('amqp://') __backchannel.connect() __adv_exchange = Exchange('adversary', durable=False, delivery_mode=1) __adv_exchange.maybe_bind(__backchannel) __backch_producer = __backchannel.Producer(__backchannel) __adv_queue = Queue('adversary', exchange=__adv_exchange, routing_key='adversary', durable=False) __adv_queue = __adv_queue(__backchannel) __adv_queue.declare()
class KWriteQueue(object): def __init__(self, channel, exchange, **kwargs): self._exchange_declare = kwargs.get("exchange_declare", False) if isinstance(exchange, Queue): self.exchange = exchange.exchange elif isinstance(exchange, basestring): self.exchange = Exchange(exchange, type="fanout") # , durable=True) else: assert isinstance(exchange, Exchange) self.exchange = exchange self.channel = maybe_channel(channel) self.exchange.maybe_bind(self.channel) if self._exchange_declare: self.exchange.declare() self.producer = messaging.Producer(channel, self.exchange, serializer='json', routing_key='', compression=None, auto_declare=False) def __enter__(self): return self def __exit__(self, *exc_info): self.close() def put(self, message, serializer=None, headers=None, compression=None, routing_key=None, **kwargs): self.producer.publish(message, content_type="application/octet-stream", serializer=serializer, routing_key=routing_key, headers=headers, compression=compression, **kwargs)
def init(username, msgtype): global __username, __my_type, __connection, __global_exchange, __producer, __my_queue, __backchannel, __backch_producer, __adv_exchange, __adv_queue __username = username __my_type = msgtype print "Username: "******"Q:" #print __my_queue #__consumer = __connection.Consumer(queues=__my_queue) #we're not using consumer 'cause we're using queue get #__recv_loop = drain_consumer(__consumer, timeout=1) __backchannel = Connection('amqp://') __backchannel.connect() __adv_exchange = Exchange('adversary', durable=False, delivery_mode=1) __adv_exchange.maybe_bind(__backchannel) __backch_producer = __backchannel.Producer(__backchannel) __adv_queue = Queue(username + '-adv', exchange=__adv_exchange, routing_key=username + '-adv', durable=False) __adv_queue = __adv_queue(__backchannel) __adv_queue.declare()
class Publisher(object): """ Publisher generic class. Also known as Producer. """ auto_delete = False def __init__(self, name, routing_key, service=None): """ Messages will be published to `exchange`, using these different settings. :param name: The exchange name, required. :type name: str :param routing_key: The key used to route the message. :type routing_key: None, str :param service: The service's name. Used to get the configuration :type service: None, str :rtype: None """ self.settings = getattr(farine.settings, service or name) self.routing_key = routing_key self.exchange = Exchange(name, type=self.settings['type'], durable=self.settings['durable'], auto_delete=self.settings.get('auto_delete', self.auto_delete), delivery_mode=self.settings['delivery_mode']) def get_connection(self): """ Retrieve the connection, lazily. :returns: The broker connection. :rtype: kombu.connection.Connection """ return Connection(self.settings['amqp_uri']) def send(self, message, *args, **kwargs): """ Send the the `message` to the broker. :param message: The message to send. Its type depends on the serializer used. :type message: object :rtype: None """ routing_keys = kwargs.get('routing_key') or self.routing_key routing_keys = [routing_keys] if isinstance(routing_keys, basestring) else routing_keys correlation_id = kwargs.get('correlation_id', None) reply_to = kwargs.get('reply_to', None) declare=[self.exchange] + kwargs.get('declare', []) conn = self.get_connection() with connections[conn].acquire(block=True) as connection: self.exchange.maybe_bind(connection) #reply_to.maybe_bind(connection) #reply_to.declare(True) with producers[connection].acquire(block=True) as producer: for routing_key in routing_keys: LOGGER.info('Send message %s to exchange %s with routing_key %s reply_to %s correlation_id %s', message, self.exchange.name, routing_key, reply_to, correlation_id) producer.publish( message, exchange=self.exchange, declare=declare, serializer=self.settings['serializer'], routing_key=routing_key, correlation_id=correlation_id, retry=self.settings['retry'], delivery_mode=self.settings['delivery_mode'], reply_to=reply_to, retry_policy=self.settings['retry_policy']) def __call__(self, *args, **kwargs): return self.send(*args, **kwargs) def close(self): """ Release the connection to the broker. :rtype: None """ self.get_connection().release()