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.15 by root, Fri May 20 22:57:52 2011 UTC

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
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
62Porttracker (L<http://www.porttracker.com/>) is a product that (among 63Porttracker (L<http://www.porttracker.com/>) is a product that (among
63other things) scans switches and routers in a network and gives a coherent 64other 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 65view of which end devices are connected to which switch ports on which
93 94
94package AnyEvent::Porttracker; 95package AnyEvent::Porttracker;
95 96
96use common::sense; 97use common::sense;
97 98
99use Carp ();
98use Scalar::Util (); 100use Scalar::Util ();
99 101
100use AnyEvent (); 102use AnyEvent ();
101use AnyEvent::Handle (); 103use AnyEvent::Handle ();
102 104
103use MIME::Base64 (); 105use MIME::Base64 ();
104use Digest::HMAC_MD6 (); 106use Digest::HMAC_MD6 ();
105use JSON (); 107use JSON ();
106 108
107our $VERSION = '0.1'; 109our $VERSION = '1.0';
108 110
109sub call { 111sub call {
110 my ($self, $type, @args) = @_; 112 my ($self, $type, @args) = @_;
111 113
112 $self->{$type} 114 $self->{$type}
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
319Similar to C<< ->req >>, but waits for the results of the request and on
320success, returns the values instead (without the success flag, and only
321the first value in scalar context). On failure, the method will C<croak>
322with the error message.
323
324=cut
325
326sub 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
317Just like C<< ->req >>, with two differences: first, a failure will not 339Just like C<< ->req >>, with two differences: first, a failure will not
318raise an error, second, the initial status reply which indicates success 340raise an error, second, the initial status reply which indicates success
319or failure is not removed before calling the callback. 341or failure is not removed before calling the callback.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines