Class: Pione::Notification::Transmitter

Inherits:
Object
  • Object
show all
Defined in:
lib/pione/notification/transmitter.rb

Overview

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

Constant Summary collapse

LOCK =

a lock for transmitting notification messages

Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Transmitter

Returns a new instance of Transmitter.

Parameters:

  • uri (URI)

    target URI



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Instance Attribute Details

#uriObject (readonly)

target URI that the transmitter transmits notification messages to



29
30
31
# File 'lib/pione/notification/transmitter.rb', line 29

def uri
  @uri
end

Class Method Details

.transmit(message, targets = Global.notification_targets) ⇒ Object

Transmit the notification message to the targets.

Parameters:



12
13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Method Details

#closeObject

Close the transmitter's socket.



60
61
62
# File 'lib/pione/notification/transmitter.rb', line 60

def close
  @socket.close
end

#transmit(message) ⇒ void

This method returns an undefined value.

Transmit the notification message to target URI.

Parameters:



53
54
55
56
57
# 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