FileDocCategorySizeDatePackage
RestoPeer.javaAPI DocExample20469Wed May 08 22:35:36 BST 2002None

RestoPeer

public class RestoPeer extends Object

Fields Summary
private net.jxta.peergroup.PeerGroup
netpg
private net.jxta.peergroup.PeerGroup
restoNet
private String
brand
private net.jxta.discovery.DiscoveryService
disco
private net.jxta.pipe.PipeService
pipes
private net.jxta.protocol.PipeAdvertisement
myAdv
private int
timeout
private net.jxta.platform.ModuleClassID
mcID
private net.jxta.platform.ModuleSpecID
msID
private static net.jxta.peergroup.PeerGroupID
restoPeerGroupID
Constructors Summary
Methods Summary
private net.jxta.peergroup.PeerGroupcreatePeerGroup(net.jxta.peergroup.PeerGroup parent, java.lang.String groupName, net.jxta.protocol.ModuleImplAdvertisement srvImpl)


        PeerGroup myPeerGroup = null;
        PeerGroupAdvertisement myPeerGroupAdvertisement = null;
        ModuleImplAdvertisement myModuleImplAdv = null;
        
        // Create the PeerGroup Module Implementation adding the
	// new peergroup service
        myModuleImplAdv = createPeerGroupModuleImplAdv(parent, srvImpl);

        // Publish the new peergroup module implementation in the
        // parent peer group
        DiscoveryService parentDiscovery = parent.getDiscoveryService();

        // Publish advertisement in the parent peergroup with the default
        // lifetime and expiration time for remote publishing
        try {
            parentDiscovery.publish(myModuleImplAdv, DiscoveryService.ADV, 
		PeerGroup.DEFAULT_LIFETIME, PeerGroup.DEFAULT_EXPIRATION);
            parentDiscovery.remotePublish(myModuleImplAdv,
                DiscoveryService.ADV, PeerGroup.DEFAULT_EXPIRATION);
        }
        catch (java.io.IOException e) {
            System.err.println(
                "Cannot publish the new peergroup ModuleImplAdv");
            return null;
        }    

	System.out.println(
                "Published new Peergroup ModulImpl advertisement for : "
                + groupName);

        // Create and publish the new PeerGroup Advertisement
        // corresponding to the new peergroup implementation
        myPeerGroupAdvertisement= createPeerGroupAdvertisement(
				  myModuleImplAdv,groupName);

        // Publish the advertisementin the parent peer group with the
        // default lifetime and expiration time
        try {
            parentDiscovery.publish(myPeerGroupAdvertisement,
                DiscoveryService.GROUP, PeerGroup.DEFAULT_LIFETIME,
                PeerGroup.DEFAULT_EXPIRATION);
            parentDiscovery.remotePublish(myPeerGroupAdvertisement,
                DiscoveryService.GROUP, PeerGroup.DEFAULT_EXPIRATION);
        }
        catch (java.io.IOException e) {
            System.err.println("Cannot create the new PeerGroupAdvertisement");
	    return null;
        }

	System.out.println("Published the new PeerGroup advertisement for :"
	    + groupName);

	try {
	    // Finally, instantiate the new PeerGroup
            myPeerGroup = parent.newGroup(myPeerGroupAdvertisement);
	    System.out.println("Instantiated the new PeerGroup " + groupName);
        } catch (net.jxta.exception.PeerGroupException e) {
            System.err.println("Cannot create the new Peer Group");
            return null;
        }

        return myPeerGroup;
    
private net.jxta.protocol.PeerGroupAdvertisementcreatePeerGroupAdvertisement(net.jxta.protocol.ModuleImplAdvertisement implAdv, java.lang.String groupName)


	// Create a new peergroup advertisement
        PeerGroupAdvertisement myadv = (PeerGroupAdvertisement) 
	    AdvertisementFactory.newAdvertisement(
	        PeerGroupAdvertisement.getAdvertisementType());
        
        // Instead of creating a new group ID we use the
        // RestoPeer pre-defined peergroup id, so we create the same
        // peergroupId each time the group
	// is created
        myadv.setPeerGroupID(restoPeerGroupID);

	// Assign the pre-defined module spec id that corresponds
	// to our peergroup module implementation
        myadv.setModuleSpecID(implAdv.getModuleSpecID());
	
	// Assign peergroup name
        myadv.setName(groupName);

	// Set the peergroup description
        myadv.setDescription("This is RestoNet Peergroup");
        
        return myadv;
    
