How to Create Peer-to-Peer
XML-RPC Clients and Servers
Brad GNUberg, bkn3@columbia.edu

Summary
The P2P Sockets Project has ported over existing software to seamlessly work on the Jxta P2P network, including the Apache XML-RPC Client and Server libraries.  This tutorial describes how to use these ported libraries in your applications.

Questions? Comments?
See the P2P Sockets Homepage or contact Brad Neuberg at bkn3@columbia.edu.  Feel free to call him at 1-510-938-3263 (Pacific Standard Time, San Francisco) Monday through Friday, not including weekends.  
Also see his weblog, www.codinginparadise.org, for Mozilla, Java, Jxta, and P2P news.

What is the P2P Sockets Project?
Are you interested in:
If you answered yes to any of the above, then welcome to the Peer-to-Peer Sockets project!  The P2P Sockets project rebuilds Java's java.net classes to work on the Jxta peer-to-peer network.1.  While standard TCP/IP sockets and server-sockets are theoretically peer-to-peer,  in practice they are not due to firewalls, Network Address Translation (NAT) devices, and political and technical issues with the Domain Name System (DNS)2.  The P2P Sockets project can automatically include these machines that are usually unreachable, and includes a simple, light-weight distributed domain name system that can be extended into interesting new directions for your applications.  

The P2P Sockets project already includes a large amount of software ported to use the peer-to-peer network, including a web server (Jetty) that can receive requests and serve content over the peer-to-peer network; a servlet and JSP engine (Jetty and Jasper) that allows existing servlets and JSPs to serve P2P clients; an XML-RPC client and server (Apache XML-RPC) for accessing and exposing P2P XML-RPC endpoints; an HTTP/1.1 client (Apache Commons HTTP-Client) that can access P2P web servers; a gateway (Smart Cache) to make it possible for existing browsers to access P2P web sites; and a WikiWiki (JSPWiki) that can be used to host WikiWikis on your local machine that other peers can access and edit through the P2P network. Even better, all of this software works and looks exactly as it did before being ported.  The P2P Sockets abstraction is so strong that porting each of these pieces of software took as little as 30 minutes to several hours.  Everything included in the P2P sockets project is open-source, mostly under BSD-type licenses, and cross-platform due to being written in Java.

This tutorial explains how to create XML-RPC servers that expose their functions over a peer-to-peer network, and XML-RPC clients that access this functionality over the peer network.  Why would you want to do this?  First, XML-RPC servers can be run on machines that would ordinarily not be resolvable or reachable, due to the issues discussed above.  Second, you can now leverage your existing XML-RPC skills to create peer-to-peer networks without learning a new API.  Peers in your network can assume the roles of both XML-RPC client and server, interacting with other peers who are themselves both XML-RPC clients and servers.  The XML-RPC functions exposed could be related to instant messaging, blogging using the MetaWeblog XML-RPC API, editing WikiWikis using the WikiWiki XML-RPC API, and more.  Now every peer in the network, regardless of whether their cable ISP treats them as second-class citizens, can easily be both a writeable and readable node, opening the possibility of a Two Way Web.  Even better, everything is built above a modified version of Jetty, an open-source web server that is specifically designed to be small and embeddable in other applications, making it easy for you to roll P2P functionality into your own applications.

For more information on using the raw P2P sockets and server sockets in your own code, and for details information on how the peer network itself is created, see the tutorial Introduction to Peer-to-Peer Sockets.  For information on creating P2P web servers, servlets, JSPs, and Web Application Archives (WARs), see the tutorial How to Create Peer-to-Peer Web Servers, Servlets, and JSPs.

Requirements and Configuration
You must download and install the following software to develop and work with P2P Sockets:
Install and configure the JDK and Ant, and make sure both are in your path so they can be run from the command-line.  Unzip the P2PSockets zip file into the top-level of your hard-drive; spaces are not allowed in the directory names, or the P2P Sockets build files will not work correctly.

