📄 genericpeergroup.java
字号:
/**
* Stops the group and all its services.
*/
public void stopApp() {
removeAllServicesSync();
}
/**
* Implement the Service API so that we can make groups services when we
* decide to.
*/
/**
* Service objects are not manipulated directly to protect usage
* of the service. A Service interface is returned to access the service
* methods.
*
* @return Service public interface of the service
*
* @since JXTA 1.0
*/
public Service getInterface() {
// Not messing with that for now, just implement the intf.
return this;
}
/**
* Returns the advertisment for this service.
*
* @return Advertisement the advertisement.
*
* @since JXTA 1.0
*/
public Advertisement getImplAdvertisement() {
return (Advertisement) implAdvertisement.clone();
}
/**
* Force publication of this group if hasn't been done already.
* Calling this routine is only usefull if the group is being created
* from scratch and the PeerGroup advertisement has not been
* created beforehand. In such a case, the group has never been named or
* described. Therefore this information has to be supplied here.
*
* If this group has already been published, this method does nothing.
*
* @param name The name of this group.
* @param Description The description of this group.
*/
public void publishGroup(String name, String description)
throws IOException {
if (published) return;
peerGroupAdvertisement.setName(name);
peerGroupAdvertisement.setDescription(description);
if (parentGroup == null) return;
parentGroup.getDiscoveryService().publish(peerGroupAdvertisement,
DiscoveryService.GROUP,
DEFAULT_LIFETIME,
DEFAULT_EXPIRATION);
published = true;
}
/**
* Instantiate a group from its given advertisement
* Use this when a published implAdv for the groupSubclass
* can be discovered. The pgAdv itself may be all new and unpublished.
* Therefore, the two typical uses of this routine are:
*
* - Creating an all new group with a new ID while using an existing
* and published implementation. (Possibly a new one published for that
* purpose). The information should first be gathered in a new
* PeerGroupAdvertisement which is then passed to this method.
*
* - Instantiating a group which advertisement has already been discovered
* (therefore there is no need to find it by groupID again).
*
* To create a group from a known implAdv, use
* newGroup(gid, impladv, name, description);
*
* @param pgAdv The advertisement of that group.
* @return PeerGroup the initialized (but not started) peergroup.
*/
public PeerGroup newGroup(Advertisement pgAdv)
throws PeerGroupException {
PeerGroupAdvertisement adv = (PeerGroupAdvertisement) pgAdv;
PeerGroup theNewGroup = (PeerGroup) loadModule(adv.getPeerGroupID(),
adv.getModuleSpecID(),
Here);
if (theNewGroup == null) {
throw new PeerGroupException("Could not find all implementations.");
}
try {
// Since we do not know if the grp adv had been previously
// published or not...
theNewGroup.publishGroup(adv.getName(), adv.getDescription());
} catch (Exception any) {
if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Could not publish the group advertisement: "
+ any.getMessage());
}
return theNewGroup;
}
/**
* Convenience method, instantiate a group from its elementary pieces
* and publish the corresponding PeerGroupAdvertisement.
* The pieces are: the groups implementation adv, the group id,
* the name and description.
*
* The typical use of this routine is creating a whole new group based
* on a newly created and possibly unpublished implementation adv.
*
* This is equivalent to either:
*
* newGrp = thisGroup.loadModule(gid, impl);
* newGrp.publishGroup(name, description);
*
* or, but only if the implementation adv has been published:
*
* newPGAdv = AdvertisementFactory.newAdvertisement(
* new MimeMediaType("text", "xml")),
* PeerGroupAdvertisement.getAdvertisementType());
* newPGAdv.setPeerGroupID(gid);
* newPGAdv.setModuleSpecID(impl.getModuleSpecID());
* newPGAdv.setName(name);
* newPGAdv.setDescription(description);
* newGrp = thisGroup.newGroup(newPGAdv);
*
* @param gid The ID of that group.
* @param impl The advertisement of the implementation to be used.
* @param name The name of the group.
* @param description A description of this group.
*
* @return PeerGroup the initialized (but not started) peergroup.
*/
public PeerGroup newGroup(PeerGroupID gid, Advertisement impl,
String name, String description)
throws PeerGroupException {
PeerGroup theNewGroup;
try {
theNewGroup = (PeerGroup) loadModule(gid,
impl);
} catch (Exception any) {
throw new PeerGroupException(any.getMessage());
}
try {
// The group adv definitely needs to be published.
theNewGroup.publishGroup(name, description);
} catch (Exception any) {
if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Could not publish group or implementation:"
+ any.toString());
}
return theNewGroup;
}
/**
* Instantiate a group from its groupID only.
* Use this when using a group that has already been published and
* discovered.
*
* The typical uses of this routine are therefore:
*
* - instantiating a group which is assumed to exist and which GID is
* known.
*
* - creating a new group using an already published advertisement.
* Typically published for that purpose. All other implied advertisements
* must also have been published beforehand if they did not exist.
*
* To create a group from a known implAdv, just use
* loadModule(gid, implAdv) or even:
* grp = new GroupSubClass();
* grp.init(parentGroup, gid, impladv);
* then, REMEMBER TO PUBLISH THE GROUP IF IT IS ALL NEW.
*
* @param groupID the groupID.
* @return PeerGroup the initialized (but not started) peergroup.
*/
public PeerGroup newGroup(PeerGroupID gid)
throws PeerGroupException {
PeerGroupAdvertisement adv;
try {
adv = (PeerGroupAdvertisement)
discoverOne(DiscoveryService.GROUP, "GID", gid.toString(), 120,
PeerGroupAdvertisement.class);
} catch (Exception any) {
throw new PeerGroupException(any.getMessage());
}
if (adv == null) {
throw new PeerGroupException("Could not find group advertisement");
}
PeerGroup result = (PeerGroup) loadModule(gid, adv.getModuleSpecID(),
Here);
// Renew the lease of that advertisement since we're using it.
try {
result.publishGroup(adv.getName(), adv.getDescription());
} catch (Exception notCritical) {
}
if (result == null) {
throw new PeerGroupException("Could not find all implementations.");
}
return result;
}
/**
* Returns the loader for this group.
* Right now, all GenericPeerGroups have the same one.
*
* @return JxtaLoader The loader
*/
public net.jxta.platform.JxtaLoader getLoader() {
return loader;
}
/**
* Ask a group the name of the Peer it is running on.
*/
public String getPeerName()
{
return peerAdvertisement.getName();
}
/**
* Ask a group for its name.
*/
public String getPeerGroupName()
{
return peerGroupAdvertisement.getName();
}
/**
* Ask a group its group id.
* @return PeerGroupId this Group's ID
*/
public PeerGroupID getPeerGroupID()
{
return peerGroupAdvertisement.getPeerGroupID();
}
/**
* Ask a group its peerId.
*/
public PeerID getPeerID()
{
return peerAdvertisement.getPeerID();
}
/**
* Ask this group its peer advertisement.
* @return PeerAdvertisement this Peer's advertisement in this group.
*/
public PeerAdvertisement getPeerAdvertisement()
{
return peerAdvertisement;
}
/**
* Ask this group its own PeerGroupAdvertisement.
* @return PeerGroupAdvertisement this group's advertisement.
*/
public PeerGroupAdvertisement getPeerGroupAdvertisement() {
return peerGroupAdvertisement;
}
/**
* Returns whether the group is a Rendezvous.
*/
public boolean isRendezvous() {
return rendezvous != null && rendezvous.isRendezVous();
}
/*
* shortcuts to the well-known services, in order to avoid calls to lookup.
*/
/**
* @return EndpointService an object implementing the EndpointService service for this
* group.
*/
public EndpointService getEndpointService()
{
return endpoint;
}
/**
* @return ResolverService an object implementing the ResolverService service for this
* group.
*/
public ResolverService getResolverService() {
return resolver;
}
/**
* @return DiscoveryService an object implementing the DiscoveryService service for this
* group.
*/
public DiscoveryService getDiscoveryService() {
return discovery;
}
/**
* @return PeerInfoService an object implementing the PeerInfoService service for this
* group.
*/
public PeerInfoService getPeerInfoService() {
return peerinfo;
}
/**
* @return MembershipService an object implementing the MembershipService service for
* this group.
*/
public MembershipService getMembershipService() {
return membership;
}
/**
* @return PipeService an object implementing the PipeService service for this
* group.
*/
public PipeService getPipeService() {
return pipe;
}
/**
* @return RendezVousService an object implementing the RendezVousService service for
* this group.
*/
public RendezVousService getRendezVousService() {
return rendezvous;
}
/**
* Get an allPurpose peerGroup ModuleImplAdvertisement compatible with this
* group. This impl adv can be used to create any group that relies only
* on the standard services. Or to derive other impl advs, using this one
* as a basis.
*
* @return ModuleImplAdvertisement the adv.
*/
abstract public
ModuleImplAdvertisement getAllPurposePeerGroupImplAdvertisement()
throws Exception;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -