… | |
… | |
35 | The import tag to use is named C<event=xyz>, e.g. C<event=Event>, |
35 | The import tag to use is named C<event=xyz>, e.g. C<event=Event>, |
36 | C<event=Glib> etc. |
36 | C<event=Glib> etc. |
37 | |
37 | |
38 | You should specify the event module to use only in the main program. |
38 | You should specify the event module to use only in the main program. |
39 | |
39 | |
|
|
40 | If no event model has been specified, FCP tries to autodetect it on first |
|
|
41 | use (e.g. first transaction), in this order: Coro, Event, Glib, Tk. |
|
|
42 | |
|
|
43 | =head2 FREENET BASICS |
|
|
44 | |
|
|
45 | Ok, this section will not explain any freenet basics to you, just some |
|
|
46 | problems I found that you might want to avoid: |
|
|
47 | |
|
|
48 | =over 4 |
|
|
49 | |
|
|
50 | =item freenet URIs are _NOT_ URIs |
|
|
51 | |
|
|
52 | Whenever a "uri" is required by the protocol, freenet expects a kind of |
|
|
53 | URI prefixed with the "freenet:" scheme, e.g. "freenet:CHK...". However, |
|
|
54 | these are not URIs, as freeent fails to parse them correctly, that is, you |
|
|
55 | must unescape an escaped characters ("%2c" => ",") yourself. Maybe in the |
|
|
56 | future this library will do it for you, so watch out for this incompatible |
|
|
57 | change. |
|
|
58 | |
|
|
59 | =item Numbers are in HEX |
|
|
60 | |
|
|
61 | Virtually every number in the FCP protocol is in hex. Be sure to use |
|
|
62 | C<hex()> on all such numbers, as the module (currently) does nothing to |
|
|
63 | convert these for you. |
|
|
64 | |
|
|
65 | =back |
|
|
66 | |
40 | =head2 THE Net::FCP CLASS |
67 | =head2 THE Net::FCP CLASS |
41 | |
68 | |
42 | =over 4 |
69 | =over 4 |
43 | |
70 | |
44 | =cut |
71 | =cut |
45 | |
72 | |
46 | package Net::FCP; |
73 | package Net::FCP; |
47 | |
74 | |
48 | use Carp; |
75 | use Carp; |
49 | |
76 | |
50 | $VERSION = 0.07; |
77 | $VERSION = 0.08; |
51 | |
78 | |
52 | no warnings; |
79 | no warnings; |
53 | |
80 | |
54 | our $EVENT = Net::FCP::Event::Auto::; |
81 | our $EVENT = Net::FCP::Event::Auto::; |
55 | $EVENT = Net::FCP::Event::Event;#d# |
|
|
56 | |
82 | |
57 | sub import { |
83 | sub import { |
58 | shift; |
84 | shift; |
59 | |
85 | |
60 | for (@_) { |
86 | for (@_) { |
61 | if (/^event=(\w+)$/) { |
87 | if (/^event=(\w+)$/) { |
62 | $EVENT = "Net::FCP::Event::$1"; |
88 | $EVENT = "Net::FCP::Event::$1"; |
|
|
89 | eval "require $EVENT"; |
63 | } |
90 | } |
64 | } |
91 | } |
65 | eval "require $EVENT"; |
|
|
66 | die $@ if $@; |
92 | die $@ if $@; |
67 | } |
93 | } |
68 | |
94 | |
69 | sub touc($) { |
95 | sub touc($) { |
70 | local $_ = shift; |
96 | local $_ = shift; |
… | |
… | |
81 | |
107 | |
82 | =item $meta = Net::FCP::parse_metadata $string |
108 | =item $meta = Net::FCP::parse_metadata $string |
83 | |
109 | |
84 | Parse a metadata string and return it. |
110 | Parse a metadata string and return it. |
85 | |
111 | |
86 | The metadata will be a hashref with key C<version> (containing |
112 | The metadata will be a hashref with key C<version> (containing the |
87 | the mandatory version header entries). |
113 | mandatory version header entries) and key C<raw> containing the original |
|
|
114 | metadata string. |
88 | |
115 | |
89 | All other headers are represented by arrayrefs (they can be repeated). |
116 | All other headers are represented by arrayrefs (they can be repeated). |
90 | |
117 | |
91 | Since this is confusing, here is a rather verbose example of a parsed |
118 | Since this description is confusing, here is a rather verbose example of a |
92 | manifest: |
119 | parsed manifest: |
93 | |
120 | |
94 | ( |
121 | ( |
|
|
122 | raw => "Version...", |
95 | version => { revision => 1 }, |
123 | version => { revision => 1 }, |
96 | document => [ |
124 | document => [ |
97 | { |
125 | { |
98 | "info.format" => "image/jpeg", |
126 | info => { format" => "image/jpeg" }, |
99 | name => "background.jpg", |
127 | name => "background.jpg", |
100 | "redirect.target" => "freenet:CHK\@ZcagI,ra726bSw" |
128 | redirect => { target => "freenet:CHK\@ZcagI,ra726bSw" }, |
101 | }, |
129 | }, |
102 | { |
130 | { |
103 | "info.format" => "text/html", |
131 | info => { format" => "text/html" }, |
104 | name => ".next", |
132 | name => ".next", |
105 | "redirect.target" => "freenet:SSK\@ilUPAgM/TFEE/3" |
133 | redirect => { target => "freenet:SSK\@ilUPAgM/TFEE/3" }, |
106 | }, |
134 | }, |
107 | { |
135 | { |
108 | "info.format" => "text/html", |
136 | info => { format" => "text/html" }, |
109 | "redirect.target" => "freenet:CHK\@8M8Po8ucwI,8xA" |
137 | redirect => { target => "freenet:CHK\@8M8Po8ucwI,8xA" }, |
110 | } |
138 | } |
111 | ] |
139 | ] |
112 | ) |
140 | ) |
113 | |
141 | |
114 | =cut |
142 | =cut |
115 | |
143 | |
116 | sub parse_metadata { |
144 | sub parse_metadata { |
117 | my $meta; |
|
|
118 | |
|
|
119 | my $data = shift; |
145 | my $data = shift; |
|
|
146 | my $meta = { raw => $data }; |
|
|
147 | |
120 | if ($data =~ /^Version\015?\012/gc) { |
148 | if ($data =~ /^Version\015?\012/gc) { |
121 | my $hdr = $meta->{version} = {}; |
149 | my $hdr = $meta->{version} = {}; |
122 | |
150 | |
123 | for (;;) { |
151 | for (;;) { |
124 | while ($data =~ /\G([^=\015\012]+)=([^\015\012]*)\015?\012/gc) { |
152 | while ($data =~ /\G([^=\015\012]+)=([^\015\012]*)\015?\012/gc) { |
… | |
… | |
131 | die "FATAL: 4+ dot metadata" if @p >= 4; |
159 | die "FATAL: 4+ dot metadata" if @p >= 4; |
132 | } |
160 | } |
133 | |
161 | |
134 | if ($data =~ /\GEndPart\015?\012/gc) { |
162 | if ($data =~ /\GEndPart\015?\012/gc) { |
135 | # nop |
163 | # nop |
136 | } elsif ($data =~ /\GEnd\015?\012/gc) { |
164 | } elsif ($data =~ /\GEnd(\015?\012|$)/gc) { |
137 | last; |
165 | last; |
138 | } elsif ($data =~ /\G([A-Za-z0-9.\-]+)\015?\012/gcs) { |
166 | } elsif ($data =~ /\G([A-Za-z0-9.\-]+)\015?\012/gcs) { |
139 | push @{$meta->{tolc $1}}, $hdr = {}; |
167 | push @{$meta->{tolc $1}}, $hdr = {}; |
140 | } elsif ($data =~ /\G(.*)/gcs) { |
168 | } elsif ($data =~ /\G(.*)/gcs) { |
|
|
169 | print STDERR "metadata format error ($1), please report this string: <<$data>>"; |
141 | die "metadata format error ($1)"; |
170 | die "metadata format error"; |
142 | } |
171 | } |
143 | } |
172 | } |
144 | } |
173 | } |
145 | |
174 | |
146 | #$meta->{tail} = substr $data, pos $data; |
175 | #$meta->{tail} = substr $data, pos $data; |
… | |
… | |
152 | |
181 | |
153 | Create a new virtual FCP connection to the given host and port (default |
182 | Create a new virtual FCP connection to the given host and port (default |
154 | 127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>). |
183 | 127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>). |
155 | |
184 | |
156 | Connections are virtual because no persistent physical connection is |
185 | Connections are virtual because no persistent physical connection is |
|
|
186 | established. |
|
|
187 | |
|
|
188 | =begin comment |
|
|
189 | |
157 | established. However, the existance of the node is checked by executing a |
190 | However, the existance of the node is checked by executing a |
158 | C<ClientHello> transaction. |
191 | C<ClientHello> transaction. |
|
|
192 | |
|
|
193 | =end |
159 | |
194 | |
160 | =cut |
195 | =cut |
161 | |
196 | |
162 | sub new { |
197 | sub new { |
163 | my $class = shift; |
198 | my $class = shift; |
… | |
… | |
172 | $self; |
207 | $self; |
173 | } |
208 | } |
174 | |
209 | |
175 | sub progress { |
210 | sub progress { |
176 | my ($self, $txn, $type, $attr) = @_; |
211 | my ($self, $txn, $type, $attr) = @_; |
177 | warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; |
212 | #warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; |
178 | } |
213 | } |
179 | |
214 | |
180 | =item $txn = $fcp->txn(type => attr => val,...) |
215 | =item $txn = $fcp->txn(type => attr => val,...) |
181 | |
216 | |
182 | The low-level interface to transactions. Don't use it. |
217 | The low-level interface to transactions. Don't use it. |
… | |
… | |
213 | my $txn = "Net::FCP::Txn::$type"->new(fcp => $self, type => tolc $type, attr => \%attr); |
248 | my $txn = "Net::FCP::Txn::$type"->new(fcp => $self, type => tolc $type, attr => \%attr); |
214 | |
249 | |
215 | $txn; |
250 | $txn; |
216 | } |
251 | } |
217 | |
252 | |
218 | sub _txn($&) { |
253 | { # transactions |
|
|
254 | |
|
|
255 | my $txn = sub { |
219 | my ($name, $sub) = @_; |
256 | my ($name, $sub) = @_; |
220 | *{"$name\_txn"} = $sub; |
257 | *{"txn_$name"} = $sub; |
221 | *{$name} = sub { $sub->(@_)->result }; |
258 | *{$name} = sub { $sub->(@_)->result }; |
222 | } |
259 | }; |
223 | |
260 | |
224 | =item $txn = $fcp->txn_client_hello |
261 | =item $txn = $fcp->txn_client_hello |
225 | |
262 | |
226 | =item $nodehello = $fcp->client_hello |
263 | =item $nodehello = $fcp->client_hello |
227 | |
264 | |
… | |
… | |
233 | protocol => "1.2", |
270 | protocol => "1.2", |
234 | } |
271 | } |
235 | |
272 | |
236 | =cut |
273 | =cut |
237 | |
274 | |
238 | _txn client_hello => sub { |
275 | $txn->(client_hello => sub { |
239 | my ($self) = @_; |
276 | my ($self) = @_; |
240 | |
277 | |
241 | $self->txn ("client_hello"); |
278 | $self->txn ("client_hello"); |
242 | }; |
279 | }); |
243 | |
280 | |
244 | =item $txn = $fcp->txn_client_info |
281 | =item $txn = $fcp->txn_client_info |
245 | |
282 | |
246 | =item $nodeinfo = $fcp->client_info |
283 | =item $nodeinfo = $fcp->client_info |
247 | |
284 | |
… | |
… | |
271 | routing_time => "a5", |
308 | routing_time => "a5", |
272 | } |
309 | } |
273 | |
310 | |
274 | =cut |
311 | =cut |
275 | |
312 | |
276 | _txn client_info => sub { |
313 | $txn->(client_info => sub { |
277 | my ($self) = @_; |
314 | my ($self) = @_; |
278 | |
315 | |
279 | $self->txn ("client_info"); |
316 | $self->txn ("client_info"); |
280 | }; |
317 | }); |
281 | |
318 | |
282 | =item $txn = $fcp->txn_generate_chk ($metadata, $data) |
319 | =item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher]) |
283 | |
320 | |
284 | =item $uri = $fcp->generate_chk ($metadata, $data) |
321 | =item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) |
285 | |
322 | |
286 | Creates a new CHK, given the metadata and data. UNTESTED. |
323 | Calculcates a CHK, given the metadata and data. C<$cipher> is either |
|
|
324 | C<Rijndael> or C<Twofish>, with the latter being the default. |
287 | |
325 | |
288 | =cut |
326 | =cut |
289 | |
327 | |
290 | _txn generate_chk => sub { |
328 | $txn->(generate_chk => sub { |
291 | my ($self, $metadata, $data) = @_; |
329 | my ($self, $metadata, $data, $cipher) = @_; |
292 | |
330 | |
293 | $self->txn (generate_chk => data => "$data$metadata", metadata_length => length $metadata); |
331 | $self->txn (generate_chk => |
|
|
332 | data => "$metadata$data", |
|
|
333 | metadata_length => length $metadata, |
|
|
334 | cipher => $cipher || "Twofish"); |
294 | }; |
335 | }); |
295 | |
336 | |
296 | =item $txn = $fcp->txn_generate_svk_pair |
337 | =item $txn = $fcp->txn_generate_svk_pair |
297 | |
338 | |
298 | =item ($public, $private) = @{ $fcp->generate_svk_pair } |
339 | =item ($public, $private) = @{ $fcp->generate_svk_pair } |
299 | |
340 | |
… | |
… | |
304 | "ZnmvMITaTXBMFGl4~jrjuyWxOWg" |
345 | "ZnmvMITaTXBMFGl4~jrjuyWxOWg" |
305 | ] |
346 | ] |
306 | |
347 | |
307 | =cut |
348 | =cut |
308 | |
349 | |
309 | _txn generate_svk_pair => sub { |
350 | $txn->(generate_svk_pair => sub { |
310 | my ($self) = @_; |
351 | my ($self) = @_; |
311 | |
352 | |
312 | $self->txn ("generate_svk_pair"); |
353 | $self->txn ("generate_svk_pair"); |
313 | }; |
354 | }); |
314 | |
355 | |
315 | =item $txn = $fcp->txn_insert_private_key ($private) |
356 | =item $txn = $fcp->txn_insert_private_key ($private) |
316 | |
357 | |
317 | =item $uri = $fcp->insert_private_key ($private) |
358 | =item $public = $fcp->insert_private_key ($private) |
318 | |
359 | |
319 | Inserts a private key. $private can be either an insert URI (must start |
360 | Inserts a private key. $private can be either an insert URI (must start |
320 | with freenet:SSK@) or a raw private key (i.e. the private value you get back |
361 | with C<freenet:SSK@>) or a raw private key (i.e. the private value you get |
321 | from C<generate_svk_pair>). |
362 | back from C<generate_svk_pair>). |
322 | |
363 | |
323 | Returns the public key. |
364 | Returns the public key. |
324 | |
365 | |
325 | UNTESTED. |
366 | UNTESTED. |
326 | |
367 | |
327 | =cut |
368 | =cut |
328 | |
369 | |
329 | _txn insert_private_key => sub { |
370 | $txn->(insert_private_key => sub { |
330 | my ($self, $privkey) = @_; |
371 | my ($self, $privkey) = @_; |
331 | |
372 | |
332 | $self->txn (invert_private_key => private => $privkey); |
373 | $self->txn (invert_private_key => private => $privkey); |
333 | }; |
374 | }); |
334 | |
375 | |
335 | =item $txn = $fcp->txn_get_size ($uri) |
376 | =item $txn = $fcp->txn_get_size ($uri) |
336 | |
377 | |
337 | =item $length = $fcp->get_size ($uri) |
378 | =item $length = $fcp->get_size ($uri) |
338 | |
379 | |
… | |
… | |
341 | |
382 | |
342 | UNTESTED. |
383 | UNTESTED. |
343 | |
384 | |
344 | =cut |
385 | =cut |
345 | |
386 | |
346 | _txn get_size => sub { |
387 | $txn->(get_size => sub { |
347 | my ($self, $uri) = @_; |
388 | my ($self, $uri) = @_; |
348 | |
389 | |
349 | $self->txn (get_size => URI => $uri); |
390 | $self->txn (get_size => URI => $uri); |
350 | }; |
391 | }); |
351 | |
392 | |
352 | =item $txn = $fcp->txn_client_get ($uri [, $htl = 15 [, $removelocal = 0]]) |
393 | =item $txn = $fcp->txn_client_get ($uri [, $htl = 15 [, $removelocal = 0]]) |
353 | |
394 | |
354 | =item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal) |
395 | =item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal) |
355 | |
396 | |
… | |
… | |
365 | ) |
406 | ) |
366 | }; |
407 | }; |
367 | |
408 | |
368 | =cut |
409 | =cut |
369 | |
410 | |
370 | _txn client_get => sub { |
411 | $txn->(client_get => sub { |
371 | my ($self, $uri, $htl, $removelocal) = @_; |
412 | my ($self, $uri, $htl, $removelocal) = @_; |
372 | |
413 | |
373 | $self->txn (client_get => URI => $uri, hops_to_live => ($htl || 15), remove_local_key => $removelocal ? "true" : "false"); |
414 | $self->txn (client_get => URI => $uri, hops_to_live => (defined $htl ? $htl :15), |
|
|
415 | remove_local_key => $removelocal ? "true" : "false"); |
374 | }; |
416 | }); |
375 | |
417 | |
|
|
418 | =item $txn = $fcp->txn_client_put ($uri, $metadata, $data, $htl, $removelocal) |
|
|
419 | |
|
|
420 | =item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal); |
|
|
421 | |
|
|
422 | Insert a new key. If the client is inserting a CHK, the URI may be |
|
|
423 | abbreviated as just CHK@. In this case, the node will calculate the |
|
|
424 | CHK. |
|
|
425 | |
|
|
426 | C<$meta> can be a reference or a string (ONLY THE STRING CASE IS IMPLEMENTED!). |
|
|
427 | |
|
|
428 | THIS INTERFACE IS UNTESTED AND SUBJECT TO CHANGE. |
|
|
429 | |
|
|
430 | =cut |
|
|
431 | |
|
|
432 | $txn->(client_put => sub { |
|
|
433 | my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; |
|
|
434 | |
|
|
435 | $self->txn (client_put => URI => $uri, hops_to_live => (defined $htl ? $htl :15), |
|
|
436 | remove_local_key => $removelocal ? "true" : "false", |
|
|
437 | data => "$meta$data", metadata_length => length $meta); |
|
|
438 | }); |
|
|
439 | |
|
|
440 | } # transactions |
|
|
441 | |
376 | =item MISSING: ClientPut |
442 | =item MISSING: (ClientPut), InsretKey |
377 | |
443 | |
378 | =back |
444 | =back |
379 | |
445 | |
380 | =head2 THE Net::FCP::Txn CLASS |
446 | =head2 THE Net::FCP::Txn CLASS |
381 | |
447 | |
… | |
… | |
417 | while (my ($k, $v) = each %{$self->{attr}}) { |
483 | while (my ($k, $v) = each %{$self->{attr}}) { |
418 | $attr .= (Net::FCP::touc $k) . "=$v\012" |
484 | $attr .= (Net::FCP::touc $k) . "=$v\012" |
419 | } |
485 | } |
420 | |
486 | |
421 | if (defined $data) { |
487 | if (defined $data) { |
422 | $attr .= "DataLength=" . (length $data) . "\012"; |
488 | $attr .= sprintf "DataLength=%x\012", length $data; |
423 | $data = "Data\012$data"; |
489 | $data = "Data\012$data"; |
424 | } else { |
490 | } else { |
425 | $data = "EndMessage\012"; |
491 | $data = "EndMessage\012"; |
426 | } |
492 | } |
427 | |
493 | |
… | |
… | |
434 | and !$!{EINPROGRESS} |
500 | and !$!{EINPROGRESS} |
435 | and Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; |
501 | and Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; |
436 | |
502 | |
437 | $self->{sbuf} = |
503 | $self->{sbuf} = |
438 | "\x00\x00\x00\x02" |
504 | "\x00\x00\x00\x02" |
439 | . Net::FCP::touc $self->{type} |
505 | . (Net::FCP::touc $self->{type}) |
440 | . "\012$attr$data"; |
506 | . "\012$attr$data"; |
441 | |
507 | |
442 | #$fh->shutdown (1); # freenet buggy?, well, it's java... |
508 | #shutdown $fh, 1; # freenet buggy?, well, it's java... |
443 | |
509 | |
444 | $self->{fh} = $fh; |
510 | $self->{fh} = $fh; |
445 | |
511 | |
446 | $self->{w} = $EVENT->new_from_fh ($fh)->cb(sub { $self->fh_ready_w })->poll(0, 1, 1); |
512 | $self->{w} = $EVENT->new_from_fh ($fh)->cb(sub { $self->fh_ready_w })->poll(0, 1, 1); |
447 | |
513 | |
… | |
… | |
483 | |
549 | |
484 | sub userdata($$) { |
550 | sub userdata($$) { |
485 | my ($self, $data) = @_; |
551 | my ($self, $data) = @_; |
486 | $self->{userdata} = $data; |
552 | $self->{userdata} = $data; |
487 | $self; |
553 | $self; |
|
|
554 | } |
|
|
555 | |
|
|
556 | =item $txn->cancel (%attr) |
|
|
557 | |
|
|
558 | Cancels the operation with a C<cancel> exception anf the given attributes |
|
|
559 | (consider at least giving the attribute C<reason>). |
|
|
560 | |
|
|
561 | UNTESTED. |
|
|
562 | |
|
|
563 | =cut |
|
|
564 | |
|
|
565 | sub cancel { |
|
|
566 | my ($self, %attr) = @_; |
|
|
567 | $self->throw (Net::FCP::Exception->new (cancel => { %attr })); |
|
|
568 | $self->set_result; |
|
|
569 | $self->eof; |
488 | } |
570 | } |
489 | |
571 | |
490 | sub fh_ready_w { |
572 | sub fh_ready_w { |
491 | my ($self) = @_; |
573 | my ($self) = @_; |
492 | |
574 | |
… | |
… | |
532 | } else { |
614 | } else { |
533 | $self->eof; |
615 | $self->eof; |
534 | } |
616 | } |
535 | } |
617 | } |
536 | |
618 | |
537 | sub rcv_data { |
|
|
538 | my ($self, $chunk) = @_; |
|
|
539 | |
|
|
540 | $self->{data} .= $chunk; |
|
|
541 | |
|
|
542 | $self->progress ("data", { chunk => length $chunk, total => length $self->{data}, end => $self->{datalength} }); |
|
|
543 | } |
|
|
544 | |
|
|
545 | sub rcv { |
619 | sub rcv { |
546 | my ($self, $type, $attr) = @_; |
620 | my ($self, $type, $attr) = @_; |
547 | |
621 | |
548 | $type = Net::FCP::tolc $type; |
622 | $type = Net::FCP::tolc $type; |
549 | |
623 | |
… | |
… | |
567 | |
641 | |
568 | sub throw { |
642 | sub throw { |
569 | my ($self, $exc) = @_; |
643 | my ($self, $exc) = @_; |
570 | |
644 | |
571 | $self->{exception} = $exc; |
645 | $self->{exception} = $exc; |
572 | $self->set_result (1); |
646 | $self->set_result; |
573 | $self->eof; # must be last to avoid loops |
647 | $self->eof; # must be last to avoid loops |
574 | } |
648 | } |
575 | |
649 | |
576 | sub set_result { |
650 | sub set_result { |
577 | my ($self, $result) = @_; |
651 | my ($self, $result) = @_; |
… | |
… | |
589 | delete $self->{w}; |
663 | delete $self->{w}; |
590 | delete $self->{fh}; |
664 | delete $self->{fh}; |
591 | |
665 | |
592 | delete $self->{fcp}{txn}{$self}; |
666 | delete $self->{fcp}{txn}{$self}; |
593 | |
667 | |
594 | $self->set_result; # just in case |
668 | unless (exists $self->{result}) { |
|
|
669 | $self->throw (Net::FCP::Exception->new (short_data => { |
|
|
670 | reason => "unexpected eof or internal node error", |
|
|
671 | })); |
|
|
672 | } |
595 | } |
673 | } |
596 | |
674 | |
597 | sub progress { |
675 | sub progress { |
598 | my ($self, $type, $attr) = @_; |
676 | my ($self, $type, $attr) = @_; |
599 | $self->{fcp}->progress ($self, $type, $attr); |
677 | $self->{fcp}->progress ($self, $type, $attr); |
… | |
… | |
643 | use base Net::FCP::Txn; |
721 | use base Net::FCP::Txn; |
644 | |
722 | |
645 | sub rcv_success { |
723 | sub rcv_success { |
646 | my ($self, $attr) = @_; |
724 | my ($self, $attr) = @_; |
647 | |
725 | |
648 | $self->set_result ($attr); |
726 | $self->set_result ($attr->{uri}); |
649 | } |
727 | } |
650 | |
728 | |
651 | package Net::FCP::Txn::GenerateSVKPair; |
729 | package Net::FCP::Txn::GenerateSVKPair; |
652 | |
730 | |
653 | use base Net::FCP::Txn; |
731 | use base Net::FCP::Txn; |
654 | |
732 | |
655 | sub rcv_success { |
733 | sub rcv_success { |
656 | my ($self, $attr) = @_; |
734 | my ($self, $attr) = @_; |
657 | |
|
|
658 | $self->set_result ([$attr->{PublicKey}, $attr->{PrivateKey}]); |
735 | $self->set_result ([$attr->{PublicKey}, $attr->{PrivateKey}]); |
659 | } |
736 | } |
660 | |
737 | |
661 | package Net::FCP::Txn::InvertPrivateKey; |
738 | package Net::FCP::Txn::InsertPrivateKey; |
662 | |
739 | |
663 | use base Net::FCP::Txn; |
740 | use base Net::FCP::Txn; |
664 | |
741 | |
665 | sub rcv_success { |
742 | sub rcv_success { |
666 | my ($self, $attr) = @_; |
743 | my ($self, $attr) = @_; |
667 | |
|
|
668 | $self->set_result ($attr->{PublicKey}); |
744 | $self->set_result ($attr->{PublicKey}); |
669 | } |
745 | } |
670 | |
746 | |
671 | package Net::FCP::Txn::GetSize; |
747 | package Net::FCP::Txn::GetSize; |
672 | |
748 | |
673 | use base Net::FCP::Txn; |
749 | use base Net::FCP::Txn; |
674 | |
750 | |
675 | sub rcv_success { |
751 | sub rcv_success { |
676 | my ($self, $attr) = @_; |
752 | my ($self, $attr) = @_; |
677 | |
|
|
678 | $self->set_result ($attr->{Length}); |
753 | $self->set_result ($attr->{Length}); |
679 | } |
754 | } |
680 | |
755 | |
681 | package Net::FCP::Txn::GetPut; |
756 | package Net::FCP::Txn::GetPut; |
682 | |
757 | |
… | |
… | |
701 | |
776 | |
702 | use base Net::FCP::Txn::GetPut; |
777 | use base Net::FCP::Txn::GetPut; |
703 | |
778 | |
704 | *rcv_data_not_found = \&Net::FCP::Txn::rcv_throw_exception; |
779 | *rcv_data_not_found = \&Net::FCP::Txn::rcv_throw_exception; |
705 | |
780 | |
706 | sub rcv_data_found { |
781 | sub rcv_data { |
707 | my ($self, $attr, $type) = @_; |
|
|
708 | |
|
|
709 | $self->progress ($type, $attr); |
|
|
710 | |
|
|
711 | $self->{datalength} = hex $attr->{data_length}; |
|
|
712 | $self->{metalength} = hex $attr->{metadata_length}; |
|
|
713 | } |
|
|
714 | |
|
|
715 | sub eof { |
|
|
716 | my ($self) = @_; |
782 | my ($self, $chunk) = @_; |
|
|
783 | |
|
|
784 | $self->{data} .= $chunk; |
|
|
785 | |
|
|
786 | $self->progress ("data", { chunk => length $chunk, received => length $self->{data}, total => $self->{datalength} }); |
717 | |
787 | |
718 | if ($self->{datalength} == length $self->{data}) { |
788 | if ($self->{datalength} == length $self->{data}) { |
719 | my $data = delete $self->{data}; |
789 | my $data = delete $self->{data}; |
720 | my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; |
790 | my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; |
721 | |
791 | |
722 | $self->set_result ([$meta, $data]); |
792 | $self->set_result ([$meta, $data]); |
723 | } elsif (!exists $self->{result}) { |
|
|
724 | $self->throw (Net::FCP::Exception->new (short_data => { |
|
|
725 | reason => "unexpected eof or internal node error", |
|
|
726 | received => length $self->{data}, |
|
|
727 | expected => $self->{datalength}, |
|
|
728 | })); |
|
|
729 | } |
793 | } |
|
|
794 | } |
|
|
795 | |
|
|
796 | sub rcv_data_found { |
|
|
797 | my ($self, $attr, $type) = @_; |
|
|
798 | |
|
|
799 | $self->progress ($type, $attr); |
|
|
800 | |
|
|
801 | $self->{datalength} = hex $attr->{data_length}; |
|
|
802 | $self->{metalength} = hex $attr->{metadata_length}; |
730 | } |
803 | } |
731 | |
804 | |
732 | package Net::FCP::Txn::ClientPut; |
805 | package Net::FCP::Txn::ClientPut; |
733 | |
806 | |
734 | use base Net::FCP::Txn::GetPut; |
807 | use base Net::FCP::Txn::GetPut; |
… | |
… | |
743 | |
816 | |
744 | sub rcv_success { |
817 | sub rcv_success { |
745 | my ($self, $attr, $type) = @_; |
818 | my ($self, $attr, $type) = @_; |
746 | $self->set_result ($attr); |
819 | $self->set_result ($attr); |
747 | } |
820 | } |
|
|
821 | |
|
|
822 | =back |
|
|
823 | |
|
|
824 | =head2 The Net::FCP::Exception CLASS |
|
|
825 | |
|
|
826 | Any unexpected (non-standard) responses that make it impossible to return |
|
|
827 | the advertised result will result in an exception being thrown when the |
|
|
828 | C<result> method is called. |
|
|
829 | |
|
|
830 | These exceptions are represented by objects of this class. |
|
|
831 | |
|
|
832 | =over 4 |
|
|
833 | |
|
|
834 | =cut |
748 | |
835 | |
749 | package Net::FCP::Exception; |
836 | package Net::FCP::Exception; |
750 | |
837 | |
751 | use overload |
838 | use overload |
752 | '""' => sub { |
839 | '""' => sub { |
753 | "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>\n"; |
840 | "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>\n"; |
754 | }; |
841 | }; |
755 | |
842 | |
|
|
843 | =item $exc = new Net::FCP::Exception $type, \%attr |
|
|
844 | |
|
|
845 | Create a new exception object of the given type (a string like |
|
|
846 | C<route_not_found>), and a hashref containing additional attributes |
|
|
847 | (usually the attributes of the message causing the exception). |
|
|
848 | |
|
|
849 | =cut |
|
|
850 | |
756 | sub new { |
851 | sub new { |
757 | my ($class, $type, $attr) = @_; |
852 | my ($class, $type, $attr) = @_; |
758 | |
853 | |
759 | bless [Net::FCP::tolc $type, { %$attr }], $class; |
854 | bless [Net::FCP::tolc $type, { %$attr }], $class; |
760 | } |
855 | } |
761 | |
856 | |
|
|
857 | =item $exc->type([$type]) |
|
|
858 | |
|
|
859 | With no arguments, returns the exception type. Otherwise a boolean |
|
|
860 | indicating wether the exception is of the given type is returned. |
|
|
861 | |
|
|
862 | =cut |
|
|
863 | |
|
|
864 | sub type { |
|
|
865 | my ($self, $type) = @_; |
|
|
866 | |
|
|
867 | @_ >= 2 |
|
|
868 | ? $self->[0] eq $type |
|
|
869 | : $self->[0]; |
|
|
870 | } |
|
|
871 | |
|
|
872 | =item $exc->attr([$attr]) |
|
|
873 | |
|
|
874 | With no arguments, returns the attributes. Otherwise the named attribute |
|
|
875 | value is returned. |
|
|
876 | |
|
|
877 | =cut |
|
|
878 | |
|
|
879 | sub attr { |
|
|
880 | my ($self, $attr) = @_; |
|
|
881 | |
|
|
882 | @_ >= 2 |
|
|
883 | ? $self->[1]{$attr} |
|
|
884 | : $self->[1]; |
|
|
885 | } |
|
|
886 | |
762 | =back |
887 | =back |
763 | |
888 | |
764 | =head1 SEE ALSO |
889 | =head1 SEE ALSO |
765 | |
890 | |
766 | L<http://freenet.sf.net>. |
891 | L<http://freenet.sf.net>. |
… | |
… | |
772 | Marc Lehmann <pcg@goof.com> |
897 | Marc Lehmann <pcg@goof.com> |
773 | http://www.goof.com/pcg/marc/ |
898 | http://www.goof.com/pcg/marc/ |
774 | |
899 | |
775 | =cut |
900 | =cut |
776 | |
901 | |
|
|
902 | package Net::FCP::Event::Auto; |
|
|
903 | |
|
|
904 | my @models = ( |
|
|
905 | [Coro => Coro::Event:: ], |
|
|
906 | [Event => Event::], |
|
|
907 | [Glib => Glib:: ], |
|
|
908 | [Tk => Tk::], |
|
|
909 | ); |
|
|
910 | |
|
|
911 | sub AUTOLOAD { |
|
|
912 | $AUTOLOAD =~ s/.*://; |
|
|
913 | |
|
|
914 | for (@models) { |
|
|
915 | my ($model, $package) = @$_; |
|
|
916 | if (defined ${"$package\::VERSION"}) { |
|
|
917 | $EVENT = "Net::FCP::Event::$model"; |
|
|
918 | eval "require $EVENT"; die if $@; |
|
|
919 | goto &{"$EVENT\::$AUTOLOAD"}; |
|
|
920 | } |
|
|
921 | } |
|
|
922 | |
|
|
923 | for (@models) { |
|
|
924 | my ($model, $package) = @$_; |
|
|
925 | $EVENT = "Net::FCP::Event::$model"; |
|
|
926 | if (eval "require $EVENT") { |
|
|
927 | goto &{"$EVENT\::$AUTOLOAD"}; |
|
|
928 | } |
|
|
929 | } |
|
|
930 | |
|
|
931 | die "No event module selected for Net::FCP and autodetect failed. Install any of these: Coro, Event, Glib or Tk."; |
|
|
932 | } |
|
|
933 | |
777 | 1; |
934 | 1; |
778 | |
935 | |