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.8 by root, Tue Nov 16 01:22:03 2010 UTC vs.
Revision 1.15 by root, Fri May 20 22:57:52 2011 UTC

3AnyEvent::Porttracker - Porttracker/PortIQ API client interface. 3AnyEvent::Porttracker - Porttracker/PortIQ API client interface.
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use AnyEvent::Porttracker; 7 use AnyEvent::Porttracker;
8
9 my $api = new AnyEvent::Porttracker
10 host => "10.0.0.1",
11 user => "admin",
12 pass => "31331",
13 tls => 1,
14 ;
15
16 # Example 1
17 # a simple request: ping the server synchronously
18
19 my ($timestamp, $pid) = $api->req_sync ("ping");
20
21 # Example 2
22 # find all realms, start a discovery on all of them
23 # and wait until all discovery processes have finished
24 # but execute individual discoveries in parallel,
25 # asynchronously
26
27 my $cv = AE::cv;
28
29 $cv->begin;
30 # find all realms
31 $api->req (realm_info => ["gid", "name"], sub {
32 my ($api, @realms) = @_;
33
34 # start discovery on all realms
35 for my $realm (@realms) {
36 my ($gid, $name) = @$realm;
37
38 $cv->begin;
39 $api->req (realm_discover => $gid, sub {
40 warn "discovery for realm '$name' finished\n";
41 $cv->end;
42 });
43 }
44
45 $cv->end;
46 });
47
48 $cv->recv;
49
50 # Example 3
51 # subscribe to realm_poll_stop events and report each occurance
52
53 $api->req (subscribe => "realm_poll_stop", sub {});
54 $api->on (realm_poll_stop_event => sub {
55 my ($api, $gid) = @_;
56 warn "this just in: poll for realm <$gid> finished.\n";
57 });
58
59 AE::cv->recv; # wait forever
8 60
9=head1 DESCRIPTION 61=head1 DESCRIPTION
10 62
11Porttracker (L<http://www.porttracker.com/>) is a product that (among 63Porttracker (L<http://www.porttracker.com/>) is a product that (among
12other 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
42 94
43package AnyEvent::Porttracker; 95package AnyEvent::Porttracker;
44 96
45use common::sense; 97use common::sense;
46 98
99use Carp ();
47use Scalar::Util (); 100use Scalar::Util ();
48 101
49use AnyEvent (); 102use AnyEvent ();
50use AnyEvent::Handle (); 103use AnyEvent::Handle ();
51 104
52use MIME::Base64 (); 105use MIME::Base64 ();
53use Digest::HMAC_MD6 (); 106use Digest::HMAC_MD6 ();
54use JSON (); 107use JSON ();
55 108
56our $VERSION = '0.0'; 109our $VERSION = '1.0';
57 110
58sub call { 111sub call {
59 my ($self, $type, @args) = @_; 112 my ($self, $type, @args) = @_;
60 113
61 $self->{$type} 114 $self->{$type}
108verify => 1, verify_peername => [1, 1, 1] >> or whatever verification mode 161verify => 1, verify_peername => [1, 1, 1] >> or whatever verification mode
109you wish to use. 162you wish to use.
110 163
111=item tls_ctx => $tls_ctx 164=item tls_ctx => $tls_ctx
112 165
113The L<AnyEvent::TLS> object to use. 166The L<AnyEvent::TLS> object to use. See C<tls>, above.
114
115#TODO#
116 167
117=item on_XYZ => $coderef 168=item on_XYZ => $coderef
118 169
119You can specify event callbacks either by subclassing and overriding the 170You can specify event callbacks either by sub-classing and overriding the
120respective methods or by specifying coderefs as key-value pairs when 171respective methods or by specifying code-refs as key-value pairs when
121constructing the object. 172constructing the object. You add or remove event handlers at any time with
173the C<event> method.
122 174
123=back 175=back
124 176
125=cut 177=cut
126 178
128 my $class = shift; 180 my $class = shift;
129 181
130 my $self = bless { 182 my $self = bless {
131 id => "a", 183 id => "a",
132 ids => [], 184 ids => [],
133 queue => [], # ininitially queue everything 185 queue => [], # initially queue everything
134 @_, 186 @_,
135 }, $class; 187 }, $class;
136 188
137 { 189 {
138 Scalar::Util::weaken (my $self = $self); 190 Scalar::Util::weaken (my $self = $self);
260 $_[0]{queue} 312 $_[0]{queue}
261 ? push @{ $_[0]{queue} }, [@_] 313 ? push @{ $_[0]{queue} }, [@_]
262 : &_req 314 : &_req
263} 315}
264 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
265=item $api->req_failok ($type => @args, $callback->($api, $success, @reply)) 337=item $api->req_failok ($type => @args, $callback->($api, $success, @reply))
266 338
267Just like C<< ->req >>, with two differences: first, a failure will not 339Just like C<< ->req >>, with two differences: first, a failure will not
268raise an error, second, the initial status reply which indicates success 340raise an error, second, the initial status reply which indicates success
269or failure is not removed before calling the callback. 341or failure is not removed before calling the callback.
272 344
273sub req_failok { 345sub req_failok {
274 $_[0]{queue} 346 $_[0]{queue}
275 ? push @{ $_[0]{queue} }, [@_] 347 ? push @{ $_[0]{queue} }, [@_]
276 : &_req 348 : &_req
349}
350
351=item $api->on (XYZ => $callback)
352
353Overwrites any currently registered handler for C<on_XYZ> or
354installs a new one. Or, when C<$callback> is undef, unregisters any
355currently-registered handler.
356
357Example: replace/set the handler for C<on_discover_stop_event>.
358
359 $api->on (discover_stop_event => sub {
360 my ($api, $gid) = @_;
361 ...
362 });
363
364=cut
365
366sub on {
367 my $self = shift;
368
369 while (@_) {
370 my ($event, $cb) = splice @_, 0, 2;
371 $event =~ s/^on_//;
372
373 $self->{"on_$event"} = $cb;
374 }
277} 375}
278 376
279sub on_start_tls_notify { 377sub on_start_tls_notify {
280 my ($self) = @_; 378 my ($self) = @_;
281 379
387 call $self, "on_${event}_event", @args; 485 call $self, "on_${event}_event", @args;
388} 486}
389 487
390=back 488=back
391 489
392=head2 EVENTS 490=head1 EVENTS/CALLBACKS
393 491
394AnyEvent::Porttracker conenctions are fully event-driven, and naturally 492AnyEvent::Porttracker connections are fully event-driven, and naturally
395there are a number of events that can occur. All these events have a name 493there are a number of events that can occur. All these events have a name
396starting with C<on_> (example: C<on_login_failure>). 494starting with C<on_> (example: C<on_login_failure>).
397 495
398Programs can catch these events in two ways: either by providing 496Programs can catch these events in two ways: either by providing
399constructor arguments with the event name as key and a coderef as value: 497constructor arguments with the event name as key and a code-ref as value:
400 498
401 my $api = new AnyEvent::Porttracker 499 my $api = new AnyEvent::Porttracker
402 host => ..., 500 host => ...,
403 user => ..., pass => ..., 501 user => ..., pass => ...,
404 on_error => sub { 502 on_error => sub {
406 warn $msg; 504 warn $msg;
407 exit 1; 505 exit 1;
408 }, 506 },
409 ; 507 ;
410 508
411Or by subclassing C<AnyEvent::Porttracker> and overriding methods of the 509Or by sub-classing C<AnyEvent::Porttracker> and overriding methods of the
412same name: 510same name:
413 511
414 package MyClass; 512 package MyClass;
415 513
416 use base AnyEvent::Porttracker; 514 use base AnyEvent::Porttracker;
464 562
465=item on_start_tls_notify $api 563=item on_start_tls_notify $api
466 564
467Called when the server wants to start TLS negotiation. This is used 565Called when the server wants to start TLS negotiation. This is used
468internally and - while it is possible to override it - should not be 566internally and - while it is possible to override it - should not be
469overriden. 567overridden.
470 568
471=item on_event_notify $api, $eventname, @args 569=item on_event_notify $api, $eventname, @args
472 570
473Called when the server broadcasts an event the API object is subscribed 571Called when the server broadcasts an event the API object is subscribed
474to. The default implementation (which should not be overridden) simply 572to. The default implementation (which should not be overridden) simply
475re-issues an "on_evenname_event" event with the @args. 573re-issues an "on_eventname_event" event with the @args.
476 574
477=item on_XYZ_notify $api, ... 575=item on_XYZ_notify $api, ...
478 576
479In general, any protocol notification will result in an event of the form 577In general, any protocol notification will result in an event of the form
480C<on_NOTIFICATION_notify>. 578C<on_NOTIFICATION_notify>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines