class Pione::Notification::Transmitter

‘Notification::Transmitter` is a class for transmitting notification messages to target URI.

Constants

LOCK

a lock for transmitting notification messages

Attributes

uri[R]

target URI that the transmitter transmits notification messages to

Public Class Methods

new(uri) click to toggle source

@param uri [URI]

target URI
# File lib/pione/notification/transmitter.rb, line 33
def initialize(uri)
  # URI scheme should be "pnb"(UDP broadcast), "pnm"(UDP multicast), or
  # "pnu"(UDP unicast)
  unless ["pnb", "pnm", "pnu"].include?(uri.scheme)
    raise ArgumentError.new(uri)
  end

  if uri.host.nil? or uri.port.nil?
    raise ArgumentError.new(uri)
  end

  @uri = uri
  open
end
transmit(message, targets=Global.notification_targets) click to toggle source

Transmit the notification message to the targets.

@param message [Notification::Message]

a notfication message

@param targets [Array<URI>]

target URIs
# File lib/pione/notification/transmitter.rb, line 12
def self.transmit(message, targets=Global.notification_targets)
  targets.each do |uri|
    transmitter = self.new(uri)
    begin
      transmitter.transmit(message)
    rescue => e
      Log::SystemLog.warn('Notification transmitter has failed to transmit to "%s": %s' % [uri, e.message])
    ensure
      transmitter.close
    end
  end
end

Public Instance Methods

close() click to toggle source

Close the transmitter’s socket.

# File lib/pione/notification/transmitter.rb, line 60
def close
  @socket.close
end
transmit(message) click to toggle source

Transmit the notification message to target URI.

@param message [Notification::Message]

a notification message

@return [void]

# File lib/pione/notification/transmitter.rb, line 53
def transmit(message)
  LOCK.synchronize do
    @socket.send(message.dump, 0, @uri.host, @uri.port)
  end
end

Private Instance Methods

open() click to toggle source

Open a UPD socket and configure it by URI scheme.

# File lib/pione/notification/transmitter.rb, line 67
def open
  @socket = UDPSocket.open

  case @uri.scheme
  when "pnb"
    # enable broadcast flag
    @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, 1)
  when "pnm"
    if @uri.interface
      # enable to bind interface
      interface = IPAddr.new(@uri.interface).hton
      @socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_MULTICAST_IF, interface)
    end
  end
end