ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/AnyEvent-Porttracker/Porttracker.pm
(Generate patch)

Comparing cvsroot/AnyEvent-Porttracker/Porttracker.pm (file contents):
Revision 1.9 by root, Tue Nov 16 01:54:15 2010 UTC vs.
Revision 1.17 by root, Mon Mar 11 08:43:53 2013 UTC

9 my $api = new AnyEvent::Porttracker 9 my $api = new AnyEvent::Porttracker
10 host => "10.0.0.1", 10 host => "10.0.0.1",
11 user => "admin", 11 user => "admin",
12 pass => "31331", 12 pass => "31331",
13 tls => 1, 13 tls => 1,
14 on_error => sub {
15 die $_[1];
16 },
14 ; 17 ;
15 18
16 # Example 1 19 # Example 1
17 # a simple request: ping the server 20 # a simple request: ping the server synchronously
18 21
19 $api->req ("ping", sub { 22 my ($timestamp, $pid) = $api->req_sync ("ping");
20 my ($api, $ok, $timestamp, $pid) = @_;
21 ...
22 });
23 23
24 # Example 2 24 # Example 2
25 # find all realms, start a discovery on all of them 25 # find all realms, start a discovery on all of them
26 # and wait until all discovery processes have finished 26 # and wait until all discovery processes have finished
27 # but execute individual discoveries in parallel,
28 # asynchronously
27 29
28 my $cv = AE::cv; 30 my $cv = AE::cv;
29 31
30 $cv->begin; 32 $cv->begin;
31 # find all realms 33 # find all realms
35 # start discovery on all realms 37 # start discovery on all realms
36 for my $realm (@realms) { 38 for my $realm (@realms) {
37 my ($gid, $name) = @$realm; 39 my ($gid, $name) = @$realm;
38 40
39 $cv->begin; 41 $cv->begin;
40 $api->req (realm_discover => $realm->[0], sub { 42 $api->req (realm_discover => $gid, sub {
41 warn "discovery for realm '$realm->[1]' finished\n"; 43 warn "discovery for realm '$name' finished\n";
42 $cv->end; 44 $cv->end;
43 }); 45 });
44 } 46 }
45 47
46 $cv->end; 48 $cv->end;
55 $api->on (realm_poll_stop_event => sub { 57 $api->on (realm_poll_stop_event => sub {
56 my ($api, $gid) = @_; 58 my ($api, $gid) = @_;
57 warn "this just in: poll for realm <$gid> finished.\n"; 59 warn "this just in: poll for realm <$gid> finished.\n";
58 }); 60 });
59 61
62 AE::cv->recv; # wait forever
63
60=head1 DESCRIPTION 64=head1 DESCRIPTION
61 65
62Porttracker (L<http://www.porttracker.com/>) is a product that (among 66Porttracker (L<http://www.porttracker.com/>) is a product that (among
63other things) scans switches and routers in a network and gives a coherent 67other things) scans switches and routers in a network and gives a coherent
64view of which end devices are connected to which switch ports on which 68view of which end devices are connected to which switch ports on which
93 97
94package AnyEvent::Porttracker; 98package AnyEvent::Porttracker;
95 99
96use common::sense; 100use common::sense;
97 101
102use Carp ();
98use Scalar::Util (); 103use Scalar::Util ();
99 104
100use AnyEvent (); 105use AnyEvent ();
101use AnyEvent::Handle (); 106use AnyEvent::Handle ();
102 107
103use MIME::Base64 (); 108use MIME::Base64 ();
104use Digest::HMAC_MD6 (); 109use Digest::HMAC_MD6 ();
105use JSON (); 110use JSON ();
106 111
107our $VERSION = '0.0'; 112our $VERSION = '1.01';
108 113
109sub call { 114sub call {
110 my ($self, $type, @args) = @_; 115 my ($self, $type, @args) = @_;
111 116
112 $self->{$type} 117 $self->{$type}
163 168
164The L<AnyEvent::TLS> object to use. See C<tls>, above. 169The L<AnyEvent::TLS> object to use. See C<tls>, above.
165 170
166=item on_XYZ => $coderef 171=item on_XYZ => $coderef
167 172
168You can specify event callbacks either by subclassing and overriding the 173You can specify event callbacks either by sub-classing and overriding the
169respective methods or by specifying coderefs as key-value pairs when 174respective methods or by specifying code-refs as key-value pairs when
170constructing the object. You add or remove event handlers at any time with 175constructing the object. You add or remove event handlers at any time with
171the C<event> method. 176the C<event> method.
172 177
173=back 178=back
174 179
178 my $class = shift; 183 my $class = shift;
179 184
180 my $self = bless { 185 my $self = bless {
181 id => "a", 186 id => "a",
182 ids => [], 187 ids => [],
183 queue => [], # ininitially queue everything 188 queue => [], # initially queue everything
184 @_, 189 @_,
185 }, $class; 190 }, $class;
186 191
187 { 192 {
188 Scalar::Util::weaken (my $self = $self); 193 Scalar::Util::weaken (my $self = $self);
310 $_[0]{queue} 315 $_[0]{queue}
311 ? push @{ $_[0]{queue} }, [@_] 316 ? push @{ $_[0]{queue} }, [@_]
312 : &_req 317 : &_req
313} 318}
314 319
320=item @res = $api->req_sync ($type => @args)
321
322Similar to C<< ->req >>, but waits for the results of the request and on
323success, returns the values instead (without the success flag, and only
324the first value in scalar context). On failure, the method will C<croak>
325with the error message.
326
327=cut
328
329sub req_sync {
330 push @_, my $cv = AE::cv;
331 &req;
332 my ($ok, @res) = $cv->recv;
333
334 $ok
335 or Carp::croak $res[0];
336
337 wantarray ? @res : $res[0]
338}
339
315=item $api->req_failok ($type => @args, $callback->($api, $success, @reply)) 340=item $api->req_failok ($type => @args, $callback->($api, $success, @reply))
316 341
317Just like C<< ->req >>, with two differences: first, a failure will not 342Just like C<< ->req >>, with two differences: first, a failure will not
318raise an error, second, the initial status reply which indicates success 343raise an error, second, the initial status reply which indicates success
319or failure is not removed before calling the callback. 344or failure is not removed before calling the callback.
463 call $self, "on_${event}_event", @args; 488 call $self, "on_${event}_event", @args;
464} 489}
465 490
466=back 491=back
467 492
468=head2 EVENTS 493=head1 EVENTS/CALLBACKS
469 494
470AnyEvent::Porttracker conenctions are fully event-driven, and naturally 495AnyEvent::Porttracker connections are fully event-driven, and naturally
471there are a number of events that can occur. All these events have a name 496there are a number of events that can occur. All these events have a name
472starting with C<on_> (example: C<on_login_failure>). 497starting with C<on_> (example: C<on_login_failure>).
473 498
474Programs can catch these events in two ways: either by providing 499Programs can catch these events in two ways: either by providing
475constructor arguments with the event name as key and a coderef as value: 500constructor arguments with the event name as key and a code-ref as value:
476 501
477 my $api = new AnyEvent::Porttracker 502 my $api = new AnyEvent::Porttracker
478 host => ..., 503 host => ...,
479 user => ..., pass => ..., 504 user => ..., pass => ...,
480 on_error => sub { 505 on_error => sub {
482 warn $msg; 507 warn $msg;
483 exit 1; 508 exit 1;
484 }, 509 },
485 ; 510 ;
486 511
487Or by subclassing C<AnyEvent::Porttracker> and overriding methods of the 512Or by sub-classing C<AnyEvent::Porttracker> and overriding methods of the
488same name: 513same name:
489 514
490 package MyClass; 515 package MyClass;
491 516
492 use base AnyEvent::Porttracker; 517 use base AnyEvent::Porttracker;
540 565
541=item on_start_tls_notify $api 566=item on_start_tls_notify $api
542 567
543Called when the server wants to start TLS negotiation. This is used 568Called when the server wants to start TLS negotiation. This is used
544internally and - while it is possible to override it - should not be 569internally and - while it is possible to override it - should not be
545overriden. 570overridden.
546 571
547=item on_event_notify $api, $eventname, @args 572=item on_event_notify $api, $eventname, @args
548 573
549Called when the server broadcasts an event the API object is subscribed 574Called when the server broadcasts an event the API object is subscribed
550to. The default implementation (which should not be overridden) simply 575to. The default implementation (which should not be overridden) simply
565 590
566L<AnyEvent>, L<http://www.porttracker.com/>, L<http://www.infoblox.com/en/products/portiq.html>. 591L<AnyEvent>, L<http://www.porttracker.com/>, L<http://www.infoblox.com/en/products/portiq.html>.
567 592
568=head1 AUTHOR 593=head1 AUTHOR
569 594
570 Marc Lehmann <marc@porttracker.net> 595 Marc Lehmann <marc@nethype.de>
571 596
572=cut 597=cut
573 598
5741 5991

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines