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.28 by root, Thu May 13 16:13:42 2004 UTC vs.
Revision 1.29 by root, Thu May 13 21:43:16 2004 UTC

182 #$meta->{tail} = substr $data, pos $data; 182 #$meta->{tail} = substr $data, pos $data;
183 183
184 $meta; 184 $meta;
185} 185}
186 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
187=item $fcp = new Net::FCP [host => $host][, port => $port][, progress => \&cb] 234=item $fcp = new Net::FCP [host => $host][, port => $port][, progress => \&cb]
188 235
189Create 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
190127.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>).
191 238
262sub txn { 309sub txn {
263 my ($self, $type, %attr) = @_; 310 my ($self, $type, %attr) = @_;
264 311
265 $type = touc $type; 312 $type = touc $type;
266 313
267 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);
268 315
269 $txn; 316 $txn;
270} 317}
271 318
272{ # transactions 319{ # transactions
355 402
356=item $txn = $fcp->txn_generate_svk_pair 403=item $txn = $fcp->txn_generate_svk_pair
357 404
358=item ($public, $private) = @{ $fcp->generate_svk_pair } 405=item ($public, $private) = @{ $fcp->generate_svk_pair }
359 406
360Creates 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.
361 409
362 [ 410 [
363 "hKs0-WDQA4pVZyMPKNFsK1zapWY", 411 "acLx4dux9fvvABH15Gk6~d3I-yw",
364 "ZnmvMITaTXBMFGl4~jrjuyWxOWg" 412 "cPoDkDMXDGSMM32plaPZDhJDxSs",
413 "BH7LXCov0w51-y9i~BoB3g",
365 ] 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!
366 425
367=cut 426=cut
368 427
369$txn->(generate_svk_pair => sub { 428$txn->(generate_svk_pair => sub {
370 my ($self) = @_; 429 my ($self) = @_;
371 430
372 $self->txn ("generate_svk_pair"); 431 $self->txn ("generate_svk_pair");
373}); 432});
374 433
375=item $txn = $fcp->txn_insert_private_key ($private) 434=item $txn = $fcp->txn_invert_private_key ($private)
376 435
377=item $public = $fcp->insert_private_key ($private) 436=item $public = $fcp->invert_private_key ($private)
378 437
379Inserts 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
380with 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.
381back from C<generate_svk_pair>). 440the private value you get back from C<generate_svk_pair>).
382 441
383Returns the public key. 442Returns the public key.
384 443
385UNTESTED.
386
387=cut 444=cut
388 445
389$txn->(insert_private_key => sub { 446$txn->(invert_private_key => sub {
390 my ($self, $privkey) = @_; 447 my ($self, $privkey) = @_;
391 448
392 $self->txn (invert_private_key => private => $privkey); 449 $self->txn (invert_private_key => private => $privkey);
393}); 450});
394 451
396 453
397=item $length = $fcp->get_size ($uri) 454=item $length = $fcp->get_size ($uri)
398 455
399Finds 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
400given document. 457given document.
401
402UNTESTED.
403 458
404=cut 459=cut
405 460
406$txn->(get_size => sub { 461$txn->(get_size => sub {
407 my ($self, $uri) = @_; 462 my ($self, $uri) = @_;
444 499
445=item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal); 500=item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal);
446 501
447Insert 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
448abbreviated as just CHK@. In this case, the node will calculate the 503abbreviated as just CHK@. In this case, the node will calculate the
449CHK. 504CHK. If the key is a private SSK key, the node will calculcate the public
505key and the resulting public URI.
450 506
451C<$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.
452 509
453THIS INTERFACE IS UNTESTED AND SUBJECT TO CHANGE. 510The result is an arrayref with the keys C<uri>, C<public_key> and C<private_key>.
454 511
455=cut 512=cut
456 513
457$txn->(client_put => sub { 514$txn->(client_put => sub {
458 my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; 515 my ($self, $uri, $meta, $data, $htl, $removelocal) = @_;
459 516
460 $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),
461 remove_local_key => $removelocal ? "true" : "false", 521 remove_local_key => $removelocal ? "true" : "false",
462 data => "$meta$data", metadata_length => xeh length $meta); 522 data => "$meta$data", metadata_length => xeh length $meta);
463}); 523});
464 524
465} # transactions 525} # transactions
466
467=item MISSING: (ClientPut), InsertKey
468 526
469=back 527=back
470 528
471=head2 THE Net::FCP::Txn CLASS 529=head2 THE Net::FCP::Txn CLASS
472 530
759 817
760use base Net::FCP::Txn; 818use base Net::FCP::Txn;
761 819
762sub rcv_success { 820sub rcv_success {
763 my ($self, $attr) = @_; 821 my ($self, $attr) = @_;
764 $self->set_result ([$attr->{PublicKey}, $attr->{PrivateKey}]); 822 $self->set_result ([$attr->{public_key}, $attr->{private_key}, $attr->{crypto_key}]);
765} 823}
766 824
767package Net::FCP::Txn::InsertPrivateKey; 825package Net::FCP::Txn::InvertPrivateKey;
768 826
769use base Net::FCP::Txn; 827use base Net::FCP::Txn;
770 828
771sub rcv_success { 829sub rcv_success {
772 my ($self, $attr) = @_; 830 my ($self, $attr) = @_;
773 $self->set_result ($attr->{PublicKey}); 831 $self->set_result ($attr->{public_key});
774} 832}
775 833
776package Net::FCP::Txn::GetSize; 834package Net::FCP::Txn::GetSize;
777 835
778use base Net::FCP::Txn; 836use base Net::FCP::Txn;
779 837
780sub rcv_success { 838sub rcv_success {
781 my ($self, $attr) = @_; 839 my ($self, $attr) = @_;
782 $self->set_result (hex $attr->{Length}); 840 $self->set_result (hex $attr->{length});
783} 841}
784 842
785package Net::FCP::Txn::GetPut; 843package Net::FCP::Txn::GetPut;
786 844
787# base class for get and put 845# base class for get and put

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines