Class: Pione::Notification::Receiver

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

Overview

Notification::Receiver provides the function to receive notification packets. Be careful we assume notification's packet is small.

Constant Summary collapse

LOCK =

a lock for receiving notifications

Mutex.new
MAX_SIZE =
65535

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Receiver

Returns a new instance of Receiver.

Parameters:

  • uri (URI)

    URI of receiver address



13
14
15
16
17
18
19
20
# File 'lib/pione/notification/receiver.rb', line 13

def initialize(uri)
  unless ["pnb", "pnm", "pnu"].include?(uri.scheme)
    raise ArgumentError.new(uri)
  end

  @uri = uri
  open
end

Instance Method Details

#closeObject

Close the receiver socket.



41
42
43
# File 'lib/pione/notification/receiver.rb', line 41

def close
  @socket.close
end

#closed?Boolean

Return true if receiver socket is closed.

Returns:

  • (Boolean)

    true if receiver socket is closed



49
50
51
# File 'lib/pione/notification/receiver.rb', line 49

def closed?
  @socket.closed?
end

#receiveObject

Receive a notification packet.



23
24
25
26
27
28
29
30
31
32
# File 'lib/pione/notification/receiver.rb', line 23

def receive
  data, addr = LOCK.synchronize {@socket.recvfrom(MAX_SIZE)}
  ip_address = addr[3]
  message = Message.load(data)
  return [ip_address, message]
rescue TypeError, NoMethodError
  ip_address = ip_address || "(unknown)"
  Log::Debug.notification("Invalid data has been received from %s." % ip_address)
  retry
end

#reopenObject

Close and open receiver socket.



35
36
37
38
# File 'lib/pione/notification/receiver.rb', line 35

def reopen
  close
  open(@host, @port)
end