📄 restopeer.java
字号:
import java.io.*;import java.util.*;import java.net.URL; 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.document.AdvertisementFactory;import net.jxta.document.Advertisement;import net.jxta.document.Element;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredTextDocument;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.StructuredDocument;import net.jxta.document.StructuredDocumentUtils;import net.jxta.pipe.PipeService;import net.jxta.pipe.PipeID;import net.jxta.protocol.PipeAdvertisement;import net.jxta.protocol.PeerGroupAdvertisement;import net.jxta.protocol.ModuleSpecAdvertisement;import net.jxta.protocol.ModuleImplAdvertisement;import net.jxta.id.IDFactory;import net.jxta.id.ID;import net.jxta.platform.ModuleClassID;import net.jxta.platform.ModuleSpecID;import net.jxta.impl.peergroup.StdPeerGroupParamAdv;// 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 resturant 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 // 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 int timeout = 3000; // discovery wait timeout // IDs within RestoNet private ModuleClassID mcID = IDFactory.newModuleClassID(); private ModuleSpecID msID = IDFactory.newModuleSpecID(mcID); private static PeerGroupID restoPeerGroupID; // main method to start our 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); } // Wait while we process requests synchronized(RestoPeer.class) { try { RestoPeer.class.wait(); } catch (InterruptedException ie) { System.out.println("Interrupted; exiting"); } } } // 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", "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; } // This method is used to create a new instance of // the RestoNet peergroup. // Peergroups are implemented as modules. // Modules are used in JXTA to load and manage dynamic code on a peer. // // A peergroup is represented by a set of advertisements: // 1) A peergroup advertisement that advertises the peergroup // 2) A module spec advertisement that uniquely // specifies the peergroup (set of peergroup services) // 3) A module impl advertisement that describes a // peergroup implementation // This method must create all these advertisements private PeerGroup createRestoPeerGroup() { // 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -