IO-Reactor /

Filename Size Date modified Message
examples
lib
spec
480 B
7 B
421 B
416 B
1.4 KB
1.5 KB
157 B
3.4 KB
2.0 KB

io_reactor

  • http://deveiate.org/projects/IO-Reactor

Description

An implementation of the Reactor design pattern for multiplexed asynchronous single-thread IO.

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.

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.

Installation

gem install io-reactor

Contributing

You can check out the current development source with Mercurial like so:

hg clone http://repo.deveiate.org/IO-Reactor

Or if you prefer Git, via its Github mirror:

https://github.com/ged/io-reactor

After checking out the source, run:

$ rake newb

This task will install any missing dependencies, run the tests/specs, and generate the API documentation.

License

Copyright (c) 2002-2011, 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.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.