📄 genericpeergroup.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: GenericPeerGroup.java,v 1.2 2002/03/04 21:42:59 echtcherbina Exp $
*/
package net.jxta.impl.peergroup;
import java.util.*;
import java.io.*;
import java.net.URL;
import net.jxta.id.*;
import net.jxta.peer.*;
import net.jxta.peergroup.*;
import net.jxta.exception.*;
import net.jxta.resolver.*;
import net.jxta.service.*;
import net.jxta.discovery.*;
import net.jxta.protocol.*;
import net.jxta.rendezvous.*;
import net.jxta.pipe.*;
import net.jxta.membership.*;
import net.jxta.endpoint.*;
import net.jxta.platform.*;
import net.jxta.document.*;
import net.jxta.impl.loader.*;
import net.jxta.impl.endpoint.*;
import net.jxta.impl.protocol.PeerAdv;
import net.jxta.impl.protocol.PeerGroupAdv;
import org.apache.log4j.Category; import org.apache.log4j.Priority;
public abstract class GenericPeerGroup implements RefPeerGroup {
private static final Category LOG =
Category.getInstance(GenericPeerGroup.class.getName());
/*
* Shortcuts to well known services.
*/
private EndpointService endpoint;
private ResolverService resolver;
private DiscoveryService discovery;
private PipeService pipe;
private MembershipService membership;
private RendezVousService rendezvous;
private PeerInfoService peerinfo;
// The loader for everyone.
public static final net.jxta.platform.JxtaLoader loader = new RefJxtaLoader("./Downloaded");
// This peer's advertisement in this group.
private PeerAdvertisement peerAdvertisement = null;
// This group's advertisement.
private PeerGroupAdvertisement peerGroupAdvertisement = null;
// This Module's assigned ID (for a group, normally, the group ID.
// it probably always will be, but it could change).
// Currently we do not use it past init(), so don't store it.
// private ID assignedID = null;
// This group's implAdvertisement.
private ModuleImplAdvertisement implAdvertisement = null;
// This peer's config advertisement.
private PeerAdvertisement configAdvertisement = null;
// This service implements a group but, being a Service, it
// runs inside some group. That's its home group.
// Exception:The platform itself does not have one. It has to be self
// sufficient.
private RefPeerGroup parentGroup = null;
// All the plug-ins that do the work.
private Hashtable services = new Hashtable();
// True when we have decided to stop this group.
private boolean stopping = false;
// True when the PG adv has been published.
private boolean published = false; // on the safe side
// Just a small wrapper around two common idioms that we do use here and
// there.
private Enumeration discoverSome(int type,
String attr,
String value,
int seconds,
Class thisClass) {
return discoverSome(discovery,
type,
attr,
value,
seconds,
thisClass);
}
private Enumeration discoverSome(DiscoveryService discovery,
int type,
String attr,
String value,
int seconds,
Class thisClass) {
Vector results = new Vector();
try {
int count = 0;
Enumeration res;
do {
res = discovery.getLocalAdvertisements(type,
attr,
value);
while (res.hasMoreElements()) {
Advertisement a = (Advertisement) res.nextElement();
if (thisClass.isInstance(a)) {
//PDA requirements 18.02.2002
//java.util.Vector.add(Object obj) -> java.util.Vector.addElement(Object obj)
//results.add(a);
results.addElement(a);
//PDA requirements 18.02.2002
}
}
if (results.size() > 0) {
break;
}
if (count % 30 == 0) {
discovery.getRemoteAdvertisements(null,
type,
attr,
value,
20);
}
Thread.sleep(1000);
} while (count++ < seconds);
} catch (Exception whatever) {
whatever.printStackTrace();
}
return results.elements();
}
private Advertisement discoverOne(int type, String attr,
String value, int seconds,
Class thisClass) {
Enumeration res = discoverSome(type, attr, value, seconds, thisClass);
if (!res.hasMoreElements()) return null;
return (Advertisement) res.nextElement();
}
/*
* Shortcuts to the standard basic services.
*/
private void setShortCut(ID name, Service service) {
if (endpointClassID.equals(name)) {
endpoint = (EndpointService) service;
return;
}
if (resolverClassID.equals(name)) {
resolver = (ResolverService) service;
return;
}
if (discoveryClassID.equals(name)) {
discovery = (DiscoveryService) service;
return;
}
if (pipeClassID.equals(name)) {
pipe = (PipeService) service;
return;
}
if (membershipClassID.equals(name)) {
membership = (MembershipService) service;
return;
}
if (peerinfoClassID.equals(name)) {
peerinfo = (PeerInfoService) service;
return;
}
if (rendezvousClassID.equals(name)) {
rendezvous = (RendezVousService) service;
return;
}
}
private void clearShortCut(ModuleClassID name) {
if (endpointClassID.equals(name)) {
endpoint = null;
return;
}
if (resolverClassID.equals(name)) {
resolver = null;
return;
}
if (discoveryClassID.equals(name)) {
discovery = null;
return;
}
if (pipeClassID.equals(name)) {
pipe = null;
return;
}
if (membershipClassID.equals(name)) {
membership = null;
return;
}
if (peerinfoClassID.equals(name)) {
peerinfo = null;
return;
}
if (rendezvousClassID.equals(name)) {
rendezvous = null;
return;
}
}
/**
* Adds a service to the set.
* Returns any pre-existing one.
*/
private synchronized Service addServiceSync(ID name,
Service service)
{
if (stopping) return null;
Service p = (Service) services.remove(name);
services.put(name, service);
return p;
}
/**
* Adds a service to the set.
* removes any pre-existing one with the same name.
*/
protected void addService(ID name, Service service)
{
Service oldp = addServiceSync(name, service);
setShortCut(name, service);
// if (oldp != null) oldp.finalize();
}
/**
* Call a service by name.
* @param name the service name
* @return Service, the Service registered by that name
*/
synchronized public Service lookupService(ID name)
throws ServiceNotFoundException
{
// Null services are never registered, so we do not need to test that
// case.
Service p = (Service) services.get(name);
if (p == null) {
throw new ServiceNotFoundException(name.toString());
}
return p.getInterface();
}
/**
* check that all required services are there.
*/
protected void checkServices()
throws ServiceNotFoundException
{
Service ignored;
ignored = lookupService(endpointClassID);
ignored = lookupService(resolverClassID);
ignored = lookupService(discoveryClassID);
ignored = lookupService(pipeClassID);
ignored = lookupService(rendezvousClassID);
ignored = lookupService(membershipClassID);
ignored = lookupService(peerinfoClassID);
}
/**
* We're stopping. Remove all services and stop them.
*/
synchronized private void removeAllServicesSync () {
stopping = true;
// Here's a quick way of removing them all.
Hashtable myTable = services;
services = new Hashtable();
Enumeration allServices = myTable.elements();
while (allServices.hasMoreElements()) {
Service s = (Service) allServices.nextElement();
s.stopApp();
}
}
/**
* Ask a group to unregister and unload a service
* @param service handle to the service to be removed
*/
synchronized private void removeServiceSync (ModuleClassID name,
Service service)
throws ServiceNotFoundException, ViolationException
{
// Weak check: you need to actually posess a handle to the
// service object you're removing, not its name, but
// you can fake by doing getService() first.
Service p = (Service) services.get(name);
if (p == null) throw new ServiceNotFoundException(name.toString());
if (p != service) throw new ViolationException(name.toString());
services.remove(name);
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -