Categories
technology

p2(very small)p

This is getting ridiculous. Following my recent post about Ed Shelton’s P2P program in 15 lines of Perl there’s been a P2P app done in 9 lines of Perl and now (wait for it) a full peer-2-peer application in 6 lines of Ruby with a 3 lines of comments.
Just to show everybody how nuts this has become the code is reproduced below…
# Server: ruby p2p.rb password server server-uri merge-servers
# Sample: ruby p2p.rb foobar server druby://localhost:1337 druby://foo.bar:1337
# Client: ruby p2p.rb password client server-uri download-pattern
# Sample: ruby p2p.rb foobar client druby://localhost:1337 *.rb
require'drb';F,D,C,P,M,U,*O=File,Class,Dir,*ARGV;def s(p)F.split(p[/[^|].*/])[-1
]end;def c(u);DRbObject.new((),u)end;def x(u)[P,u].hash;end;M=="client"&&c(U).f(
x(U)).each{|n|p,c=x(n),c(n);(c.f(p,O[0],0).map{|f|s f}-D["*"]).each{|f|F.open(f,
"w"){|o|o<<c.f(p,f,1)}}}||(DRb.start_service U,C.new{def f(c,a=[],t=2)c==x(U)&&(
t==0&&D[s(a)]||t==1&&F.read(s(a))||p(a))end;def y()(p(U)+p).each{|u|c(u).f(x(u),
p(U))rescue()};self;end;private;def p(x=[]);O.push(*x).uniq!;O;end}.new.y;sleep)

I think I’ve had more than enough of this. Pick a suitably high-level language, use single character variable names and some whacky formatting to exchange a file over a socket and call it P2P. Next someone is gonna write a java programme using SUN’s JXTA that just inits a class or two, format it all on about 4 lines and say, wow it’s the shortest P2P app ever… More interesting would be a P2P application written in a declarative language like Prolog or a functional language like Haskell or Hope. Haven’t done much Haskell programming in a while (damn rusty and for some reason don’t feel like breaking out the books) but prolog looks tempting. Expect a post. I may have to use some file IO/socket programming but it sounds like an interesting project. I’ll let readers know how I get on 😉

2 replies on “p2(very small)p”

Note that this is not only exchanging a file over a socket. It runs different server nodes which know of each other and which are protected by a password. This means that you can connect to the whole network by connecting to a single node.

What this will not do:
– Forward the file over multiple peer nodes. (Like Freenet does)
– Cut files into handy parts so that different parts can be downloaded from multiple nodes in parallel.

While I agree that those features are very useful in P2P file sharing networks it is not inherent to them. Also note that the original Python solution did not do them either and that it was both longer and less secure than the Perl and Ruby approaches.

Oh, and there’s more to the art of Golf than single letter variable names and stripping of whitespace.

I’m not sure if that JXTA thingy is part of Java’s standard library (DRb is part of Ruby’s standard library and XML-RPC is part of Python’s standard library), but I would still be interested in seeing the Java solution in four lines. Seeing this implemented in truely functional languages would also be interesting. Have fun!

Oh, and it looks like you did not use HTML entities for representing the less than and greater than signs in the code in the posting which causes some of it to be interpreted as HTML.

I’ve escaped the necessary characters. I normally run code files through my xslt to escape them but I must have just forgotten this time. Very remiss and I apologise. I posted in a hurry and was reacting to the “me too” natures of several of these smallest P2P apps ever posts. I’ve read through your source and I think that it’s cleverly constructed and then optimised to occupy the smallest character footprint, beyond just single character variable names and whacky formatting. JXTA isn’t a standard Java library so it’s not comparing like with like. However, I could construct a P2P program using JXTA that’s very short indeed as it would pull-in a fully-fledged P2P comms library. However, I’ve still had more than enough of these super small P2P applications regardless of the elegance of the solution or the level of features involved. I’m not sure that it proves anything…

Leave a Reply

Your email address will not be published. Required fields are marked *