By Scott Oaks, Bernard Traversat, Li Gong
Price: $34.95 USD
£24.95 GBP
Cover | Table of Contents | Colophon
jxta.c.jxta) and a Personal Java
Profile edition are both available.
jxta.c.jxta) and a Personal Java
Profile edition are both available.
http://java.sun.com/j2se/. For other
operating systems, check with your system vendor. More commonly, many
integrated development environments (IDEs) come with support for Java
(and hence a Java platform).
java executable is in your
standard path.
http://download.jxta.org/easyinstall/install.html.
On this page, you can obtain the JXTA demo package for a variety of
platforms. In fact, the JXTA demo implementation at this site is
written completely in Java; the difference between the platforms lies
only in how the parts of the implementation are packaged and how they
are installed. Therefore, for Microsoft Windows, download an
executable (JXTA>)
at which you type in commands.
env command produces as its
output a list of all the shell variables and their values:
JXTA>env
stdin = Default InputPipe (class net.jxta.impl.shell.ShellInputPipe)
parentShell = Root Shell (class net.jxta.impl.shell.bin.Shell.Shell)
Shell = Root Shell (class net.jxta.impl.shell.bin.Shell.Shell)
stdout = Default OutputPipe (class net.jxta.impl.pipe.NonBlockingOutputPipe)
consout = Console OutputPipe (class net.jxta.impl.shell.ShellOutputPipe)
consin = Default Console InputPipe (class
net.jxta.impl.shell.ShellInputPipe)
stdgroup = Default Peergroup (class net.jxta.impl.peergroup.StdPeerGroup)
mkadvpiccolo% cd /files/JXTA_Demo/Shell piccolo% sh shell.sh
myusername and entered a password.
cd to that directory.piccolo% cat log | grep "Exception" | sort | uniq | wc -l
cat command
prints the log file to its standard output. The
grep command reads this file from its standard
input and searches for lines containing the string
"Exception"; it prints matching
lines to its standard output. The sort command
reads these lines from its standard input and sends the sorted list
to its standard output, where it is read by the
uniq command, which removes duplicate lines. The
unduplicated lines are sent to its standard output, where they are
read by the wc command, which counts the number of
lines and finally prints that number.
mkadv command.
Advertisements are one of the basic building blocks of JXTA: they
help peers discover any kind of service, including peergroups, other
peers, and pipes. In fact, at a basic level, the JXTA infrastructure
is simply about sending advertisements for various resources around
to interested parties. A JXTA application simply searches for
advertisements it is interested in and responds to requests for
advertisements that it has published. The basic protocols
we've mentioned all use advertisements as the
mechanism by which they send data. Advertisements provide a
platform-independent representation of platform objects that can be
exchanged between different platform implementations (Java, C, etc.).
PeerGroup class
(net.jxta.peergroup.PeerGroup). There are ways to
create your own peergroups, of course, but an object that represents
the special NetPeerGroup is returned from the static
newNetPeerGroup( ) method of the
PeerGroupFactory class
(net.jxta.peergroup.PeerGroupFactory). Therefore,
the simplest of all JXTA applications looks like this:
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.exception.PeerGroupException;
public class HelloWorld {
static PeerGroup group = null;
public static void main(String args[]) {
HelloWorld myapp = new HelloWorld( );
myapp.startJxta( );
System.exit(0);
}
private void startJxta( ) {
try {
// Create and start the default JXTA NetPeerGroup
group = PeerGroupFactory.newNetPeerGroup( );
} catch (PeerGroupException e) {
// Could not instantiate the group; print the stack and exit
System.out.println(
"Fatal error : creating the net PeerGroup");
System.exit(1);
}
System.out.println("Started Hello World");
}
}
HelloWorld class:
piccolo% javac HelloWorld.java
piccolo% java HelloWorld
Started Hello World
Advertisement class
(net.jxta.document.Advertisement). Each
advertisement has a unique type, which is returned by the
getAdvertisementType( ) method. The JXTA API
defines the following core advertisement types (each of which is an
abstract class in the net.jxta.protocol package):
PeerAdvertisement
PeerGroupAdvertisement
PipeAdvertisement
PeerInfoAdvertisement
EndpointAdvertisement
ModuleClassAdvertisement
,
ModuleSpecAdvertisement, and
ModuleImplAdver-tisement
(module-related advertisements)
RdvAdvertisement
TransportAdvertisement
private void startJxta( ) {
try {
// Create and start the default JXTA NetPeerGroup
group = PeerGroupFactory.newNetPeerGroup( );
} catch (PeerGroupException e) {
// Could not instantiate the group; print the stack and exit
System.out.println("Fatal error : creating the net PeerGroup");
System.exit(1);
}
System.out.println("Started Hello World");
// Now we'll access the peergroup service
// and get various information from it
PeerGroupID pgid = group.getPeerGroupID( );
System.out.println ("pgid= " + pgid);
PeerID pid = group.getPeerID( );
System.out.println("pid= " + pid);
String name = group.getPeerName( );
System.out.println("peer name=" + name);
// Get the core services. We don't use the core services in this
// example, but this is how you retrieve them if you need them.
DiscoveryService disco = group.getDiscoveryService( );
PipeService pipe = group.getPipeService( );
MembershipService member = group.getMembershipService( );
ResolverService resolv = group.getResolverService( );
System.out.println("All done");
} catch (Exception ex) {
ex.printStackTrace( );
}
}
Started Hello World pgid= urn:jxta:jxta-NetGroup pid= urn:jxta:uuid-59616261646162614A7874615032503368D0F1C1B8B54A3ABE2F8D DEEE47489F03 peer name=Test Application 1 All done
HelloWorld class is a complete JXTA
application and has allowed us to understand the basic structure of
programming in JXTA, it doesn't actually do anything
except print out a lot of interesting information. In the next few
chapters, we'll develop a more complete example, one
that leverages the features of the JXTA platform to create a
community of cooperating peers.
RestoPeer class (the code
that provides the restaurant service). A RestoPeer is responsible for
discovering and joining the RestoNet peergroup; if the peer cannot
find the RestoNet peergroup, the peer will create it.
import java.io.*;
import java.net.*;
import java.util.*;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.exception.PeerGroupException;
import net.jxta.discovery.DiscoveryService;
import net.jxta.id.IDFactory;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PeerGroupAdvertisement;
import net.jxta.protocol.ModuleImplAdvertisement;
// RestoPeer represents a restaurant that receives auction requests
// for food from HungryPeers. The RestoPeer will discover and join
// the RestoNet and publish itself as a provider for HungryPeers
public class RestoPeer {
private PeerGroup netpg = null; // The NetPeerGroup
private PeerGroup restoNet = null; // The RestoNet peergroup
private String brand = "Chez JXTA"; // Brand of my restaurants
private int timeout = 3000; // Time-out; can be adjusted
// Services within the RestoNet peergroup
private DiscoveryService disco; // Discovery service
private PipeService pipes; // Pipe service
static String groupURL =
"jxta:uuid-4d6172676572696e204272756e6f202002";
public static void main(String args[]) {
RestoPeer myapp = new RestoPeer( );
myapp.startJxta( );
System.exit(0);
}
// Start the JXTA application
private void startJxta( ) {
try {
// Discover and join (or start) the default peergroup
netpg = PeerGroupFactory.newNetPeerGroup( );
} catch (PeerGroupException e) {
// Couldn't initialize; can't co