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

📄 endpointrouter.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
      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);
    }
    pendingQueries.remove(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;
    
    mediaType = new MimeMediaType("text/xml");
    
    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
    
    boolean found = false;
    if (qReqAddr.equals(localPeerAddr)) {
      found = true;
    } else {
      if (group.isRendezvous()) {
        
        if (isLocalRoute(qReqAddr)) {
          found = true;
        } else {
          route = getRoute(qReqAddr);
          if (route != null) {
            found = true;
          }
        }
      }
    }
    
    if (!found) {
      if (!group.isRendezvous()) {
        throw new DiscardQueryException();
      }
      throw new NoResponseException();
    }
    
    try {
      // Build the answer
      doc = (StructuredTextDocument)
      StructuredDocumentFactory.newStructuredDocument(
      new MimeMediaType( "text/xml" ), "jxta:EndpointRouter");
      
      Element e = null;
      e = doc.createElement(VersionTag, Integer.toString(currentVersion));
      doc.appendChild(e);
      e = doc.createElement(TypeTag, RouteResponse);
      doc.appendChild(e);
      e = doc.createElement(DestPeerIdTag, qReqAddr);
      doc.appendChild(e);
      e = doc.createElement(RoutingPeerIdTag, localPeerAddr);
      doc.appendChild(e);
      if (route != null) {
        e = doc.createElement(NbOfHopsTag, String.valueOf(route.nbOfHops + 1));
      } else {
        e = doc.createElement(NbOfHopsTag, String.valueOf(1));
      }
      doc.appendChild(e);
      
      if (localPeerAdv != null) {
        e = doc.createElement(RoutingPeerAdvTag, localPeerAdv);
        doc.appendChild(e);
      }
      
      // The first gateway is the local peer
      e = doc.createElement(GatewayForwardTag, localPeerAddr);
      doc.appendChild(e);
      
      if (route != null) {
        Vector gateways = route.gateways;
        String gateway = null;
        if ((gateways != null) && (gateways.size() > 0)) {
          for (int i = 0; i < gateways.size(); ++i) {
            try {
              gateway = (String) gateways.elementAt(i);
              e = doc.createElement(GatewayForwardTag, gateway);
              doc.appendChild(e);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -