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.3 by root, Mon Nov 15 19:49:36 2010 UTC vs.
Revision 1.5 by root, Mon Nov 15 20:43:11 2010 UTC

25 25
26To quickly understand how this module works you should read how to 26To quickly understand how this module works you should read how to
27construct a new connection object and then read about the event/callback 27construct a new connection object and then read about the event/callback
28system. 28system.
29 29
30The actual low-level protocol and, more importantly, the existing
31requests and responses, are documented in the official Porttracker
32API documentation (a copy of which is included in this module as
33L<AnyEvent::Porttracker::protocol>.
34
30=head1 THE AnyEvent::Porttracker CLASS 35=head1 THE AnyEvent::Porttracker CLASS
31 36
32The AnyEvent::Porttracker class represents a single connection. 37The AnyEvent::Porttracker class represents a single connection.
33 38
34=over 4 39=over 4
58 : ($type = (UNIVERSAL::can $self, $type)) 63 : ($type = (UNIVERSAL::can $self, $type))
59 ? $type->($self, @args) 64 ? $type->($self, @args)
60 : () 65 : ()
61} 66}
62 67
63=item new AnyEvent::Porttracker [key => value...] 68=item $api = new AnyEvent::Porttracker [key => value...]
64 69
65Creates a new porttracker API connection object and tries to connect to 70Creates a new porttracker API connection object and tries to connect to
66the specified host (see below). After the connection has been established, 71the specified host (see below). After the connection has been established,
67the TLS handshake (if requested) will take place, followed by a login 72the TLS handshake (if requested) will take place, followed by a login
68attempt using either the C<none>, C<login_cram_md6> or C<login> methods, 73attempt using either the C<none>, C<login_cram_md6> or C<login> methods,
107sub new { 112sub new {
108 my $class = shift; 113 my $class = shift;
109 114
110 my $self = bless { 115 my $self = bless {
111 id => "a", 116 id => "a",
117 ids => [],
112 queue => [], # ininitially queue everything 118 queue => [], # ininitially queue everything
113 @_, 119 @_,
114 }, $class; 120 }, $class;
115 121
116 { 122 {
138 144
139 if (defined $id) { 145 if (defined $id) {
140 my $cb = delete $self->{cb}{$id} 146 my $cb = delete $self->{cb}{$id}
141 or return $self->error ("received unexpected reply msg with id $id"); 147 or return $self->error ("received unexpected reply msg with id $id");
142 148
149 push @{ $self->{ids} }, $id;
150
143 $cb->($self, @$msg); 151 $cb->($self, @$msg);
144 } else { 152 } else {
145 $msg->[0] = "on_$msg->[0]_notify"; 153 $msg->[0] = "on_$msg->[0]_notify";
146 call $self, @$msg; 154 call $self, @$msg;
147 } 155 }
170 178
171sub _req { 179sub _req {
172 my $self = shift; 180 my $self = shift;
173 my $cb = pop; 181 my $cb = pop;
174 182
175 my $id = ++$self->{id}; 183 my $id = (pop @{ $self->{ids} }) || $self->{id}++;
176 184
177 unshift @_, $id; 185 unshift @_, $id;
178 $self->{cb}{$id} = $cb; 186 $self->{cb}{$id} = $cb;
179 187
180 my $msg = JSON::encode_json \@_; 188 my $msg = JSON::encode_json \@_;
181 189
182 $self->{hdl}->push_write ($msg); 190 $self->{hdl}->push_write ($msg);
183} 191}
192
193=item $api->req ($type => @args, $callback->($api, @args))
194
195Sends a generic request of type C<$type> to the server. When the server
196responds, the API object and the response arguments are passed to the
197callback, which is the last argument to this method.
198
199The available requests are documented in the Porttracker API
200documentation (a copy of which is included in this module as
201L<AnyEvent::Porttracker::protocol>.
202
203It is permissible to call this (or any other request function) at any
204time, even before the connection has been established - the API object
205always waits until after login before it actually sends the requests, and
206queues them until then.
207
208Example: ping the porttracker server.
209
210 $api->req ("ping", sub {
211 my ($api, $ok, $timestamp, $pid) = @_;
212 ...
213 });
214
215Example: determine the product ID.
216
217 $api->req (product_id => sub {
218 my ($api, $ok, $branding, $product_id) = @_;
219 ...
220 });
221
222Example: set a new license.
223
224 $api->req (set_license => $LICENSE_STRING, sub {
225 my ($api, $ok) = @_;
226
227 $ok or die "failed to set license";
228 });
229
230=cut
184 231
185sub req { 232sub req {
186 $_[0]{queue} 233 $_[0]{queue}
187 ? push @{ $_[0]{queue} }, [@_] 234 ? push @{ $_[0]{queue} }, [@_]
188 : &_req 235 : &_req

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines