/usr/lib/x86_64-linux-gnu/perl5/5.26/Net/DBus/Reactor.pm is in libnet-dbus-perl 1.1.0-4build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 | # -*- perl -*-
#
# Copyright (C) 2004-2011 Daniel P. Berrange
#
# This program is free software; You can redistribute it and/or modify
# it under the same terms as Perl itself. Either:
#
# a) the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any
# later version,
#
# or
#
# b) the "Artistic License"
#
# The file "COPYING" distributed along with this file provides full
# details of the terms and conditions of the two licenses.
=pod
=head1 NAME
Net::DBus::Reactor - application event loop
=head1 SYNOPSIS
Create and run an event loop:
use Net::DBus::Reactor;
my $reactor = Net::DBus::Reactor->main();
$reactor->run();
Manage some file handlers
$reactor->add_read($fd,
Net::DBus::Callback->new(method => sub {
my $fd = shift;
...read some data...
}, args => [$fd]));
$reactor->add_write($fd,
Net::DBus::Callback->new(method => sub {
my $fd = shift;
...write some data...
}, args => [$fd]));
Temporarily (dis|en)able a handle
# Disable
$reactor->toggle_read($fd, 0);
# Enable
$reactor->toggle_read($fd, 1);
Permanently remove a handle
$reactor->remove_read($fd);
Manage a regular timeout every 100 milliseconds
my $timer = $reactor->add_timeout(100,
Net::DBus::Callback->new(
method => sub {
...process the alarm...
}));
Temporarily (dis|en)able a timer
# Disable
$reactor->toggle_timeout($timer, 0);
# Enable
$reactor->toggle_timeout($timer, 1);
Permanently remove a timer
$reactor->remove_timeout($timer);
Add a post-dispatch hook
my $hook = $reactor->add_hook(Net::DBus::Callback->new(
method => sub {
... do some work...
}));
Remove a hook
$reactor->remove_hook($hook);
=head1 DESCRIPTION
This class provides a general purpose event loop for
the purposes of multiplexing I/O events and timeouts
in a single process. The underlying implementation is
done using the select system call. File handles can
be registered for monitoring on read, write and exception
(out-of-band data) events. Timers can be registered
to expire with a periodic frequency. These are implemented
using the timeout parameter of the select system call.
Since this parameter merely represents an upper bound
on the amount of time the select system call is allowed
to sleep, the actual period of the timers may vary. Under
normal load this variance is typically 10 milliseconds.
Finally, hooks may be registered which will be invoked on
each iteration of the event loop (ie after processing
the file events, or timeouts indicated by the select
system call returning).
=head1 METHODS
=over 4
=cut
package Net::DBus::Reactor;
use 5.006;
use strict;
use warnings;
use Net::DBus::Binding::Watch;
use Net::DBus::Callback;
use Time::HiRes qw(gettimeofday);
=item my $reactor = Net::DBus::Reactor->new();
Creates a new event loop ready for monitoring file handles, or
generating timeouts. Except in very unusual circumstances (examples
of which I can't think up) it is not necessary or desriable to
explicitly create new reactor instances. Instead call the L<main>
method to get a handle to the singleton instance.
=cut
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my %params = @_;
my $self = {};
$self->{fds} = {
read => {},
write => {},
exception => {}
};
$self->{timeouts} = [];
$self->{hooks} = [];
bless $self, $class;
return $self;
}
use vars qw($main_reactor);
=item $reactor = Net::DBus::Reactor->main;
Return a handle to the singleton instance of the reactor. This
is the recommended way of getting hold of a reactor, since it
removes the need for modules to pass around handles to their
privately created reactors.
=cut
sub main {
my $class = shift;
$main_reactor = $class->new() unless defined $main_reactor;
return $main_reactor;
}
=item $reactor->manage($connection);
=item $reactor->manage($server);
Registers a C<Net::DBus::Binding::Connection> or C<Net::DBus::Binding::Server> object
for management by the event loop. This basically involves
hooking up the watch & timeout callbacks to the event loop.
For connections it will also register a hook to invoke the
C<dispatch> method periodically.
=cut
sub manage {
my $self = shift;
my $object = shift;
if ($object->can("set_watch_callbacks")) {
$object->set_watch_callbacks(sub {
my $object = shift;
my $watch = shift;
$self->_manage_watch_on($object, $watch);
}, sub {
my $object = shift;
my $watch = shift;
$self->_manage_watch_off($object, $watch);
}, sub {
my $object = shift;
my $watch = shift;
$self->_manage_watch_toggle($object, $watch);
});
}
if ($object->can("set_timeout_callbacks")) {
$object->set_timeout_callbacks(sub {
my $object = shift;
my $timeout = shift;
my $key = $self->add_timeout($timeout->get_interval,
Net::DBus::Callback->new(object => $timeout,
method => "handle",
args => []),
$timeout->is_enabled);
$timeout->set_data($key);
}, sub {
my $object = shift;
my $timeout = shift;
my $key = $timeout->get_data;
$self->remove_timeout($key);
}, sub {
my $object = shift;
my $timeout = shift;
my $key = $timeout->get_data;
$self->toggle_timeout($key,
$timeout->is_enabled,
$timeout->get_interval);
});
}
if ($object->can("dispatch")) {
$self->add_hook(Net::DBus::Callback->new(object => $object,
method => "dispatch",
args => []),
1);
}
if ($object->can("flush")) {
$self->add_hook(Net::DBus::Callback->new(object => $object,
method => "flush",
args => []),
1);
}
}
sub _manage_watch_on {
my $self = shift;
my $object = shift;
my $watch = shift;
my $flags = $watch->get_flags;
if ($flags & &Net::DBus::Binding::Watch::READABLE) {
$self->add_read($watch->get_fileno,
Net::DBus::Callback->new(object => $watch,
method => "handle",
args => [&Net::DBus::Binding::Watch::READABLE]),
$watch->is_enabled);
}
if ($flags & &Net::DBus::Binding::Watch::WRITABLE) {
$self->add_write($watch->get_fileno,
Net::DBus::Callback->new(object => $watch,
method => "handle",
args => [&Net::DBus::Binding::Watch::WRITABLE]),
$watch->is_enabled);
}
# $self->add_exception($watch->get_fileno, $watch,
# Net::DBus::Callback->new(object => $watch,
# method => "handle",
# args => [&Net::DBus::Binding::Watch::ERROR]),
# $watch->is_enabled);
}
sub _manage_watch_off {
my $self = shift;
my $object = shift;
my $watch = shift;
my $flags = $watch->get_flags;
if ($flags & &Net::DBus::Binding::Watch::READABLE) {
$self->remove_read($watch->get_fileno);
}
if ($flags & &Net::DBus::Binding::Watch::WRITABLE) {
$self->remove_write($watch->get_fileno);
}
# $self->remove_exception($watch->get_fileno);
}
sub _manage_watch_toggle {
my $self = shift;
my $object = shift;
my $watch = shift;
my $flags = $watch->get_flags;
if ($flags & &Net::DBus::Binding::Watch::READABLE) {
$self->toggle_read($watch->get_fileno, $watch->is_enabled);
}
if ($flags & &Net::DBus::Binding::Watch::WRITABLE) {
$self->toggle_write($watch->get_fileno, $watch->is_enabled);
}
$self->toggle_exception($watch->get_fileno, $watch->is_enabled);
}
=item $reactor->run();
Starts the event loop monitoring any registered
file handles and timeouts. At least one file
handle, or timer must have been registered prior
to running the reactor, otherwise it will immediately
exit. The reactor will run until all registered
file handles, or timeouts have been removed, or
disabled. The reactor can be explicitly stopped by
calling the C<shutdown> method.
=cut
sub run {
my $self = shift;
$self->{running} = 1;
while ($self->{running}) { $self->step };
}
=item $reactor->shutdown();
Explicitly shutdown the reactor after pending
events have been processed.
=cut
sub shutdown {
my $self = shift;
$self->{running} = 0;
}
=item $reactor->step();
Perform one iteration of the event loop, going to
sleep until an event occurs on a registered file
handle, or a timeout occurrs. This method is generally
not required in day-to-day use.
=cut
sub step {
my $self = shift;
my @callbacks = $self->_dispatch_hook();
foreach my $callback (@callbacks) {
$callback->invoke;
}
my ($ri, $ric) = $self->_bits("read");
my ($wi, $wic) = $self->_bits("write");
my ($ei, $eic) = $self->_bits("exception");
my $timeout = $self->_timeout($self->_now);
if (!$ric && !$wic && !$eic && !(defined $timeout)) {
$self->{running} = 0;
}
# One of the hooks we ran might have requested shutdown
# so check here to avoid a undesirable wait in select()
# cf RT #39068
return unless $self->{running};
my ($ro, $wo, $eo);
my $n = select($ro=$ri,$wo=$wi,$eo=$ei, (defined $timeout ? ($timeout ? $timeout/1000 : 0) : undef));
@callbacks = ();
if ($n > 0) {
push @callbacks, $self->_dispatch_fd("read", $ro);
push @callbacks, $self->_dispatch_fd("write", $wo);
push @callbacks, $self->_dispatch_fd("error", $eo);
}
push @callbacks, $self->_dispatch_timeout($self->_now);
#push @callbacks, $self->_dispatch_hook();
foreach my $callback (@callbacks) {
$callback->invoke;
}
return 1;
}
sub _now {
my $self = shift;
my @now = gettimeofday;
return $now[0] * 1000 + (($now[1] - ($now[1] % 1000)) / 1000);
}
sub _bits {
my $self = shift;
my $type = shift;
my $vec = '';
my $count = 0;
foreach (keys %{$self->{fds}->{$type}}) {
next unless $self->{fds}->{$type}->{$_}->{enabled};
$count++;
vec($vec, $_, 1) = 1;
}
return ($vec, $count);
}
sub _timeout {
my $self = shift;
my $now = shift;
my $timeout;
foreach (@{$self->{timeouts}}) {
next unless $_->{enabled};
my $expired = $now - $_->{last_fired};
my $interval = ($expired > $_->{interval} ? 0 : $_->{interval} - $expired);
$timeout = $interval if !(defined $timeout) ||
($interval < $timeout);
}
return $timeout;
}
sub _dispatch_fd {
my $self = shift;
my $type = shift;
my $vec = shift;
my @callbacks;
foreach my $fd (keys %{$self->{fds}->{$type}}) {
next unless $self->{fds}->{$type}->{$fd}->{enabled};
if (vec($vec, $fd, 1)) {
my $rec = $self->{fds}->{$type}->{$fd};
push @callbacks, $self->{fds}->{$type}->{$fd}->{callback};
}
}
return @callbacks;
}
sub _dispatch_timeout {
my $self = shift;
my $now = shift;
my @callbacks;
foreach my $timeout (@{$self->{timeouts}}) {
next unless $timeout->{enabled};
my $expired = $now - $timeout->{last_fired};
# Select typically returns a little (0-10 ms) before we
# asked it for. (8 milliseconds seems reasonable balance
# between early timeouts & extra select calls
if ($expired >= ($timeout->{interval}-8)) {
$timeout->{last_fired} = $now;
push @callbacks, $timeout->{callback};
}
}
return @callbacks;
}
sub _dispatch_hook {
my $self = shift;
my $now = shift;
my @callbacks;
foreach my $hook (@{$self->{hooks}}) {
next unless $hook->{enabled};
push @callbacks, $hook->{callback};
}
return @callbacks;
}
=item $reactor->add_read($fd, $callback[, $status]);
Registers a file handle for monitoring of read
events. The C<$callback> parameter specifies either
a code reference to a subroutine, or an instance of
the C<Net::DBus::Callback> object to invoke each time
an event occurs. The optional C<$status> parameter is
a boolean value to specify whether the watch is
initially enabled.
=cut
sub add_read {
my $self = shift;
$self->_add("read", @_);
}
=item $reactor->add_write($fd, $callback[, $status]);
Registers a file handle for monitoring of write
events. The C<$callback> parameter specifies either
a code reference to a subroutine, or an
instance of the C<Net::DBus::Callback> object to invoke
each time an event occurs. The optional C<$status>
parameter is a boolean value to specify whether the
watch is initially enabled.
=cut
sub add_write {
my $self = shift;
$self->_add("write", @_);
}
=item $reactor->add_exception($fd, $callback[, $status]);
Registers a file handle for monitoring of exception
events. The C<$callback> parameter specifies either
a code reference to a subroutine, or an
instance of the C<Net::DBus::Callback> object to invoke
each time an event occurs. The optional C<$status>
parameter is a boolean value to specify whether the
watch is initially enabled.
=cut
sub add_exception {
my $self = shift;
$self->_add("exception", @_);
}
=item my $id = $reactor->add_timeout($interval, $callback, $status);
Registers a new timeout to expire every C<$interval>
milliseconds. The C<$callback> parameter specifies either
a code reference to a subroutine, or an
instance of the C<Net::DBus::Callback> object to invoke
each time the timeout expires. The optional C<$status>
parameter is a boolean value to specify whether the
timeout is initially enabled. The return parameter is
a unique identifier which can be used to later remove
or disable the timeout.
=cut
sub add_timeout {
my $self = shift;
my $interval = shift;
my $callback = shift;
my $enabled = shift;
$enabled = 1 unless defined $enabled;
if (ref($callback) eq "CODE") {
$callback = Net::DBus::Callback->new(method => $callback);
}
my $key;
for (my $i = 0 ; $i <= $#{$self->{timeouts}} && !(defined $key); $i++) {
$key = $i unless defined $self->{timeouts}->[$i];
}
$key = $#{$self->{timeouts}}+1 unless defined $key;
$self->{timeouts}->[$key] = {
interval => $interval,
last_fired => $self->_now,
callback => $callback,
enabled => $enabled
};
return $key;
}
=item $reactor->remove_timeout($id);
Removes a previously registered timeout specified by
the C<$id> parameter.
=cut
sub remove_timeout {
my $self = shift;
my $key = shift;
die "no timeout active with key '$key'"
unless defined $self->{timeouts}->[$key];
$self->{timeouts}->[$key] = undef;
}
=item $reactor->toggle_timeout($id, $status[, $interval]);
Updates the state of a previously registered timeout
specified by the C<$id> parameter. The C<$status>
parameter specifies whether the timeout is to be enabled
or disabled, while the optional C<$interval> parameter
can be used to change the period of the timeout.
=cut
sub toggle_timeout {
my $self = shift;
my $key = shift;
my $enabled = shift;
$self->{timeouts}->[$key]->{enabled} = $enabled;
$self->{timeouts}->[$key]->{interval} = shift if @_;
}
=item my $id = $reactor->add_hook($callback[, $status]);
Registers a new hook to be fired on each iteration
of the event loop. The C<$callback> parameter
specifies either a code reference to a subroutine, or
an instance of the C<Net::DBus::Callback>
class to invoke. The C<$status> parameter determines
whether the hook is initially enabled, or disabled.
The return parameter is a unique id which should
be used to later remove, or disable the hook.
=cut
sub add_hook {
my $self = shift;
my $callback = shift;
my $enabled = shift;
$enabled = 1 unless defined $enabled;
if (ref($callback) eq "CODE") {
$callback = Net::DBus::Callback->new(method => $callback);
}
my $key;
for (my $i = 0 ; $i <= $#{$self->{hooks}} && !(defined $key); $i++) {
$key = $i unless defined $self->{hooks}->[$i];
}
$key = $#{$self->{hooks}}+1 unless defined $key;
$self->{hooks}->[$key] = {
callback => $callback,
enabled => $enabled
};
return $key;
}
=item $reactor->remove_hook($id)
Removes the previously registered hook identified
by C<$id>.
=cut
sub remove_hook {
my $self = shift;
my $key = shift;
die "no hook present with key '$key'"
unless defined $self->{hooks}->[$key];
$self->{hooks}->[$key] = undef;
}
=item $reactor->toggle_hook($id, $status)
Updates the status of the previously registered
hook identified by C<$id>. The C<$status> parameter
determines whether the hook is to be enabled or
disabled.
=cut
sub toggle_hook {
my $self = shift;
my $key = shift;
my $enabled = shift;
$self->{hooks}->[$key]->{enabled} = $enabled;
}
sub _add {
my $self = shift;
my $type = shift;
my $fd = shift;
my $callback = shift;
my $enabled = shift;
$enabled = 1 unless defined $enabled;
if (ref($callback) eq "CODE") {
$callback = Net::DBus::Callback->new(method => $callback);
}
$self->{fds}->{$type}->{$fd} = {
callback => $callback,
enabled => $enabled
};
}
=item $reactor->remove_read($fd);
=item $reactor->remove_write($fd);
=item $reactor->remove_exception($fd);
Removes a watch on the file handle C<$fd>.
=cut
sub remove_read {
my $self = shift;
$self->_remove("read", @_);
}
sub remove_write {
my $self = shift;
$self->_remove("write", @_);
}
sub remove_exception {
my $self = shift;
$self->_remove("exception", @_);
}
sub _remove {
my $self = shift;
my $type = shift;
my $fd = shift;
die "no handle ($type) active with fd '$fd'"
unless exists $self->{fds}->{$type}->{$fd};
delete $self->{fds}->{$type}->{$fd};
}
=item $reactor->toggle_read($fd, $status);
=item $reactor->toggle_write($fd, $status);
=item $reactor->toggle_exception($fd, $status);
Updates the status of a watch on the file handle C<$fd>.
The C<$status> parameter species whether the watch is
to be enabled or disabled.
=cut
sub toggle_read {
my $self = shift;
$self->_toggle("read", @_);
}
sub toggle_write {
my $self = shift;
$self->_toggle("write", @_);
}
sub toggle_exception {
my $self = shift;
$self->_toggle("exception", @_);
}
sub _toggle {
my $self = shift;
my $type = shift;
my $fd = shift;
my $enabled = shift;
$self->{fds}->{$type}->{$fd}->{enabled} = $enabled;
}
1;
=pod
=back
=head1 SEE ALSO
L<Net::DBus::Callback>, L<Net::DBus::Connection>, L<Net::DBus::Server>
=head1 AUTHOR
Daniel Berrange E<lt>dan@berrange.comE<gt>
=head1 COPYRIGHT
Copyright 2004-2011 by Daniel Berrange
=cut
|