class Pione::Notification::Recipient

Recipient is a wrapper for notification recipients. This wraps a command and notifies messages to it when a notification listener receives notifications. Recipients should be registerd to listner periodically because the listener forgets recipients after some minites. Note that ‘Recipient` instance is registered to a listener after it is created immediately, so the listener is launched up to that time.

Public Class Methods

new(front_uri, listener_uri) click to toggle source

@param front_uri [URI]

URI of command front that receives messages

@param listener_uri [URI]

URI of notification listener
# File lib/pione/notification/recipient.rb, line 14
def initialize(front_uri, listener_uri)
  @listener_uri = listener_uri
  @front_uri = front_uri
  @__listener_thread__ = keep_connection
  @__connection__ = false
end

Public Instance Methods

notify(message) click to toggle source

Notify the message. This is a non-blocking method.

# File lib/pione/notification/recipient.rb, line 22
def notify(message)
  name = ("receive_%s" % message.type).downcase
  if respond_to?(name)
    __send__(name, message)
  end
end
terminate() click to toggle source
# File lib/pione/notification/recipient.rb, line 29
def terminate
  @__listerner_thread__.terminate if @__listener_thread__.alive?
  disconnect
end

Private Instance Methods

disconnect() click to toggle source
# File lib/pione/notification/recipient.rb, line 60
def disconnect
  Util.ignore_exception do
    listener = DRb::DRbObject.new_with_uri(@listener_uri)
    listener.delete(@front_uri.to_s)
  end
end
keep_connection() click to toggle source

Keep the listener connection. This is non-blocking.

@return [Thread]

a keeping thread
# File lib/pione/notification/recipient.rb, line 40
def keep_connection
  Thread.new do
    loop do
      begin
        listener = DRb::DRbObject.new_with_uri(@listener_uri)
        listener.add(@front_uri.to_s)

        unless @__connection__
          Log::SystemLog.info('Notification recipient has connected to the listener "%s".' % @listener_uri)
        end
        @__connection__ = true
      rescue Object => e
        Log::SystemLog.warn('Notification recipient has failed to connect the listener "%s": %s' % [@listener_uri, e.message])
      ensure
        sleep 5
      end
    end
  end
end