コード例 #1
0
ファイル: Component.py プロジェクト: borrey/bus-component
class BusComponent:

    CONSUME_STATE = 1
    SEND_STATE = 2

    def __init__( self, bus_server, bus_listener, bus_emit, initial_state=CONSUME_STATE, verbose=False ) :
        self.event_listener = EventEmitter()
        self.state = initial_state
        self.verbose = verbose
        self.message = Message( {
            'server' :  bus_server,
            'exchange' : bus_listener['exchange'], 'queue' : bus_listener['queue']
        }, self.event_listener, 'start' )
        
        self.event_listener.on( 'message', self.messageHandler )

    def messageHandler ( self, message ) :
        #TODO
        sys.stderr.write('messageHandler %s\n' % message)

    def runComponent( self ) :
        try :
            self.event_listener.emit('start')
            while True :
                if self.state == self.CONSUME_STATE :
                    self.consumeState()
                elif self.state == self.LISTEN_STATE :
                    self.listenState()
                else :
                    sys.stderr.write( 'unknown state' )
        except ( KeyboardInterrupt, SystemExit ):
            self.event_listener.emit( 'stop' )
    def setState( self, state ):
        #This class is to set the state
        #put it here so that I can access it from outside the class (mostly for testing)
        self.state = state


    def consumeState( self ):
        #this state is to receive commands and resources needed 
        #log amqp
        #
        #Get possible resources. get instructions
        print 'consumeState'
        sleep(10)
    
    def listenState( self ):
        print 'listenState'
        sleep(10)
コード例 #2
0
ファイル: metronome.py プロジェクト: borrey/metronome-emmiter
class Module:
    def __init__( self, _config = {}, listener = None, start_message = None ):
        if 'interval' in  _config :
            self.default_interval = _config['interval']
        else :
            self.default_interval = 1000
        if 'messgae' in  _config :
            self.default_message = _config['message']
        else :
            self.default_message = 'metronome'
        if listener is None :
            self.listener = EventEmitter()
        self.interval = {}
        self.resets = {}
        
        def handle_metronome( data = {} ):
            if 'interval' in  data :
                interval = data['interval']
            else :
                interval = self.default_interval
            if 'messgae' in  data :
                message = data['message']
            else :
                message = self.default_message
            self.start( interval, message )

        self.listener.on('metronome_update', handle_metronome )

    def getListener( self ):
        return self.listener

    def emitMessage ( self, message ):
        self.listener.emit( message )

    def start( self, interval = 1000, message = 'metronome' ):
        def fn_wrapper():
            interval_fn()
        def interval_fn():
            self.emitMessage( message )
            if message in self.interval and self.interval[ message ]:
                timer = Timer( interval/1000, fn_wrapper )#convert back to milliseconds
                timer.start()
        self.interval[ message ] = True
        interval_fn()
            
    def stop( self, message ):
        self.interval[ message ] = False
コード例 #3
0
ファイル: __init__.py プロジェクト: borrey/message-amqp
class Message:
    def __init__( self, _config = {}, listener = None, start_message = None ):
        if listener is None:
            self.listener = EventEmitter()
        else:
            self.listener = listener

        if 'server' in  _config :
            self.server = _config['server']
        else :
            self.server = { 'host' : 'localhost', 'defaultExchangeName' : 'amqp_exchange', 'port' : 5672, 'virtual_host' : '/', 'login': '******', 'password': '******' }

        if 'exchange' in  _config :
            self.exchange = _config['exchange']
        else :
            self.exchange = ''

        if 'exchange_type' in  _config :
            self.exchange_type = _config['exchange_type']
        else :
            self.exchange_type = 'direct'

        if 'queue' in  _config :
            self.queue = _config['queue']
        else :
            self.queue = ''
        if start_message is not None :
            def emitMessage( ch, method, properties, body ):                
                ch.basic_ack(delivery_tag = method.delivery_tag)
                self.listener.emit( 'message', body )
            def callback( *args) :
                #try :
                self.listen( self.server, emitMessage, self.exchange, self.exchange_type, self.queue )
            self.listener.on( start_message, callback )

    def getConnection( self, server, blocking ) :

        if 'login' in server :
            login = server['login']
        else :
            login = '******'
        if 'password' in server :
            password = server['password']
        else :
            password = '******'

        credentials = pika.PlainCredentials( login, password )

        if 'host' in server :
            host = server['host']
        else :
            host = 'localhost'
        if 'port' in server :
            port = server['port']
        else :
            port = 5672
        if 'vhost' in server :
            vhost = server['vhost']
        else :
            vhost = '/'
        connection_parameters = pika.ConnectionParameters( host=host, port=port, virtual_host=vhost, credentials=credentials )#TODO:ssl_options

        connection = pika.BlockingConnection( parameters = connection_parameters )
        #TODO: pika.adapters.select_connection.SelectConnection( parameters=parameters, on_open_callback=on_open )
        # https://pika.readthedocs.org/en/latest/examples/comparing_publishing_sync_async.html
        return connection

    def listen( self, server, callback, exchange_name='', exchange_type = 'direct', queue_name ='' ):
        connection = self.getConnection( server, False )
        channel = connection.channel()
        
        channel.basic_qos(prefetch_count=1)
        if queue_name == '' :
            result = channel.queue_declare(exclusive=True)
            queue_name = result.method.queue
            #print 'Have: ',( queue_name )
        else :
            channel.queue_declare( queue=queue_name, durable=True )
        if exchange_name != '' :
            channel.exchange_declare( exchange=exchange_name, type=exchange_type, durable=True )
            channel.queue_bind(exchange=exchange_name, queue=queue_name )
        channel.basic_consume( callback, queue = queue_name )
        channel.start_consuming()

    def writeToExchange( self, server, exchange_name, exchange_type = 'direct', queue_name='', message='' ):
        print 'writeToExchange:'+queue_name
        connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost' ))
        channel = connection.channel()
        if exchange_name != '' :
            print exchange_name, exchange_type
            channel.exchange_declare( exchange=exchange_name, type=exchange_type, durable=True )
        channel.basic_publish( exchange=exchange_name, routing_key=queue_name, body=message, properties=pika.BasicProperties(
            delivery_mode = 2#make message persistent
        ))
        connection.close() 

    def getListener( self ):
        return self.listener;