home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Loop.pm < prev    next >
Encoding:
Perl POD Document  |  2004-04-17  |  14.4 KB  |  481 lines

  1. # $Id: Loop.pm,v 1.4 2004/04/17 17:20:36 sungo Exp $
  2.  
  3. package POE::Loop;
  4.  
  5. use strict;
  6.  
  7. use vars qw($VERSION);
  8. $VERSION = do {my@r=(q$Revision: 1.4 $=~/\d+/g);sprintf"%d."."%04d"x$#r,@r};
  9.  
  10. use Carp qw(croak);
  11.  
  12. sub new {
  13.   my $type = shift;
  14.   croak "$type is a virtual base class and not meant to be used directly";
  15. }
  16.  
  17. 1;
  18.  
  19. __END__
  20.  
  21. =head1 NAME
  22.  
  23. POE::Loop - documentation for POE's event loop bridge interface
  24.  
  25. =head1 SYNOPSIS
  26.  
  27.   $kernel->loop_initialize();
  28.   $kernel->loop_finalize();
  29.   $kernel->loop_do_timeslice();
  30.   $kernel->loop_run();
  31.   $kernel->loop_halt();
  32.  
  33.   $kernel->loop_watch_signal($signal_name);
  34.   $kernel->loop_ignore_signal($signal_name);
  35.   $kernel->loop_attach_uidestroy($gui_window);
  36.  
  37.   $kernel->loop_resume_time_watcher($next_time);
  38.   $kernel->loop_reset_time_watcher($next_time);
  39.   $kernel->loop_pause_time_watcher();
  40.  
  41.   $kernel->loop_watch_filehandle($handle, $mode);
  42.   $kernel->loop_ignore_filehandle($handle, $mode);
  43.   $kernel->loop_pause_filehandle($handle, $mode);
  44.   $kernel->loop_resume_filehandle($handle, $mode);
  45.  
  46. =head1 DESCRIPTION
  47.  
  48. POE's runtime kernel abstraction uses the "bridge" pattern to
  49. encapsulate services provided by different event loops.  This
  50. abstraction allows POE to cooperate with several event loops and
  51. support new ones with a minimum amount of work.
  52.  
  53. POE relies on a relatively small number of event loop services: signal
  54. callbacks, time or alarm callbacks, and filehandle activity callbacks.
  55.  
  56. The rest of the bridge interface is administrative trivia such as
  57. initializing, executing, and finalizing event loop.
  58.  
  59. =head1 GENERAL NOTES
  60.  
  61. An event loop bridge is not a proper object in itself.  Rather, it is
  62. a suite of functions that are defined within the POE::Kernel
  63. namespace.  A bridge is a plugged-in part of POE::Kernel itself.  Its
  64. functions are proper POE::Kernel methods.
  65.  
  66. Each bridge first defines its own namespace and version within it.
  67. This way CPAN and other things can track its version.
  68.  
  69.   # $Id: Loop.pm,v 1.4 2004/04/17 17:20:36 sungo Exp $
  70.  
  71.   use strict;
  72.  
  73.   # YourToolkit bridge for POE::Kernel;
  74.  
  75.   package POE::Loop::YourToolkit;
  76.  
  77.   use vars qw($VERSION);
  78.   $VERSION = do {my@r=(q$Revision: 1.4 $=~/\d+/g);sprintf"%d."."%04d"x$#r,@r};
  79.  
  80.   package POE::Kernel;
  81.  
  82.   ... private lexical data and functions defined here ...
  83.  
  84.   1;
  85.  
  86.   __END__
  87.  
  88.   =head1 NAME
  89.  
  90.   ... documentation goes here ...
  91.  
  92.   =cut
  93.  
  94. The public interface for loop bridges is broken into four parts:
  95. administrative functions, signal functions, time functions, and
  96. filehandle functions.  They will be described in detail shortly.
  97.  
  98. Bridges use lexical variables to keep track of things.  The types and
  99. number of variables depends on the needs of each event loop.  For
  100. example, POE::Loop::Select keeps bit vectors for its select() call.
  101. POE::Loop::Gtk tracks a single time watcher and multiple file watchers
  102. for each file descriptor.
  103.  
  104. Bridges often employ private functions as callbacks from their event
  105. loops.  The Event, Gtk, and Tk bridges do this.
  106.  
  107. Developers should look at existing bridges to get a feel for things.
  108. The C<-m> flag for perldoc will show a module in its entirety.
  109.  
  110.   perldoc -m POE::Loop::Select
  111.   perldoc -m POE::Loop::Gtk
  112.   ...
  113.  
  114. =head1 ADMINISTRATIVE FUNCTIONS
  115.  
  116. These functions initialize and finalize an event loop, run the loop to
  117. process events, and halt it.
  118.  
  119. =over 2
  120.  
  121. =item loop_initialize
  122.  
  123. Initialize the event loop.  Graphical toolkits especially need some
  124. sort of init() call or sequence to set up.  For example,
  125. POE::Loop::Gtk implements loop_initialize() like this.
  126.  
  127.   sub loop_initialize {
  128.     Gtk->init;
  129.   }
  130.  
  131. POE::Loop::Select does a little more work.
  132.  
  133.   sub loop_initialize {
  134.     @loop_vectors = ( '', '', '' );
  135.     vec($loop_vectors[MODE_RD], 0, 1) = 0;
  136.     vec($loop_vectors[MODE_WR], 0, 1) = 0;
  137.     vec($loop_vectors[MODE_EX], 0, 1) = 0;
  138.   }
  139.  
  140. =item loop_finalize
  141.  
  142. Finalize the event loop.  Most event loops do not require anything
  143. here since they have already stopped by the time loop_finalize() is
  144. called.  However, this is a good place to check that a bridge has not
  145. leaked memory or data.  This example comes from POE::Loop::Event.
  146.  
  147.   sub loop_finalize {
  148.     foreach my $fd (0..$#fileno_watcher) {
  149.       next unless defined $fileno_watcher[$fd];
  150.       foreach my $mode (MODE_RD, MODE_WR, MODE_EX) {
  151.         warn "Fileno $fd / mode $mode has a watcher at loop_finalize"
  152.           if defined $fileno_watcher[$fd]->[$mode];
  153.       }
  154.     }
  155.   }
  156.  
  157. =item loop_do_timeslice
  158.  
  159. Wait for time to pass or new events to occur, and dispatch events
  160. which are due.  If the underlying event loop does these things, then
  161. loop_do_timeslice() either provide- minimal glue for them or does
  162. nothing.
  163.  
  164. For example, the loop_do_timeslice() function for the Select bridge
  165. sets up and calls select().  If any files or other resources become
  166. active, it enqueues events for them.  Finally, it triggers dispatch
  167. for any events are due.
  168.  
  169. On the other hand, the Gtk event loop handles all this, so
  170. loop_do_timeslice() is empty for the Gtk bridge.
  171.  
  172. A sample loop_do_timeslice() is not presented here because it would
  173. either be quite large or empty.  See the bridges for Poll and Select
  174. for large ones.  The Event, Gtk, and Tk bridges are good examples of
  175. empty ones.
  176.  
  177. =item loop_run
  178.  
  179. Run an event loop until POE has no more sessions to handle events.
  180. This function tends to be quite small.  For example, the Poll bridge
  181. uses:
  182.  
  183.   sub loop_run {
  184.     my $self = shift;
  185.     while ($self->_data_ses_count()) {
  186.       $self->loop_do_timeslice();
  187.     }
  188.   }
  189.  
  190. This function is even more trivial when an event loop handles it.
  191. This is from the Gtk bridge:
  192.  
  193.   sub loop_run {
  194.     Gtk->main;
  195.   }
  196.  
  197. =item loop_halt
  198.  
  199. Halt an event loop, especially one which does not know about POE.
  200. This tends to be an empty function for loops written in the bridges
  201. themselves (Poll, Select) and a trivial function for ones that have
  202. their own main loops.
  203.  
  204. For example, the loop_run() function in the Poll bridge exits when
  205. sessions have run out, so its loop_halt() function is empty:
  206.  
  207.   sub loop_halt {
  208.     # does nothing
  209.   }
  210.  
  211. Gtk, however, needs to be stopped because it does not know when POE is
  212. done.
  213.  
  214.   sub loop_halt {
  215.     Gtk->main_quit();
  216.   }
  217.  
  218. =back
  219.  
  220. =head1 SIGNAL FUNCTIONS
  221.  
  222. These functions enable and disable signal watchers.
  223.  
  224. =over 2
  225.  
  226. =item loop_watch_signal SIGNAL_NAME
  227.  
  228. Watch for a given SIGNAL_NAME, most likely by registering a signal
  229. handler.  Signal names are the ones included in %SIG.  That is, they
  230. are the UNIX signal names with the leading "SIG" removed.
  231.  
  232. Most event loops do not have native signal watchers, so it is up to
  233. their bridges to register %SIG handlers.  Some bridges, such as
  234. POE::Loop::Event, register callbacks for various signals.
  235.  
  236. There are three types of signal handlers:
  237.  
  238. CHLD/CLD handlers, when managed by the bridges themselves, poll for
  239. exited children.  POE::Kernel does most of this, but
  240. loop_watch_signal() still needs to start the process.
  241.  
  242. PIPE handlers.  The PIPE signal event must be sent to the session that
  243. is active when the signal occurred.
  244.  
  245. Everything else.  Signal events for everything else are sent to
  246. POE::Kernel, where they are distributed to every session.
  247.  
  248. The loop_watch_signal() function tends to be very long, so an example
  249. is not presented here.  The Event and Select bridges have good
  250. examples, though.
  251.  
  252. =item loop_ignore_signal SIGNAL_NAME
  253.  
  254. Stop watching SIGNAL_NAME.  This usually resets the %SIG entry for
  255. SIGNAL_NAME to DEFAULT.  In the Event bridge, however, it stops and
  256. removes a watcher for the signal.
  257.  
  258. The Select bridge:
  259.  
  260.   sub loop_ignore_signal {
  261.     my ($self, $signal) = @_;
  262.     $SIG{$signal} = "DEFAULT";
  263.   }
  264.  
  265. The Event bridge:
  266.  
  267.   sub loop_ignore_signal {
  268.     my ($self, $signal) = @_;
  269.     if (defined $signal_watcher{$signal}) {
  270.       $signal_watcher{$signal}->stop();
  271.       delete $signal_watcher{$signal};
  272.     }
  273.   }
  274.  
  275. =item loop_attach_uidestroy WINDOW
  276.  
  277. Send a UIDESTROY signal when WINDOW is closed.  The UIDESTROY signal
  278. is used to shut down a POE program when its user interface is
  279. destroyed.
  280.  
  281. This function is only meaningful in bridges that interface with
  282. graphical toolkits.  All other bridges leave loop_attach_uidestroy()
  283. empty.  See POE::Loop::Gtk and POE::Loop::Tk for examples.
  284.  
  285. =back
  286.  
  287. =head1 ALARM OR TIME FUNCTIONS
  288.  
  289. These functions enable and disable a time watcher or alarm in the
  290. substrate.  POE only requires one, which is reused or re-created as
  291. necessary.
  292.  
  293. Most event loops trigger callbacks when time has passed.  Bridges for
  294. this kind of loop will need to register and unregister a callback as
  295. necessary.  The callback, in turn, will dispatch due events and do
  296. some other maintenance.
  297.  
  298. The bridge time functions accept NEXT_EVENT_TIME in the form of a UNIX
  299. epoch time.  Event times may contain fractional seconds.  Time
  300. functions may be required to translate times from the UNIX epoch into
  301. whatever representation an underlying event loop requires.
  302.  
  303. =over 2
  304.  
  305. =item loop_resume_time_watcher NEXT_EVENT_TIME
  306.  
  307. Resume an already active time watcher.  Used with
  308. loop_pause_time_watcher() to provide lightweight timer toggling.
  309. NEXT_EVENT_TIME is the UNIX epoch time of the next event in the queue.
  310. This function is used by bridges that set time watchers in other event
  311. loop libraries.  For example, Gtk uses this:
  312.  
  313.   sub loop_resume_time_watcher {
  314.     my ($self, $next_time) = @_;
  315.     $next_time -= time();
  316.     $next_time *= 1000;
  317.     $next_time = 0 if $next_time < 0;
  318.     $_watcher_timer = Gtk->timeout_add( $next_time,
  319.                                         \&_loop_event_callback
  320.                                       );
  321.   }
  322.  
  323. It is often empty in bridges that implement their own event loops.
  324.  
  325. =item loop_reset_time_watcher NEXT_EVENT_TIME
  326.  
  327. Reset a time watcher, often by stoping or destroying an existing one
  328. and creating a new one in its place.  This function has the same
  329. semantics as (and is often implemented in terms of)
  330. loop_resume_time_watcher().  It is usually more expensive than that
  331. function, however.  Again, from Gtk:
  332.  
  333.   sub loop_reset_time_watcher {
  334.     my ($self, $next_time) = @_;
  335.     Gtk->timeout_remove($_watcher_timer);
  336.     undef $_watcher_timer;
  337.     $self->loop_resume_time_watcher($next_time);
  338.   }
  339.  
  340. =item loop_pause_time_watcher
  341.  
  342. Pause a time watcher.  This should be done without destroying the
  343. timer, if the underlying event loop supports that.
  344.  
  345. POE::Loop::Event supports pausing a timer:
  346.  
  347.   sub loop_pause_time_watcher {
  348.     $_watcher_timer->stop();
  349.   }
  350.  
  351. =back
  352.  
  353. =head1 FILE ACTIVITY FUNCTIONS
  354.  
  355. These functions enable and disable file activity watchers.  The pause
  356. and resume functions are lightweight versions of ignore and watch.
  357. They are used to quickly toggle the state of a file activity watcher
  358. without incurring the overhead of destroying and creating them
  359. entirely.
  360.  
  361. All the functions take the same two parameters: a file HANDLE and a
  362. file access MODE.
  363.  
  364. Modes may be MODE_RD, MODE_WR, or MODE_EX.  These constants are
  365. defined by POE::Kernel and correspond to read, write, or exceptions.
  366.  
  367. POE calls MODE_EX "expedited" because it often signals that a file is
  368. ready for out-of-band information.  Not all event loops handle
  369. MODE_EX.  For example, Tk:
  370.  
  371.   sub loop_watch_filehandle {
  372.     my ($self, $handle, $mode) = @_;
  373.     my $fileno = fileno($handle);
  374.  
  375.     # The Tk documentation implies by omission that expedited
  376.     # filehandles aren't, uh, handled.  This is part 1 of 2.
  377.     confess "Tk does not support expedited filehandles"
  378.       if $mode == MODE_EX;
  379.     ...
  380.   }
  381.  
  382. =over 2
  383.  
  384. =item loop_watch_filehandle HANDLE, MODE
  385.  
  386. Watch a file HANDLE for activity in a given MODE.  Registers the
  387. HANDLE (or, more often its file descriptor via fileno()) in the given
  388. MODE with the underlying event loop.
  389.  
  390. POE::Loop::Select sets a vec() bit so the next select() call will know
  391. about the handle.  It also tracks which file descriptors it has
  392. active.
  393.  
  394.   sub loop_watch_filehandle {
  395.     my ($self, $handle, $mode) = @_;
  396.     my $fileno = fileno($handle);
  397.     vec($loop_vectors[$mode], $fileno, 1) = 1;
  398.     $loop_filenos{$fileno} |= (1<<$mode);
  399.   }
  400.  
  401. =item loop_ignore_filehandle HANDLE, MODE
  402.  
  403. Stop watching a file HANDLE in a given MODE.  Stops (and possibly
  404. destroys) an event watcher corresponding to the HANDLE and MODE.
  405.  
  406. POE::Loop::Poll manages the descriptor/mode bits out of its
  407. loop_ignore_filehandle() function.  It also performs some cleanup if a
  408. descriptors has been totally ignored.
  409.  
  410.   sub loop_ignore_filehandle {
  411.     my ($self, $handle, $mode) = @_;
  412.     my $fileno = fileno($handle);
  413.  
  414.     my $type = mode_to_poll($mode);
  415.     my $current = $poll_fd_masks{$fileno} || 0;
  416.     my $new = $current & ~$type;
  417.  
  418.     if ($new) {
  419.       $poll_fd_masks{$fileno} = $new;
  420.     }
  421.     else {
  422.       delete $poll_fd_masks{$fileno};
  423.     }
  424.   }
  425.  
  426. =item loop_pause_filehandle HANDLE, MODE
  427.  
  428. This is a lightweight form of loop_ignore_filehandle().  It is used
  429. along with loop_resume_filehandle() to temporarily toggle a watcher's
  430. state for a file HANDLE in a particular mode.
  431.  
  432. Some event loops, such as Event.pm, support their file watchers being
  433. disabled and re-enabled without the need to destroy and re-create
  434. entire objects.
  435.  
  436.   sub loop_pause_filehandle {
  437.     my ($self, $handle, $mode) = @_;
  438.     my $fileno = fileno($handle);
  439.     $fileno_watcher[$fileno]->[$mode]->stop();
  440.   }
  441.  
  442. By comparison, the loop_ignore_filehandle() function for Event.pm
  443. involves canceling and destroying a watcher object.  This can be quite
  444. expensive.
  445.  
  446.   sub loop_ignore_filehandle {
  447.     my ($self, $handle, $mode) = @_;
  448.     my $fileno = fileno($handle);
  449.  
  450.     # Don't bother removing a select if none was registered.
  451.     if (defined $fileno_watcher[$fileno]->[$mode]) {
  452.       $fileno_watcher[$fileno]->[$mode]->cancel();
  453.       undef $fileno_watcher[$fileno]->[$mode];
  454.     }
  455.   }
  456.  
  457. =item loop_resume_filehandle HANDLE, MODE
  458.  
  459. This is a lightweight form of loop_watch_filehandle().  It is used
  460. along with loop_pause_filehandle() to temporarily toggle a a watcher's
  461. state for a file HANDLE in a particular mode.
  462.  
  463. =back
  464.  
  465. =head1 SEE ALSO
  466.  
  467. L<POE>, L<POE::Loop::Event>, L<POE::Loop::Gtk>, L<POE::Loop::Poll>,
  468. L<POE::Loop::Select>, L<POE::Loop::Tk>.
  469.  
  470. =head1 BUGS
  471.  
  472. Signal handlers are often repeated between bridges:
  473. http://rt.cpan.org/NoAuth/Bug.html?id=1632
  474.  
  475. =head1 AUTHORS & LICENSING
  476.  
  477. Please see L<POE> for more information about authors, contributors,
  478. and POE's licensing.
  479.  
  480. =cut
  481.