ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-FCP/FCP.pm
(Generate patch)

Comparing cvsroot/Net-FCP/FCP.pm (file contents):
Revision 1.25 by root, Tue Dec 9 06:54:16 2003 UTC vs.
Revision 1.29 by root, Thu May 13 21:43:16 2004 UTC

99 $_; 99 $_;
100} 100}
101 101
102sub tolc($) { 102sub tolc($) {
103 local $_ = shift; 103 local $_ = shift;
104 1 while s/(SVK|CHK|URI)([^_])/$1\_$2/i;
105 1 while s/([^_])(SVK|CHK|URI)/$1\_$2/i;
104 s/(?<=[a-z])(?=[A-Z])/_/g; 106 s/(?<=[a-z])(?=[A-Z])/_/g;
105 lc $_; 107 lc $_;
106} 108}
107 109
108# the opposite of hex 110# the opposite of hex
180 #$meta->{tail} = substr $data, pos $data; 182 #$meta->{tail} = substr $data, pos $data;
181 183
182 $meta; 184 $meta;
183} 185}
184 186
187=item $string = Net::FCP::build_metadata $meta
188
189Takes a hash reference as returned by C<Net::FCP::parse_metadata> and
190returns the corresponding string form. If a string is given, it's returned
191as is.
192
193=cut
194
195sub build_metadata_subhash($$$) {
196 my ($prefix, $level, $hash) = @_;
197
198 join "",
199 map
200 ref $hash->{$_} ? build_metadata_subhash ($prefix . (Net::FCP::touc $_) . ".", $level + 1, $hash->{$_})
201 : $prefix . ($level > 1 ? $_ : Net::FCP::touc $_) . "=" . $hash->{$_} . "\n",
202 keys %$hash;
203}
204
205sub build_metadata_hash($$) {
206 my ($header, $hash) = @_;
207
208 if (ref $hash eq ARRAY::) {
209 join "", map build_metadata_hash ($header, $_), @$hash
210 } else {
211 (Net::FCP::touc $header) . "\n"
212 . (build_metadata_subhash "", 0, $hash)
213 . "EndPart\n";
214 }
215}
216
217sub build_metadata($) {
218 my ($meta) = @_;
219
220 return $meta unless ref $meta;
221
222 $meta = { %$meta };
223
224 delete $meta->{raw};
225
226 my $res =
227 (build_metadata_hash version => delete $meta->{version})
228 . (join "", map +(build_metadata_hash $_, $meta->{$_}), keys %$meta);
229
230 substr $res, 0, -5; # get rid of "Part". Broken Syntax....
231}
232
233
185=item $fcp = new Net::FCP [host => $host][, port => $port] 234=item $fcp = new Net::FCP [host => $host][, port => $port][, progress => \&cb]
186 235
187Create a new virtual FCP connection to the given host and port (default 236Create a new virtual FCP connection to the given host and port (default
188127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>). 237127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>).
189 238
190Connections are virtual because no persistent physical connection is 239Connections are virtual because no persistent physical connection is
191established. 240established.
241
242You can install a progress callback that is being called with the Net::FCP
243object, a txn object, the type of the transaction and the attributes. Use
244it like this:
245
246 sub progress_cb {
247 my ($self, $txn, $type, $attr) = @_;
248
249 warn "progress<$txn,$type," . (join ":", %$attr) . ">\n";
250 }
192 251
193=begin comment 252=begin comment
194 253
195However, the existance of the node is checked by executing a 254However, the existance of the node is checked by executing a
196C<ClientHello> transaction. 255C<ClientHello> transaction.
212 $self; 271 $self;
213} 272}
214 273
215sub progress { 274sub progress {
216 my ($self, $txn, $type, $attr) = @_; 275 my ($self, $txn, $type, $attr) = @_;
217 #warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; 276
277 $self->{progress}->($self, $txn, $type, $attr)
278 if $self->{progress};
218} 279}
219 280
220=item $txn = $fcp->txn(type => attr => val,...) 281=item $txn = $fcp->txn(type => attr => val,...)
221 282
222The low-level interface to transactions. Don't use it. 283The low-level interface to transactions. Don't use it.
248sub txn { 309sub txn {
249 my ($self, $type, %attr) = @_; 310 my ($self, $type, %attr) = @_;
250 311
251 $type = touc $type; 312 $type = touc $type;
252 313
253 my $txn = "Net::FCP::Txn::$type"->new(fcp => $self, type => tolc $type, attr => \%attr); 314 my $txn = "Net::FCP::Txn::$type"->new (fcp => $self, type => tolc $type, attr => \%attr);
254 315
255 $txn; 316 $txn;
256} 317}
257 318
258{ # transactions 319{ # transactions
323 384
324=item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher]) 385=item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher])
325 386
326=item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) 387=item $uri = $fcp->generate_chk ($metadata, $data[, $cipher])
327 388
328Calculcates a CHK, given the metadata and data. C<$cipher> is either 389Calculates a CHK, given the metadata and data. C<$cipher> is either
329C<Rijndael> or C<Twofish>, with the latter being the default. 390C<Rijndael> or C<Twofish>, with the latter being the default.
330 391
331=cut 392=cut
332 393
333$txn->(generate_chk => sub { 394$txn->(generate_chk => sub {
341 402
342=item $txn = $fcp->txn_generate_svk_pair 403=item $txn = $fcp->txn_generate_svk_pair
343 404
344=item ($public, $private) = @{ $fcp->generate_svk_pair } 405=item ($public, $private) = @{ $fcp->generate_svk_pair }
345 406
346Creates a new SVK pair. Returns an arrayref. 407Creates a new SVK pair. Returns an arrayref with the public key, the
408private key and a crypto key, which is just additional entropy.
347 409
348 [ 410 [
349 "hKs0-WDQA4pVZyMPKNFsK1zapWY", 411 "acLx4dux9fvvABH15Gk6~d3I-yw",
350 "ZnmvMITaTXBMFGl4~jrjuyWxOWg" 412 "cPoDkDMXDGSMM32plaPZDhJDxSs",
413 "BH7LXCov0w51-y9i~BoB3g",
351 ] 414 ]
415
416A private key (for inserting) can be constructed like this:
417
418 SSK@<private_key>,<crypto_key>/<name>
419
420It can be used to insert data. The corresponding public key looks like this:
421
422 SSK@<public_key>PAgM,<crypto_key>/<name>
423
424Watch out for the C<PAgM>-part!
352 425
353=cut 426=cut
354 427
355$txn->(generate_svk_pair => sub { 428$txn->(generate_svk_pair => sub {
356 my ($self) = @_; 429 my ($self) = @_;
357 430
358 $self->txn ("generate_svk_pair"); 431 $self->txn ("generate_svk_pair");
359}); 432});
360 433
361=item $txn = $fcp->txn_insert_private_key ($private) 434=item $txn = $fcp->txn_invert_private_key ($private)
362 435
363=item $public = $fcp->insert_private_key ($private) 436=item $public = $fcp->invert_private_key ($private)
364 437
365Inserts a private key. $private can be either an insert URI (must start 438Inverts a private key (returns the public key). C<$private> can be either
366with C<freenet:SSK@>) or a raw private key (i.e. the private value you get 439an insert URI (must start with C<freenet:SSK@>) or a raw private key (i.e.
367back from C<generate_svk_pair>). 440the private value you get back from C<generate_svk_pair>).
368 441
369Returns the public key. 442Returns the public key.
370 443
371UNTESTED.
372
373=cut 444=cut
374 445
375$txn->(insert_private_key => sub { 446$txn->(invert_private_key => sub {
376 my ($self, $privkey) = @_; 447 my ($self, $privkey) = @_;
377 448
378 $self->txn (invert_private_key => private => $privkey); 449 $self->txn (invert_private_key => private => $privkey);
379}); 450});
380 451
382 453
383=item $length = $fcp->get_size ($uri) 454=item $length = $fcp->get_size ($uri)
384 455
385Finds and returns the size (rounded up to the nearest power of two) of the 456Finds and returns the size (rounded up to the nearest power of two) of the
386given document. 457given document.
387
388UNTESTED.
389 458
390=cut 459=cut
391 460
392$txn->(get_size => sub { 461$txn->(get_size => sub {
393 my ($self, $uri) = @_; 462 my ($self, $uri) = @_;
400=item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal) 469=item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal)
401 470
402Fetches a (small, as it should fit into memory) file from 471Fetches a (small, as it should fit into memory) file from
403freenet. C<$meta> is the metadata (as returned by C<parse_metadata> or 472freenet. C<$meta> is the metadata (as returned by C<parse_metadata> or
404C<undef>). 473C<undef>).
474
475The C<$uri> should begin with C<freenet:>, but the scheme is currently
476added, if missing.
405 477
406Due to the overhead, a better method to download big files should be used. 478Due to the overhead, a better method to download big files should be used.
407 479
408 my ($meta, $data) = @{ 480 my ($meta, $data) = @{
409 $fcp->client_get ( 481 $fcp->client_get (
414=cut 486=cut
415 487
416$txn->(client_get => sub { 488$txn->(client_get => sub {
417 my ($self, $uri, $htl, $removelocal) = @_; 489 my ($self, $uri, $htl, $removelocal) = @_;
418 490
491 $uri =~ s/^freenet://;
492 $uri = "freenet:$uri";
493
419 $self->txn (client_get => URI => $uri, hops_to_live => xeh (defined $htl ? $htl : 15), 494 $self->txn (client_get => URI => $uri, hops_to_live => xeh (defined $htl ? $htl : 15),
420 remove_local_key => $removelocal ? "true" : "false"); 495 remove_local_key => $removelocal ? "true" : "false");
421}); 496});
422 497
423=item $txn = $fcp->txn_client_put ($uri, $metadata, $data, $htl, $removelocal) 498=item $txn = $fcp->txn_client_put ($uri, $metadata, $data, $htl, $removelocal)
424 499
425=item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal); 500=item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal);
426 501
427Insert a new key. If the client is inserting a CHK, the URI may be 502Insert a new key. If the client is inserting a CHK, the URI may be
428abbreviated as just CHK@. In this case, the node will calculate the 503abbreviated as just CHK@. In this case, the node will calculate the
429CHK. 504CHK. If the key is a private SSK key, the node will calculcate the public
505key and the resulting public URI.
430 506
431C<$meta> can be a reference or a string (ONLY THE STRING CASE IS IMPLEMENTED!). 507C<$meta> can be a hash reference (same format as returned by
508C<Net::FCP::parse_metadata>) or a string.
432 509
433THIS INTERFACE IS UNTESTED AND SUBJECT TO CHANGE. 510The result is an arrayref with the keys C<uri>, C<public_key> and C<private_key>.
434 511
435=cut 512=cut
436 513
437$txn->(client_put => sub { 514$txn->(client_put => sub {
438 my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; 515 my ($self, $uri, $meta, $data, $htl, $removelocal) = @_;
439 516
440 $self->txn (client_put => URI => $uri, xeh (defined $htl ? $htl : 15), 517 $meta = build_metadata $meta;
518
519 $self->txn (client_put => URI => $uri,
520 hops_to_live => xeh (defined $htl ? $htl : 15),
441 remove_local_key => $removelocal ? "true" : "false", 521 remove_local_key => $removelocal ? "true" : "false",
442 data => "$meta$data", metadata_length => xeh length $meta); 522 data => "$meta$data", metadata_length => xeh length $meta);
443}); 523});
444 524
445} # transactions 525} # transactions
446
447=item MISSING: (ClientPut), InsertKey
448 526
449=back 527=back
450 528
451=head2 THE Net::FCP::Txn CLASS 529=head2 THE Net::FCP::Txn CLASS
452 530
677 } 755 }
678} 756}
679 757
680sub progress { 758sub progress {
681 my ($self, $type, $attr) = @_; 759 my ($self, $type, $attr) = @_;
760
682 $self->{fcp}->progress ($self, $type, $attr); 761 $self->{fcp}->progress ($self, $type, $attr);
683} 762}
684 763
685=item $result = $txn->result 764=item $result = $txn->result
686 765
738 817
739use base Net::FCP::Txn; 818use base Net::FCP::Txn;
740 819
741sub rcv_success { 820sub rcv_success {
742 my ($self, $attr) = @_; 821 my ($self, $attr) = @_;
743 $self->set_result ([$attr->{PublicKey}, $attr->{PrivateKey}]); 822 $self->set_result ([$attr->{public_key}, $attr->{private_key}, $attr->{crypto_key}]);
744} 823}
745 824
746package Net::FCP::Txn::InsertPrivateKey; 825package Net::FCP::Txn::InvertPrivateKey;
747 826
748use base Net::FCP::Txn; 827use base Net::FCP::Txn;
749 828
750sub rcv_success { 829sub rcv_success {
751 my ($self, $attr) = @_; 830 my ($self, $attr) = @_;
752 $self->set_result ($attr->{PublicKey}); 831 $self->set_result ($attr->{public_key});
753} 832}
754 833
755package Net::FCP::Txn::GetSize; 834package Net::FCP::Txn::GetSize;
756 835
757use base Net::FCP::Txn; 836use base Net::FCP::Txn;
758 837
759sub rcv_success { 838sub rcv_success {
760 my ($self, $attr) = @_; 839 my ($self, $attr) = @_;
761 $self->set_result (hex $attr->{Length}); 840 $self->set_result (hex $attr->{length});
762} 841}
763 842
764package Net::FCP::Txn::GetPut; 843package Net::FCP::Txn::GetPut;
765 844
766# base class for get and put 845# base class for get and put
767 846
768use base Net::FCP::Txn; 847use base Net::FCP::Txn;
769 848
770*rcv_urierror = \&Net::FCP::Txn::rcv_throw_exception; 849*rcv_uri_error = \&Net::FCP::Txn::rcv_throw_exception;
771*rcv_route_not_found = \&Net::FCP::Txn::rcv_throw_exception; 850*rcv_route_not_found = \&Net::FCP::Txn::rcv_throw_exception;
772 851
773sub rcv_restarted { 852sub rcv_restarted {
774 my ($self, $attr, $type) = @_; 853 my ($self, $attr, $type) = @_;
775 854
776 delete $self->{datalength}; 855 delete $self->{datalength};
909=cut 988=cut
910 989
911package Net::FCP::Event::Auto; 990package Net::FCP::Event::Auto;
912 991
913my @models = ( 992my @models = (
914 [Coro => Coro::Event:: ], 993 [Coro => Coro::Event::],
915 [Event => Event::], 994 [Event => Event::],
916 [Glib => Glib:: ], 995 [Glib => Glib::],
917 [Tk => Tk::], 996 [Tk => Tk::],
918); 997);
919 998
920sub AUTOLOAD { 999sub AUTOLOAD {
921 $AUTOLOAD =~ s/.*://; 1000 $AUTOLOAD =~ s/.*://;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines