📄 endpointrouter.java
字号:
/************************************************************************
*
* $Id: EndpointRouter.java,v 1.2 2002/03/04 21:42:57 echtcherbina Exp $
*
* 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.
*********************************************************************************/
package net.jxta.impl.endpoint;
import net.jxta.endpoint.*;
import net.jxta.document.*;
import net.jxta.peergroup.*;
import net.jxta.discovery.*;
import net.jxta.id.*;
import net.jxta.protocol.*;
import net.jxta.resolver.*;
import net.jxta.exception.*;
import net.jxta.peer.PeerID;
import net.jxta.impl.util.*;
import net.jxta.impl.protocol.*;
import net.jxta.impl.discovery.*;
// Get the ID.getIDEncodingType static from the impl for now.
// We need it because, since ther's no way to remove it from
// the string version of a peer ID, we must include it when we
// make a peerID out of strings.
import java.io.*;
import java.util.*;
import java.net.URL;
import org.apache.log4j.Category; import org.apache.log4j.Priority;
import net.jxta.platform.Module;
public class EndpointRouter implements EndpointProtocol,
QueryHandler,
EndpointListener,
Module {
protected class Route {
public String dest = null;
public String router = null;
public int nbOfHops = 0;
public Vector gateways = null;
public Route (String dest,
String router,
int nbOfHops,
Vector gateways) {
this.dest = dest;
this.router = router;
this.nbOfHops = nbOfHops;
this.gateways = gateways;
}
public void display() {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("Route to = " + dest);
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug (" nextHop = " + router);
if (gateways == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug (" no gateways");
} else {
for (int i=0; i < gateways.size(); ++i) {
try {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug (" [" + i + "] " + (String) gateways.elementAt(i));
} catch (Exception ez1) {
}
}
}
}
}
private static final Category LOG = Category.getInstance(EndpointRouter.class.getName());
/**
* Key is a peer id. Value is a Route
*/
private Hashtable routedRoutes = null;
/**
* These are peers we can reach directly (ping).<BR>
* Key is peer id. Value is address.
*/
private Hashtable directRoutes = null;
// As a stringified endpointAddress.
protected String localPeerAddr = null;
protected ID localPeerId = null;
private EndpointService endpoint = null;
// XXX: MaxTTL should be a configurable. For the time being
// rely on the seven levels of separation.
// lomax@jxta.org
// private static final int MaxTTL = 7;
private DiscoveryService discovery = null;
private PeerGroup group = null;
private static final String TypeTag = "Type";
private static final String RoutingPeerIdTag = "RoutingPeer";
private static final String RoutingPeerAdvTag = "RoutingPeerAdv";
private static final String DestPeerIdTag = "DestPeer";
private static final String NbOfHopsTag = "NbOfHops";
public static final String GatewayForwardTag = "GatewayForward";
public static final String GatewayReverseTag = "GatewayReverse";
private static final String VersionTag = "Version";
private static final String RouteQuery = "RouteQuery";
private static final String RouteResponse = "RouteResponse";
private static final String PingQuery = "PingQuery";
private static final String PingResponse = "PingResponse";
private static final String NACK = "NACK";
private static final int acceptableVersion = 3;
private static final int currentVersion = 3;
private ResolverService resolver = null;
private boolean servicesInitialized = false;
private int qid = 0;
private static final String routerSName = "EndpointRouter";
private static String routerSParam = null;
private EndpointAdvertisement myAdv = null;
private String localPeerAdv = null;
protected Vector localGateway = new Vector (1);
// Until we decide otherwise, the router is *by definition* handling
// peerID addressed messages.
private static String routerPName = "jxta";
// This is not good enough to avoid redundant queries because
// we remove them as soon as they've been sent (no timer),
// but at least it avoids infinite recursions if we're looking
// for the route to a rendezvous (queries try to go there).
private Vector pendingQueries = new Vector();
public EndpointRouter() {
}
/**
* Returns true if this protocol accepts to be overloaded.
* That is let a protocol with the name protocol name in a
* descendant group be registered.
*
* @return boolean true if over load is allowed.
*/
public boolean allowOverLoad() {
return true;
}
/**
* Make this protocol as up and running.
* When this method is called, all the services are already registered
* with the peergroup. So we do not need to delay binding any further.
* All the public methods, which could be called between init and startApp
* are defensive regarding the services possibly not being there.
*/
public int startApp(String[] arg) {
discovery = group.getDiscoveryService();
resolver = group.getResolverService();
if( null == discovery )
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn( "discovery service is not available!" );
if( null == resolver )
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn( "resolver service is not available!" );
resolver.registerHandler(routerSName, this);
return 0;
}
/**
* closes this EndpointProtocol.
* Carefull that stopApp() could in theory be called before startApp().
*/
public void stopApp() {
if (resolver != null) resolver.unregisterHandler(routerSName);
if (endpoint != null) {
endpoint.removeListener(routerSName + routerSParam, this);
endpoint.removeEndpointProtocol(this);
}
}
public void init( PeerGroup g, ID assignedID, Advertisement impl)
throws PeerGroupException {
group = g;
endpoint = g.getEndpointService();
localPeerAdv = advToString(g.getPeerAdvertisement());
localPeerId = g.getPeerID();
localPeerAddr = routerPName
+ "://"
+ group.getPeerID().getUniqueValue().toString();
routerSParam = g.getPeerGroupID().getUniqueValue().toString();
try {
//PDA requirement 18.02.2002
// Vector.add -> Vector.insertElementAt
// localGateway.add (0, localPeerAddr);
localGateway.insertElementAt (localPeerAddr, 0);
} catch (Exception ez1) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn ("Cannot set myself as a gateway");
}
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("localPeerId = " + localPeerId);
directRoutes = new Hashtable();
routedRoutes = new Hashtable();
// We're ready for our methods to be called; even if they won't
// be of much help until startApp().
endpoint.addListener(routerSName + routerSParam, this);
endpoint.addEndpointProtocol(this);
}
public boolean isConnectionOriented() {
return false;
}
/**
* Returns true if the endpoint protocol can be used by the EndpointRouter
*
* @return boolean true if the protocol can be used by the EndpointRouter
*/
public boolean allowRouting() {
// Yes, this is the router, and it does not allow routing.
// Otherwise we would have a chicken and egg problem.
return false;
}
/**
* Return the endpoint address by which this peer is accessible via this
* transport.
*/
public EndpointAddress getPublicAddress() {
return endpoint.newEndpointAddress(localPeerAddr);
}
/**
* Returns the address family that this endpoint protocol handles.
* This endpoint protocol handles the address family that denotes
* peerId's.
*
* @return String the endpoint protocol name
*/
public String getProtocolName() {
return routerPName;
}
public void propagate (Message srcMsg,
String pName,
String pParam,
String prunePeer) throws IOException {
// All messages are lost in the ether
}
/**
* closes this EndpointProtocol.
*/
public void close() {
endpoint.removeListener(routerSName + routerSParam, this);
}
/**
* convert a doc to string
*
* @param adv Description of Parameter
* @return Description of the Returned Value
* @since
*/
private String advToString(Advertisement adv) {
StringWriter out = new StringWriter();
MimeMediaType displayAs = new MimeMediaType("text/xml");
try {
StructuredTextDocument doc = (StructuredTextDocument) adv.getDocument(displayAs);
doc.sendToWriter(out);
return out.toString();
} catch (Exception all) {
return null;
}
}
public Enumeration getPeerAdv(String pId) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("getPeerAdv for : " + pId);
if (discovery == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" no discovery");
return null;
}
// What we refer to in the router as PeerIDs, are generaly not
// peerIDs, they're endpoint addresses based on a peer ID.
try {
EndpointAddress asAddress = endpoint.newEndpointAddress(pId);
URL asUrl = new URL ( net.jxta.id.ID.URIEncodingName, "",
net.jxta.id.ID.URNNamespace
+ ":"
+ asAddress.getProtocolAddress());
String realPeerID = asUrl.toString();
return discovery.getLocalAdvertisements(DiscoveryService.PEER,
"PID",
realPeerID);
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" failed with " + e);
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -