… | |
… | |
25 | |
25 | |
26 | To quickly understand how this module works you should read how to |
26 | To quickly understand how this module works you should read how to |
27 | construct a new connection object and then read about the event/callback |
27 | construct a new connection object and then read about the event/callback |
28 | system. |
28 | system. |
29 | |
29 | |
|
|
30 | The actual low-level protocol and, more importantly, the existing |
|
|
31 | requests and responses, are documented in the official Porttracker |
|
|
32 | API documentation (a copy of which is included in this module as |
|
|
33 | L<AnyEvent::Porttracker::protocol>. |
|
|
34 | |
30 | =head1 THE AnyEvent::Porttracker CLASS |
35 | =head1 THE AnyEvent::Porttracker CLASS |
31 | |
36 | |
32 | The AnyEvent::Porttracker class represents a single connection. |
37 | The 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 | |
65 | Creates a new porttracker API connection object and tries to connect to |
70 | Creates a new porttracker API connection object and tries to connect to |
66 | the specified host (see below). After the connection has been established, |
71 | the specified host (see below). After the connection has been established, |
67 | the TLS handshake (if requested) will take place, followed by a login |
72 | the TLS handshake (if requested) will take place, followed by a login |
68 | attempt using either the C<none>, C<login_cram_md6> or C<login> methods, |
73 | attempt using either the C<none>, C<login_cram_md6> or C<login> methods, |
… | |
… | |
107 | sub new { |
112 | sub 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 | |
171 | sub _req { |
179 | sub _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 | |
|
|
195 | Sends a generic request of type C<$type> to the server. When the server |
|
|
196 | responds, the API object and the response arguments are passed to the |
|
|
197 | callback, which is the last argument to this method. |
|
|
198 | |
|
|
199 | The available requests are documented in the Porttracker API |
|
|
200 | documentation (a copy of which is included in this module as |
|
|
201 | L<AnyEvent::Porttracker::protocol>. |
|
|
202 | |
|
|
203 | It is permissible to call this (or any other request function) at any |
|
|
204 | time, even before the connection has been established - the API object |
|
|
205 | always waits until after login before it actually sends the requests, and |
|
|
206 | queues them until then. |
|
|
207 | |
|
|
208 | Example: ping the porttracker server. |
|
|
209 | |
|
|
210 | $api->req ("ping", sub { |
|
|
211 | my ($api, $ok, $timestamp, $pid) = @_; |
|
|
212 | ... |
|
|
213 | }); |
|
|
214 | |
|
|
215 | Example: determine the product ID. |
|
|
216 | |
|
|
217 | $api->req (product_id => sub { |
|
|
218 | my ($api, $ok, $branding, $product_id) = @_; |
|
|
219 | ... |
|
|
220 | }); |
|
|
221 | |
|
|
222 | Example: 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 | |
185 | sub req { |
232 | sub req { |
186 | $_[0]{queue} |
233 | $_[0]{queue} |
187 | ? push @{ $_[0]{queue} }, [@_] |
234 | ? push @{ $_[0]{queue} }, [@_] |
188 | : &_req |
235 | : &_req |