The P2P Sockets directory already includes two different directories, test/clientpeer and test/serverpeer, that have Jxta configuration information already set up (these are in the hidden directories test/clientpeer/.jxta and test/serverpeer/.jxta, respectively).  If you want to learn more about how to configure Jxta, read about the Jxta Configurator.  The two test peers have already been configured for the worst possible case, which is that you are behind a firewall and a NAT device, which means you must use other intermediate peers to relay your requests; this configuration will work even if you are not under these conditions.  One of the nice aspects about Jxta, though, is that this will all be transparent and hidden from you as you program and use the system.

When testing the examples in this tutorial, you must be connected to the Internet.  This is for two reasons; first, the examples use a public Jxta server Sun has set up that helps bootstrap peers into the Jxta network; and second, on some operating systems, such as on Windows XP by default, the network subsystem shuts down if you are not connected to the Internet, preventing client peers and server peers that are running on the same machine from finding or communicating with each other.

Creating an Peer-to-Peer XML-RPC Server using Apache XML-RPC
Creating a P2P XML-RPC server is very similar to creating a normal, non-peer-to-peer one.  An example is shown below, with portions that are special to creating P2P XML-RPC servers shown in bold:



Creating an Peer-to-Peer XML-RPC Client using Apache XML-RPC
text

Creating an XML-RPC Client using Mozilla
  (Shows JavaScript for an XML-RPC client, talks about how to setup the P2PWebProxy, about configuring your browser to make it your proxy, then how to start the proxy and your XML-RPC server, then how to access it using the JavaScript debugger in Mozilla.)

Limitations and Security Concerns
text

Footnotes
1Jxta is an open-source project that creates a peer-to-peer overlay network that sits on top of TCP/IP.  Ever peer on the network is given an IP-address like number, even if they are behind a firewall or don't have a stable IP address.  Super-peers on the Jxta network run application-level routers which store special information such as how to reach peers, how to join sub-groups of peers, and what content peers are making available.  Jxta application-level relays can proxy requests between peers that would not normally be able to communicate due to firewalls or NAT devices.  Peers organize themselves into Peer Groups, which scope all search requests and act as natural security containers.  Any peer can publish and create a peer group in a decentralized way, and other peers can search for and discover these peer groups using other super-peers.  Peers communicate using Pipes, which are very similar to Unix pipes.  Pipes abstract the exact way in which two peers communicate, allowing peers to communicate using other peers as intermediaries if they normally would not be able to communicate due to network partitioning.

2First, many of the peers on the Internet are given dynamic IP addresses through DHCP, shared or filtered IP addresses through NAT devices, or IP addresses that are very hard to reach due to firewalls.  Creating server sockets under such conditions is either impossible or requires elaborate application-level ruses to bypass these restrictions.  Second, TCP/IP sockets and server sockets depend on the Domain Name System (DNS) to resolve human-friendly host names into IP addresses.  DNS is theoretically semi-decentralized, but on an administrative level it is centralized under ICANN, an unresponsive, non-innovative organization.  Further, standard DNS does not deal well with edge-peers that have filtered or dynamic IP addresses, and updates take too long to propagate and have no presence information.  Developers who wish to create exciting, new applications that extend DNS into new directions, such as storing instant messaging usernames and presence info in DNS, are stymied by the DNS systems technical and political issues.





Resources

About the Author
Image of Brad Neuberg as a childBrad "GNUberg" Neuberg is an open-source developer living in San Francisco.  He is currently involved with the BlackConnect project, an effort to allow programmers to write Mozilla XPCOM components in Java; the Java-Mozilla Integration project, an umbrella effort for open-source developers to work on turning Java into a full-fledged member of the Mozilla platform; the P2P Sockets project, an initiative to port the existing web onto a peer-to-peer foundation and make working with Jxta easier; the Paper Airplane project, a browser plugin that empowers everone to be a writer as well as a reader; and submissions to the Jxta and Mozilla projects.  In the past he has worked on the OpenPortal project and the Flash/Java Integration project.  He is an avid Java, C++, JavaScript, and Mozilla Platform hacker who is a strong believer in usability.  He has a B.A. in Computer Science from Columbia University and experience building enterprise trading systems for the securities industry using J2EE technologies.  He is now slightly older than he was when the picture on the left was taken.  Occasionally, after enough beers, he'll start tap dancing.



This work is licensed under the Creative Commons Attribution-NonCommercial License by Bradley Keith Neuberg. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/1.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.