Subversion Info

Rev
86
Last Checked In
2008-08-19 23:41:58 (about one hour ago)
Checked in by
deveiant

Parent

Class Index

Quicksearch

IO::Reactor

An object-oriented multiplexing asynchronous IO mechanism for Ruby.

Synopsis

   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
   reactor.poll until reactor.empty?

Author

Michael Granger

Copyright (c) 2002-2008 The FaerieMUD Consortium. All rights reserved.

This module is free software. You may use, modify, and/or redistribute this software under the same terms as Ruby itself.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Version

 $Id: reactor.rb 86 2008-08-19 23:41:58Z deveiant $

Constants

SVNRev
SVN Revision
SVNId
SVN Id
VERSION
Package version
VALID_EVENTS
List of valid event types, in the order IO#select returns them

Attributes

handles[R]
The Hash of handles (instances of IO or its subclasses) associated with the reactor. The keys are the IO objects, and the values are a Hash of event/s => handler.
pending_events[R]
The Hash of unhandled events which occurred in the last call to #poll, keyed by handle.

Public Class Methods

new() click to toggle source

Create and return a new IO reactor object.

    # File lib/io/reactor.rb, line 78
78:     def initialize
79:         @handles      = Hash.new {|hsh,key|
80:             hsh[ key ] = {
81:                 :events     => [],
82:                 :handler    => nil,
83:                 :args       => [],
84:             }
85:         }
86:         @pending_events   = Hash.new {|hsh,key| hsh[ key ] = []}
87:     end

Public Instance Methods

add( io, *args, &handler ) click to toggle source

Alias for #register

clear() click to toggle source

Clear all registered handles from the poll object. Returns the handles that were cleared.

     # File lib/io/reactor.rb, line 223
223:     def clear
224:         rv = @handles.keys
225: 
226:         @pending_events.clear
227:         @handles.clear
228: 
229:         return rv
230:     end
disableEvents( io, *events ) click to toggle source

Alias for #disable_events

disable_events( io, *events ) click to toggle source

Remove the specified events from the list that will be polled for on the given io handle.

     # File lib/io/reactor.rb, line 154
154:     def disable_events( io, *events )
155:         raise RuntimeError, "Cannot disable the :error event" if
156:             events.include?( :error )
157:         @handles[ io ][:events] -= events
158:     end
Also aliased as: disableEvents
empty?() click to toggle source

Returns true if no handles are associated with the receiver.

     # File lib/io/reactor.rb, line 295
295:     def empty?
296:         @handles.empty?
297:     end
enableEvents( io, *events ) click to toggle source

Alias for #enable_events

enable_events( io, *events ) click to toggle source

Add the specified events to the list that will be polled for on the given io handle.

     # File lib/io/reactor.rb, line 146
146:     def enable_events( io, *events )
147:         @handles[ io ][:events] |= events
148:     end
Also aliased as: enableEvents
event_enabled?( io, event ) click to toggle source

Returns true if the specified event is enabled for the given io.

     # File lib/io/reactor.rb, line 163
163:     def event_enabled?( io, event )
164:         return false unless @handles.key?( io )
165:         return true if event == :error # Error is always enabled for all handles
166:         return @handles[ io ][ :events ].include?( event )
167:     end
Also aliased as: has_event_enabled?
has_event_enabled?( io, event ) click to toggle source

Alias for #event_enabled?

poll( timeout=-1 ) {|io, eventMask| ...} click to toggle source

Poll the handles registered to the reactor for pending events. The following event types are defined:

:read]:
Data may be read from the handle without blocking.
:write]:
Data may be written to the handle without blocking.
:error]:
An error has occurred on the handle. This event type is always enabled, regardless of whether or not it is passed as one of the events.

Any handlers specified when the handles were registered are run for those handles with events. If a block is given, it will be invoked once for each handle which doesn‘t have an explicit handler. If no block is given, events without explicit handlers are inserted into the reactor‘s pending_events attribute.

The timeout argument is the number of floating-point seconds to wait for an event before returning (ie., fourth argument to the underlying select() call); negative timeout values will cause #poll to block until there is at least one event to report.

This method returns the number of handles on which one or more events occurred.

     # File lib/io/reactor.rb, line 259
259:     def poll( timeout=-1 ) # :yields: io, eventMask
260:         timeout = timeout.to_f
261:         @pending_events.clear
262:         count = 0
263: 
264:         unless @handles.empty?
265:             timeout = nil if timeout < 0
266:             evented_handles = self.get_pending_events( timeout )
267: 
268:             # For each event of each io that had an event happen, call any
269:             # associated callback, or any provided block, or failing both of
270:             # those, add the event to the hash of unhandled pending events.
271:             evented_handles.each do |io,events|
272:                 count += 1
273:                 events.each do |ev|
274:                     # Don't continue if the io was unregistered by an earlier handler
275:                     break unless @handles.key?( io )
276:                     
277:                     args = @handles[ io ][:args]
278: 
279:                     if @handles[ io ][:handler]
280:                         @handles[ io ][:handler].call( io, ev, *args )
281:                     elsif block_given?
282:                         yield( io, ev, *args )
283:                     else
284:                         @pending_events[io].push( ev )
285:                     end
286:                 end
287:             end
288:         end
289: 
290:         return count
291:     end
register( io, *args, &handler ) click to toggle source

Register the specified IO object with the reactor for events given as args. The reactor will test the given io for the events specified whenever #poll is called. See the #poll method for a list of valid events. If no events are specified, only :error events will be polled for.

If a handler is specified, it will be called whenever the io has any of the specified events occur to it. It should take at least two parameters: the io and the event.

If args contains any objects except the Symbols ‘:read’, ‘:write’, or ‘:error’, and a handler is specified, they will be saved and passed to handler for each event.

Registering a handle will unregister any previously registered event/handler+arguments pairs associated with the handle.

     # File lib/io/reactor.rb, line 121
121:     def register( io, *args, &handler )
122:         events = VALID_EVENTS & args
123:         args -= events
124: 
125:         self.unregister( io )
126:         self.enable_events( io, *events )
127:         if handler
128:             self.set_handler( io, *args, &handler )
129:         else
130:             self.set_args( io, *args )
131:         end
132: 
133:         return self
134:     end
Also aliased as: add
registered?( io ) click to toggle source

Returns true if the given io handle is registered with the reactor.

     # File lib/io/reactor.rb, line 139
139:     def registered?( io )
140:         return @handles.key?( io )
141:     end
remove( io ) click to toggle source

Alias for #unregister

removeArgs( io ) click to toggle source

Alias for #remove_args

removeHandler( io ) click to toggle source

Alias for #remove_handler

remove_args( io ) click to toggle source

Remove the arguments for the given handle to the given args.

     # File lib/io/reactor.rb, line 205
205:     def remove_args( io )
206:         return @handles[ io ][:args].clear
207:     end
Also aliased as: removeArgs
remove_handler( io ) click to toggle source

Remove and return the handler for events on the given io handle.

     # File lib/io/reactor.rb, line 185
185:     def remove_handler( io )
186:         rval = @handles[ io ][:handler]
187:         @handles[ io ][:handler] = nil
188:         self.remove_args( io )
189:         return rval
190:     end
Also aliased as: removeHandler
setArgs( io, *args ) click to toggle source

Alias for #set_args

setHandler( io, *args, &handler ) click to toggle source

Alias for #set_handler

set_args( io, *args ) click to toggle source

Set the additional arguments to pass to the handler for the given io handle on each event to the given args.

     # File lib/io/reactor.rb, line 196
196:     def set_args( io, *args )
197:         rval = @handles[ io ][:args]
198:         @handles[ io ][:args] = args
199:         return rval
200:     end
Also aliased as: setArgs
set_handler( io, *args, &handler ) click to toggle source

Set the handler for events on the given io handle to the specified handler. If any args are present, they will be passed as an exploded array to the handler for each event. Returns the previously-registered handler, if any.

     # File lib/io/reactor.rb, line 175
175:     def set_handler( io, *args, &handler )
176:         rval = @handles[ io ][:handler]
177:         @handles[ io ][:handler] = handler
178:         self.set_args( io, *args )
179:         return rval
180:     end
Also aliased as: setHandler
unregister( io ) click to toggle source

Remove the specified io from the receiver‘s list of registered handles, if present. Returns the handle if it was registered, or nil if it was not.

     # File lib/io/reactor.rb, line 214
214:     def unregister( io )
215:         @pending_events.delete( io )
216:         @handles.delete( io )
217:     end
Also aliased as: remove

Protected Instance Methods

getPendingEvents( timeout ) click to toggle source
getReadHandles() click to toggle source

Alias for #get_read_handles

getWriteHandles() click to toggle source

Alias for #get_write_handles

get_pending_events( timeout ) click to toggle source

Select on the registered handles, returning a Hash of handles => events for handles which had events occur.

     # File lib/io/reactor.rb, line 306
