Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 341 lines (245 sloc) 11.014 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
package POE::Wheel;

use strict;

use vars qw($VERSION);
$VERSION = '1.352'; # NOTE - Should be #.### (three decimal places)

use Carp qw(croak);

# Used to generate unique IDs for wheels. This is static data, shared
# by all.
my $current_id = 0;
my %active_wheel_ids;

sub new {
  my $type = shift;
  croak "$type is not meant to be used directly";
}

sub allocate_wheel_id {
  while (1) {
    last unless exists $active_wheel_ids{ ++$current_id };
  }
  return $active_wheel_ids{$current_id} = $current_id;
}

sub free_wheel_id {
  my $id = shift;
  delete $active_wheel_ids{$id};
}

sub _test_set_wheel_id {
  $current_id = shift;
}

1;

__END__

=head1 NAME

POE::Wheel - event-driven mixins for POE::Session

=head1 SYNOPSIS

This base class has no synopsis.
Please consult one of the subclasses instead.

=head1 DESCRIPTION

A POE::Wheel object encapsulates a bundle of event handlers that
perform a specific task. It also manages the event watchers that
trigger those handlers.

Object lifetime is very important for POE wheels. At creation time,
most wheels will add anonymous event handlers to the currently active
session. In other words, the session that created the wheel is
modified to handle new events. Event watchers may also be initialized
as necessary to trigger the new handlers. These event watchers are
also owned by the session that created the wheel.

Sessions must not expose their wheels to other sessions. Doing so
will likely cause problems because wheels are tightly integrated with
the sessions that created them. For example, calling put() on a
POE::Wheel::ReadWrite instance may enable a write-okay watcher. The
handler for this watcher is already defined in the wheel's owner.
Calling put() outside that session will enable the write-okay watcher
in the wrong session, and the event will never be handled.

Likewise, wheels must be destroyed from within their creator sessions.
Otherwise breakage will occur when the wheels' DESTROY methods try to
unregister event handlers and watchers from the wrong sessions. To
simplify things, it's recommended to store POE::Wheel instances in
heaps of the sessions that created them.

For example, creating a POE::Wheel::FollowTail object will register an
event handler that periodically polls a file for new information. It
will also start the timer that triggers the periodic polling.

use POE;
use POE::Wheel::FollowTail;

my @files_to_tail = qw( messages maillog security );

foreach my $filename (@files_to_tail) {
POE::Session->create(
inline_states => {
_start => sub {
push @{$_[HEAP]{messages}}, POE::Wheel::FollowTail->new(
Filename => "/var/log/$filename",
InputEvent => "got_input",
);
},
got_input => sub {
print "$filename: $_[ARG0]\n";
},
}
);
}

POE::Kernel->run();
exit;

As illustrated in the previous example it is possible---sometimes
recommended---to create more than one POE::Wheel of a particular type
in the same session. A session with multiple wheels may scale better
than separate sessions with one wheel apiece. When in doubt,
benchmark.

Unlike components (or cheese), wheels do not stand alone. Each wheel
must be created by a session in order to register event watchers and
handlers within that session. Wheels are thusly tightly coupled to
their creator sessions and cannot be passed to other sessions.

=head1 FILTERS, AND DRIVERS

Many wheels perform data transfer operations on filehandles (which, as
you probably know, includes sockets, pipes, and just about anything
else that can store or transfer data).

To avoid subclass hell, POE::Wheel objects may be customized at
creation time by including other objects from the POE::Filter and
POE::Driver namespaces.

=head2 Filters

POE "filters" implement data parsers and serializers. For example,
POE::Filter::Line parses streams into records separated by some string
(the traditional network newline by default). The Line filter also
adds record separators to data being output.

POE::Filter::HTTPD is a more complex example. It implements a subset
of the server-side of the HTTP protocol. Input streams are parsed
into HTTP requests and wrapped in HTTP::Request objects. Server code
sends HTTP::Response objects back to the client, which are serialized
so they may be sent to a socket.

Most wheels use POE::Filter::Line by default.

=head2 Drivers

POE "drivers" implement strategies for sending data to a filehandle
and receiving input from it. A single POE::Wheel class may interact
with files, pipes, sockets, or devices by using the appropriate
driver.

POE::Driver::SysRW is the only driver that comes with POE. sysread()
and syswrite() can handle nearly every kind of stream interaction, so
there hasn't been much call for another type of driver.

=head1 METHODS

POE::Wheel defines a common interface that most subclasses use.
Subclasses may implement other methods, especially to help perform
their unique tasks. If something useful isn't documented here, see
the subclass before implementing a feature.

=head2 Required Methods

These methods are required by all subclasses.

=head3 new LOTS_OF_STUFF

new() instantiates and initializes a new wheel object and returns it.
The new wheel will continue to function for as long as it exists,
although other methods may alter the way it functions.

Part of any wheel's construction is the registration of anonymous
event handlers to perform wheel-specific tasks. Event watchers are
also started to trigger the handlers when relevant activity occurs.

Every wheel has a different purpose and requires different constructor
parameters, so LOTS_OF_STUFF is documented in each particular
subclass.

=head3 DESTROY

DESTROY is Ye Olde Perl Object Destructor. When the wheel's last
strong reference is relinquished, DESTROY triggers the wheel's
cleanup. The object removes itself from the session that created it:
Active event watchers are stopped, and anonymous event handlers are
unregistered.

=head3 event EVENT_TYPE, EVENT_NAME [, EVENT_TYPE, EVENT_NAME, ....]

event() changes the events that a wheel will emit. Its parameters are
one or more pairs of EVENT_TYPEs and the EVENT_NAMEs to emit when each
type of event occurs. If an EVENT_NAME is undefined, then the wheel
will stop emitting that type of event. Or the wheel may throw an
error if the event type is required.

EVENT_TYPEs differ for each wheel and correspond to the constructor
parameters that match /.*Event$/. For example, POE::Wheel::ReadWrite
may emit up to five different kinds of event: InputEvent, ErrorEvent,
FlushedEvent, HighEvent, LowEvent. The name of each emitted event may
be changed at run time.

This example changes the events to emit on new input and when output
is flushed. It stops the wheel from emitting events when errors
occur.

$wheel->event(
InputEvent => 'new_input_event',
ErrorEvent => undef,
FlushedEvent => 'new_flushed_event',
);

=head2 I/O Methods

Wheels that perform input and output may implement some or all of
these methods. The put() method is a common omission. Wheels that
don't perform output do not have put() methods.

=head3 put RECORD [, RECORD [, ....]]

put() sends one or more RECORDs to the wheel for transmitting. Each
RECORD is serialized by the wheel's associated POE::Filter so that it
will be ready to transmit. The serialized stream may be transmitted
immediately by the wheel's POE::Driver object, or it may be buffered
in the POE::Driver until it can be flushed to the output filehandle.

Most wheels use POE::Filter::Line and POE::Driver::SysRW by default,
so it's not necessary to specify them in most cases.

=head2 Class Static Functions

These functions expose information that is common to all wheels. They
are not methods, so they should B<not> be called as methods.

my $new_wheel_id = POE::Wheel::allocate_wheel_id();
POE::Wheel::free_wheel_id($new_wheel_id);

=head3 allocate_wheel_id

B<This is not a class method.>

Every wheel has a unique ID. allocate_wheel_id() returns the next
available unique wheel ID. Wheel constructors use it to set their IDs
internally.

package POE::Wheel::Example;
use base qw(POE::Wheel);

sub new {
# ... among other things ...
$self->[MY_WHEEL_ID] = POE::Wheel::allocate_wheel_id();
return $self;
}

Wheel IDs are used to tell apart events from similarly typed wheels.
For example, a multi-file tail utility may handle all file input with
the same function. Wheel IDs may be used to tell which wheel
generated the InputEvent being handled.

Wheel IDs are often used to store wheel-local state in a session's
heap.

sub handle_error {
my $wheel_id = $_[ARG3];
print "Wheel $wheel_id caught an error. Shutting it down.\n";
delete $_[HEAP]{wheels}{$wheel_id};
}

It is vital for wheels to free their allocated IDs when they are
destroyed. POE::Wheel class keeps track of allocated wheel IDs to
avoid collisions, and they will remain in memory until freed. See
free_wheel_id().

=head3 free_wheel_id WHEEL_ID

B<This is not a class method.>

free_wheel_id() deallocates a wheel's ID so that it stops consuming
memory and may be reused later. This is often called from a wheel's
destructor.

package POE::Wheel::Example;
use base qw(POE::Wheel);

sub DESTROY {
my $self = shift;
# ... among other things ...
POE::Wheel::free_wheel_id($self->[MY_WHEEL_ID]);
}

Wheel IDs may be reused, although it has never been reported. Two
active wheels will never share the same ID, however.

=head3 ID

B<This is usually implemented in the subclass!>

The ID() method returns a wheel's unique ID. It is commonly used to
match events with the wheels which generated them.

Again, this method is not implemented in this class! If it's missing
from the subclass, please go pester that module author---thanks!

=head1 SEE ALSO

The SEE ALSO section in L<POE> contains a table of contents covering
the entire POE distribution.

L<POE::Driver> - A base class for file access drivers that POE::Wheel
may use.

L<POE::Filter> - A base class for data parsers and marshalers that
POE::Wheel may use.

L<POE::Wheel::Curses> - Non-blocking input for Curses.

L<POE::Wheel::FollowTail> - Non-blocking file and FIFO monitoring.

L<POE::Wheel::ListenAccept> - Non-blocking server for existing
sockets.

L<POE::Wheel::ReadLine> - Non-blocking console input, with full
readline support.

L<POE::Wheel::ReadWrite> - Non-blocking stream I/O.

L<POE::Wheel::Run> - Non-blocking process creation and management.

L<POE::Wheel::SocketFactory> - Non-blocking socket creation,
supporting most protocols and modes.

Z<TODO - Link to POE::Wheel search.cpan.org module search.>

=head1 BUGS

It would be nice if wheels were more like proper Unix streams.

=head1 AUTHORS & COPYRIGHTS

Please see L<POE> for more information about authors, contributors,
and POE;s licensing.

=cut

# rocco // vim: ts=2 sw=2 expandtab
# TODO - Edit.
Something went wrong with that request. Please try again.