⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 restopeer.java

📁 这个事关于jxta编程的入门级代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	// 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);    }    // This is a utility method to create a new peergroup    // implementation. The new peergroup will have all the    // same services as the parent peergroup; it will have an    // additional service (the srvImpl).    //     // The method creates the    // new Peer Group Module Implementation Advertisement and    // publishes it; creates the associated PeerGroup Adv and    // publishes it; and instantiates the peergroup    // using the just created PeerGroup Adv.    //      // parent: parent peergroup the new group is created into    // groupName : name of the new group    // srvImpl :  new peergroup service to add to the default set of    //            NetPeergroup services    private PeerGroup  createPeerGroup(PeerGroup parent, String groupName, 				       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;    }    // This method is a generic method used to create a new    // module impl advertisement for representing a peergroup    // service. Each module implementation corresponds to a unique    // module spec id that uniquely identifies a peergroup service.    //    // A peergroup service is represented by:    //     id: unique module spec id identifying the new peergroup service    //     code: java class that implement the peergroup service    //     uri: location where we can download the java class implementing the service    //     provider: identity of the provider    //     desc: description of the service.    //     padv: pipe advertisement to invoke the service    private  ModuleImplAdvertisement createServiceModuleImplAdv(               ModuleSpecID id,	       String       code,	       String       uri,	       String       provider,	       String       desc,	       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;    }    // Create a Module Impl Advertisement that represents a peergroup    // which is a clone of the NetPeerGroup, but adds a new peergroup    // service to the peergroup.    private ModuleImplAdvertisement createPeerGroupModuleImplAdv(                PeerGroup parent, 		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;    }    // This utility method is used to create a PeerGroup advertisement    // associated with a module peergroup implementation    private PeerGroupAdvertisement createPeerGroupAdvertisement(	 ModuleImplAdvertisement implAdv, 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;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -