An implementation of the Reactor design pattern for multiplexed asynchronous single-thread IO.
Authors
- Michael Granger
Description
This module is a pure-Ruby implementation of an asynchronous multiplexed IO Reactor 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.
I would greatly appreciate feedback on any aspect of this software.
Suggestions, feature requests, questions, design critiques, and bug reports
are most welcome. Relevant patches and minimal test cases are particularly
helpful. I may be reached by email at
deveiate.org/projects/IO-Reactor/
Trivial Example
This is a very trivial example — in most circumstances you‘d only use a Reactor when you‘re trying to manage reading and writing on more than a single IO object.
require 'io/reactor' reactor = IO::Reactor.new data_to_send = "some stuff to send" reader, writer = IO.pipe # Read from the reader end of the pipe until the writer finishes reactor.register( reader, :read ) do |io,event| if io.eof? reactor.unregister( io ) io.close else puts io.read( 256 ) end end # Write to the writer end of the pipe until there's no data left reactor.register( writer, :write ) do |io,event| bytes = io.write( data_to_send ) data_to_send.slice!( 0, bytes ) if data_to_send.empty? reactor.unregister( io ) io.close end end # Now pump the reactor until both sides are done puts "Starting IO" reactor.poll until reactor.empty? puts "done, exiting."
See the examples/ directory for some working, more full-featured examples.
Requirements
- Ruby >= 1.8.6
Optional:
- RSpec, if you want to run the tests.
Installation
If you use RubyGems, you can install via:
$ sudo gem install io-reactor
You can also install as a site library via the Rakefile:
$ wget http://deveiate.org/code/io-reactor-x.y.z.tar.gz $ tar xzvf io-reactor-x.y.z.tar.gz $ cd io-reactor-x.y.z $ sudo rake install
More Information
You can find more information about IO-Reactor, including the latest distribution and API documentation, at its project page:
deveiate.org/projects/IO-Reactor/
Legal
Copyright (c) 2001-2008, Michael Granger All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the author/s, nor the names of the project‘s contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
$Id: README 90 2008-08-20 00:51:42Z deveiant $