Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 World's Shortest P2P app
Index -> General Discussion
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Martin




PostPosted: Fri Jan 07, 2005 8:01 am   Post subject: World's Shortest P2P app

Weighing in at 15 lines of python code:

code:
# tinyp2p.py 1.0 (documentation at http://freedom-to-tinker.com/tinyp2p.html)
import sys, os, SimpleXMLRPCServer, xmlrpclib, re, hmac # (C) 2004, E.W. Felten
ar,pw,res = (sys.argv,lambda u:hmac.new(sys.argv[1],u).hexdigest(),re.search)
pxy,xs = (xmlrpclib.ServerProxy,SimpleXMLRPCServer.SimpleXMLRPCServer)
def ls(p=""):return filter(lambda n:(p=="")or res(p,n),os.listdir(os.getcwd()))
if ar[2]!="client": # license: http://creativecommons.org/licenses/by-nc-sa/2.0
  myU,prs,srv = ("http://"+ar[3]+":"+ar[4], ar[5:],lambda x:x.serve_forever())
  def pr(x=[]): return ([(y in prs) or prs.append(y) for y in x] or 1) and prs
  def c(n): return ((lambda f: (f.read(), f.close()))(file(n)))[0]
  f=lambda p,n,a:(p==pw(myU))and(((n==0)and pr(a))or((n==1)and [ls(a)])or c(a))
  def aug(u): return ((u==myU) and pr()) or pr(pxy(u).f(pw(u),0,pr([myU])))
  pr() and [aug(s) for s in aug(pr()[0])]
  (lambda sv:sv.register_function(f,"f") or srv(sv))(xs((ar[3],int(ar[4]))))
for url in pxy(ar[3]).f(pw(ar[3]),0,[]):
  for fn in filter(lambda n:not n in ls(), (pxy(url).f(pw(url),1,ar[4]))[0]):
    (lambda fi:fi.write(pxy(url).f(pw(url),2,fn)) or fi.close())(file(fn,"wc"))


http://www.freedom-to-tinker.com/tinyp2p.html

Nifty, eh?

And yes, I read slashdot.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Jan 07, 2005 3:28 pm   Post subject: (No subject)

6 lines of Ruby:

code:
# 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)
Tony




PostPosted: Fri Jan 07, 2005 8:35 pm   Post subject: (No subject)

Ruby is awesome Wink

why is there Mad inside the Python syntax? Confused
wtd




PostPosted: Fri Jan 07, 2005 8:38 pm   Post subject: (No subject)

Because Martin didn't disable graphical smileys when he posted. Wink

And the syntax tag doesn't wrap code in
code:
<pre></pre>
tags, like it should.
rizzix




PostPosted: Fri Jan 07, 2005 11:21 pm   Post subject: (No subject)

the problem here wtd is that ruby uses a semicolon to denote an end of statement. hence u cant really compare.
Martin




PostPosted: Fri Jan 07, 2005 11:22 pm   Post subject: (No subject)

Who edited my post? Grr...
wtd




PostPosted: Fri Jan 07, 2005 11:27 pm   Post subject: (No subject)

rizzix wrote:
the problem here wtd is that ruby uses a semicolon to denote an end of statement. hence u cant really compare.


Not Ruby's fault that Python syntax can't do the same.
rizzix




PostPosted: Fri Jan 07, 2005 11:28 pm   Post subject: (No subject)

Razz Laughing

edit: well u could still compare # of statements.
just wait till i post my perl code.. pfft. pwnage..

edit:edit: i can't find it darn it Evil or Very Mad
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Jan 07, 2005 11:45 pm   Post subject: (No subject)

No semi-colons, whiner, and slightly cleaned up. Wink

And it's 11 lines, sans comments.

code:
require'drb'
P, M, U, *O = ARGV
def s(p) File.split(p[/[^|].*/]).last 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 = x(n) and c = c(n) and (c.f(p, O[0], 0).map { |f| s f } - Dir["*"]).each { |f| File.open(f, "w") { |o| o << c.f(p, f, 1) } } } || (DRb.start_service U, Class.new {  def f(c, a = [],t = 2)  c == x(U) && (t == 0 && Dir[s(a)] || t == 1 && File.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! or O end }.new.y
sleep)
Display posts from previous:   
   Index -> General Discussion
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: