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