Class: Pione::Agent::NotificationListener

Inherits:
BasicAgent
  • Object
show all
Defined in:
lib/pione/agent/notification-listener.rb

Instance Attribute Summary collapse

Attributes inherited from BasicAgent

#chain_threads

Instance Method Summary collapse

Methods inherited from BasicAgent

agent_type, inherited, set_agent_type, #start, #start!, #states, #terminate, #terminated?, #transit, #wait_until, #wait_until_after, #wait_until_before, #wait_until_terminated

Methods included from StateTransitionSingletonMethod

#chain, #define_exception_handler, #define_transition, #exception_handler, #start, #transition_chain

Constructor Details

#initialize(model, uri) ⇒ NotificationListener

Returns a new instance of NotificationListener.

Parameters:

  • model (NotificationListenerModel)

    notification-listner model

  • uri (URI)

    listening port



15
16
17
18
19
20
21
# File 'lib/pione/agent/notification-listener.rb', line 15

def initialize(model, uri)
  super()
  @uri = uri
  @model = model
  @notification_handlers = ThreadGroup.new
  @lock = Mutex.new
end

Instance Attribute Details

#notification_handlersObject (readonly)

notification handler threads



9
10
11
# File 'lib/pione/agent/notification-listener.rb', line 9

def notification_handlers
  @notification_handlers
end

Instance Method Details

#transit_to_initObject

Initialize the agent.



37
38
39
40
# File 'lib/pione/agent/notification-listener.rb', line 37

def transit_to_init
  Log::SystemLog.info('Notification listener starts listening notification messages on "%s".' % @uri)
  @receiver = Notification::Receiver.new(@uri)
end

#transit_to_receiveObject

Receive notification messages and make a message handler thread.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pione/agent/notification-listener.rb', line 43

def transit_to_receive
  # receive a notification
  transmitter_host, message = @receiver.receive

  # handle the notification in new thread
  thread = Util::FreeThreadGenerator.generate do
    handle_notification(transmitter_host, message)
  end
  @notification_handlers.add(thread)
rescue StandardError => e
  Log::Debug.notification("Receiver agent has received bad data from %s, so it ignored and reopen socket." % ip_address)
  @receiver.reopen
end

#transit_to_terminateObject

Close receiver socket.



58
59
60
61
62
63
# File 'lib/pione/agent/notification-listener.rb', line 58

def transit_to_terminate
  # kill threads of notification handler
  @notification_handlers.list.each {|thread| thread.kill.join}
  # close socket
  @receiver.close unless @receiver.closed?
end