Class: Connfu::Dispatcher

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from ConnfuLogger

included, #logger

Constructor Details

#initialize(queue, listener_channels, app_channels = []) ⇒ Dispatcher

Initializer

Parameters

  • queue Connfu::Events instance to wait for incoming Message events

  • listener_channels Hash of listener_channels.

    • :key => channel name (valid ListenerChannel::CHANNEL_TYPES)

    • :value => ListenerChannel instance

  • app_channels information 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

#counterObject (readonly)

Returns the value of attribute counter.



12
13
14
# File 'lib/connfu/dispatcher.rb', line 12

def counter
  @counter
end

#max_messagesObject

Returns the value of attribute max_messages.



13
14
15
# File 'lib/connfu/dispatcher.rb', line 13

def max_messages
  @max_messages
end

Instance Method Details

#joinObject

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

  • queue optional 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)
          process_message(ev)
        }
      else
        set_channels!(event)
        process_message(event)
      end

    end
  }
end

#stopObject

Stop waiting for incoming events



77
78
79
# File 'lib/connfu/dispatcher.rb', line 77

def stop
  @continue = false
end