Class: Celluloid::IO::Reactor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ringleader/celluloid_fix.rb

Overview

React to external I/O events. This is kinda sorta supposed to resemble the Reactor design pattern.

Defined Under Namespace

Classes: Monitor

Instance Method Summary collapse

Constructor Details

#initializeReactor

Returns a new instance of Reactor.



19
20
21
22
# File 'lib/ringleader/celluloid_fix.rb', line 19

def initialize
  @selector = NIO::Selector.new
  @monitors = {}
end

Instance Method Details

#run_once(timeout = nil) ⇒ Object

Run the reactor, waiting for events or wakeup signal



60
61
62
63
64
# File 'lib/ringleader/celluloid_fix.rb', line 60

def run_once(timeout = nil)
  @selector.select(timeout) do |monitor|
    monitor.value.resume
  end
end

#wait(io) {|monitor| ... } ⇒ Object

Wait for the given IO operation to complete

Yields:

  • (monitor)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ringleader/celluloid_fix.rb', line 39

def wait(io)
  # zomg ugly type conversion :(
  unless io.is_a?(::IO) or io.is_a?(OpenSSL::SSL::SSLSocket)
    if io.respond_to? :to_io
      io = io.to_io
    elsif ::IO.respond_to? :try_convert
      io = ::IO.try_convert(io)
    end

    raise TypeError, "can't convert #{io.class} into IO" unless io.is_a?(::IO)
  end

  unless monitor = @monitors[io]
    monitor = Monitor.new(@selector, io)
    @monitors[io] = monitor
  end

  yield monitor
end

#wait_readable(io) ⇒ Object

Wait for the given IO object to become readable



25
26
27
28
29
# File 'lib/ringleader/celluloid_fix.rb', line 25

def wait_readable(io)
  wait io do |monitor|
    monitor.wait_readable
  end
end

#wait_writable(io) ⇒ Object

Wait for the given IO object to become writable



32
33
34
35
36
# File 'lib/ringleader/celluloid_fix.rb', line 32

def wait_writable(io)
  wait io do |monitor|
    monitor.wait_writable
  end
end