306:     def get_pending_events( timeout )
307:         @handles.delete_if {|io,_| io.closed? }
308: 
309:         eventHandles = select( self.get_read_handles, self.get_write_handles,
310:             @handles.keys, timeout ) or return {}
311:         eventHash = Hash.new {|hsh,io| hsh[io] = []}
312: 
313:         # Fill in the hash with pending events of each type
314:         VALID_EVENTS.each_with_index do |event,i|
315:             eventHandles[i].each {|io| eventHash[io].push( event )}
316:         end
317:         return eventHash
318:     end
Also aliased as: getPendingEvents
get_read_handles() click to toggle source

Return an Array of handles which have handlers for the :read event.

     # File lib/io/reactor.rb, line 324
324:     def get_read_handles
325:         @handles.
326:             find_all {|io,hsh| hsh[:events].include?( :read )}.
327:             collect {|io,_| io }
328:     end
Also aliased as: getReadHandles
get_write_handles() click to toggle source

Return an Array of handles which have handlers for the :write event.

     # File lib/io/reactor.rb, line 334
334:     def get_write_handles
335:         @handles.
336:             find_all {|io,hsh| hsh[:events].include?( :write )}.
337:             collect {|io,_| io }
338:     end
Also aliased as: getWriteHandles

secsequence

--- SEC00004

seccomment

--- ""

attributes

--- 
- name: handles
  rw: R
  a_desc: |+
    
    The Hash of handles (instances of IO or its subclasses) associated with the
    reactor. The keys are the IO objects, and the values are a Hash of event/s
    => handler.
    
- name: pending_events
  rw: R
  a_desc: |+
    
    The Hash of unhandled events which occurred in the last call to <a
    href="Reactor.html#M000022">#poll</a>, keyed by handle.
    

method_list

--- 
- methods: 
  - visibility: public
    aref: M000001
    name: new
    sourcecode: "    <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 78</span>\n\
      78:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">initialize</span>\n\
      79:         <span class=\"ruby-ivar\">@handles</span>      = <span class=\"ruby-constant\">Hash</span>.<span class=\"ruby-identifier\">new</span> {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">hsh</span>,<span class=\"ruby-identifier\">key</span><span class=\"ruby-operator\">|</span>\n\
      80:             <span class=\"ruby-identifier\">hsh</span>[ <span class=\"ruby-identifier\">key</span> ] = {\n\
      81:                 <span class=\"ruby-identifier\">:events</span>     =<span class=\"ruby-operator\">&gt;</span> [],\n\
      82:                 <span class=\"ruby-identifier\">:handler</span>    =<span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-keyword kw\">nil</span>,\n\
      83:                 <span class=\"ruby-identifier\">:args</span>       =<span class=\"ruby-operator\">&gt;</span> [],\n\
      84:             }\n\
      85:         }\n\
      86:         <span class=\"ruby-ivar\">@pending_events</span>   = <span class=\"ruby-constant\">Hash</span>.<span class=\"ruby-identifier\">new</span> {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">hsh</span>,<span class=\"ruby-identifier\">key</span><span class=\"ruby-operator\">|</span> <span class=\"ruby-identifier\">hsh</span>[ <span class=\"ruby-identifier\">key</span> ] = []}\n\
      87:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Create and return a new IO reactor object.
      </p>
    params: ()
  category: Class
  type: Public
