import java.io.*;
import java.util.*;
import java.net.URL;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.exception.PeerGroupException;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.Advertisement;
import net.jxta.document.Element;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredTextDocument;
import net.jxta.discovery.DiscoveryService;
import net.jxta.pipe.PipeService;
import net.jxta.pipe.InputPipe;
import net.jxta.pipe.OutputPipe;
import net.jxta.pipe.PipeID;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.protocol.PeerGroupAdvertisement;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.endpoint.Message;
import net.jxta.id.IDFactory;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.membership.MembershipService;
import net.jxta.membership.Authenticator;
import net.jxta.credential.AuthenticationCredential;
import net.jxta.impl.peergroup.StdPeerGroupParamAdv;
import net.jxta.impl.id.UUID.ModuleClassID;
import net.jxta.impl.membership.PasswdMembershipService;
// RestoPeer represents a restaurant that receives auction requests
// for french fries from HungryPeers. RestoPeers offers three sizes of
// french fries (small, large, medium). Each restaurant assignes a
// different price to each size. Each restaurant also offers a special
// offering.
//
// Each restaurant is uniquely identified by its brand name.
public class RestoPeer {
private PeerGroup netpg = null; // The NetPeerGroup
private PeerGroup restoNet = null; // The restoNet Peergroup
private String brand = "Chez JXTA"; // Brand of this restaurant
private String specials = "large ($3.00)"; // Current restaurant special
// Services within the RestoNet peergroup
private DiscoveryService disco = null; // Discovery service
private PipeService pipes = null; // Pipe service
private PipeAdvertisement myAdv = null; // My RestoPeer pipe advertisement
private InputPipe pipeIn = null; // Input pipe that we listen to
// for requests from Hungry Peers
private int timeout = 3000; // discovery wait timeout
private int rtimeout = 8000; // resolver pipe timeout
private ModuleClassID membershipClassID;
private static PeerGroupID restoPeerGroupID;
static {
try {
restoPeerGroupID =
(PeerGroupID) IDFactory.fromURL(new URL(
"urn", "", "jxta:uuid-058C59E291174BEFB804AACB9C6A3B5002"));
} catch (Exception e) {
throw new RuntimeException("Can't load RestoPeer");
}
}
public static void main(String args[]) {
RestoPeer myapp = new RestoPeer();
myapp.startJxta();
System.exit(0);
}
// Method to start the JXTA platform, join the RestoNet peergroup and
// advertise the RestoPeer service
private void startJxta() {
try {
//Discover and join (or start) the default peergroup
netpg = PeerGroupFactory.newNetPeerGroup();
} catch (PeerGroupException e) {
//Couldn't initialize; can't continue
System.out.println("Fatal error : creating the NetPeerGroup");
System.exit(1);
}
// Discover (or create) and join the RestoNet peergroup
try {
joinRestoNet();
} catch (Exception e) {
System.out.println("Can't join or create RestoNet");
System.exit(1);
}
// Discover (or create) and publish a RestoPeer pipe to receive
// auction request for fries from HungryPeers
if (!createRestoPipe()) {
System.out.println("Aborting due to failure to create RestoPeer pipe");
System.exit(1);
}
// Start the RestoPeer server loop to respond to Hungry peers
// fries requests.
handleFriesRequest();
}
// Discover (or create) and join the RestoNet peergroup
private void joinRestoNet() throws Exception {
int count = 3; // maximun number of attempts to discover
System.out.println("Attempting to Discover the RestoNet PeerGroup");
// Get the discovery service from the NetPeergroup
DiscoveryService hdisco = netpg.getDiscoveryService();
Enumeration ae = null; // Holds the discovered peers
// Loop until wediscover the RestoNet or
// until we've exhausted the desired number of attempts
while (count-- > 0) {
try {
// search first in the peer local cache to find
// the RestoNet peergroup advertisement
ae = hdisco.getLocalAdvertisements(DiscoveryService.GROUP,
"Name", "RestoNetAuth");
// If we found the RestoNet advertisement we are done
if ((ae != null) && ae.hasMoreElements())
break;
// If we did not find it, we send a discovery request
hdisco.getRemoteAdvertisements(null,
DiscoveryService.GROUP, "Name", "RestoNetAuth", 1, null);
// Sleep to allow time for peers to respond to the
// discovery request
try {
Thread.sleep(timeout);
} catch (InterruptedException ie) {}
} catch (IOException e){
// Found nothing! Move on
}
}
PeerGroupAdvertisement restoNetAdv = null;
// Check if we found the RestoNet advertisement.
// If we didn't, then either
// we are the first peer to join or
// no other RestoNet peers are up.
// In either case, we must create the RestoNet peergroup
if (ae == null || !ae.hasMoreElements()) {
System.out.println(
"Could not find the RestoNet peergroup; creating one");
try {
// Create a new, all-purpose peergroup. Because we
// have a custom peergroup advertisement, we must
// create the module impl advertisement and then
// the peergroup advertisement (just as we did when
// we created the peergroup as a service).
ModuleImplAdvertisement implAdv =
createAuthPeerGroupModuleImplAdv(netpg);
hdisco.publish(implAdv, DiscoveryService.ADV,
PeerGroup.DEFAULT_LIFETIME,
PeerGroup.DEFAULT_EXPIRATION);
hdisco.remotePublish(implAdv, DiscoveryService.ADV,
PeerGroup.DEFAULT_EXPIRATION);
restoNetAdv =
createPeerGroupAdvertisement(implAdv, "RestoNetAuth");
// The peergroup advertisement membership service needs
// to have a parameter that initializes the valid
// users and passwords. Create a document that holds that
// information. There can be any number of login:passwd:
// pairs in the login tag value.
StructuredTextDocument pwds = (StructuredTextDocument)
StructuredDocumentFactory.newStructuredDocument(
new MimeMediaType("text/xml"), "Parm");
Element e = pwds.createElement("login",
"hungrypeer:" +
PasswdMembershipService.makePsswd("password") + ":");
pwds.appendChild(e);
restoNetAdv.putServiceParam(membershipClassID, pwds);
// Now publish the modified advertisement as usual
hdisco.publish(restoNetAdv, DiscoveryService.GROUP,
PeerGroup.DEFAULT_LIFETIME,
PeerGroup