Class: Pione::Notification::Receiver
- Inherits:
-
Object
- Object
- Pione::Notification::Receiver
- 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
-
#close ⇒ Object
Close the receiver socket.
-
#closed? ⇒ Boolean
Return true if receiver socket is closed.
-
#initialize(uri) ⇒ Receiver
constructor
A new instance of Receiver.
-
#receive ⇒ Object
Receive a notification packet.
-
#reopen ⇒ Object
Close and open receiver socket.
Constructor Details
#initialize(uri) ⇒ Receiver
Returns a new instance of Receiver.
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
#close ⇒ Object
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.
49 50 51 |
# File 'lib/pione/notification/receiver.rb', line 49 def closed? @socket.closed? end |
#receive ⇒ Object
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.load(data) return [ip_address, ] rescue TypeError, NoMethodError ip_address = ip_address || "(unknown)" Log::Debug.notification("Invalid data has been received from %s." % ip_address) retry end |
#reopen ⇒ Object
Close and open receiver socket.
35 36 37 38 |
# File 'lib/pione/notification/receiver.rb', line 35 def reopen close open(@host, @port) end |