… | |
… | |
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.1'; |
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} |
… | |
… | |
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. |
… | |
… | |
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 |