Class: Connfu::Dispatcher
- Inherits:
-
Object
- Object
- Connfu::Dispatcher
- Includes:
- ConnfuLogger
- Defined in:
- lib/connfu/dispatcher.rb
Overview
Class that dispatches the external events. Currently there is no thread pool, so each event is executed sequentially
Instance Attribute Summary collapse
-
#counter ⇒ Object
readonly
Returns the value of attribute counter.
-
#max_messages ⇒ Object
Returns the value of attribute max_messages.
Instance Method Summary collapse
-
#initialize(queue, listener_channels, app_channels = []) ⇒ Dispatcher
constructor
Initializer.
-
#join ⇒ Object
This method should be called for the thread that started the dispatcher in order to wait for dispatching incoming events from the listener.
-
#start(queue = nil) ⇒ Object
start waiting for incoming Message.
-
#stop ⇒ Object
Stop waiting for incoming events.
Methods included from ConnfuLogger
Constructor Details
#initialize(queue, listener_channels, app_channels = []) ⇒ Dispatcher
Initializer
Parameters
-
queueConnfu::Events instance to wait for incoming Message events -
listener_channelsHash of listener_channels.-
:key => channel name (valid ListenerChannel::CHANNEL_TYPES)
-
:value => ListenerChannel instance
-
-
app_channelsinformation about application channels to set the channel_name associated to an inbound event
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/connfu/dispatcher.rb', line 24 def initialize(queue, listener_channels, app_channels = []) if listener_channels.nil? or !listener_channels.is_a?(Hash) or listener_channels.length == 0 raise Exception, "Unable to dispatch events if no channel is defined" end @continue = true @counter = 0 @max_messages = 0 @listener_channels = listener_channels @app_channels = app_channels logger.debug("Dispatcher initializer") @queue = queue end |
Instance Attribute Details
#counter ⇒ Object (readonly)
Returns the value of attribute counter.
12 13 14 |
# File 'lib/connfu/dispatcher.rb', line 12 def counter @counter end |
#max_messages ⇒ Object
Returns the value of attribute max_messages.
13 14 15 |
# File 'lib/connfu/dispatcher.rb', line 13 def @max_messages end |
Instance Method Details
#join ⇒ Object
This method should be called for the thread that started the dispatcher in order to wait for dispatching incoming events from the listener
71 72 73 |
# File 'lib/connfu/dispatcher.rb', line 71 def join @thread.join end |
#start(queue = nil) ⇒ Object
start waiting for incoming Message. Should create a new thread and wait to new events to come
Parameters
-
queueoptional Connfu::Events instance to wait for incoming Message events. If nil, the value got in the initializer is used
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/connfu/dispatcher.rb', line 43 def start(queue = nil) queue.nil? and queue = @queue logger.debug("Dispatcher starts") @thread = Thread.new { while continue logger.debug("Dispatcher waiting for a message from the Listener") event = queue.get @counter = @counter + 1 logger.debug "---------------------------------" logger.debug "#{self.class} => #{event}" if event.is_a?(Array) event.each { |ev| set_channels!(ev) (ev) } else set_channels!(event) (event) end end } end |
#stop ⇒ Object
Stop waiting for incoming events
77 78 79 |
# File 'lib/connfu/dispatcher.rb', line 77 def stop @continue = false end |