… | |
… | |
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.06; |
77 | $VERSION = 0.6; |
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; |
… | |
… | |
77 | local $_ = shift; |
103 | local $_ = shift; |
78 | s/(?<=[a-z])(?=[A-Z])/_/g; |
104 | s/(?<=[a-z])(?=[A-Z])/_/g; |
79 | lc $_; |
105 | lc $_; |
80 | } |
106 | } |
81 | |
107 | |
|
|
108 | # the opposite of hex |
|
|
109 | sub xeh($) { |
|
|
110 | sprintf "%x", $_[0]; |
|
|
111 | } |
|
|
112 | |
82 | =item $meta = Net::FCP::parse_metadata $string |
113 | =item $meta = Net::FCP::parse_metadata $string |
83 | |
114 | |
84 | Parse a metadata string and return it. |
115 | Parse a metadata string and return it. |
85 | |
116 | |
86 | The metadata will be a hashref with key C<version> (containing |
117 | The metadata will be a hashref with key C<version> (containing the |
87 | the mandatory version header entries). |
118 | mandatory version header entries) and key C<raw> containing the original |
|
|
119 | metadata string. |
88 | |
120 | |
89 | All other headers are represented by arrayrefs (they can be repeated). |
121 | All other headers are represented by arrayrefs (they can be repeated). |
90 | |
122 | |
91 | Since this is confusing, here is a rather verbose example of a parsed |
123 | Since this description is confusing, here is a rather verbose example of a |
92 | manifest: |
124 | parsed manifest: |
93 | |
125 | |
94 | ( |
126 | ( |
|
|
127 | raw => "Version...", |
95 | version => { revision => 1 }, |
128 | version => { revision => 1 }, |
96 | document => [ |
129 | document => [ |
97 | { |
130 | { |
98 | "info.format" => "image/jpeg", |
131 | info => { format" => "image/jpeg" }, |
99 | name => "background.jpg", |
132 | name => "background.jpg", |
100 | "redirect.target" => "freenet:CHK\@ZcagI,ra726bSw" |
133 | redirect => { target => "freenet:CHK\@ZcagI,ra726bSw" }, |
101 | }, |
134 | }, |
102 | { |
135 | { |
103 | "info.format" => "text/html", |
136 | info => { format" => "text/html" }, |
104 | name => ".next", |
137 | name => ".next", |
105 | "redirect.target" => "freenet:SSK\@ilUPAgM/TFEE/3" |
138 | redirect => { target => "freenet:SSK\@ilUPAgM/TFEE/3" }, |
106 | }, |
139 | }, |
107 | { |
140 | { |
108 | "info.format" => "text/html", |
141 | info => { format" => "text/html" }, |
109 | "redirect.target" => "freenet:CHK\@8M8Po8ucwI,8xA" |
142 | redirect => { target => "freenet:CHK\@8M8Po8ucwI,8xA" }, |
110 | } |
143 | } |
111 | ] |
144 | ] |
112 | ) |
145 | ) |
113 | |
146 | |
114 | =cut |
147 | =cut |
115 | |
148 | |
116 | sub parse_metadata { |
149 | sub parse_metadata { |
117 | my $meta; |
|
|
118 | |
|
|
119 | my $data = shift; |
150 | my $data = shift; |
|
|
151 | my $meta = { raw => $data }; |
|
|
152 | |
120 | if ($data =~ /^Version\015?\012/gc) { |
153 | if ($data =~ /^Version\015?\012/gc) { |
121 | my $hdr = $meta->{version} = {}; |
154 | my $hdr = $meta->{version} = {}; |
122 | |
155 | |
123 | for (;;) { |
156 | for (;;) { |
124 | while ($data =~ /\G([^=\015\012]+)=([^\015\012]*)\015?\012/gc) { |
157 | while ($data =~ /\G([^=\015\012]+)=([^\015\012]*)\015?\012/gc) { |
… | |
… | |
131 | die "FATAL: 4+ dot metadata" if @p >= 4; |
164 | die "FATAL: 4+ dot metadata" if @p >= 4; |
132 | } |
165 | } |
133 | |
166 | |
134 | if ($data =~ /\GEndPart\015?\012/gc) { |
167 | if ($data =~ /\GEndPart\015?\012/gc) { |
135 | # nop |
168 | # nop |
136 | } elsif ($data =~ /\GEnd\015?\012/gc) { |
169 | } elsif ($data =~ /\GEnd(\015?\012|$)/gc) { |
137 | last; |
170 | last; |
138 | } elsif ($data =~ /\G([A-Za-z0-9.\-]+)\015?\012/gcs) { |
171 | } elsif ($data =~ /\G([A-Za-z0-9.\-]+)\015?\012/gcs) { |
139 | push @{$meta->{tolc $1}}, $hdr = {}; |
172 | push @{$meta->{tolc $1}}, $hdr = {}; |
140 | } elsif ($data =~ /\G(.*)/gcs) { |
173 | } elsif ($data =~ /\G(.*)/gcs) { |
|
|
174 | print STDERR "metadata format error ($1), please report this string: <<$data>>"; |
141 | die "metadata format error ($1)"; |
175 | die "metadata format error"; |
142 | } |
176 | } |
143 | } |
177 | } |
144 | } |
178 | } |
145 | |
179 | |
146 | #$meta->{tail} = substr $data, pos $data; |
180 | #$meta->{tail} = substr $data, pos $data; |
… | |
… | |
152 | |
186 | |
153 | Create a new virtual FCP connection to the given host and port (default |
187 | 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>). |
188 | 127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>). |
155 | |
189 | |
156 | Connections are virtual because no persistent physical connection is |
190 | Connections are virtual because no persistent physical connection is |
|
|
191 | established. |
|
|
192 | |
|
|
193 | =begin comment |
|
|
194 | |
157 | established. However, the existance of the node is checked by executing a |
195 | However, the existance of the node is checked by executing a |
158 | C<ClientHello> transaction. |
196 | C<ClientHello> transaction. |
|
|
197 | |
|
|
198 | =end |
159 | |
199 | |
160 | =cut |
200 | =cut |
161 | |
201 | |
162 | sub new { |
202 | sub new { |
163 | my $class = shift; |
203 | my $class = shift; |
… | |
… | |
172 | $self; |
212 | $self; |
173 | } |
213 | } |
174 | |
214 | |
175 | sub progress { |
215 | sub progress { |
176 | my ($self, $txn, $type, $attr) = @_; |
216 | my ($self, $txn, $type, $attr) = @_; |
177 | warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; |
217 | #warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; |
178 | } |
218 | } |
179 | |
219 | |
180 | =item $txn = $fcp->txn(type => attr => val,...) |
220 | =item $txn = $fcp->txn(type => attr => val,...) |
181 | |
221 | |
182 | The low-level interface to transactions. Don't use it. |
222 | 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); |
253 | my $txn = "Net::FCP::Txn::$type"->new(fcp => $self, type => tolc $type, attr => \%attr); |
214 | |
254 | |
215 | $txn; |
255 | $txn; |
216 | } |
256 | } |
217 | |
257 | |
218 | sub _txn($&) { |
258 | { # transactions |
|
|
259 | |
|
|
260 | my $txn = sub { |
219 | my ($name, $sub) = @_; |
261 | my ($name, $sub) = @_; |
220 | *{"$name\_txn"} = $sub; |
262 | *{"txn_$name"} = $sub; |
221 | *{$name} = sub { $sub->(@_)->result }; |
263 | *{$name} = sub { $sub->(@_)->result }; |
222 | } |
264 | }; |
223 | |
265 | |
224 | =item $txn = $fcp->txn_client_hello |
266 | =item $txn = $fcp->txn_client_hello |
225 | |
267 | |
226 | =item $nodehello = $fcp->client_hello |
268 | =item $nodehello = $fcp->client_hello |
227 | |
269 | |
… | |
… | |
233 | protocol => "1.2", |
275 | protocol => "1.2", |
234 | } |
276 | } |
235 | |
277 | |
236 | =cut |
278 | =cut |
237 | |
279 | |
238 | _txn client_hello => sub { |
280 | $txn->(client_hello => sub { |
239 | my ($self) = @_; |
281 | my ($self) = @_; |
240 | |
282 | |
241 | $self->txn ("client_hello"); |
283 | $self->txn ("client_hello"); |
242 | }; |
284 | }); |
243 | |
285 | |
244 | =item $txn = $fcp->txn_client_info |
286 | =item $txn = $fcp->txn_client_info |
245 | |
287 | |
246 | =item $nodeinfo = $fcp->client_info |
288 | =item $nodeinfo = $fcp->client_info |
247 | |
289 | |
… | |
… | |
271 | routing_time => "a5", |
313 | routing_time => "a5", |
272 | } |
314 | } |
273 | |
315 | |
274 | =cut |
316 | =cut |
275 | |
317 | |
276 | _txn client_info => sub { |
318 | $txn->(client_info => sub { |
277 | my ($self) = @_; |
319 | my ($self) = @_; |
278 | |
320 | |
279 | $self->txn ("client_info"); |
321 | $self->txn ("client_info"); |
280 | }; |
322 | }); |
281 | |
323 | |
282 | =item $txn = $fcp->txn_generate_chk ($metadata, $data) |
324 | =item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher]) |
283 | |
325 | |
284 | =item $uri = $fcp->generate_chk ($metadata, $data) |
326 | =item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) |
285 | |
327 | |
286 | Creates a new CHK, given the metadata and data. UNTESTED. |
328 | Calculcates a CHK, given the metadata and data. C<$cipher> is either |
|
|
329 | C<Rijndael> or C<Twofish>, with the latter being the default. |
287 | |
330 | |
288 | =cut |
331 | =cut |
289 | |
332 | |
290 | _txn generate_chk => sub { |
333 | $txn->(generate_chk => sub { |
291 | my ($self, $metadata, $data) = @_; |
334 | my ($self, $metadata, $data, $cipher) = @_; |
292 | |
335 | |
293 | $self->txn (generate_chk => data => "$data$metadata", metadata_length => length $metadata); |
336 | $self->txn (generate_chk => |
|
|
337 | data => "$metadata$data", |
|
|
338 | metadata_length => xeh length $metadata, |
|
|
339 | cipher => $cipher || "Twofish"); |
294 | }; |
340 | }); |
295 | |
341 | |
296 | =item $txn = $fcp->txn_generate_svk_pair |
342 | =item $txn = $fcp->txn_generate_svk_pair |
297 | |
343 | |
298 | =item ($public, $private) = @{ $fcp->generate_svk_pair } |
344 | =item ($public, $private) = @{ $fcp->generate_svk_pair } |
299 | |
345 | |
… | |
… | |
304 | "ZnmvMITaTXBMFGl4~jrjuyWxOWg" |
350 | "ZnmvMITaTXBMFGl4~jrjuyWxOWg" |
305 | ] |
351 | ] |
306 | |
352 | |
307 | =cut |
353 | =cut |
308 | |
354 | |
309 | _txn generate_svk_pair => sub { |
355 | $txn->(generate_svk_pair => sub { |
310 | my ($self) = @_; |
356 | my ($self) = @_; |
311 | |
357 | |
312 | $self->txn ("generate_svk_pair"); |
358 | $self->txn ("generate_svk_pair"); |
313 | }; |
359 | }); |
314 | |
360 | |
315 | =item $txn = $fcp->txn_insert_private_key ($private) |
361 | =item $txn = $fcp->txn_insert_private_key ($private) |
316 | |
362 | |
317 | =item $uri = $fcp->insert_private_key ($private) |
363 | =item $public = $fcp->insert_private_key ($private) |
318 | |
364 | |
319 | Inserts a private key. $private can be either an insert URI (must start |
365 | 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 |
366 | with C<freenet:SSK@>) or a raw private key (i.e. the private value you get |
321 | from C<generate_svk_pair>). |
367 | back from C<generate_svk_pair>). |
322 | |
368 | |
323 | Returns the public key. |
369 | Returns the public key. |
324 | |
370 | |
325 | UNTESTED. |
371 | UNTESTED. |
326 | |
372 | |
327 | =cut |
373 | =cut |
328 | |
374 | |
329 | _txn insert_private_key => sub { |
375 | $txn->(insert_private_key => sub { |
330 | my ($self, $privkey) = @_; |
376 | my ($self, $privkey) = @_; |
331 | |
377 | |
332 | $self->txn (invert_private_key => private => $privkey); |
378 | $self->txn (invert_private_key => private => $privkey); |
333 | }; |
379 | }); |
334 | |
380 | |
335 | =item $txn = $fcp->txn_get_size ($uri) |
381 | =item $txn = $fcp->txn_get_size ($uri) |
336 | |
382 | |
337 | =item $length = $fcp->get_size ($uri) |
383 | =item $length = $fcp->get_size ($uri) |
338 | |
384 | |
… | |
… | |
341 | |
387 | |
342 | UNTESTED. |
388 | UNTESTED. |
343 | |
389 | |
344 | =cut |
390 | =cut |
345 | |
391 | |
346 | _txn get_size => sub { |
392 | $txn->(get_size => sub { |
347 | my ($self, $uri) = @_; |
393 | my ($self, $uri) = @_; |
348 | |
394 | |
349 | $self->txn (get_size => URI => $uri); |
395 | $self->txn (get_size => URI => $uri); |
350 | }; |
396 | }); |
351 | |
397 | |
352 | =item $txn = $fcp->txn_client_get ($uri [, $htl = 15 [, $removelocal = 0]]) |
398 | =item $txn = $fcp->txn_client_get ($uri [, $htl = 15 [, $removelocal = 0]]) |
353 | |
399 | |
354 | =item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal) |
400 | =item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal) |
355 | |
401 | |
… | |
… | |
365 | ) |
411 | ) |
366 | }; |
412 | }; |
367 | |
413 | |
368 | =cut |
414 | =cut |
369 | |
415 | |
370 | _txn client_get => sub { |
416 | $txn->(client_get => sub { |
371 | my ($self, $uri, $htl, $removelocal) = @_; |
417 | my ($self, $uri, $htl, $removelocal) = @_; |
372 | |
418 | |
373 | $self->txn (client_get => URI => $uri, hops_to_live => ($htl || 15), remove_local_key => $removelocal ? "true" : "false"); |
419 | $self->txn (client_get => URI => $uri, hops_to_live => xeh (defined $htl ? $htl : 15), |
|
|
420 | remove_local_key => $removelocal ? "true" : "false"); |
374 | }; |
421 | }); |
375 | |
422 | |
|
|
423 | =item $txn = $fcp->txn_client_put ($uri, $metadata, $data, $htl, $removelocal) |
|
|
424 | |
|
|
425 | =item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal); |
|
|
426 | |
|
|
427 | Insert a new key. If the client is inserting a CHK, the URI may be |
|
|
428 | abbreviated as just CHK@. In this case, the node will calculate the |
|
|
429 | CHK. |
|
|
430 | |
|
|
431 | C<$meta> can be a reference or a string (ONLY THE STRING CASE IS IMPLEMENTED!). |
|
|
432 | |
|
|
433 | THIS INTERFACE IS UNTESTED AND SUBJECT TO CHANGE. |
|
|
434 | |
|
|
435 | =cut |
|
|
436 | |
|
|
437 | $txn->(client_put => sub { |
|
|
438 | my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; |
|
|
439 | |
|
|
440 | $self->txn (client_put => URI => $uri, xeh (defined $htl ? $htl : 15), |
|
|
441 | remove_local_key => $removelocal ? "true" : "false", |
|
|
442 | data => "$meta$data", metadata_length => xeh length $meta); |
|
|
443 | }); |
|
|
444 | |
|
|
445 | } # transactions |
|
|
446 | |
376 | =item MISSING: ClientPut |
447 | =item MISSING: (ClientPut), InsertKey |
377 | |
448 | |
378 | =back |
449 | =back |
379 | |
450 | |
380 | =head2 THE Net::FCP::Txn CLASS |
451 | =head2 THE Net::FCP::Txn CLASS |
381 | |
452 | |
382 | All requests (or transactions) are executed in a asynchroneous way (LIE: |
453 | All requests (or transactions) are executed in a asynchronous way. For |
383 | uploads are blocking). For each request, a C<Net::FCP::Txn> object is |
454 | each request, a C<Net::FCP::Txn> object is created (worse: a tcp |
384 | created (worse: a tcp connection is created, too). |
455 | connection is created, too). |
385 | |
456 | |
386 | For each request there is actually a different subclass (and it's possible |
457 | For each request there is actually a different subclass (and it's possible |
387 | to subclass these, although of course not documented). |
458 | to subclass these, although of course not documented). |
388 | |
459 | |
389 | The most interesting method is C<result>. |
460 | The most interesting method is C<result>. |
… | |
… | |
417 | while (my ($k, $v) = each %{$self->{attr}}) { |
488 | while (my ($k, $v) = each %{$self->{attr}}) { |
418 | $attr .= (Net::FCP::touc $k) . "=$v\012" |
489 | $attr .= (Net::FCP::touc $k) . "=$v\012" |
419 | } |
490 | } |
420 | |
491 | |
421 | if (defined $data) { |
492 | if (defined $data) { |
422 | $attr .= "DataLength=" . (length $data) . "\012"; |
493 | $attr .= sprintf "DataLength=%x\012", length $data; |
423 | $data = "Data\012$data"; |
494 | $data = "Data\012$data"; |
424 | } else { |
495 | } else { |
425 | $data = "EndMessage\012"; |
496 | $data = "EndMessage\012"; |
426 | } |
497 | } |
427 | |
498 | |
… | |
… | |
434 | and !$!{EINPROGRESS} |
505 | and !$!{EINPROGRESS} |
435 | and Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; |
506 | and Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; |
436 | |
507 | |
437 | $self->{sbuf} = |
508 | $self->{sbuf} = |
438 | "\x00\x00\x00\x02" |
509 | "\x00\x00\x00\x02" |
439 | . Net::FCP::touc $self->{type} |
510 | . (Net::FCP::touc $self->{type}) |
440 | . "\012$attr$data"; |
511 | . "\012$attr$data"; |
441 | |
512 | |
442 | #$fh->shutdown (1); # freenet buggy?, well, it's java... |
513 | #shutdown $fh, 1; # freenet buggy?, well, it's java... |
443 | |
514 | |
444 | $self->{fh} = $fh; |
515 | $self->{fh} = $fh; |
445 | |
516 | |
446 | $self->{w} = $EVENT->new_from_fh ($fh)->cb(sub { $self->fh_ready_w })->poll(0, 1, 1); |
517 | $self->{w} = $EVENT->new_from_fh ($fh)->cb(sub { $self->fh_ready_w })->poll(0, 1, 1); |
447 | |
518 | |
… | |
… | |
483 | |
554 | |
484 | sub userdata($$) { |
555 | sub userdata($$) { |
485 | my ($self, $data) = @_; |
556 | my ($self, $data) = @_; |
486 | $self->{userdata} = $data; |
557 | $self->{userdata} = $data; |
487 | $self; |
558 | $self; |
|
|
559 | } |
|
|
560 | |
|
|
561 | =item $txn->cancel (%attr) |
|
|
562 | |
|
|
563 | Cancels the operation with a C<cancel> exception anf the given attributes |
|
|
564 | (consider at least giving the attribute C<reason>). |
|
|
565 | |
|
|
566 | UNTESTED. |
|
|
567 | |
|
|
568 | =cut |
|
|
569 | |
|
|
570 | sub cancel { |
|
|
571 | my ($self, %attr) = @_; |
|
|
572 | $self->throw (Net::FCP::Exception->new (cancel => { %attr })); |
|
|
573 | $self->set_result; |
|
|
574 | $self->eof; |
488 | } |
575 | } |
489 | |
576 | |
490 | sub fh_ready_w { |
577 | sub fh_ready_w { |
491 | my ($self) = @_; |
578 | my ($self) = @_; |
492 | |
579 | |
… | |
… | |
532 | } else { |
619 | } else { |
533 | $self->eof; |
620 | $self->eof; |
534 | } |
621 | } |
535 | } |
622 | } |
536 | |
623 | |
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 { |
624 | sub rcv { |
546 | my ($self, $type, $attr) = @_; |
625 | my ($self, $type, $attr) = @_; |
547 | |
626 | |
548 | $type = Net::FCP::tolc $type; |
627 | $type = Net::FCP::tolc $type; |
549 | |
628 | |
… | |
… | |
567 | |
646 | |
568 | sub throw { |
647 | sub throw { |
569 | my ($self, $exc) = @_; |
648 | my ($self, $exc) = @_; |
570 | |
649 | |
571 | $self->{exception} = $exc; |
650 | $self->{exception} = $exc; |
572 | $self->set_result (1); |
651 | $self->set_result; |
573 | $self->eof; # must be last to avoid loops |
652 | $self->eof; # must be last to avoid loops |
574 | } |
653 | } |
575 | |
654 | |
576 | sub set_result { |
655 | sub set_result { |
577 | my ($self, $result) = @_; |
656 | my ($self, $result) = @_; |
… | |
… | |
589 | delete $self->{w}; |
668 | delete $self->{w}; |
590 | delete $self->{fh}; |
669 | delete $self->{fh}; |
591 | |
670 | |
592 | delete $self->{fcp}{txn}{$self}; |
671 | delete $self->{fcp}{txn}{$self}; |
593 | |
672 | |
594 | $self->set_result; # just in case |
673 | unless (exists $self->{result}) { |
|
|
674 | $self->throw (Net::FCP::Exception->new (short_data => { |
|
|
675 | reason => "unexpected eof or internal node error", |
|
|
676 | })); |
|
|
677 | } |
595 | } |
678 | } |
596 | |
679 | |
597 | sub progress { |
680 | sub progress { |
598 | my ($self, $type, $attr) = @_; |
681 | my ($self, $type, $attr) = @_; |
599 | $self->{fcp}->progress ($self, $type, $attr); |
682 | $self->{fcp}->progress ($self, $type, $attr); |
… | |
… | |
602 | =item $result = $txn->result |
685 | =item $result = $txn->result |
603 | |
686 | |
604 | Waits until a result is available and then returns it. |
687 | Waits until a result is available and then returns it. |
605 | |
688 | |
606 | This waiting is (depending on your event model) not very efficient, as it |
689 | This waiting is (depending on your event model) not very efficient, as it |
607 | is done outside the "mainloop". |
690 | is done outside the "mainloop". The biggest problem, however, is that it's |
|
|
691 | blocking one thread of execution. Try to use the callback mechanism, if |
|
|
692 | possible, and call result from within the callback (or after is has been |
|
|
693 | run), as then no waiting is necessary. |
608 | |
694 | |
609 | =cut |
695 | =cut |
610 | |
696 | |
611 | sub result { |
697 | sub result { |
612 | my ($self) = @_; |
698 | my ($self) = @_; |
… | |
… | |
643 | use base Net::FCP::Txn; |
729 | use base Net::FCP::Txn; |
644 | |
730 | |
645 | sub rcv_success { |
731 | sub rcv_success { |
646 | my ($self, $attr) = @_; |
732 | my ($self, $attr) = @_; |
647 | |
733 | |
648 | $self->set_result ($attr); |
734 | $self->set_result ($attr->{uri}); |
649 | } |
735 | } |
650 | |
736 | |
651 | package Net::FCP::Txn::GenerateSVKPair; |
737 | package Net::FCP::Txn::GenerateSVKPair; |
652 | |
738 | |
653 | use base Net::FCP::Txn; |
739 | use base Net::FCP::Txn; |
654 | |
740 | |
655 | sub rcv_success { |
741 | sub rcv_success { |
656 | my ($self, $attr) = @_; |
742 | my ($self, $attr) = @_; |
657 | |
|
|
658 | $self->set_result ([$attr->{PublicKey}, $attr->{PrivateKey}]); |
743 | $self->set_result ([$attr->{PublicKey}, $attr->{PrivateKey}]); |
659 | } |
744 | } |
660 | |
745 | |
661 | package Net::FCP::Txn::InvertPrivateKey; |
746 | package Net::FCP::Txn::InsertPrivateKey; |
662 | |
747 | |
663 | use base Net::FCP::Txn; |
748 | use base Net::FCP::Txn; |
664 | |
749 | |
665 | sub rcv_success { |
750 | sub rcv_success { |
666 | my ($self, $attr) = @_; |
751 | my ($self, $attr) = @_; |
667 | |
|
|
668 | $self->set_result ($attr->{PublicKey}); |
752 | $self->set_result ($attr->{PublicKey}); |
669 | } |
753 | } |
670 | |
754 | |
671 | package Net::FCP::Txn::GetSize; |
755 | package Net::FCP::Txn::GetSize; |
672 | |
756 | |
673 | use base Net::FCP::Txn; |
757 | use base Net::FCP::Txn; |
674 | |
758 | |
675 | sub rcv_success { |
759 | sub rcv_success { |
676 | my ($self, $attr) = @_; |
760 | my ($self, $attr) = @_; |
677 | |
|
|
678 | $self->set_result ($attr->{Length}); |
761 | $self->set_result (hex $attr->{Length}); |
679 | } |
762 | } |
680 | |
763 | |
681 | package Net::FCP::Txn::GetPut; |
764 | package Net::FCP::Txn::GetPut; |
682 | |
765 | |
683 | # base class for get and put |
766 | # base class for get and put |
… | |
… | |
701 | |
784 | |
702 | use base Net::FCP::Txn::GetPut; |
785 | use base Net::FCP::Txn::GetPut; |
703 | |
786 | |
704 | *rcv_data_not_found = \&Net::FCP::Txn::rcv_throw_exception; |
787 | *rcv_data_not_found = \&Net::FCP::Txn::rcv_throw_exception; |
705 | |
788 | |
706 | sub rcv_data_found { |
789 | 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) = @_; |
790 | my ($self, $chunk) = @_; |
|
|
791 | |
|
|
792 | $self->{data} .= $chunk; |
|
|
793 | |
|
|
794 | $self->progress ("data", { chunk => length $chunk, received => length $self->{data}, total => $self->{datalength} }); |
717 | |
795 | |
718 | if ($self->{datalength} == length $self->{data}) { |
796 | if ($self->{datalength} == length $self->{data}) { |
719 | my $data = delete $self->{data}; |
797 | my $data = delete $self->{data}; |
720 | my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; |
798 | my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; |
721 | |
799 | |
722 | $self->set_result ([$meta, $data]); |
800 | $self->set_result ([$meta, $data]); |
723 | } elsif (!exists $self->{result}) { |
801 | $self->eof; |
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 | } |
802 | } |
|
|
803 | } |
|
|
804 | |
|
|
805 | sub rcv_data_found { |
|
|
806 | my ($self, $attr, $type) = @_; |
|
|
807 | |
|
|
808 | $self->progress ($type, $attr); |
|
|
809 | |
|
|
810 | $self->{datalength} = hex $attr->{data_length}; |
|
|
811 | $self->{metalength} = hex $attr->{metadata_length}; |
730 | } |
812 | } |
731 | |
813 | |
732 | package Net::FCP::Txn::ClientPut; |
814 | package Net::FCP::Txn::ClientPut; |
733 | |
815 | |
734 | use base Net::FCP::Txn::GetPut; |
816 | use base Net::FCP::Txn::GetPut; |
… | |
… | |
744 | sub rcv_success { |
826 | sub rcv_success { |
745 | my ($self, $attr, $type) = @_; |
827 | my ($self, $attr, $type) = @_; |
746 | $self->set_result ($attr); |
828 | $self->set_result ($attr); |
747 | } |
829 | } |
748 | |
830 | |
|
|
831 | =back |
|
|
832 | |
|
|
833 | =head2 The Net::FCP::Exception CLASS |
|
|
834 | |
|
|
835 | Any unexpected (non-standard) responses that make it impossible to return |
|
|
836 | the advertised result will result in an exception being thrown when the |
|
|
837 | C<result> method is called. |
|
|
838 | |
|
|
839 | These exceptions are represented by objects of this class. |
|
|
840 | |
|
|
841 | =over 4 |
|
|
842 | |
|
|
843 | =cut |
|
|
844 | |
749 | package Net::FCP::Exception; |
845 | package Net::FCP::Exception; |
750 | |
846 | |
751 | use overload |
847 | use overload |
752 | '""' => sub { |
848 | '""' => sub { |
753 | "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>\n"; |
849 | "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>"; |
754 | }; |
850 | }; |
|
|
851 | |
|
|
852 | =item $exc = new Net::FCP::Exception $type, \%attr |
|
|
853 | |
|
|
854 | Create a new exception object of the given type (a string like |
|
|
855 | C<route_not_found>), and a hashref containing additional attributes |
|
|
856 | (usually the attributes of the message causing the exception). |
|
|
857 | |
|
|
858 | =cut |
755 | |
859 | |
756 | sub new { |
860 | sub new { |
757 | my ($class, $type, $attr) = @_; |
861 | my ($class, $type, $attr) = @_; |
758 | |
862 | |
759 | bless [Net::FCP::tolc $type, { %$attr }], $class; |
863 | bless [Net::FCP::tolc $type, { %$attr }], $class; |
760 | } |
864 | } |
761 | |
865 | |
|
|
866 | =item $exc->type([$type]) |
|
|
867 | |
|
|
868 | With no arguments, returns the exception type. Otherwise a boolean |
|
|
869 | indicating wether the exception is of the given type is returned. |
|
|
870 | |
|
|
871 | =cut |
|
|
872 | |
|
|
873 | sub type { |
|
|
874 | my ($self, $type) = @_; |
|
|
875 | |
|
|
876 | @_ >= 2 |
|
|
877 | ? $self->[0] eq $type |
|
|
878 | : $self->[0]; |
|
|
879 | } |
|
|
880 | |
|
|
881 | =item $exc->attr([$attr]) |
|
|
882 | |
|
|
883 | With no arguments, returns the attributes. Otherwise the named attribute |
|
|
884 | value is returned. |
|
|
885 | |
|
|
886 | =cut |
|
|
887 | |
|
|
888 | sub attr { |
|
|
889 | my ($self, $attr) = @_; |
|
|
890 | |
|
|
891 | @_ >= 2 |
|
|
892 | ? $self->[1]{$attr} |
|
|
893 | : $self->[1]; |
|
|
894 | } |
|
|
895 | |
762 | =back |
896 | =back |
763 | |
897 | |
764 | =head1 SEE ALSO |
898 | =head1 SEE ALSO |
765 | |
899 | |
766 | L<http://freenet.sf.net>. |
900 | L<http://freenet.sf.net>. |
… | |
… | |
772 | Marc Lehmann <pcg@goof.com> |
906 | Marc Lehmann <pcg@goof.com> |
773 | http://www.goof.com/pcg/marc/ |
907 | http://www.goof.com/pcg/marc/ |
774 | |
908 | |
775 | =cut |
909 | =cut |
776 | |
910 | |
|
|
911 | package Net::FCP::Event::Auto; |
|
|
912 | |
|
|
913 | my @models = ( |
|
|
914 | [Coro => Coro::Event:: ], |
|
|
915 | [Event => Event::], |
|
|
916 | [Glib => Glib:: ], |
|
|
917 | [Tk => Tk::], |
|
|
918 | ); |
|
|
919 | |
|
|
920 | sub AUTOLOAD { |
|
|
921 | $AUTOLOAD =~ s/.*://; |
|
|
922 | |
|
|
923 | for (@models) { |
|
|
924 | my ($model, $package) = @$_; |
|
|
925 | if (defined ${"$package\::VERSION"}) { |
|
|
926 | $EVENT = "Net::FCP::Event::$model"; |
|
|
927 | eval "require $EVENT"; die if $@; |
|
|
928 | goto &{"$EVENT\::$AUTOLOAD"}; |
|
|
929 | } |
|
|
930 | } |
|
|
931 | |
|
|
932 | for (@models) { |
|
|
933 | my ($model, $package) = @$_; |
|
|
934 | $EVENT = "Net::FCP::Event::$model"; |
|
|
935 | if (eval "require $EVENT") { |
|
|
936 | goto &{"$EVENT\::$AUTOLOAD"}; |
|
|
937 | } |
|
|
938 | } |
|
|
939 | |
|
|
940 | die "No event module selected for Net::FCP and autodetect failed. Install any of these: Coro, Event, Glib or Tk."; |
|
|
941 | } |
|
|
942 | |
777 | 1; |
943 | 1; |
778 | |
944 | |