… | |
… | |
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 | |
62 | Porttracker (L<http://www.porttracker.com/>) is a product that (among |
66 | 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 |
67 | 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 |
68 | view of which end devices are connected to which switch ports on which |
… | |
… | |
93 | |
97 | |
94 | package AnyEvent::Porttracker; |
98 | package AnyEvent::Porttracker; |
95 | |
99 | |
96 | use common::sense; |
100 | use common::sense; |
97 | |
101 | |
|
|
102 | use Carp (); |
98 | use Scalar::Util (); |
103 | use Scalar::Util (); |
99 | |
104 | |
100 | use AnyEvent (); |
105 | use AnyEvent (); |
101 | use AnyEvent::Handle (); |
106 | use AnyEvent::Handle (); |
102 | |
107 | |
103 | use MIME::Base64 (); |
108 | use MIME::Base64 (); |
104 | use Digest::HMAC_MD6 (); |
109 | use Digest::HMAC_MD6 (); |
105 | use JSON (); |
110 | use JSON (); |
106 | |
111 | |
107 | our $VERSION = '0.0'; |
112 | our $VERSION = '1.01'; |
108 | |
113 | |
109 | sub call { |
114 | sub call { |
110 | my ($self, $type, @args) = @_; |
115 | my ($self, $type, @args) = @_; |
111 | |
116 | |
112 | $self->{$type} |
117 | $self->{$type} |
… | |
… | |
163 | |
168 | |
164 | The L<AnyEvent::TLS> object to use. See C<tls>, above. |
169 | The L<AnyEvent::TLS> object to use. See C<tls>, above. |
165 | |
170 | |
166 | =item on_XYZ => $coderef |
171 | =item on_XYZ => $coderef |
167 | |
172 | |
168 | You can specify event callbacks either by subclassing and overriding the |
173 | You can specify event callbacks either by sub-classing and overriding the |
169 | respective methods or by specifying coderefs as key-value pairs when |
174 | 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 |
175 | constructing the object. You add or remove event handlers at any time with |
171 | the C<event> method. |
176 | the 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 | |
|
|
322 | Similar to C<< ->req >>, but waits for the results of the request and on |
|
|
323 | success, returns the values instead (without the success flag, and only |
|
|
324 | the first value in scalar context). On failure, the method will C<croak> |
|
|
325 | with the error message. |
|
|
326 | |
|
|
327 | =cut |
|
|
328 | |
|
|
329 | sub 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 | |
317 | Just like C<< ->req >>, with two differences: first, a failure will not |
342 | Just like C<< ->req >>, with two differences: first, a failure will not |
318 | raise an error, second, the initial status reply which indicates success |
343 | raise an error, second, the initial status reply which indicates success |
319 | or failure is not removed before calling the callback. |
344 | or 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 | |
470 | AnyEvent::Porttracker conenctions are fully event-driven, and naturally |
495 | 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 |
496 | 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>). |
497 | starting with C<on_> (example: C<on_login_failure>). |
473 | |
498 | |
474 | Programs can catch these events in two ways: either by providing |
499 | 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: |
500 | constructor 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 | |
487 | Or by subclassing C<AnyEvent::Porttracker> and overriding methods of the |
512 | Or by sub-classing C<AnyEvent::Porttracker> and overriding methods of the |
488 | same name: |
513 | same 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 | |
543 | Called when the server wants to start TLS negotiation. This is used |
568 | 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 |
569 | internally and - while it is possible to override it - should not be |
545 | overriden. |
570 | overridden. |
546 | |
571 | |
547 | =item on_event_notify $api, $eventname, @args |
572 | =item on_event_notify $api, $eventname, @args |
548 | |
573 | |
549 | Called when the server broadcasts an event the API object is subscribed |
574 | Called when the server broadcasts an event the API object is subscribed |
550 | to. The default implementation (which should not be overridden) simply |
575 | to. The default implementation (which should not be overridden) simply |
… | |
… | |
565 | |
590 | |
566 | L<AnyEvent>, L<http://www.porttracker.com/>, L<http://www.infoblox.com/en/products/portiq.html>. |
591 | L<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 | |
574 | 1 |
599 | 1 |