📄 endpointrouter.java
字号:
// Get its EndpointService advertisement
TextElement endpParam = (TextElement)
adv.getServiceParam(PeerGroup.endpointClassID);
if (endpParam == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("checkPeer: no Endpoint Params");
continue;
}
Enumeration endps = endpParam.getChildren("Addr");
if (!endps.hasMoreElements()) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("checkPeer: no Addresses in adv");
continue;
}
Vector addrs = new Vector();
while (endps.hasMoreElements()) {
String saddr = "";
try {
saddr =
((TextElement) endps.nextElement()).getTextValue();
addrs.addElement(endpoint.newEndpointAddress(saddr));
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("bad address");
continue;
}
}
if (addrs.size() > 0) {
EndpointAddress bestAddr = null;
bestAddr = getBestLocalRoute(addrs.elements());
if (bestAddr != null) {
// Found a direct route. Return it.
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("found direct route");
return bestAddr;
}
}
}
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" failed with " + e);
triedAndFailed.put(peer,
new Long(System.currentTimeMillis()));
return null;
}
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" did not find a good route");
triedAndFailed.put(peer,
new Long(System.currentTimeMillis()));
return null;
}
// This part is for the ResolverServiceImpl part of the EndpointRouter
private void findRoute(String peer) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("findRoute starts");
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" peer= " + peer);
if (pendingQueries.contains(peer)) {
return;
}
pendingQueries.addElement(peer);
try {
// First create the request message.
StructuredTextDocument doc = ( StructuredTextDocument )
StructuredDocumentFactory.newStructuredDocument(
new MimeMediaType( "text/xml" ),
"jxta:EndpointRouter");
Element e = null;
e = doc.createElement(TypeTag, RouteQuery);
doc.appendChild(e);
e = doc.createElement(DestPeerIdTag, peer);
doc.appendChild(e);
e = doc.createElement(RoutingPeerAdvTag, localPeerAdv);
doc.appendChild(e);
StringWriter dumped = new StringWriter();
doc.sendToWriter( dumped );
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("findRoute sends query " + routerSName);
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("findRoute sends query for peer : " + peer);
ResolverQuery query = new ResolverQuery(routerSName,
null,
localPeerId.toString(),
dumped.toString(),
qid++);
resolver.sendQuery(null, query);
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("getAddress() query sent");
} catch (Exception ee) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Exception in findRoute", ee);
}
//PDA requirement 18.02.2002
// Vector.remove -> Vector.removeElement
// pendingQueries.remove(peer);
pendingQueries.removeElement(peer);
}
// This is called by the Generic ResolverServiceImpl when processing a response to
// a query.
public void processResponse(ResolverResponseMsg response) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processResponse got a response");
Enumeration enum = null;
String routingPeer = null;
String routingPeerAdv = null;
String destPeer = null;
MimeMediaType mediaType = null;
String ipId = null;
try {
mediaType = new MimeMediaType("text/xml");
} catch (RuntimeException e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processResponse: cannot get MimeMediaType " + e);
return;
}
ByteArrayInputStream ip =
new ByteArrayInputStream(response.getResponse().getBytes());
StructuredDocument doc = null;
try {
doc = StructuredDocumentFactory.newStructuredDocument(mediaType,
ip);
} catch( Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processResponse: malformed response - discard");
return;
}
// XXX: we are doing no document sanity check here.
// Maybe should we be more careful.
// to be fixed. lomax@jxta.org
Vector gatewaysForward = new Vector();
int nbOfHops = 0;
int version = 0;
String type = null;
enum = doc.getChildren();
while (enum.hasMoreElements()) {
TextElement elem = (TextElement) enum.nextElement();
if (elem.getName().equals(DestPeerIdTag)) {
destPeer = (String) elem.getValue();
continue;
}
if (elem.getName().equals(RoutingPeerIdTag)) {
routingPeer = (String) elem.getValue();
continue;
}
if (elem.getName().equals(RoutingPeerAdvTag)) {
routingPeerAdv = (String) elem.getValue();
continue;
}
if (elem.getName().equals(NbOfHopsTag)) {
try {
nbOfHops = Integer.parseInt ((String) elem.getValue());
} catch (NumberFormatException ez1) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn ("Cannot parse nbOfHops");
}
continue;
}
if (elem.getName().equals(VersionTag)) {
try {
version = Integer.parseInt((String) elem.getValue());
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("cannot decode version");
continue;
}
continue;
}
if (elem.getName().equals(TypeTag)) {
type = (String) elem.getValue();
continue;
}
if (elem.getName().equals(GatewayForwardTag)) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Gateway forward adding " + ((String) elem.getValue()));
gatewaysForward.addElement((String) elem.getValue());
continue;
}
}
// Check the version of the router.
if (version < acceptableVersion) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("reject answer. Wrong version " + version);
return;
}
if (type == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("No type: invalid.");
return;
}
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("processResponse got message of type " + type);
// This implementation currentely only implements RouteQuery/RouteResponse.
// PingQuery, PingResponse and NACK must be implemented.
if (!type.equals(RouteResponse)) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("unimplemented: " + type);
return;
}
if ((routingPeer == null) || (destPeer == null)) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processResponse: malformed response - discard.");
// Malformed response. Just discard.
return;
}
// If there was an advertisement in the answer, publish it.
if (routingPeerAdv != null) {
Advertisement adv = null;
try {
adv = AdvertisementFactory.newAdvertisement(
new MimeMediaType("text/xml"),
new ByteArrayInputStream(routingPeerAdv.getBytes()));
// This is not our own peer adv, so we must not keep it
// for more than its expiration time.
discovery.publish(adv, DiscoveryService.PEER,
DiscoveryService.DEFAULT_EXPIRATION,
DiscoveryService.DEFAULT_EXPIRATION);
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" no advertisement " + e);
}
}
// We just dumped an adv for that peer, so we want to do a real check
// on its addresses. Remove the entry from the negative cache.
triedAndFailed.remove(routingPeer);
if (destPeer.equals (routingPeer)) {
// This must be a local route. Check it first.
learnLocalRoute (routingPeer);
} else {
// This is the answer to a remote route. First make sure that the
// routing peer is known.
learnLocalRoute (routingPeer);
// Now learn the remote route
setRoute( new Route (destPeer,
routingPeer,
nbOfHops,
gatewaysForward));
}
}
/**
* Process the Query, and genrate response
* @param GenericResolverMsg the query to process
* @return ResolverResponseMsg "Response"
*/
public ResolverResponseMsg processQuery(ResolverQueryMsg query)
throws NoResponseException,
IOException,
ResendQueryException,
DiscardQueryException {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processQuery starts");
MimeMediaType mediaType = null;
try {
mediaType = new MimeMediaType("text/xml");
} catch (RuntimeException e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Malformed query [1] " + e);
throw new IOException();
}
ByteArrayInputStream ip =
new ByteArrayInputStream(query.getQuery().getBytes());
StructuredTextDocument doc = null;
try {
doc = (StructuredTextDocument)
StructuredDocumentFactory.newStructuredDocument(mediaType,
ip);
} catch( Exception e ) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processQuery: malformed request [2] " + e);
throw new IOException();
}
// FIXME lomax@jxta.org we are doing no document sanity check here.
// Maybe should we be more careful.
String pId = null;
String type = null;
Enumeration enum = doc.getChildren();
while (enum.hasMoreElements()) {
TextElement elem = (TextElement) enum.nextElement();
if (elem.getName().equals(DestPeerIdTag)) {
pId = (String) elem.getValue();
continue;
}
if (elem.getName().equals(TypeTag)) {
type = (String) elem.getValue();
continue;
}
if (elem.getName().equals(RoutingPeerAdvTag)) {
String remoteAdv = (String) elem.getValue();
PeerAdvertisement padv = null;
try {
padv = (PeerAdvertisement) AdvertisementFactory.newAdvertisement(
new MimeMediaType("text/xml"),
new ByteArrayInputStream(remoteAdv.getBytes()));
if (!(padv.getPeerID()).equals(localPeerId)) {
// This is not our own peer adv so we must not keep it
// longer than its expiration time.
discovery.publish(padv, DiscoveryService.PEER,
DiscoveryService.DEFAULT_EXPIRATION,
DiscoveryService.DEFAULT_EXPIRATION);
}
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(e);
}
continue;
}
}
if (type == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("No type: invalid.");
throw new DiscardQueryException();
}
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug (" type= " + type);
// This implementation currentely only implements RouteQuery/RouteResponse.
// PingQuery, PingResponse and NACK must be implemented.
if (!type.equals(RouteQuery)) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("unimplemented: " + type);
throw new DiscardQueryException();
}
if (pId == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processQuery: malformed request, no PeerId.");
throw new IOException();
}
int queryId = query.getQueryId();
// We have more luck with that one because, since it is part of OUR
// message,m and not part of the resolver protocol, it is in OUR
// format.
String qReqAddr = pId;
Route route = null;
// XXX: Only one query at the time...
// Maybe that should be changed later. lomax@jxta.org
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -