<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: p2p code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 19:34:28 GMT</pubDate>
    <description>DZone Snippets: p2p code</description>
    <item>
      <title>Trivial P2P in newLISP</title>
      <link>http://snippets.dzone.com/posts/show/2564</link>
      <description>; a trivial P2P file sharing program written in newLISP for demo purpose&lt;br /&gt;;&lt;br /&gt;;  based on ideas from http://www.freedom-to-tinker.com/tinyp2p.html&lt;br /&gt;; and http://ansuz.sooke.bc.ca/software/molester/ and &lt;br /&gt;; http://ansuz.sooke.bc.ca/software/molester/2005010301.php&lt;br /&gt;;&lt;br /&gt;; command reference&lt;br /&gt;; i/ advertise presence of your node to the peer&lt;br /&gt;; g&lt;filename&gt;/ requests a file &lt;br /&gt;; f&lt;message&gt; forward to peers&lt;br /&gt;; h/ gets list of all peers&lt;br /&gt;;&lt;br /&gt;; used internally&lt;br /&gt;; e&lt;filename&gt;/ expect a file&lt;br /&gt;; x  sent after receiving a file to make sure&lt;br /&gt;;&lt;br /&gt;; the program below is a toy, not a serious p2p program.&lt;br /&gt;; &lt;br /&gt;; differences from original mole-ster:&lt;br /&gt;; use of 'x' -- to allow data receipt on receiving side when file is sent&lt;br /&gt;; data is read in 8k chunks at a time.  this is to avoid having to read&lt;br /&gt;; the entire file into a buffer before writing. it allows larger files to be &lt;br /&gt;; transferred.&lt;br /&gt;;&lt;br /&gt;; more more information refer to the original mole-ster web sites.&lt;br /&gt;;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;(context 'P2P)&lt;br /&gt;&lt;br /&gt;(constant 'SIGINT 2)&lt;br /&gt;(define (interrupted)&lt;br /&gt;  (println "interruted by user!")&lt;br /&gt;  (exit))&lt;br /&gt;&lt;br /&gt;(signal SIGINT interrupted)&lt;br /&gt;&lt;br /&gt;(set 'my-address "")&lt;br /&gt;(set 'my-password "")&lt;br /&gt;(set 'peers '())&lt;br /&gt;&lt;br /&gt;(define (get-addr addr-and-port)  (regex "(.*):(.*)" addr-and-port)  $1)&lt;br /&gt;(define (get-port addr-and-port)  (regex "(.*):(.*)" addr-and-port)  (integer $2))&lt;br /&gt;&lt;br /&gt;(define (op-send dest-addr source-addr filename data)&lt;br /&gt;  (if (set 'socket (net-connect (get-addr dest-addr) (get-port dest-addr)))&lt;br /&gt;      (begin&lt;br /&gt;			(net-send socket (format "%s %s %s/" my-password source-addr filename))&lt;br /&gt;			(net-send socket data )&lt;br /&gt;			(if (!= data "")&lt;br /&gt;				(net-receive socket 'buf 1))&lt;br /&gt;			(close socket))))&lt;br /&gt;&lt;br /&gt;(define (P2P:P2P my-password peer-address my-address commands )&lt;br /&gt;  (set 'peers (append peers (list peer-address)))&lt;br /&gt;  (dolist (cmd commands)  (op-send peer-address my-address cmd ""))&lt;br /&gt;  (set 'socket (net-listen  (get-port my-address)))&lt;br /&gt;  (while true &lt;br /&gt;	 (while  (and (not (net-error)) (not (net-select socket "read" 1000)))&lt;br /&gt;		 (if (net-error) (print (net-error))))&lt;br /&gt;	 (set 'peer-socket (net-accept socket)) (net-receive peer-socket 'buf 1024 "/") &lt;br /&gt;	 (regex "^([a-zA-Z0-9]*) ([0-9:.]*) ([e-i])([^/]*)(/)" buf) &lt;br /&gt;	 (set 'peer-password $1) &lt;br /&gt;	 (set 'peer-address $2) &lt;br /&gt;	 (set 'peer-command $3) &lt;br /&gt;	 (set 'requested-filename $4) &lt;br /&gt;	 (set 'data $6) &lt;br /&gt;	 (if (= peer-password my-password) &lt;br /&gt;	     (case peer-command&lt;br /&gt;	       ("e" (begin&lt;br /&gt;	       			(set 'finished false)&lt;br /&gt;	       			(while (not finished)&lt;br /&gt;	       		     	(while (and (not (net-error)) (not (net-select peer-socket "read" 1000)))&lt;br /&gt;	       		     		(if (net-error) (print (net-error))))&lt;br /&gt;			    		(if (!= nil (net-receive peer-socket 'input-data 8192))&lt;br /&gt;			    			(begin&lt;br /&gt;								(append-file  requested-filename input-data)&lt;br /&gt;							(set 'finished true)))&lt;br /&gt;					(net-send socket "x")))&lt;br /&gt;	       ("f"  (dolist (peer peers)   (op-send peer my-address requested-filename data)))&lt;br /&gt;	       ("g" (op-send peer-address my-address (append "e" requested-filename) &lt;br /&gt;	       			(read-file requested-filename)))&lt;br /&gt;	       ("h" (dolist (peer peers)  (op-send peer-address peer "i" "")))&lt;br /&gt;	       ("i" (append peers peer-address))))&lt;br /&gt;	 (close peer-socket)))&lt;br /&gt;&lt;br /&gt;(context 'MAIN)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(P2P:P2P (main-args 2) (main-args 3) (main-args 4)  (slice (main-args) 5 -1))&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Sep 2006 08:06:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2564</guid>
      <author>frontera000 (bob bae)</author>
    </item>
    <item>
      <title>uP2P &#8212; micro P2P file sharing application</title>
      <link>http://snippets.dzone.com/posts/show/1783</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;# uP2P.sh 0.0.1, 436 characters (excluding comments)&lt;br /&gt;[ $3 ]&amp;&amp;export W=$1 H="$2 $3" K=`mktemp`;Z=/dev/null;e(){ echo "$*";};n(){&lt;br /&gt;nc $* 2&gt;$Z;};x(){ nc -lp ${H#* } -e $1 &amp;&gt;$Z &lt;$Z&amp;};f(){ cat $K|while read h;do&lt;br /&gt;e $W $1 "$2"|n $h;done };case $# in 4)e $W s "$4"|n $H|while read h p f; do&lt;br /&gt;e $W g "$f"|n $h $p&gt;"$f";done;;5)e $H&gt;$K;e $W d $H|n $4 $5&gt;&gt;$K;x $0;;0)x $0&lt;br /&gt;read w c r;[ $W = $w ]&amp;&amp;case $c in s)f l "$r";;g)cat "$r";;a)e $r&gt;&gt;$K;;d)cat $K&lt;br /&gt;f a "$r";;l)ls|grep "$r"|sed "s/^/$H /";;esac;;esac&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Source: &lt;a href="http://www.crossflux.org/uP2P/"&gt;uP2P&lt;/a&gt;, mentioned &lt;a href="http://ansuz.sooke.bc.ca/software/molester/"&gt;here&lt;/a&gt;</description>
      <pubDate>Tue, 28 Mar 2006 15:45:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1783</guid>
      <author>413x ()</author>
    </item>
    <item>
      <title>six line peer-to-peer client/server in ruby</title>
      <link>http://snippets.dzone.com/posts/show/1782</link>
      <description>&lt;pre&gt;&lt;br /&gt;slonik AZ wrote:&lt;br /&gt;&gt; slashdot published an article on someone's&lt;br /&gt;&gt; 15 lines long Peer-2-Peer application&lt;br /&gt;&gt; http://developers.slashdot.org/article.pl?sid=04/12/15/1953227&lt;br /&gt;&lt;br /&gt;&gt; Another person followed up with a 9 line equivalent Perl code.&lt;br /&gt;&lt;br /&gt;&gt; I wonder what an equivalent Ruby program would look like?&lt;br /&gt;&lt;br /&gt;I did this 9.5 hours ago. Compared to the python one it is not&lt;br /&gt;vulnerable to File stealing attacks (a client can request a file&lt;br /&gt;../foobar and ~/foobar from the python server and will get it back&lt;br /&gt;AFAIK) and 6 lines long. It is however vulnerable to the DRb style&lt;br /&gt;.instance_eval exploits. I will fix this shortly, but I might have to&lt;br /&gt;use 7 lines then.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Server: ruby p2p.rb password server server-uri merge-servers&lt;br /&gt;# Sample: ruby p2p.rb foobar server druby://localhost:1337 druby://foo.bar:1337&lt;br /&gt;# Client: ruby p2p.rb password client server-uri download-pattern&lt;br /&gt;# Sample: ruby p2p.rb foobar client druby://localhost:1337 *.rb&lt;br /&gt;require'drb';F,D,C,P,M,U,*O=File,Class,Dir,*ARGV;def s(p)F.split(p[/[^|].*/])[-1&lt;br /&gt;]end;def c(u);DRbObject.new((),u)end;def x(u)[P,u].hash;end;M=="client"&amp;&amp;c(U).f(&lt;br /&gt;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,&lt;br /&gt;"w"){|o|o&lt;&lt;c.f(p,f,1)}}}||(DRb.start_service U,C.new{def f(c,a=[],t=2)c==x(U)&amp;&amp;(&lt;br /&gt;t==0&amp;&amp;D[s(a)]||t==1&amp;&amp;F.read(s(a))||p(a))end;def y()(p(U)+p).each{|u|c(u).f(x(u),&lt;br /&gt;p(U))rescue()};self;end;private;def p(x=[]);O.push(*x).uniq!;O;end}.new.y;sleep) &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Source: &lt;a href="http://groups.google.ca/group/comp.lang.ruby/msg/8fc335f67f99536c"&gt;p2p.rb (Florian Gross)&lt;/a&gt;, mentioned &lt;a href="http://ansuz.sooke.bc.ca/software/molester/"&gt;here&lt;/a&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 28 Mar 2006 15:39:33 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1782</guid>
      <author>413x ()</author>
    </item>
  </channel>
</rss>