private net.jxta.protocol.ModuleImplAdvertisementcreatePeerGroupModuleImplAdv(net.jxta.peergroup.PeerGroup parent, net.jxta.protocol.ModuleImplAdvertisement serviceModuleImplAdv)


        ModuleImplAdvertisement allPurposePeerGroupImplAdv = null;
        StdPeerGroupParamAdv PeerGroupParamAdv = null;

        try {
	    // Clone the parent (NetPeerGroup) implementation to get
	    // a module implementation
            allPurposePeerGroupImplAdv =
                parent.getAllPurposePeerGroupImplAdvertisement();
        }
        catch (Exception e) {
            System.err.println("Cannot get allPurposePeerGroupImplAdv " + e);
	    return null;
        }
        
	// Get the param field that conatins all the peergroup services
	// associated with the peergroup
        try {
            PeerGroupParamAdv = new StdPeerGroupParamAdv(
			        allPurposePeerGroupImplAdv.getParam());
        }
        catch (PeerGroupException e) {
            System.err.println("Cannot get StdPeerGroupParamAdv " + e);
 	    return null;
        }
	
	// Get the hashtable containaing the list of all the peergroup
	// services from the param advertisements
        Hashtable allPurposePeerGroupServicesHashtable =
            PeerGroupParamAdv.getServices();

	// Add our new peergroup service to the list of peergroup services
	allPurposePeerGroupServicesHashtable.put(mcID, serviceModuleImplAdv);
	
	// Update the PeerGroupModuleImplAdv with our new list
	// of peergroup services
	allPurposePeerGroupImplAdv.setParam((Element) 
	        PeerGroupParamAdv.getDocument(new MimeMediaType("text/xml")));

	// Set the new unique Spec ID that identifies this new peergroup
	// implementation
	allPurposePeerGroupImplAdv.setModuleSpecID(msID);
 
        // We are done creating our new peergroup implementation
        return allPurposePeerGroupImplAdv;
    
private net.jxta.peergroup.PeerGroupcreateRestoPeerGroup()


	// Use a unique PeerGroup id as a constant so that the same
	// peergroup ID is used each time the RestoNet Peergroup is created.
	// It is essential that each RestoPeer use the same unique ID
	// so that two peers do not create different IDs for the
	// RestoNet peergroup. 
	//
	// The UUID in the URL constructor was generated via the
	// Shell mkpgrp command that created a new peergroup with a
	// unique peergroup ID.
        try {
            restoPeerGroupID = (PeerGroupID) IDFactory.fromURL(
                  new URL("urn", "",
                          "jxta:uuid-4d6172676572696e204272756e6f202002"));
        } catch (java.net.MalformedURLException e) {
            System.err.println("Can't create restoPeerGroupID: " + e);
            System.exit(1);
        } catch (java.net.UnknownServiceException e) {
            System.err.println("Can't create restoPeerGroupID: " + e);
            System.exit(1);
        }

	// Create a new Module Implementation advertisement that will
	// represent the new RestoPeer peergroup service

	// Assign a new SpecID that uniquely identifies the RestoPeer
	// peergroup service. Ths spec ID must be shared between all
	// instances of the RestoNet peergroup; it again is created via
	// the mkpgrp Shell command.
	ModuleSpecID  msrvID = null;
	try {
	    msrvID = (ModuleSpecID) IDFactory.fromURL(new URL("urn","",
		"jxta:uuid-737D1ED776B043E7A8718B102B62055A614CAC047AD240A8960ABDE6F7847C2306"));
	} catch (java.net.MalformedURLException e) { 
            System.err.println("Can't create restoPeer Spec Id:" + e);
            System.exit(1);
        } catch (java.net.UnknownServiceException e) {
            System.err.println("Can't create restoPeer Spec Id:" + e);
            System.exit(1);
        }        

	// Create a pipe advertisement to be used by Hungry Peers
	// to communicate with the RestoNet peergroup service
	PipeAdvertisement myAdv = (PipeAdvertisement)
	    AdvertisementFactory.newAdvertisement(
	    PipeAdvertisement.getAdvertisementType());
		
	// Again we assign a hardwired ID to the pipe, so everytime a
	// peer recreates the RestoNet peergroup, the same pipe id
	// is used.
	try {
	    myAdv.setPipeID((PipeID) IDFactory.fromURL(new URL("urn","",
              "jxta:uuid-9CCCDF5AD8154D3D87A391210404E59BE4B888209A2241A4A162A10916074A9504")));
	} catch (java.net.MalformedURLException e) {
            System.err.println("Can't create restoPeer PipeID: " + e);
            System.exit(1);
        } catch (java.net.UnknownServiceException e) {
            System.err.println(" Can't create restoPeer PipeID: " + e);
            System.exit(1);
        }        

	// The symbolic name of the pipe is built from
	// the brand name of RestoPeer. So each RestoPeer instance
	// must have a unique name.
	myAdv.setName("RestoNet:RestoPipe:" + brand);
	myAdv.setType(PipeService.UnicastType);

	// Create the module impl advertisement that represents
	// the RestoPeer service.
	// Peergroup services are also represented by modules since a
	// peergroup service needs to be dynamically loaded when the
	// corresponding peergroup is instantiated on the peer.
	// The constructor of the service 
	// module impl takes a set of arguments to describe the service:
	//    msrvID : unique spec ID of the restoNet service
	//    "RestoPeerService" : is the java class implementing the service
	//    "http://jxta.org/tutorial/RestoPeer.jar" : location to find jar file
	//    "sun.com" : provider of the service
	//    a description of the service
        ModuleImplAdvertisement restoImplAdv =
                createServiceModuleImplAdv(msrvID,
		"RestoPeerService", "http://jxta.org/tutorial/RestoPeer.jar",
		"sun.com","RestoPeer Service Module Implementation", myAdv);

        // Create a new peergroup that is a clone of the NetPeergroup
        // but with the added RestoPeer Service.
        return createPeerGroup(netpg, "RestoNet", restoImplAdv);
    
private net.jxta.protocol.ModuleImplAdvertisementcreateServiceModuleImplAdv(net.jxta.platform.ModuleSpecID id, java.lang.String code, java.lang.String uri, java.lang.String provider, java.lang.String desc, net.jxta.protocol.PipeAdvertisement padv)


	// Construct a new module implementation advertisment
        ModuleImplAdvertisement ServiceModuleImplAdv =
            (ModuleImplAdvertisement)
            AdvertisementFactory.newAdvertisement(
                ModuleImplAdvertisement.getAdvertisementType());

	// Set a unique module Spec ID associated with this
	// service implementation
        ServiceModuleImplAdv.setModuleSpecID(id);

	// Set the java class that implements this service
        ServiceModuleImplAdv.setCode(code);
	
	// Set the description of the new service
        ServiceModuleImplAdv.setDescription(desc);

	// Define the compatibility Java JDK1.4 for loading this module
	StructuredTextDocument doc = (StructuredTextDocument)
                StructuredDocumentFactory.newStructuredDocument(
                    new MimeMediaType("text", "xml"), "Comp");
        Element e = doc.createElement("Efmt", "JDK1.4");
        doc.appendChild(e);
        e = doc.createElement("Bind", "V1.0 Ref Impl");
        doc.appendChild(e);
        ServiceModuleImplAdv.setCompat(doc);

	// Set the URL where we can download the code for this module
        ServiceModuleImplAdv.setUri(uri);

	// Set the provider for this module
        ServiceModuleImplAdv.setProvider(provider);

	// Set the pipe advertisement into a param doc to be used 
	// by a peer to connect to the service
	StructuredTextDocument paramDoc = (StructuredTextDocument)
                 StructuredDocumentFactory.newStructuredDocument
                 (new MimeMediaType("text/xml"), "Parm");
        StructuredDocumentUtils.copyElements(paramDoc, paramDoc,	
		 (Element) padv.getDocument(new MimeMediaType("text/xml")));
        ServiceModuleImplAdv.setParam(paramDoc);

	// Return the new module implementation for this service
        return ServiceModuleImplAdv;
    
private voidjoinRestoNet()


        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", "RestoNet");

                // 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", "RestoNet", 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 the RestoNetPeerGroup
		restoNet = createRestoPeerGroup();
		
		// Get the PeerGroup Advertisement
		restoNetAdv = netpg.getPeerGroupAdvertisement();
            } catch (Exception e) {
                System.out.println("Error in creating RestoNet Peergroup");
                throw e;
            }
        } else {
            // The RestoNet advertisement was found in the cache;
            // that means we can join the existing RestoNet peergroup

            try {
                restoNetAdv = (PeerGroupAdvertisement) ae.nextElement();
                restoNet = netpg.newGroup(restoNetAdv);
                System.out.println(
                    "Found the RestoNet Peergroup advertisement");
            } catch (Exception e) {
                System.out.println("Error in creating RestoNet PeerGroup from existing adv");
                throw e;
            }
        }

        try {
            // Get the discovery and pipe services for the RestoNet Peergroup
            disco = restoNet.getDiscoveryService();
            pipes = restoNet.getPipeService();
        } catch (Exception e) {
            System.out.println("Error getting services from RestoNet");
            throw e;
        }

        System.out.println("RestoNet Restaurant (" + brand + ") is on-line");
        return;
    
public static voidmain(java.lang.String[] args)


   // main method to start our RestoPeer
         
	RestoPeer myapp = new RestoPeer();
	myapp.startJxta();
	System.exit(0);
    
private voidstartJxta()

        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);
        }

        // Wait while we process requests
        synchronized(RestoPeer.class) {
            try {
                RestoPeer.class.wait();
	    } catch (InterruptedException ie) {
	        System.out.println("Interrupted; exiting");
	    }
        }