📄 peergroup.java
字号:
/*
* Copyright (c) 2001 Sun Microsystems, Inc. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*
* $Id: PeerGroup.java,v 1.40 2002/06/18 20:45:57 hamada Exp $
*/
package net.jxta.peergroup;
import net.jxta.discovery.DiscoveryService;
import net.jxta.document.Advertisement;
import net.jxta.document.Element;
import net.jxta.endpoint.EndpointService;
import net.jxta.exception.ServiceNotFoundException;
import net.jxta.exception.PeerGroupException;
import net.jxta.exception.ProtocolNotSupportedException;
import net.jxta.exception.JxtaError;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import net.jxta.membership.MembershipService;
import net.jxta.peer.PeerID;
import net.jxta.peer.PeerInfoService;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.pipe.PipeService;
import net.jxta.platform.JxtaLoader;
import net.jxta.platform.Module;
import net.jxta.platform.ModuleClassID;
import net.jxta.platform.ModuleSpecID;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.protocol.PeerGroupAdvertisement;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.rendezvous.RendezVousService;
import net.jxta.resolver.ResolverService;
import net.jxta.service.Service;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.UnknownServiceException;
import java.lang.ClassNotFoundException;
import java.lang.ref.WeakReference;
import java.util.HashMap;
/**
* Peer groups are formed
* as a collection of peers that have agreed upon a common set of services.
* Each peer group is assigned a unique peer group ID and a peer group
* advertisement. The peer group advertisement contains a ModuleSpecID
* which refers to a module specification for this peer group.
*
* <p>The peer group specification mandates each of the group services (membership,
* discovery, resolver, etc). Implementations of that specification are
* described by ModuleImplAdvertisements which are identified by the group's
* ModuleSpecID. Implementations are responsible for providing the services mandated
* by the specification.
*
* The java reference implementation achieves
* this by loading additional Modules which ModuleSpecIDs are listed by the group
* implementation advertisement.
*
* <p>In order to participate in a group, a peer needs to join the group
* using the peer group membership service.
*
* <p> JXTA comes with two subclasses of peer groups:
*
* <ul type-disc>
*
* <li> <strong>Platform</strong>: this is used to represent the <em>World</em>
* group. Every peer, when booting, becomes part of this group. This group
* provides the minimum core services needed to find and instantiate other
* groups on a peer. <strong>Platform</strong> has the privilege of assigning
* a new ID to the peer, if it does not already have one. The <em>World</em>
* group's ID is invariant.</li>
*
* <li> <strong>StdPeergroup</strong>: this is currently used to implement
* all other kinds of peer groups.
* The first such group that is instantiated after booting is known as
* <em>The Net Peer Group</em>. When <strong>Platform</strong> starts it may
* optionaly search for <em>The Net Peer Group</em> on the local network and,
* if found, instantiate it. Otherwise a default configuration of
* <em>The Net Peer Group</em> is instanciated.
*
* A non-default configuration of <em>The Net Peer Group</em> may be set-up
* by the administrator in charge of the network domain inside which the peer
* is booting. <em>The Net Peer Group</em> is discovered via the DiscoveryService
* protocol. Many such groups may be configured by an administrator.<br>
*
* <strong>StdPeergroup</strong> may also be used to implement User-defined
* peer groups: Users can create new peer groups which use their own set of
* customized services.
*</li>
*</ul>
*
* @see net.jxta.peergroup.PeerGroupID
* @see net.jxta.service.Service
* @see net.jxta.peergroup.PeerGroupFactory
* @see net.jxta.protocol.PeerGroupAdvertisement
* @see net.jxta.protocol.ModuleImplAdvertisement
* @see net.jxta.platform.ModuleSpecID
* @see net.jxta.platform.ModuleClassID
*
* @version $Revision: 1.40 $
* @since JXTA 1.0 */
public interface PeerGroup extends Service {
/**
* Look for needed ModuleImplAdvertisement in this group.
*/
public static final int Here = 0;
/**
* Look for needed ModuleImplAdvertisement in the parent group of this group.
*/
public static final int FromParent = 1;
/**
* Look for needed ModuleImplAdvertisement in both this group and its parent.
*/
public static final int Both = 2;
/**
* Default life time for group advertisements in the publisher's cache.
* (a year)
*/
public static final long DEFAULT_LIFETIME = 1000 * 3600 * 24 * 365;
/**
* Default expiration time for discovered group advertisements. (2 weeks)
*/
public static final long DEFAULT_EXPIRATION = 1000 * 3600 * 24 * 14;
/*
* Global registry of instanciated groups.
*/
class GlobalRegistry {
private HashMap registry = new HashMap(8);
/**
* Registers a new instance.
* @param gid the ID of the group of which an instance is being registered.
* @param pg the group instance being registered.
* @return false if the instance could not be registered because there
* was already such an instance registered.
*/
public synchronized boolean
registerInstance(PeerGroupID gid, PeerGroup pg) {
WeakReference ref = (WeakReference) registry.get(gid);
if ((ref != null) && (ref.get() != null)) return false;
// If the ref is a dead instance, we can also replace it.
registry.put(gid, new WeakReference(pg));
return true;
}
/**
* Unregisters a group instance (normaly because the grou is being
* stopped.
*
* @param gid the ID of the group of which an instance is unregistered.
* @param pg the group instance itself (serves as a creadential).
* @return false if the group could not be unregistered because no such
* registration (exact ID, exact object) was not found.
*/
public synchronized boolean
unRegisterInstance(PeerGroupID gid, PeerGroup pg) {
WeakReference ref = (WeakReference) registry.get(gid);
if (ref == null) return false;
PeerGroup found = (PeerGroup) ref.get();
if (found == null) {
// Dead instance. Remove from table.
registry.remove(gid);
return false;
}
// Note the use of "!=", not "!equals()"
if (pg != found) return false;
registry.remove(gid);
return true;
}
/**
* Returns a running instance of the peergroup with given ID if any
* exists.
* @param gid the id of the group of which an instance is wanted.
* @return the group, or null if no instance exists.
*/
public synchronized PeerGroup lookupInstance(PeerGroupID gid) {
WeakReference ref = (WeakReference) registry.get(gid);
if (ref == null) return null;
PeerGroup pg = (PeerGroup) ref.get();
if (pg == null) {
// Dead instance. remove from table.
registry.remove(gid);
return null;
}
// Returns an interface object. Therefore a module that got the
// peergroup through lookup cannot unregister it if the group
// protects itself by returning an interface object different from
// the group object. In general only the group itself can
// unregister when being torn down. Unregistration will also be
// automatic if the grp object is GC'ed (the references are weak
// references).
return (PeerGroup) pg.getInterface();
}
}
/**
* An inner class used to create static well-known identifiers.
*/
class IdMaker {
// This method serves to catch the MalformedURLException.
// Don't know how to catch it from the static init context.
static ID mkID( String s ) {
try {
return IDFactory.fromURL(IDFactory.jxtaURL( "urn", "", "jxta:uuid-" + s));
} catch (MalformedURLException absurd) {
throw new JxtaError( "Hardcoded Spec and Class IDs are malformed." + s );
} catch (UnknownServiceException absurd3) {
throw new JxtaError( "Unknown Service");
}
}
}
/**
* Well known classes for the basic services.
* FIXME: we should make a "well-known ID" encoding implementation that
* has its own little name space of human readable names...later.
* To keep their string representation shorter, we put our small spec
* or role pseudo unique ID at the front of the second UUID string.
* Base classes do not need an explicit second UUID string because it is
* all 0.
* The type is always the last two characters, nomatter the total length.
*/
/**
* Well known module class identifier: peer group
*/
public static final ModuleClassID peerGroupClassID = (ModuleClassID)
IdMaker.mkID( "DeadBeefDeafBabaFeedBabe00000001"
+ "05" );
/**
* Well known module class identifier: resolver service
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -