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

📄 genericpeergroup.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    /**
     * May be called by a module which has a direct reference to
     * the group object and wants to notify its abandoning it.
     * Has no effect on the real group object.
     */
    public void unref() {
    }

    /**
     * Called everytime an interface object that refers to this group
     * goes away, either by being finalized or by its unref method being
     * invoked explicitly.
     */
    protected void decRefCount() {
	synchronized(this) {
	    if (LOG.isEnabledFor(Priority.INFO)) LOG.info("GROUP " + getPeerGroupID().toString() + " REF COUNT DECREMENTED TO: " + (masterRefCount - 1));
	    if (--masterRefCount != 0) return;
	}
	if (LOG.isEnabledFor(Priority.INFO)) LOG.info("STOPPING UNREFERENCED GROUP " + getPeerGroupID().toString());
	stopApp();
    }

    /**
     * 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. The interface object also serves as a super-strong reference
     * to this group object
     *
     * @return Service public interface of the service
     *
     * @since JXTA 1.0 
     */
    public Service getInterface() {
	synchronized(this) {
	    if (LOG.isEnabledFor(Priority.INFO)) LOG.info("GROUP " + getPeerGroupID().toString() + " REF COUNT INCREMENTED TO: " + (masterRefCount + 1));
	    ++masterRefCount;
	}
        return new PeerGroupInterface(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;
 
	PeerGroupID gid = adv.getPeerGroupID();
	PeerGroup theNewGroup = globalRegistry.lookupInstance(gid);
	if (theNewGroup != null) return theNewGroup;

        theNewGroup = (PeerGroup) loadModule(adv.getPeerGroupID(),
					     adv.getModuleSpecID(),
					     Here, false);

        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 (PeerGroup) theNewGroup.getInterface();
    }

    /**
     * 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 = globalRegistry.lookupInstance(gid);
	if (theNewGroup != null) return theNewGroup;

        try {
            theNewGroup = (PeerGroup) loadModule(gid,
                                                 impl, false);
        } 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 (PeerGroup) theNewGroup.getInterface();
    }


    /**
     * 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 {

	PeerGroup result = globalRegistry.lookupInstance(gid);
	if (result != null) return result;

        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");
        }

        result = (PeerGroup) loadModule(gid, adv.getModuleSpecID(),
					Here, false);

        // 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 (PeerGroup) result.getInterface();
    }


    /**
     * 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 + -