Class: Rpush::Daemon::InterruptibleSleep
- Inherits:
-
Object
- Object
- Rpush::Daemon::InterruptibleSleep
- Defined in:
- lib/rpush/daemon/interruptible_sleep.rb
Instance Method Summary collapse
- #close ⇒ Object
-
#enable_wake_on_udp(host, port) ⇒ String, Integer
enable wake on receiving udp packets at the given address and port this returns the host,port used by bind in case an ephemeral port was indicated by specifying 0 as the port number.
-
#initialize ⇒ InterruptibleSleep
constructor
A new instance of InterruptibleSleep.
-
#interrupt_sleep ⇒ Object
writing to the pipe will wake the sleeping thread.
-
#sleep(timeout) ⇒ boolean
wait for the given timeout in seconds, or data was written to the pipe or the udp wakeup port if enabled.
Constructor Details
#initialize ⇒ InterruptibleSleep
Returns a new instance of InterruptibleSleep.
5 6 7 8 |
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 5 def initialize @sleep_reader, @wake_writer = IO.pipe @udp_wakeup = nil end |
Instance Method Details
#close ⇒ Object
40 41 42 43 44 |
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 40 def close @sleep_reader.close rescue nil @wake_writer.close rescue nil @udp_wakeup.close if @udp_wakeup rescue nil end |
#enable_wake_on_udp(host, port) ⇒ String, Integer
enable wake on receiving udp packets at the given address and port this returns the host,port used by bind in case an ephemeral port was indicated by specifying 0 as the port number.
14 15 16 17 18 |
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 14 def enable_wake_on_udp(host, port) @udp_wakeup = UDPSocket.new @udp_wakeup.bind(host, port) @udp_wakeup.addr.values_at(3,1) end |
#interrupt_sleep ⇒ Object
writing to the pipe will wake the sleeping thread
36 37 38 |
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 36 def interrupt_sleep @wake_writer.write('.') end |
#sleep(timeout) ⇒ boolean
wait for the given timeout in seconds, or data was written to the pipe or the udp wakeup port if enabled.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 23 def sleep(timeout) read_ports = [@sleep_reader] read_ports << @udp_wakeup if @udp_wakeup rs, = IO.select(read_ports, nil, nil, timeout) rescue nil # consume all data on the readable io's so that our next call will wait for more data perform_io(rs, @sleep_reader, :read_nonblock) perform_io(rs, @udp_wakeup, :recv_nonblock) !rs.nil? && rs.any? end |