📄 hungrypeer.java
字号:
import java.io.*;import java.util.Enumeration;import java.util.Vector;import java.net.URL;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupFactory;import net.jxta.exception.PeerGroupException;import net.jxta.exception.ServiceNotFoundException;import net.jxta.document.AdvertisementFactory;import net.jxta.document.Advertisement;import net.jxta.document.StructuredDocument;import net.jxta.document.Element;import net.jxta.document.TextElement;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.MimeMediaType;import net.jxta.discovery.DiscoveryService;import net.jxta.pipe.PipeService;import net.jxta.pipe.InputPipe;import net.jxta.pipe.PipeID;import net.jxta.pipe.OutputPipe;import net.jxta.endpoint.Message;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;// The HungryPeer is joining the RestoNet PeerGroup and searching// for RestoPeers. The HungryPeer is establishing a pipe connection// with all the RestoPeers that it has discovered. The Hungry peer// sends auction requests for French fries to RestoPeers and// then wait for auction bids from RestoPeerspublic class HungryPeer { private PeerGroup netpg = null; // NetPeergroup private PeerGroup restoNet = null; // Resto Peergroup private DiscoveryService disco; // Discovery Service private PipeService pipes; // Pipe Service private PipeAdvertisement myAdv; // Hungry peer pipe advertisement private InputPipe myPipe; // Input pipe to talk to hungry peer private MimeMediaType mimeType = new MimeMediaType("text", "xml"); // mime-type private int timeout = 3000; // Discovery timeout private int rtimeout = 30000; // Pipe Resolver Timeout private PeerGroupAdvertisement adv; //RestoNet Peergroup advertisement private Vector restoPeerAdvs = new Vector(); // RestoPeers adv found private Vector restoPeerPipes = new Vector(); // PestoPeers Pipe connection private String myIdentity = "Bill Joy"; // Identity of this HungryPeer private String friesRequest ="medium"; // Fries Auction request // Hungry Peer Main method to start our HungryPeer public static void main(String args[]) { HungryPeer myapp = new HungryPeer(); myapp.startJxta(); System.exit(0); } public void HungryPeer() { } // method to start the JXTA platform and our // HungryPeer private void startJxta() { try { // create, and Start the default jxta NetPeerGroup netpg = PeerGroupFactory.newNetPeerGroup(); } catch (PeerGroupException e) { // could not instanciate the NetPeerGroup System.out.println("Fatal error : creating the NetPeerGroup"); System.exit(1); } // Discover and Join the RestoNet Peergroup joinRestoNet(); //Set our HungryPeer communication pipe so RestoPeers //can talk to us if (!setHungryPeerPipe()) { System.out.println( "Aborting due to failure to create our HungryPeer pipe"); return; } //Attempt to locate RestoPeers in RestoNet discoverRestoServices(); // Connect to RestoPeers that have been discovered connectToRestoPeers(); // I am hungry send an auction request for French Fries // to the RestoPeers I have connected to sendFriesAuctionRequests(); //Process incoming bids from RestoPeers receiveFriesBids(); } // This method is used to attempt to discover the RestoNet // Peergroup in the current NetPeerGroup. If found the peer join // the RestoNet peergroup private boolean joinRestoNet() { int count = 3; // number of tries to search for the peergroup System.out.println("Attempting to discover the RestoNet Peergroup"); // get the Discovery service handle from the NetPeerGroup pointer DiscoveryService hdisco = netpg.getDiscoveryService(); Enumeration ae = null; // loop until we found the "RestoNet" Peergroup advertisement while (count-->0) { try { // Check if we have the advertisement in the local // peer cache ae = hdisco.getLocalAdvertisements(DiscoveryService.GROUP , "Name" , "RestoNet"); // check if we got the advertisement, if we got // it we can exit the loop. Not necessary to go // further if ((ae != null) && ae.hasMoreElements()) break; // The "RestoNet" advertisement is not in the local // cache send a discovery request to search for the advertisement hdisco.getRemoteAdvertisements(null, DiscoveryService.GROUP, "Name", "RestoNet", 1, null); // wait to give a chance to the discovery try { Thread.sleep(timeout); } catch (Exception e){ } } catch (IOException e){ // found nothing! move on } } // check if we found the RestoNet advertisement if (ae == null || !ae.hasMoreElements()) { System.out.println("Sorry could not find the RestoNet Peergroup"); return false; } System.out.println("Found the RestoNet PeerGroup Advertisement"); // Get the advertisement PeerGroupAdvertisement adv = (PeerGroupAdvertisement) ae.nextElement(); try { // Call the PeerGroup Factory to instantiate a new // peergroup instance on that peer restoNet = netpg.newGroup(adv); // get the Discovery and Pipe Service to // be used within the RestoNet Peergroup disco = restoNet.getDiscoveryService(); pipes = restoNet.getPipeService(); } catch (Exception e) { System.out.println("Could not create RestoPeerGroup"); return false; } System.out.println("The HungryPeer joined the restoNet PeerGroup"); return true; } // Create the HungryPeer Communication pipe to receive bid responses // from RestoPeers. The advertisement of this pipe is sent as part // of the auction request for RestoPeers to respond. private boolean setHungryPeerPipe() { try { // get the Discovery and Pipe Service to // be used within the RestoNet Peergroup disco = restoNet.getDiscoveryService(); pipes = restoNet.getPipeService(); // Create a pipe advertisement for our hungry peer. This // pipe will be used within the RestoNet peergroup for other // peers to talk to our hungry peer myAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement( PipeAdvertisement.getAdvertisementType() ); // Initialize the advertisement with unique peer information // So we can communicate myAdv.setPipeID(IDFactory.newPipeID(restoNet.getPeerGroupID() ) ); myAdv.setName("restoNet:HungryPipe:" + myIdentity); myAdv.setType(PipeService.UnicastType); // Create the input pipe myPipe = pipes.createInputPipe(myAdv); } catch (Exception e) { System.out.println("Could not create the HungryPeer pipe"); return false; } return true; } // Discover RestoPeers that have joined RestoNet. We discover RestoPeer // via their published Service advertisement private void discoverRestoServices() { int found = 0; // Count of RestoPeer found RestoPeerService restoSrv = null; // RestoPeer Service Advertisement System.out.println("Locating RestoPeers in the RestoNet Peergroup");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -