Permalink
Browse files

Renamed to Net::FCP::Tiny and added the module

  • Loading branch information...
1 parent c741417 commit 0cac7f6aa4daf15ac9e68bc3475ea994bc3c707f @avar committed Jun 18, 2010
Showing with 111 additions and 8 deletions.
  1. +2 −2 Changes
  2. +2 −6 dist.ini
  3. +107 −0 lib/Net/FCP/Tiny.pm
View
@@ -1,5 +1,5 @@
-Revision history for FCP::Tiny
+Revision history for Net::FCP::Tiny
{{$NEXT}}
- -
+ - Initial release
View
@@ -1,11 +1,7 @@
-name = FCP-Tiny
+name = Net-FCP-Tiny
author = Ævar Arnfjörð Bjarmason <avar@cpan.org>
copyright_holder = Ævar Arnfjörð Bjarmason
license = Perl_5
[@AVAR]
-dist = FCP-Tiny
-bugtracker = rt
-;no_AutoPrereq = 1
-
-;[Prereq]
+dist = Net-FCP-Tiny
View
@@ -0,0 +1,107 @@
+package Net::FCP::Tiny;
+use strict;
+use warnings;
+use IO::Socket::INET;
+use Exporter 'import';
+
+sub new {
+ my ($pkg, %args) = @_;
+
+ $args{name} ||= __PACKAGE__;
+ $args{host} ||= 'localhost';
+ $args{port} ||= 9481;
+
+ $args{sock} ||= IO::Socket::INET->new(
+ PeerAddr => $args{host},
+ PeerPort => $args{port},
+ Proto => 'tcp',
+ Blocking => 1,
+ );
+
+ my $self = bless \%args => $pkg;
+
+ my $helo = $self->send_msg(<<'END');
+ClientHello
+Name=$args{$name};
+ExpectedVersion=2.0
+EndMessage
+END
+
+ return $self;
+}
+
+sub send_msg {
+ my ($self, $msg) = @_;
+ my $sock = $self->{sock};
+ my @line;
+
+ print $sock $msg;
+
+ while (my $line = <$sock>) {
+ chomp $line;
+ last if $line eq 'EndMessage';
+ push @line => $line;
+ }
+
+ return \@line;
+}
+
+sub array2hash {
+ my ($self, $lines) = @_;
+ return +{
+ map {
+ my ($k, $v) = split(/=/, $_, 2);
+ $k => $v;
+ } @$lines
+ };
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Net:FCP::Tiny - A Tiny and incomplete interface to the Freenet Client Protocol (FCPv2)
+
+=head1 SYNOPSIS
+
+ use FPC::Tiny;
+
+ my $fcp = Net::FCP::Tiny->new(
+ name => 'Freenet Munin Plugin',
+ host => $ENV{FREENET_HOST},
+ port => $ENV{FREENET_PORT},
+ );
+
+ my $info = $fcp->array2hash($fcp->send_msg(<<'END'));
+GetNode
+WithPrivate=false
+WithVolatile=true
+EndMessage
+END
+
+ print "Java is using ", $info->{"volatile.usedJavaMemory"}, " bytes of memory";
+
+=head1 DESCRIPTION
+
+This is a tiny (~60 line) and stupid wrapper that talks the Freenet
+Client Protocol. It sets up a communication channel for you, and you
+can L<send messages|http://new-wiki.freenetproject.org/FCPv2> by
+copy/pasting examples from the FCPv2 docs.
+
+I wrote it for a munin plugin because L<Net::FCP> was ancient, and
+L<AnyEvent::FCP> hurt my brain.
+
+=head1 AUTHOR
+
+E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org>
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright 2010 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org>
+
+This program is free software, you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut

0 comments on commit 0cac7f6

Please sign in to comment.