The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Revisiting the File-Sharing Paragraph

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Revisiting the File-Sharing Paragraph Posted: Jan 9, 2006 12:14 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Revisiting the File-Sharing Paragraph
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

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=abc -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].
 gsub(/\.{2,}/){};end;$L?(k=[$L];k+=n($R,:d,$L)if$L!=$R;k.uniq!;s=s($L,1);while
 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<<

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|192.168.0.5:2006
  • d lists the peer directory. d|foobar|192.168.0.5:2006.
  • 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!

Read: Revisiting the File-Sharing Paragraph

Topic: The fall of dynamic languages? I don&#8217;t think so Previous Topic   Next Topic Topic: Specialist Project Risk

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use