hoodwink.d enhanced


Revisiting the File-Sharing Paragraph #

by why in bits

RedHanded is just over a year old, designed as an exercise to help me flesh out some better code examples and to horse around with dodgy Ruby ideas that perhaps weren’t destined to be full projects. At the time, in December of last year, Florian Gross and Mauricio Fernandez had been golfing with a number of tantalizing scripts like flgr’s 6-line p2p and batsman’s 5-line wiki.

For a while, I ran flgr’s file-sharing app to distribute songs from a Ruby mixtape (day 1, day 2, day 3.) Sadly, the little server didn’t really have concurrency. The project got pocketed.

Lately, I’ve been playing with a rewrite of miniature file-sharing and I really like what’s come out:

 #!/usr/bin/ruby -s -rsocket
 # Servers:
 #   ruby p2p.rb -L=localhost:2005 -R=localhost:2005 -W=foobar
 #   ruby p2p.rb -L=localhost:2006 -R=localhost:2005 -W=foobar
 # Client:
 #   ruby ~/bin/p2p.rb -R=localhost:2005 -W=foobar -S=*.rb
 def s(h,t);h[/:/];[TCPSocket,TCPServer][t].new($`,$');end;def n(h,a,*r,&b);c=
 s(h,0);c.puts(([$W,a]+r)*'|');(b)?(while l=c.read(1024);b.call l;end):c.read.
 split("\n");end;def f(k,a,r);k.map{|_|n(_,a,r)};end;def o(x);x[/\/?(.*)/, 1].
 i=s.accept;Thread.new(k,i){|k,c|w,a,r=c.gets.strip.split'|';c.puts(begin;case a
 when 's':f(k,:l,r);when 'g':IO.read o(r);when 'a':k<<r;k.uniq!;k;when 'd':f(k,
 :a,r);k;when 'l':Dir[o(r)].map{|_|"#$L "+_};end;rescue;end||"\n") if w==$W;c.
 close};end):($R?(n($R,:s,$S).each{|f|h,f=f.split' ';open(f,'w'){|o|n(h,:g,f){

This little client/server is patterned after uP2P. It shares everything under the working directory. And, if you can spot the case statement, there are five commands. If you run a single server and telnet into it, you can try these out:

  • s recursively searches for a glob. s|foobar|*.rb
  • g reads a file’s contents. g|foobar|p2p.rb
  • a announces your peer. a|foobar|
  • d lists the peer directory. d|foobar|
  • l lists the files for the connected peer only. l|foobar|*.rb

The client mode just downloads from all peers anything matching the glob. I probably need to do some exception handling at some point. This will stream IO, though!

said on 09 Jan 2006 at 14:05

Cool. Any more Ruby mixtapery? That was keen.

said on 09 Jan 2006 at 15:26

Hmmm, is this a challenge for further golfing? :)

said on 09 Jan 2006 at 15:40

flgr: Sure, I’d like to see it go sub-500 bytes. Like these.

said on 09 Jan 2006 at 18:20

Why keep this a golfing script? Might it not be more useful if you go ahead and add the line breaks needed to make it readable (and thus more easily modifyable)?

said on 09 Jan 2006 at 19:00

This looks like a great obfuscated Ruby entry (perhaps a valid tragically misintentioned subproject?) but like a horrible coding example. What’s the goal here? To minimize the number of ASCII lines? Every ; essentially counts as a newline to the parser so i don’t see the point here. This is almost as unreadable as the line noise they call perl. We aren’t trying to emulate that are we? Ooh, look at me, I detest newlines.

said on 10 Jan 2006 at 00:34

It’s just more compact like this. Fits into view.

said on 10 Jan 2006 at 10:11

erik: Our goal here, I think, isn’t great code, but rather interesting code.

said on 10 Jan 2006 at 10:36

And this is interesting without a doubt.

said on 10 Jan 2006 at 10:59

I agree, I love this site because it has interesting code, and a small p2p app in ruby is interesting. I’m only attempting to deriding the notion that it has to be unreadable.

said on 10 Jan 2006 at 14:41

erik: Making things as short as possible doesn’t always mean that readability goes up.

Of course in usual case Rubyists would go for maximum readability and simplicity, but please don’t spoil the fun of Golf by telling us that we are producing unmaintainable code.

We’re doing it on purpose after all. ;)

Comments are closed for this entry.