Class: Connfu::Listener

Inherits:
Object
  • Object
show all
Includes:
ConnfuLogger
Defined in:
lib/connfu/listener.rb

Overview

Class that listen to external events. Delegates the behavior in ConnfuStream so any new external resource can be added and no need to change the interface is required

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConnfuLogger

included, #logger

Constructor Details

#initialize(queue, app_stream, token, stream_endpoint) ⇒ Listener

Listener initializer.

Parameters

  • queue Connfu::Events instance to forward incoming events to be processed by the Dispatcher class

  • app_stream valid HTTP stream url to connect and listen events

  • token valid token to get access to a connFu Stream

  • stream_endpoint endpoint to open a keepalive HTTP connection



27
28
29
30
31
32
33
# File 'lib/connfu/listener.rb', line 27

def initialize(queue, app_stream, token, stream_endpoint)
  @connfu_stream = Connfu::ConnfuStream.new(app_stream, token, stream_endpoint)
  @continue = true
  @counter = 0
  @max_messages = 0
  @queue = queue
end

Instance Attribute Details

#app_streamObject (readonly)

Returns the value of attribute app_stream.



16
17
18
# File 'lib/connfu/listener.rb', line 16

def app_stream
  @app_stream
end

#continueObject (readonly)

checks if the thread should continue listening or return



75
76
77
# File 'lib/connfu/listener.rb', line 75

def continue
  @continue
end

#counterObject (readonly)

Returns the value of attribute counter.



15
16
17
# File 'lib/connfu/listener.rb', line 15

def counter
  @counter
end

#max_messagesObject

max amount of messages to receive



17
18
19
# File 'lib/connfu/listener.rb', line 17

def max_messages
  @max_messages
end

Instance Method Details

#joinObject

Wait to get external events



63
64
65
# File 'lib/connfu/listener.rb', line 63

def join
  @thread.join
end

#start(queue = nil) ⇒ Object

start listening. Should create a new thread and wait to new events to come

Parameters

  • queue to send incoming events to the Dispatcher class



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/connfu/listener.rb', line 40

def start(queue = nil)
  queue.nil? and queue = @queue
  logger.debug "Listener starts..."

  @thread = Thread.new {
    # listen to connFu
    @connfu_stream.start_listening
  }

  while continue do
    logger.debug "Waiting for a message from connFu stream"
    message = @connfu_stream.get
    @counter = @counter + 1

    logger.debug "#{self.class} got message => #{message}"
    # Write in internal Queue
    queue.put(message)
  end

end

#stopObject

stop waiting for external events



69
70
71
# File 'lib/connfu/listener.rb', line 69

def stop
  @continue = false
end