- methods: 
  - visibility: public
    aref: M000003
    name: add
    m_desc: |-
      <p>
      Alias for <a href="Reactor.html#M000002">#register</a>
      </p>
    params: ( io, *args, &amp;handler )
  - visibility: public
    aref: M000021
    name: clear
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 223</span>\n\
      223:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">clear</span>\n\
      224:         <span class=\"ruby-identifier\">rv</span> = <span class=\"ruby-ivar\">@handles</span>.<span class=\"ruby-identifier\">keys</span>\n\
      225: \n\
      226:         <span class=\"ruby-ivar\">@pending_events</span>.<span class=\"ruby-identifier\">clear</span>\n\
      227:         <span class=\"ruby-ivar\">@handles</span>.<span class=\"ruby-identifier\">clear</span>\n\
      228: \n\
      229:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">rv</span>\n\
      230:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Clear all registered handles from the poll object. Returns the handles that
      were cleared.
      </p>
    params: ()
  - visibility: public
    aref: M000008
    name: disableEvents
    m_desc: |-
      <p>
      Alias for <a href="Reactor.html#M000007">#disable_events</a>
      </p>
    params: ( io, *events )
  - visibility: public
    aref: M000007
    name: disable_events
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 154</span>\n\
      154:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">disable_events</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-operator\">*</span><span class=\"ruby-identifier\">events</span> )\n\
      155:         <span class=\"ruby-identifier\">raise</span> <span class=\"ruby-constant\">RuntimeError</span>, <span class=\"ruby-value str\">&quot;Cannot disable the :error event&quot;</span> <span class=\"ruby-keyword kw\">if</span>\n\
      156:             <span class=\"ruby-identifier\">events</span>.<span class=\"ruby-identifier\">include?</span>( <span class=\"ruby-identifier\">:error</span> )\n\
      157:         <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:events</span>] <span class=\"ruby-operator\">-=</span> <span class=\"ruby-identifier\">events</span>\n\
      158:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000008
      name: disableEvents
    m_desc: |-
      <p>
      Remove the specified <tt>events</tt> from the list that will be polled for
      on the given <tt>io</tt> handle.
      </p>
    params: ( io, *events )
  - visibility: public
    aref: M000023
    name: empty?
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 295</span>\n\
      295:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">empty?</span>\n\
      296:         <span class=\"ruby-ivar\">@handles</span>.<span class=\"ruby-identifier\">empty?</span>\n\
      297:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Returns <tt>true</tt> if no handles are associated with the receiver.
      </p>
    params: ()
  - visibility: public
    aref: M000006
    name: enableEvents
    m_desc: |-
      <p>
      Alias for <a href="Reactor.html#M000005">#enable_events</a>
      </p>
    params: ( io, *events )
  - visibility: public
    aref: M000005
    name: enable_events
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 146</span>\n\
      146:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">enable_events</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-operator\">*</span><span class=\"ruby-identifier\">events</span> )\n\
      147:         <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:events</span>] <span class=\"ruby-operator\">|=</span> <span class=\"ruby-identifier\">events</span>\n\
      148:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000006
      name: enableEvents
    m_desc: |-
      <p>
      Add the specified <tt>events</tt> to the list that will be polled for on
      the given <tt>io</tt> handle.
      </p>
    params: ( io, *events )
  - visibility: public
    aref: M000009
    name: event_enabled?
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 163</span>\n\
      163:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">event_enabled?</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-identifier\">event</span> )\n\
      164:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-keyword kw\">false</span> <span class=\"ruby-keyword kw\">unless</span> <span class=\"ruby-ivar\">@handles</span>.<span class=\"ruby-identifier\">key?</span>( <span class=\"ruby-identifier\">io</span> )\n\
      165:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-keyword kw\">true</span> <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-identifier\">event</span> <span class=\"ruby-operator\">==</span> <span class=\"ruby-identifier\">:error</span> <span class=\"ruby-comment cmt\"># Error is always enabled for all handles</span>\n\
      166:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][ <span class=\"ruby-identifier\">:events</span> ].<span class=\"ruby-identifier\">include?</span>( <span class=\"ruby-identifier\">event</span> )\n\
      167:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000010
      name: has_event_enabled?
    m_desc: |-
      <p>
      Returns <tt>true</tt> if the specified <tt>event</tt> is enabled for the
      given <tt>io</tt>.
      </p>
    params: ( io, event )
  - visibility: public
    aref: M000010
    name: has_event_enabled?
    m_desc: |-
      <p>
      Alias for #event_enabled?
      </p>
    params: ( io, event )
  - visibility: public
    aref: M000022
    name: poll
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 259</span>\n\
      259:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">poll</span>( <span class=\"ruby-identifier\">timeout</span>=<span class=\"ruby-value\">-1</span> ) <span class=\"ruby-comment cmt\"># :yields: io, eventMask</span>\n\
      260:         <span class=\"ruby-identifier\">timeout</span> = <span class=\"ruby-identifier\">timeout</span>.<span class=\"ruby-identifier\">to_f</span>\n\
      261:         <span class=\"ruby-ivar\">@pending_events</span>.<span class=\"ruby-identifier\">clear</span>\n\
      262:         <span class=\"ruby-identifier\">count</span> = <span class=\"ruby-value\">0</span>\n\
      263: \n\
      264:         <span class=\"ruby-keyword kw\">unless</span> <span class=\"ruby-ivar\">@handles</span>.<span class=\"ruby-identifier\">empty?</span>\n\
      265:             <span class=\"ruby-identifier\">timeout</span> = <span class=\"ruby-keyword kw\">nil</span> <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-identifier\">timeout</span> <span class=\"ruby-operator\">&lt;</span> <span class=\"ruby-value\">0</span>\n\
      266:             <span class=\"ruby-identifier\">evented_handles</span> = <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">get_pending_events</span>( <span class=\"ruby-identifier\">timeout</span> )\n\
      267: \n\
      268:             <span class=\"ruby-comment cmt\"># For each event of each io that had an event happen, call any</span>\n\
      269:             <span class=\"ruby-comment cmt\"># associated callback, or any provided block, or failing both of</span>\n\
      270:             <span class=\"ruby-comment cmt\"># those, add the event to the hash of unhandled pending events.</span>\n\
      271:             <span class=\"ruby-identifier\">evented_handles</span>.<span class=\"ruby-identifier\">each</span> <span class=\"ruby-keyword kw\">do</span> <span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">io</span>,<span class=\"ruby-identifier\">events</span><span class=\"ruby-operator\">|</span>\n\
      272:                 <span class=\"ruby-identifier\">count</span> <span class=\"ruby-operator\">+=</span> <span class=\"ruby-value\">1</span>\n\
      273:                 <span class=\"ruby-identifier\">events</span>.<span class=\"ruby-identifier\">each</span> <span class=\"ruby-keyword kw\">do</span> <span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">ev</span><span class=\"ruby-operator\">|</span>\n\
      274:                     <span class=\"ruby-comment cmt\"># Don't continue if the io was unregistered by an earlier handler</span>\n\
      275:                     <span class=\"ruby-keyword kw\">break</span> <span class=\"ruby-keyword kw\">unless</span> <span class=\"ruby-ivar\">@handles</span>.<span class=\"ruby-identifier\">key?</span>( <span class=\"ruby-identifier\">io</span> )\n\
      276:                     \n\
      277:                     <span class=\"ruby-identifier\">args</span> = <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:args</span>]\n\
      278: \n\
      279:                     <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:handler</span>]\n\
      280:                         <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:handler</span>].<span class=\"ruby-identifier\">call</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-identifier\">ev</span>, <span class=\"ruby-operator\">*</span><span class=\"ruby-identifier\">args</span> )\n\
      281:                     <span class=\"ruby-keyword kw\">elsif</span> <span class=\"ruby-identifier\">block_given?</span>\n\
      282:                         <span class=\"ruby-keyword kw\">yield</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-identifier\">ev</span>, <span class=\"ruby-operator\">*</span><span class=\"ruby-identifier\">args</span> )\n\
      283:                     <span class=\"ruby-keyword kw\">else</span>\n\
      284:                         <span class=\"ruby-ivar\">@pending_events</span>[<span class=\"ruby-identifier\">io</span>].<span class=\"ruby-identifier\">push</span>( <span class=\"ruby-identifier\">ev</span> )\n\
      285:                     <span class=\"ruby-keyword kw\">end</span>\n\
      286:                 <span class=\"ruby-keyword kw\">end</span>\n\
      287:             <span class=\"ruby-keyword kw\">end</span>\n\
      288:         <span class=\"ruby-keyword kw\">end</span>\n\
      289: \n\
      290:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">count</span>\n\
      291:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Poll the handles registered to the reactor for pending events. The
      following event types are defined:
      </p>
      <dl>
      <dt><tt>:read</tt>]:</dt><dd>Data may be read from the handle without blocking.
      
      </dd>
      <dt><tt>:write</tt>]:</dt><dd>Data may be written to the handle without blocking.
      
      </dd>
      <dt><tt>:error</tt>]:</dt><dd>An error has occurred on the handle. This event type is always enabled,
      regardless of whether or not it is passed as one of the <tt>events</tt>.
      
      </dd>
      </dl>
      <p>
      Any handlers specified when the handles were registered are run for those
      handles with events. If a block is given, it will be invoked once for each
      handle which doesn&#8216;t have an explicit handler. If no block is given,
      events without explicit handlers are inserted into the reactor&#8216;s
      <tt>pending_events</tt> attribute.
      </p>
      <p>
      The <tt>timeout</tt> argument is the number of floating-point seconds to
      wait for an event before returning (ie., fourth argument to the underlying
      <tt>select()</tt> call); negative timeout values will cause <a
      href="Reactor.html#M000022">#poll</a> to block until there is at least one
      event to report.
      </p>
      <p>
      This method returns the number of handles on which one or more events
      occurred.
      </p>
    params: ( timeout=-1 ) {|io, eventMask| ...}
  - visibility: public
    aref: M000002
    name: register
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 121</span>\n\
      121:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">register</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-operator\">*</span><span class=\"ruby-identifier\">args</span>, <span class=\"ruby-operator\">&amp;</span><span class=\"ruby-identifier\">handler</span> )\n\
      122:         <span class=\"ruby-identifier\">events</span> = <span class=\"ruby-constant\">VALID_EVENTS</span> <span class=\"ruby-operator\">&amp;</span> <span class=\"ruby-identifier\">args</span>\n\
      123:         <span class=\"ruby-identifier\">args</span> <span class=\"ruby-operator\">-=</span> <span class=\"ruby-identifier\">events</span>\n\
      124: \n\
      125:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">unregister</span>( <span class=\"ruby-identifier\">io</span> )\n\
      126:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">enable_events</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-operator\">*</span><span class=\"ruby-identifier\">events</span> )\n\
      127:         <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-identifier\">handler</span>\n\
      128:             <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">set_handler</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-operator\">*</span><span class=\"ruby-identifier\">args</span>, <span class=\"ruby-operator\">&amp;</span><span class=\"ruby-identifier\">handler</span> )\n\
      129:         <span class=\"ruby-keyword kw\">else</span>\n\
      130:             <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">set_args</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-operator\">*</span><span class=\"ruby-identifier\">args</span> )\n\
      131:         <span class=\"ruby-keyword kw\">end</span>\n\
      132: \n\
      133:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-keyword kw\">self</span>\n\
      134:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000003
      name: add
    m_desc: |-
      <p>
      Register the specified IO object with the reactor for events given as
      <tt>args</tt>. The reactor will test the given <tt>io</tt> for the events
      specified whenever <a href="Reactor.html#M000022">#poll</a> is called. See
      the <a href="Reactor.html#M000022">#poll</a> method for a list of valid
      events. If no events are specified, only <tt>:error</tt> events will be
      polled for.
      </p>
      <p>
      If a <tt>handler</tt> is specified, it will be called whenever the
      <tt>io</tt> has any of the specified <tt>events</tt> occur to it. It should
      take at least two parameters: the <tt>io</tt> and the event.
      </p>
      <p>
      If <tt>args</tt> contains any objects except the Symbols
      &#8216;<tt>:read</tt>&#8217;, &#8216;<tt>:write</tt>&#8217;, or
      &#8216;<tt>:error</tt>&#8217;, and a <tt>handler</tt> is specified, they
      will be saved and passed to handler for each event.
      </p>
      <p>
      Registering a handle will unregister any previously registered
      event/handler+arguments pairs associated with the handle.
      </p>
    params: ( io, *args, &amp;handler )
  - visibility: public
    aref: M000004
    name: registered?
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 139</span>\n\
      139:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">registered?</span>( <span class=\"ruby-identifier\">io</span> )\n\
      140:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-ivar\">@handles</span>.<span class=\"ruby-identifier\">key?</span>( <span class=\"ruby-identifier\">io</span> )\n\
      141:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Returns <tt>true</tt> if the given <tt>io</tt> handle is registered with
      the reactor.
      </p>
    params: ( io )
  - visibility: public
    aref: M000020
    name: remove
    m_desc: |-
      <p>
      Alias for <a href="Reactor.html#M000019">#unregister</a>
      </p>
    params: ( io )
  - visibility: public
    aref: M000018
    name: removeArgs
    m_desc: |-
      <p>
      Alias for <a href="Reactor.html#M000017">#remove_args</a>
      </p>
    params: ( io )
  - visibility: public
    aref: M000014
    name: removeHandler
    m_desc: |-
      <p>
      Alias for <a href="Reactor.html#M000013">#remove_handler</a>
      </p>
    params: ( io )
  - visibility: public
    aref: M000017
    name: remove_args
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 205</span>\n\
      205:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">remove_args</span>( <span class=\"ruby-identifier\">io</span> )\n\
      206:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:args</span>].<span class=\"ruby-identifier\">clear</span>\n\
      207:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000018
      name: removeArgs
    m_desc: |-
      <p>
      Remove the arguments for the given handle to the given <tt>args</tt>.
      </p>
    params: ( io )
  - visibility: public
    aref: M000013
    name: remove_handler
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 185</span>\n\
      185:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">remove_handler</span>( <span class=\"ruby-identifier\">io</span> )\n\
      186:         <span class=\"ruby-identifier\">rval</span> = <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:handler</span>]\n\
      187:         <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:handler</span>] = <span class=\"ruby-keyword kw\">nil</span>\n\
      188:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">remove_args</span>( <span class=\"ruby-identifier\">io</span> )\n\
      189:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">rval</span>\n\
      190:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000014
      name: removeHandler
    m_desc: |-
      <p>
      Remove and return the handler for events on the given <tt>io</tt> handle.
      </p>
    params: ( io )
  - visibility: public
    aref: M000016
    name: setArgs
    m_desc: |-
      <p>
      Alias for <a href="Reactor.html#M000015">#set_args</a>
      </p>
    params: ( io, *args )
  - visibility: public
    aref: M000012
    name: setHandler
    m_desc: |-
      <p>
      Alias for <a href="Reactor.html#M000011">#set_handler</a>
      </p>
    params: ( io, *args, &amp;handler )
  - visibility: public
    aref: M000015
    name: set_args
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 196</span>\n\
      196:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">set_args</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-operator\">*</span><span class=\"ruby-identifier\">args</span> )\n\
      197:         <span class=\"ruby-identifier\">rval</span> = <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:args</span>]\n\
      198:         <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:args</span>] = <span class=\"ruby-identifier\">args</span>\n\
      199:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">rval</span>\n\
      200:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000016
      name: setArgs
    m_desc: |-
      <p>
      Set the additional arguments to pass to the handler for the given
      <tt>io</tt> handle on each event to the given <tt>args</tt>.
      </p>
    params: ( io, *args )
  - visibility: public
    aref: M000011
    name: set_handler
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 175</span>\n\
      175:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">set_handler</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-operator\">*</span><span class=\"ruby-identifier\">args</span>, <span class=\"ruby-operator\">&amp;</span><span class=\"ruby-identifier\">handler</span> )\n\
      176:         <span class=\"ruby-identifier\">rval</span> = <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:handler</span>]\n\
      177:         <span class=\"ruby-ivar\">@handles</span>[ <span class=\"ruby-identifier\">io</span> ][<span class=\"ruby-identifier\">:handler</span>] = <span class=\"ruby-identifier\">handler</span>\n\
      178:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">set_args</span>( <span class=\"ruby-identifier\">io</span>, <span class=\"ruby-operator\">*</span><span class=\"ruby-identifier\">args</span> )\n\
      179:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">rval</span>\n\
      180:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000012
      name: setHandler
    m_desc: |-
      <p>
      Set the handler for events on the given <tt>io</tt> handle to the specified
      <tt>handler</tt>. If any <tt>args</tt> are present, they will be passed as
      an exploded array to the handler for each event. Returns the
      previously-registered handler, if any.
      </p>
    params: ( io, *args, &amp;handler )
  - visibility: public
    aref: M000019
    name: unregister
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 214</span>\n\
      214:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">unregister</span>( <span class=\"ruby-identifier\">io</span> )\n\
      215:         <span class=\"ruby-ivar\">@pending_events</span>.<span class=\"ruby-identifier\">delete</span>( <span class=\"ruby-identifier\">io</span> )\n\
      216:         <span class=\"ruby-ivar\">@handles</span>.<span class=\"ruby-identifier\">delete</span>( <span class=\"ruby-identifier\">io</span> )\n\
      217:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000020
      name: remove
    m_desc: |-
      <p>
      Remove the specified <tt>io</tt> from the receiver&#8216;s list of
      registered handles, if present. Returns the handle if it was registered, or
      <tt>nil</tt> if it was not.
      </p>
    params: ( io )
  category: Instance
  type: Public
- methods: 
  - visibility: protected
    aref: M000025
    name: getPendingEvents
    m_desc: |-
      <p>
      Alias for <a href="Reactor.html#M000024">#get_pending_events</a>
      </p>
    params: ( timeout )
  - visibility: protected
    aref: M000027
    name: getReadHandles
    m_desc: |-
      <p>
      Alias for <a href="Reactor.html#M000026">#get_read_handles</a>
      </p>
    params: ()
  - visibility: protected
    aref: M000029
    name: getWriteHandles
    m_desc: |-
      <p>
      Alias for <a href="Reactor.html#M000028">#get_write_handles</a>
      </p>
    params: ()
  - visibility: protected
    aref: M000024
    name: get_pending_events
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 306</span>\n\
      306:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">get_pending_events</span>( <span class=\"ruby-identifier\">timeout</span> )\n\
      307:         <span class=\"ruby-ivar\">@handles</span>.<span class=\"ruby-identifier\">delete_if</span> {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">io</span>,<span class=\"ruby-identifier\">_</span><span class=\"ruby-operator\">|</span> <span class=\"ruby-identifier\">io</span>.<span class=\"ruby-identifier\">closed?</span> }\n\
      308: \n\
      309:         <span class=\"ruby-identifier\">eventHandles</span> = <span class=\"ruby-identifier\">select</span>( <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">get_read_handles</span>, <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">get_write_handles</span>,\n\
      310:             <span class=\"ruby-ivar\">@handles</span>.<span class=\"ruby-identifier\">keys</span>, <span class=\"ruby-identifier\">timeout</span> ) <span class=\"ruby-keyword kw\">or</span> <span class=\"ruby-keyword kw\">return</span> {}\n\
      311:         <span class=\"ruby-identifier\">eventHash</span> = <span class=\"ruby-constant\">Hash</span>.<span class=\"ruby-identifier\">new</span> {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">hsh</span>,<span class=\"ruby-identifier\">io</span><span class=\"ruby-operator\">|</span> <span class=\"ruby-identifier\">hsh</span>[<span class=\"ruby-identifier\">io</span>] = []}\n\
      312: \n\
      313:         <span class=\"ruby-comment cmt\"># Fill in the hash with pending events of each type</span>\n\
      314:         <span class=\"ruby-constant\">VALID_EVENTS</span>.<span class=\"ruby-identifier\">each_with_index</span> <span class=\"ruby-keyword kw\">do</span> <span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">event</span>,<span class=\"ruby-identifier\">i</span><span class=\"ruby-operator\">|</span>\n\
      315:             <span class=\"ruby-identifier\">eventHandles</span>[<span class=\"ruby-identifier\">i</span>].<span class=\"ruby-identifier\">each</span> {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">io</span><span class=\"ruby-operator\">|</span> <span class=\"ruby-identifier\">eventHash</span>[<span class=\"ruby-identifier\">io</span>].<span class=\"ruby-identifier\">push</span>( <span class=\"ruby-identifier\">event</span> )}\n\
      316:         <span class=\"ruby-keyword kw\">end</span>\n\
      317:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">eventHash</span>\n\
      318:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000025
      name: getPendingEvents
    m_desc: |-
      <p>
      Select on the registered handles, returning a Hash of handles => events for
      handles which had events occur.
      </p>
    params: ( timeout )
  - visibility: protected
    aref: M000026
    name: get_read_handles
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 324</span>\n\
      324:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">get_read_handles</span>\n\
      325:         <span class=\"ruby-ivar\">@handles</span>.\n\
      326:             <span class=\"ruby-identifier\">find_all</span> {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">io</span>,<span class=\"ruby-identifier\">hsh</span><span class=\"ruby-operator\">|</span> <span class=\"ruby-identifier\">hsh</span>[<span class=\"ruby-identifier\">:events</span>].<span class=\"ruby-identifier\">include?</span>( <span class=\"ruby-identifier\">:read</span> )}.\n\
      327:             <span class=\"ruby-identifier\">collect</span> {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">io</span>,<span class=\"ruby-identifier\">_</span><span class=\"ruby-operator\">|</span> <span class=\"ruby-identifier\">io</span> }\n\
      328:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000027
      name: getReadHandles
    m_desc: |-
      <p>
      Return an Array of handles which have handlers for the <tt>:read</tt>
      event.
      </p>
    params: ()
  - visibility: protected
    aref: M000028
    name: get_write_handles
    sourcecode: "     <span class=\"ruby-comment cmt\"># File lib/io/reactor.rb, line 334</span>\n\
      334:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">get_write_handles</span>\n\
      335:         <span class=\"ruby-ivar\">@handles</span>.\n\
      336:             <span class=\"ruby-identifier\">find_all</span> {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">io</span>,<span class=\"ruby-identifier\">hsh</span><span class=\"ruby-operator\">|</span> <span class=\"ruby-identifier\">hsh</span>[<span class=\"ruby-identifier\">:events</span>].<span class=\"ruby-identifier\">include?</span>( <span class=\"ruby-identifier\">:write</span> )}.\n\
      337:             <span class=\"ruby-identifier\">collect</span> {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">io</span>,<span class=\"ruby-identifier\">_</span><span class=\"ruby-operator\">|</span> <span class=\"ruby-identifier\">io</span> }\n\
      338:     <span class=\"ruby-keyword kw\">end</span>"
    aka: 
    - aref: Reactor.html#M000029
      name: getWriteHandles
    m_desc: |-
      <p>
      Return an Array of handles which have handlers for the <tt>:write</tt>
      event.
      </p>
    params: ()
  category: Instance
  type: Protected

sectitle

--- 

constants

--- 
- name: SVNRev
  desc: |+
    
    SVN Revision
    
  value: "%q$Rev: 86 $"
- name: SVNId
  desc: |+
    
    SVN Id
    
  value: "%q$Id: reactor.rb 86 2008-08-19 23:41:58Z deveiant $"
- name: VERSION
  desc: |+
    
    Package version
    
  value: "'1.0.4'"
- name: VALID_EVENTS
  desc: |+
    
    List of valid event types, in the order IO#select returns them
    
  value: "[:read, :write, :error]"

aliases

--- 
- old_name: pending_events
  new_name: pendingEvents

[Validate]

Generated with the Darkfish Rdoc Generator.