A First JXTA Program


You can download the current version of the J2SE implementation from http://www.jxta.org; follow the download instructions for the appropriate packages. My recommendation is that you choose the full download option, which includes all the JXTA libraries, as well as the JXTA Shell and other sample applications. The only requirements for the JXTA J2SE binding is the presence of a J2SE implementation. Other Java-based JXTA implementations might require various configurations of J2ME.

Although the JXTA download comes with several sample applications that enable you to experiment with the platform, we will write here a very simple JXTA program that initializes the platform and displays some information about peers and peer groups. Successful termination of this program indicates that you have correctly installed the JXTA libraries. Listing 16.1 shows the complete source code for this simple program that starts up the JXTA runtime environment and then prints out the peer and peer group names and IDs, respectively.

Listing 16.1 PlatformTest
 import net.jxta.peergroup.PeerGroup; import net.jxta.peergroup.PeerGroupFactory; import net.jxta.exception.PeerGroupException; /**  * Platform test. This program simply initializes the JXTA platform,  * obtains a reference to the NetPeerGroup, and displays the names and IDs  * of the peer and the group, respectively.  */ public class PlatformTest {         public static void main(String[] argv) {                PeerGroup netPeerGroup = null;                try {                       netPeerGroup = PeerGroupFactory.newNetPeerGroup();                       System.out.println("Group name: " +                               netPeerGroup.getPeerGroupName());                       System.out.println("Group ID: " +                               netPeerGroup.getPeerGroupID());                       System.out.println("Peer name: " +                               netPeerGroup.getPeerName());                       System.out.println("Peer ID: " +                               netPeerGroup.getPeerID());                } catch (PeerGroupException e) {                       System.out.println("Can't initialize net peer group: " +                               e.getMessage());                }                System.exit(0);         } } 

When you compile and run this program for the first time, the JXTA runtime brings up a window requesting that you configure your peer. This window represents the JXTA Configurator, shown in Figure 16.16. It asks you to give your peer a name, and to indicate whether your peer will offer any relay services, such becoming a rendezvous, a router, or a gateway. You also need to specify whether to use any outside relays to aid the discovery of advertisements outside of your local network. You have the option of asking the system to download a list of well-known rendezvous and relays. Once you opt to download that list, your peer will use those in message propagation and discovery. Finally, you also need to specify a secure username and password for your peer. This information will be used when your peer applies for group membership, and in other situations when it needs to be authenticated.

Figure 16.16. The JXTA Configurator window.

graphics/16fig16.jpg

As we mentioned earlier, at startup a peer initializes the World Peer Group and the Net Peer Group. This application simply obtains a reference to the latter, and prints out that peer group's name, group ID, as well as the peer's own name and ID:

 aquinas% java PlatformTest   Group name: NetPeerGroup  Group ID: urn:jxta:jxta-NetGroup  Peer name: aquinas  Peer ID: urn:jxta:uuid59616261646162614A7874 6150325033D5A4650661A84DF1867897A0FC52E4FF03 

If you examine the files in the directory you started the application from, you will see that the JXTA runtime created several files and subdirectories. The peer's configuration itself is stored in the file PlatformConfig; this is an XML file, and you can examine its contents with a text editor. If you ever need to reconfigure the peer, removing this file will cause the JXTA Configurator to display the set of configuration screens again. The cm directory stands for the cache manager, and contains cached advertisements. The files in that directory are also XML files; examining them is a good way to learn about the structure of JXTA advertisements. Finally, the pse directory contains credential information.



JavaT P2P Unleashed
JavaT P2P Unleashed
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 209

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net