def connectionMade(self): AMQClient.connectionMade(self) yield self.authenticate(self.vumi_options['username'], self.vumi_options['password']) # authentication was successful log.msg("Got an authenticated connection") yield self.connected_callback(self)
def __init__(self, *args, **kwargs): self.log.debug('init protocol') self._stop = False # callback on authenticated self._auth_succ = Deferred() # callback on read_channel opened self._read_opened = Deferred() # callback on write_channel opened self._write_opened = Deferred() # callback on read_loop started self._read_loop_started = Deferred() # callback on shutdown read loop self._read_loop_down = Deferred() # read queue timeout self.q_timeout = 1 # read loop call self._rloop_call = None self._sloop_call = None # ensure that we start read loop only once self._read_loop_enabled = False self.read_queue = None self.read_chan = None kwargs['heartbeat'] = kwargs.get('heartbeat', 10) self.__messages = set() AMQClient.__init__(self, *args, **kwargs)
def __init__(self, *args, **kwargs): print 'init protocol' self._stop = False # callback on authenticated self._auth_succ = Deferred() # callback on read_channel opened self._read_opened = Deferred() # callback on write_channel opened self._write_opened = Deferred() # callback on read_loop started self._read_loop_started = Deferred() # callback on shutdown read loop self._read_loop_down = Deferred() # read queue timeout self.q_timeout = 1 # read loop call self._rloop_call = None self._sloop_call = None self.read_queue = None self.read_chan = None kwargs['heartbeat'] = kwargs.get('heartbeat', 10) # failure traps #self.log.warning('AUTO SHUTDOWN 15 sec') #reactor.callLater(15, lambda _: self.shutdown_protocol(), (None,)) self.__messages = set() AMQClient.__init__(self, *args, **kwargs)
def connectionMade(self): AMQClient.connectionMade(self) # Authenticate. assert self.factory is not None deferred = self.start({"LOGIN": self.factory.user, "PASSWORD": self.factory.password}) deferred.addCallbacks(self._authenticated, self._authentication_failed)
class AMQFactory(protocol.ReconnectingClientFactory): VHOST = '/' def __init__(self, creds): self.spec = txamqp.spec.load(SPECFILE) self.creds = creds self.client = None self.channel = None def buildProtocol(self, addr): self.resetDelay() delegate = TwistedDelegate() self.client = AMQClient(delegate=delegate, vhost=self.VHOST, spec=self.spec) self.client.start(self.creds) return self.client @defer.inlineCallbacks def publish(self, exchange, msg, routing_key): if not self.client: raise NotImplementedError if not self.channel: yield self._createChannel() content = Content(msg) yield self.channel.basic_publish(exchange=exchange, content=content, routing_key=routing_key) @defer.inlineCallbacks def _createChannel(self): self.channel = yield self.client.channel(1) yield self.channel.channel_open()
def setUp(self): super(AMQPumpTest, self).setUp() delegate = TwistedDelegate() spec = load(DEFAULT_SPEC) self.client = AMQClient(delegate, "/", spec, clock=Clock()) self.transport = AMQPump() self.transport.connect(self.client)
def connectionMade(self): """ Hook called when the connection is made; we'll use this to perform exchange setup, etc. """ try: connectionInfo = self.factory.connectionInfo set_keepalive(self.transport.socket, connectionInfo.amqpconnectionheartbeat) AMQClient.connectionMade(self) log.debug('Made initial connection to message broker') self._connected = False # Authenticate try: yield self.start({'LOGIN':connectionInfo.user, 'PASSWORD':connectionInfo.password}) self.factory.onAuthenticated(True) log.debug('Successfully authenticated as %s' % connectionInfo.user) except Exception as e: log.warn("Error authenticating to %s as %s" % (connectionInfo.host, connectionInfo.user)) self.factory.onAuthenticated(e.args[0]) return # Get a channel self.chan = yield self.get_channel() self._connected = True # Initialize the queues yield self.begin_listening() # Call back our deferred self.factory.onConnectionMade(self) # Flush any messages that have been sent before now yield self.send() returnValue(None) except Exception: log.exception("Unable to connect")
def connectionLost(self, reason): """ Remove this protocol as a consumer of log events. """ self.chan = None self.dispatcher.unregister(self.producer.put) log.err(reason, "Connection lost") AMQClient.connectionLost(self, reason)
def buildProtocol(self, addr): self.resetDelay() delegate = TwistedDelegate() self.client = AMQClient(delegate=delegate, vhost=self.VHOST, spec=self.spec) self.client.start(self.creds) return self.client
def connectionMade(self): """Called when a connection has been made.""" self.factory.log.info( "Connection made to %s:%s" % (self.factory.config.host, self.factory.config.port)) AMQClient.connectionMade(self) self.factory.connectDeferred.callback(self)
def makeConnection(self, transport): ''' This only for debug errors ''' try: AMQClient.makeConnection(self, transport) except Exception, mess: self.log.error('During makeConnection: %r'%mess)
def setUp(self): super(AMQClientTest, self).setUp() self.delegate = TwistedDelegate() self.clock = Clock() self.heartbeat = 1 self.protocol = AMQClient( self.delegate, "/", load(DEFAULT_SPEC), clock=self.clock, heartbeat=self.heartbeat) self.transport = AMQPump(Logger()) self.transport.connect(self.protocol)
def connectionMade(self): AMQClient.connectionMade(self) # set that we are not connected # since we should authenticate and open channels self.connected = False self.log.debug('go authentication %r'%self.factory.user) d = self.authenticate(self.factory.user, self.factory.password) d.addCallback(self._authenticated) d.addErrback(self._error) return d
def connectionMade(self): """Called when a connection has been made.""" AMQClient.connectionMade(self) # Flag that this protocol is not connected yet. self.connected = False # Authenticate. deferred = self.authenticate(self.factory.user, self.factory.password) deferred.addCallback(self._authenticated) deferred.addErrback(self._authentication_failed)
def __init__(self, *args, **kwargs): AMQClient.__init__(self, *args, **kwargs) if self.check_0_8(): self.replyToField = "reply to" else: self.replyToField = "reply-to" self.thriftBasicReturnQueueLock = defer.DeferredLock() self.thriftBasicReturnQueues = {}
def __init__(self, host, port, login, password, vhost, spec): self.host = host self.port = port self.login = login self.password = password self.vhost = vhost self.spec = spec AMQClient.__init__(self, delegate=TwistedDelegate(), vhost=vhost, spec=txamqp.spec.load(spec))
def connectionMade(self): """ authenticate and start the Node """ AMQClient.connectionMade(self) username = self.factory.username password = self.factory.password # authentication should happen automatically, and fail asap # XXX need to know how it can fail still d = self.authenticate(username, password) d.addCallback(self._auth_result) d.addErrback(self._auth_fail)
def connectionMade(self): """ Add this protocol as a consumer of log events. """ AMQClient.connectionMade(self) def eb(failure): log.err(failure) self.transport.loseConnection() d = self.gotConnection() d.addErrback(eb)
def connectionMade(self): """Called when a connection has been made.""" AMQClient.connectionMade(self) # Flag that this protocol is not connected yet. self.connected = False self.consumer_tags = {} # Authenticate. deferred = self.start({"LOGIN": self.factory.user, "PASSWORD": self.factory.password}) deferred.addCallback(self._authenticated) deferred.addErrback(self._authentication_failed)
def connectionMade(self): """Called when a connection has been made.""" AMQClient.connectionMade(self) # Flag that this protocol is not connected yet. self.connected = False # Authenticate. deferred = self.start({ "LOGIN": self.factory.user, "PASSWORD": self.factory.password }) deferred.addCallback(self._authenticated) deferred.addErrback(self._authentication_failed)
def start(self, *args, **kargs): yield AMQClient.start(self, *args, **kargs) self.channel_conn = yield self.channel(0) self.channel_rx = yield self.channel(1) yield self.channel_rx.channel_open() #todo: it's not correct for server side #self.queue_rx = yield self.channel_rx.queue_declare(exclusive=True, auto_delete=True) yield self._declare_queue_rx() self.replies = {} # correlation_id:deferred yield self.channel_rx.basic_consume(queue=self.queue_rx.queue, no_ack=True, consumer_tag='qtag') self.queue = yield AMQClient.queue(self, 'qtag') self.finish_defer = Deferred() self._update_work_defer()
def connect_to_exchange(self): """ Connect to an AMQP exchange as a publisher. """ exchange = self.pub_exchange vhost = self.pub_vhost user = self.pub_user passwd = self.pub_passwd endpoint_s = self.pub_endpoint_s spec = self.pub_spec e = clientFromString(self.reactor, endpoint_s) delegate = TwistedDelegate() amqp_protocol = AMQClient( delegate=delegate, vhost=vhost, spec=spec) try: conn = yield connectProtocol(e, amqp_protocol) except Exception: self.log.failure( "Failed to establish AMQP connection to endpoint '{0}'".format( endpoint_s)) raise yield conn.authenticate(user, passwd) self.pub_channel = yield conn.channel(1) yield self.pub_channel.channel_open()
def __init__(self, dispatcher, username='******', password='******', vhost='/', exchange='logs', queueSize=None): self.dispatcher = dispatcher self.username = username self.password = password self.exchange = exchange self.queueSize = queueSize self.chan = None specDir = FilePath(__file__).parent() specFilePath = specDir.child('amqp0-9-1.extended.xml') spec = txamqp.spec.load(specFilePath.path) delegate = TwistedDelegate() AMQClient.__init__(self, delegate=delegate, vhost=vhost, spec=spec)
def connectionMade(self): """Called when a connection has been made.""" AMQClient.connectionMade(self) self.default_log_level = self.factory.log_level is not None and self.factory.log_level or self.default_log_level # Flag that this protocol is not connected yet. self.connected = False # Authenticate. try: yield self.start(self.factory.credentials) except Exception: logger.error("Authentication failed: {}".format(traceback.format_exc())) returnValue(None) # Authenticated! try: self.chan = yield self.channel(self.get_channel_number()) except Exception: logger.error("Failed to get channel: {}".format(traceback.format_exc())) returnValue(None) # You now have a channel! try: yield self.chan.channel_open() except Exception: logger.error("Failed to open channel: {}".format(traceback.format_exc())) returnValue(None) # Mark the connection as open. self.connected = True logger.info('AMQP connection made.') # Now that the channel is open add any readers the user has specified. for consumer in self.factory.consumers: self.read(consumer) # Send any messages waiting to be sent. self.send() # Fire the factory's 'initial connect' deferred if it hasn't already if not self.factory.deferred.called: self.factory.deferred.callback(self)
def connectionMade(self): """ Hook called when the connection is made; we'll use this to perform exchange setup, etc. """ try: connectionInfo = self.factory.connectionInfo set_keepalive(self.transport.socket, connectionInfo.amqpconnectionheartbeat) AMQClient.connectionMade(self) log.debug('Made initial connection to message broker') self._connected = False # Authenticate try: yield self.start({ 'LOGIN': connectionInfo.user, 'PASSWORD': connectionInfo.password }) self.factory.onAuthenticated(True) log.debug('Successfully authenticated as %s' % connectionInfo.user) except Exception as e: log.warn("Error authenticating to %s as %s" % (connectionInfo.host, connectionInfo.user)) self.factory.onAuthenticated(e.args[0]) return # Get a channel self.chan = yield self.get_channel() self._connected = True # Initialize the queues yield self.begin_listening() # Call back our deferred self.factory.onConnectionMade(self) # Flush any messages that have been sent before now yield self.send() returnValue(None) except Exception: log.exception("Unable to connect")
class AMQPumpTest(TestCase): def setUp(self): super(AMQPumpTest, self).setUp() delegate = TwistedDelegate() spec = load(DEFAULT_SPEC) self.client = AMQClient(delegate, "/", spec, clock=Clock()) self.transport = AMQPump() self.transport.connect(self.client) def test_send_receive(self): """ Test sending and receiving frames. """ client_channel = self.successResultOf(self.client.channel(1)) server_channel = self.transport.channel(1) d = client_channel.basic_consume(queue="test-queue") server_channel.basic_consume_ok(consumer_tag="consumer") reply = self.successResultOf(d) queue = self.successResultOf(self.client.queue(reply.consumer_tag)) d = queue.get(timeout=1) server_channel.deliver("hello", consumer_tag="consumer") message = self.successResultOf(d) self.assertEqual(b"hello", message.content.body)
def connect(self): host = self.host port = self.port spec = self.spec user = self.username password = self.password vhost = self.vhost delegate = TwistedDelegate() onConn = Deferred() p = AMQClient(delegate, vhost, txamqp.spec.load(spec), heartbeat=0) f = protocol._InstanceFactory(reactor, p, onConn) c = reactor.connectTCP(host, port, f) def errb(thefailure): thefailure.trap(error.ConnectionRefusedError) logging.error(traceback.format_exc()) onConn.addErrback(errb) client = yield onConn self.client = client yield self.authenticate(self.client, user, password) returnValue(client)
def start_amqp_client(self): amqp_info = self.amqp_info log = self.amqp_log endpoint_str = amqp_info['endpoint'] vhost = amqp_info['vhost'] spec_path = amqp_info['spec'] queue_name = amqp_info['queue'] user = amqp_info['user'] passwd = amqp_info['passwd'] creds = (user, passwd) log.debug( "endpoint='{endpoint}', vhost='{vhost}', user='******', queue={queue}", endpoint=endpoint_str, vhost=vhost, user=user, spec=spec_path, queue=queue_name) delegate = TwistedDelegate() spec = txamqp.spec.load(spec_path) try: ep = clientFromString(self.reactor, endpoint_str) except filepath.UnlistableError as ex: msg = "One of the file paths in endpoint string '{0}' could not be listed.".format( endpoint_str) d = self.reactor.callLater(0, self.reactor.stop) raise EndpointError(msg) d = connectProtocol( ep, AMQClient(delegate=delegate, vhost=vhost, spec=spec)) d.addCallback(self.on_amqp_connect, queue_name, creds) def onError(err): if reactor.running: log.failure(err) reactor.stop() d.addErrback(onError)
def connectionMade(self): """Called when a connection has been made.""" self.factory.log.info("Connection made to %s:%s" % (self.factory.config.host, self.factory.config.port)) AMQClient.connectionMade(self) self.factory.connectDeferred.callback(self)
def __init__(self, connected_callback, *args, **kwargs): AMQClient.__init__(self, *args, **kwargs) self.connected_callback = connected_callback
def __init__(self, *args, **kwargs): AMQClient.__init__(self, *args, **kwargs) self.factory = None self.logTraffic = False
def sendHeartbeat(self): AMQClient.sendHeartbeat(self) self.called_send_hb += 1
def reschedule_checkHB(self, dummy=None): AMQClient.reschedule_checkHB(self) self.called_reschedule_check += 1
def reschedule_check_heartbeat(self, dummy=None): AMQClient.reschedule_check_heartbeat(self) self.called_reschedule_check += 1
def connectionLost(self, *args, **kwargs): AMQClient.connectionLost(self, *args, **kwargs) self.factory.onConnectionLost.callback(self)
def reschedule_sendHB(self): log.msg('Reschedule send heartbeat') AMQClient.reschedule_sendHB(self)
def connectionMade(self): yield AMQClient.connectionMade(self) log.listener("New AMQP connection made") yield self.setup() yield self.receive_loop()
def connectionMade(self): AMQClient.connectionMade(self) log.listener("New AMQP connection made") self.setup() wfd = waitForDeferred(self.receive_loop()) yield wfd
def doClose(self, reason): ''' override standart method ''' self.log.warning('Close due %r'%reason) AMQClient.doClose(self, reason)
def connectionMade(self): AMQClient.connectionMade(self) self.factory.gotConnection(self)
def connectionLost(self, reason): AMQClient.connectionLost(self, reason) self.connected = 0 self.factory.delConnection(self)
def connectionLost(self, reason): log.debug("connection lost %s" % reason) AMQClient.connectionLost(self, reason) self.factory.onConnectionLost(reason)
def sendHeartbeat(self): log.msg('sendHeartbeat') AMQClient.sendHeartbeat(self)
def reschedule_checkHB(self): log.msg('Reschedule check heartbeat') AMQClient.reschedule_checkHB(self)
def connectionMade(self): self.closed = False AMQClient.connectionMade(self)
def connectionMade(self): AMQClient.connectionMade(self) self.connected_callback(self)
def connectionMade(self): AMQClient.connectionMade(self) df = self.connect() df.addCallback(self.connect_success)
def connectionLost(self, failure): AMQClient.connectionLost(self, failure) self.factory.lostConnection(failure)