[ANN] IO-Reactor 0.04

Hi fellow Rubyists,

I’d like to announce version 0.04 of the ‘IO-Reactor’ module, which
used to be called ‘Ruby-Poll’, and used to wrap the poll(2) system
call. It no longer does so, for two reasons:

  • poll() is not very portable
  • poll() doesn’t work with Ruby’s threads (ie., a blocking call to
    poll() blocks all threads).

Since I find that the API simplifies some IO tasks, and is (I think) a
more Ruby-ish IO idiom, I’ve rewritten the backend using IO::select,
while keeping (mostly) the same interface. The last version of the
’Ruby-Poll’ module will continue to be available for the foreseeable
future, but will no longer be actively maintained.

So, what’s IO-Reactor?

It’s an implementation of an asynchronous multiplexed IO Reactor class
which is based on the Reactor design pattern found in Pattern-Oriented
Software Architecture, Volume 2: Patterns for Concurrent and Networked
Objects
. It allows a single thread to demultiplex and dispatch events
from one or more IO objects to the appropriate handler. In other words,
you can take a collection of IO objects, register them all with the
reactor with handlers for read, write, and error events, and then drive
IO in your application all with one call.

A simple example:

require 'io/reactor’
require ‘socket’

reactorobj = IO::Reactor::new

Open a listening socket on port 1138 and register it with the

reactor. When a :read event occurs on it, it means an incoming

connection has been established

sock = TCPServer::new(‘localhost’, 1138)
reactorobj.register( sock, :read ) {|sock,event|
case event

   # Accept the incoming connection, registering it also with
   # the reactor; events on client sockets are handled by the
   # #clientHandler method (not shown).
   when :read
       clsock = sock.accept
       reactorobj.register( clsock, :read, :write,
           &method(:clientHandler) )

   # Errors on the main listening socket cause the whole
   # reactor to be shut down
   when :error
       reactorobj.clear
       $stderr.puts "Server error: Shutting down"

   else
       $stderr.puts "Unhandled event: #{event}"
   end

}

Drive the reactor until it’s empty with a single thread

Thread::new { reactorobj.poll until reactorobj.empty? }

The module can be downloaded from its project page at:

http://www.deveiate.org/code/IO-Reactor.shtml

The API documentation can be viewed at:

http://www.deveiate.org/code/IO-Reactor/

Comments, bug reports, and feedback are welcomed.

Thanks for your time,

···


Michael Granger ged@FaerieMUD.org
Rubymage, Believer, Architect
The FaerieMUD Consortium http://www.FaerieMUD.org/