📄 peer.java
字号:
/* * Created on 22-ott-2005 * *//** * @author magowiz * */package p2p_system;import gui.MainWindow;import org.w3c.dom.*;import java.net.MalformedURLException;import java.net.URL;import java.net.UnknownServiceException;import java.util.Hashtable;import java.util.regex.Matcher;import java.util.regex.Pattern;import net.jxta.discovery.DiscoveryService;import net.jxta.document.AdvertisementFactory;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocument;import net.jxta.exception.PeerGroupException;import net.jxta.exception.ServiceNotFoundException;import net.jxta.id.*;import net.jxta.impl.peergroup.StdPeerGroupParamAdv;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupFactory;import net.jxta.peergroup.PeerGroupID;import net.jxta.platform.ModuleClassID;import net.jxta.platform.ModuleSpecID;import net.jxta.protocol.ModuleClassAdvertisement;import net.jxta.protocol.ModuleImplAdvertisement;import net.jxta.protocol.ModuleSpecAdvertisement;import net.jxta.protocol.PeerGroupAdvertisement;public class Peer implements catalog_exchange.CatalogExchangeListener, BCListener{ private static final String refPeerGroupSpec = "urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE00000001C1457D70B58D4B58BE50A57CE53D844706"; private static final String refPeerGroupID = "urn:jxta:uuid-C5463CB0A2D5442891B3F34CC80FB0A502"; private static final String refModuleClassID = "urn:jxta:uuid-1DE3866428A649E4A5A4B3738C4A0F1305"; private static final String refModuleSpecID = "urn:jxta:uuid-1DE3866428A649E4A5A4B3738C4A0F131A04C7BBC9364C628582F3939278AEBE06"; private static String rPGS= //IDFactory.newModuleSpecID( //PeerGroup.allPurposePeerGroupSpecID.getBaseClass()); "urn:jxta:uuid-DEADBEEFDEAFBABAFEEDBABE00000001D7E32D12887E4F83A0A68114E55D83DF06"; private static String rPGID = //IDFactory.newPeerGroupID(); "urn:jxta:uuid-4B66A3027E434D47930EEBB0E5A4606602"; private static String rMCID = //IDFactory.newModuleClassID(); "urn:jxta:uuid-F37F018F2CEA4896AA3E900E4BF53A3305"; private static String rMSID = //IDFactory.newModuleSpecID(rMCID); "urn:jxta:uuid-F37F018F2CEA4896AA3E900E4BF53A333289FF55F4E340B990F3960FD9BBE16406"; private PeerGroup newGroup = null; private PeerGroup newGroup2=null; private PeerGroup netPeerGroup = null; private local_catalog_manager.LocalCatalogManager lcm=null; private int qID; private int qIDS; private catalog_exchange.CatalogExchangeService ceserv=null; private catalog_search.CatalogSearchService csserv=null; private MainWindow w; private BookmarkedCatalogs bcats; private boolean received; private String clabel; private String cname; private URL curl; public void initializeJXTA() throws PeerGroupException { netPeerGroup = PeerGroupFactory.newNetPeerGroup(); } public void createPeerGroup() throws Exception, PeerGroupException { // The name and description for the peer group. String name = "University P2P Sharing"; String description = "Sharing Knowledge Group"; String n2="up2p sharing"; String d2="shKG"; // The Discovery service to use to publish the module and peer // group advertisements. DiscoveryService discovery = netPeerGroup.getDiscoveryService(); // Obtain a preformed ModuleImplAdvertisement to use when creating // the new peer group. This is the Module Implementation // Advertisement of the Net Peer Group and contains all of the // services and applications already configured to run in that peer // group. Using this method simplifies the task of creating a new // peer group and configuring a new service. ModuleImplAdvertisement implAdv = netPeerGroup.getAllPurposePeerGroupImplAdvertisement(); ModuleImplAdvertisement iA= netPeerGroup.getAllPurposePeerGroupImplAdvertisement(); // Create the Module Class Advertisement. ModuleClassAdvertisement moduleClassAdv = createModuleClassAdv(); ModuleClassAdvertisement mca=createModuleClassAdvCS(); //System.out.println("mca:"+mca); // Create the Module Specification Advertisement. ModuleSpecAdvertisement moduleSpecAdv = createModuleSpecAdv(); ModuleSpecAdvertisement msa = createModuleSpecAdvCS(); //System.out.println("msa:"+msa); // Create the Module Implementation Advertisement. ModuleImplAdvertisement moduleImplAdv = createModuleImplAdv(implAdv,moduleSpecAdv); ModuleImplAdvertisement mia = createModuleImplAdvCS(iA,msa); //System.out.println("mia:"+mia); // Get the parameters for the peer group's Module Implementation // Advertisement to add our service. StdPeerGroupParamAdv params = new StdPeerGroupParamAdv(implAdv.getParam()); StdPeerGroupParamAdv p = new StdPeerGroupParamAdv(iA.getParam()); // Get the services from the parameters. Hashtable services = params.getServices(); Hashtable s=p.getServices(); // Add our service to the set of services. services.put(moduleClassAdv.getModuleClassID(), moduleImplAdv); s.put(mca.getModuleClassID(), mia); // Set the services on the parameters, and set the parameters on // the implementation advertisement. params.setServices(services); p.setServices(s); implAdv.setParam((StructuredDocument) params.getDocument(new MimeMediaType("text", "xml"))); iA.setParam((StructuredDocument) p.getDocument(new MimeMediaType("text", "xml"))); //implAdv.setParam((StructuredDocument) params.getDocument( // MimeMediaType.XMLUTF8)); // VERY IMPORTANT! You must change the Module Specification ID // for the implementation advertisement. If you don't, the new // peer group's Module Specification ID will still point to the // old specification, and the new service will not be loaded. implAdv.setModuleSpecID((ModuleSpecID) IDFactory.fromURL( new URL(refPeerGroupSpec))); iA.setModuleSpecID((ModuleSpecID) IDFactory.fromURL( new URL(rPGS))); // implAdv.setModuleSpecID(IDFactory); // Publish the Module Class and Specification Advertisements. discovery.publish(moduleClassAdv,DiscoveryService.ADV); discovery.remotePublish(moduleClassAdv,DiscoveryService.ADV); discovery.publish(mca,DiscoveryService.ADV); discovery.remotePublish(mca,DiscoveryService.ADV); discovery.publish(moduleSpecAdv, DiscoveryService.ADV); discovery.remotePublish(moduleSpecAdv,DiscoveryService.ADV); discovery.publish(msa, DiscoveryService.ADV); discovery.remotePublish(msa,DiscoveryService.ADV); discovery.publish(implAdv, DiscoveryService.ADV); discovery.remotePublish(implAdv,DiscoveryService.ADV); discovery.publish(iA, DiscoveryService.ADV); discovery.remotePublish(iA,DiscoveryService.ADV); // Create the Peer Group ID. PeerGroupID groupID = (PeerGroupID) IDFactory.fromURL( new URL((refPeerGroupID))); PeerGroupID gID = (PeerGroupID) IDFactory.fromURL( new URL((rPGID))); //PeerGroupID groupID= IDFactory.newPeerGroupID(); // Create the new group using the group ID, advertisement, name, // and description. newGroup = netPeerGroup.newGroup(groupID, implAdv, name, description); newGroup2 = netPeerGroup.newGroup(gID,iA,n2,d2); //System.out.println("pg:"+newGroup2); // Need to publish the group remotely only because newGroup() // handles publishing to the local peer. PeerGroupAdvertisement groupAdv = newGroup.getPeerGroupAdvertisement(); discovery.remotePublish(groupAdv, DiscoveryService.GROUP); PeerGroupAdvertisement gA = newGroup2.getPeerGroupAdvertisement(); discovery.remotePublish(gA, DiscoveryService.GROUP); //System.out.println(gA); } public void processAnswer(catalog_exchange.CatServEvt event) { // Extract the response message from the event object. catalog_exchange.CatalogExchangeResponse er = event.getResponse(); // Print out the answer given in the response. System.out.println("response from peer: "+er.getGot()); w.insertLogEntry("response from peer: "+er.getGot()); // Print out the answer given in the response. if ((er.getGot().equals("false"))) { try { // Find the service on the peer group. ModuleClassID classID = (ModuleClassID) IDFactory.fromURL( new URL((refModuleClassID))); //ModuleClassID classID=IDFactory.newModuleClassID(); catalog_exchange.CatalogExchangeService scs = (catalog_exchange.CatalogExchangeService) newGroup.lookupService(classID); cname=new String(er.getCatalogName()); if(!(scs.isInLocalCache(cname))) { curl=new URL(er.getCatalogUrl().toString()); scs.getCatalogFromWeb(er.getCatalogName(),er.getCatalogUrl()); } else { w.insertLogEntry("Noone of the peers have catalog "+cname+" but you've got it in local cache using that version"); System.out.println("Noone of the peers have catalog "+cname+" but you've got it in local cache using that version"); this.received=true; } } catch (Exception e2) { // Warn the user. e2.printStackTrace(); } } else if((er.getGot().equals("newer"))||(er.getGot().equals("equal"))||(er.getGot().equals("true"))){ received=true; try { BookmarkedCatalogItem bci=new BookmarkedCatalogItem(new String(clabel),new String(er.getCatalogName()),new URL(er.getCatalogUrl().toString())); bcats.insert(bci); } catch(Exception e3) { e3.printStackTrace(); } } } private ModuleClassAdvertisement createModuleClassAdvCS() throws UnknownServiceException, MalformedURLException { ModuleClassID classID = (ModuleClassID) IDFactory.fromURL( new URL((rMCID))); ModuleClassAdvertisement moduleClassAdv = (ModuleClassAdvertisement) AdvertisementFactory.newAdvertisement( ModuleClassAdvertisement.getAdvertisementType()); // Configure the Module Class Advertisement. moduleClassAdv.setDescription("A service to handle University Catalog Search"); moduleClassAdv.setModuleClassID(classID); moduleClassAdv.setName("Distributed catalog search Class"); // Return the advertisement to the caller. return moduleClassAdv; } private ModuleClassAdvertisement createModuleClassAdv() throws UnknownServiceException, MalformedURLException{ // Create the class ID from the refModuleClassID string. ModuleClassID classID = (ModuleClassID) IDFactory.fromURL( new URL((refModuleClassID))); //ModuleClassID classID=IDFactory.newModuleClassID(); //System.out.println("classid"+classID.toString()); // Create the Module Class Advertisement. ModuleClassAdvertisement moduleClassAdv = (ModuleClassAdvertisement) AdvertisementFactory.newAdvertisement( ModuleClassAdvertisement.getAdvertisementType()); // Configure the Module Class Advertisement. moduleClassAdv.setDescription("A service to handle University Catalog Exchange and Management"); moduleClassAdv.setModuleClassID(classID); moduleClassAdv.setName("Distributed catalog management Class"); // Return the advertisement to the caller. return moduleClassAdv;} private ModuleSpecAdvertisement createModuleSpecAdvCS() { // Create the specification ID from the refModuleSpecID string. ModuleSpecID specID=null; try{specID = (ModuleSpecID) IDFactory.fromURL( new URL((rMSID)));} catch(UnknownServiceException use){System.out.println("use");use.printStackTrace();} catch(MalformedURLException mue){mue.printStackTrace();} //ModuleSpecID specID=IDFactory.newModuleSpecID(); // Create the Module Specification Advertisement. ModuleSpecAdvertisement moduleSpecAdv = (ModuleSpecAdvertisement) AdvertisementFactory.newAdvertisement( ModuleSpecAdvertisement.getAdvertisementType()); // Configure the Module Specification Advertisement. //moduleSpecAdv.setCreator("magowiz"); moduleSpecAdv.setDescription("A specification for a University Catalog search."); moduleSpecAdv.setModuleSpecID(specID); moduleSpecAdv.setName("University Catalog search Spec"); moduleSpecAdv.setSpecURI("http://localhost"); moduleSpecAdv.setVersion("1.0"); // Return the advertisement to the caller. return moduleSpecAdv; } private ModuleSpecAdvertisement createModuleSpecAdv() { // Create the specification ID from the refModuleSpecID string. ModuleSpecID specID=null; try{specID = (ModuleSpecID) IDFactory.fromURL( new URL((refModuleSpecID)));} catch(MalformedURLException mue){mue.printStackTrace();} catch(UnknownServiceException use){use.printStackTrace();} //ModuleSpecID specID=IDFactory.newModuleSpecID(); // Create the Module Specification Advertisement. ModuleSpecAdvertisement moduleSpecAdv = (ModuleSpecAdvertisement) AdvertisementFactory.newAdvertisement( ModuleSpecAdvertisement.getAdvertisementType()); // Configure the Module Specification Advertisement. //moduleSpecAdv.setCreator("magowiz"); moduleSpecAdv.setDescription("A specification for a University Catalog service."); moduleSpecAdv.setModuleSpecID(specID); moduleSpecAdv.setName("University Catalog service Spec"); //moduleSpecAdv.setSpecURI("http://www.brendonwilson.com/projects/jxta"); moduleSpecAdv.setVersion("1.0"); // Return the advertisement to the caller. return moduleSpecAdv;} private ModuleImplAdvertisement createModuleImplAdvCS( ModuleImplAdvertisement groupImpl, ModuleSpecAdvertisement moduleSpecAdv) { // Get the specification ID from the passed advertisement. ModuleSpecID specID = moduleSpecAdv.getModuleSpecID(); // Create the Module Implementation Advertisement. ModuleImplAdvertisement moduleImplAdv = (ModuleImplAdvertisement) AdvertisementFactory.newAdvertisement( ModuleImplAdvertisement.getAdvertisementType()); // Configure the Module Implementation Advertisement. moduleImplAdv.setCompat(groupImpl.getCompat()); moduleImplAdv.setCode("catalog_search.CatalogSearchServiceImpl"); moduleImplAdv.setUri("http://localhost/CatSearchService.jar"); // moduleImplAdv.setUri("file://home/magowiz/workspace/university-sharing/catalog_exchange/CatExcService.jar"); moduleImplAdv.setDescription("Reference Catalog Search implementation"); moduleImplAdv.setModuleSpecID(specID); //moduleImplAdv.setProvider("magowiz"); // Return the advertisement to the caller. return moduleImplAdv; } private ModuleImplAdvertisement createModuleImplAdv( ModuleImplAdvertisement groupImpl, ModuleSpecAdvertisement moduleSpecAdv) { // Get the specification ID from the passed advertisement. ModuleSpecID specID = moduleSpecAdv.getModuleSpecID(); // Create the Module Implementation Advertisement. ModuleImplAdvertisement moduleImplAdv = (ModuleImplAdvertisement) AdvertisementFactory.newAdvertisement( ModuleImplAdvertisement.getAdvertisementType()); // Configure the Module Implementation Advertisement. moduleImplAdv.setCompat(groupImpl.getCompat()); moduleImplAdv.setCode("catalog_exchange.CatalogExchangeServiceImpl"); moduleImplAdv.setUri("http://localhost/CatExcService.jar"); // moduleImplAdv.setUri("file://home/magowiz/workspace/university-sharing/catalog_exchange/CatExcService.jar"); moduleImplAdv.setDescription("Reference Catalog Exchange implementation"); moduleImplAdv.setModuleSpecID(specID); //moduleImplAdv.setProvider("magowiz"); // Return the advertisement to the caller. return moduleImplAdv; } private void showGUI() { w=new MainWindow(); w.init(this,bcats); } public static void main(String args[]) { Peer p = new Peer(); try { // Initialize the JXTA platform. p.initializeJXTA(); // Create the group. p.createPeerGroup(); p.init(); // Show a GUI to accept input. p.showGUI(); /*while(true) { p.sendMessage(); }*/ p.downloadAllBookmarked(); p.refreshCatalogs(); while(true){ try{Thread.sleep(60000);}catch(InterruptedException ie){} } } catch (Exception e) { System.out.println("Error starting JXTA platform: " + e);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -