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.14 by root, Tue Nov 16 19:43: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
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.1'; 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}
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.
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