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

📄 socketsourceroutemanager.java

📁 pastry的java实现的2.0b版
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
      setDeadForever();    }    /**     * This method should be called when a known route has its proximity updated     *     * @param route The route     * @param proximity The proximity     */    protected synchronized void markProximity(SourceRoute route, int proximity) {      getRouteManager(route).markAlive();      getRouteManager(route).markProximity(proximity);      // first, we check and see if we have no best route (this can happen if the best just died)      if (best == null) {        if (logger.level <= Logger.FINE) {          logger.log("(SSRM) No previous best route existed to " + address + " route " + route + " is now the best");        }        best = route;      }      setAlive();      // next, we update everyone if this is the active route      if (route.equals(best)) {        if (address != null) {          address.update(SocketNodeHandle.PROXIMITY_CHANGED);        }      }    }    /**     * Method which enqueues a message to this address     *     * @param message The message to send     */    public synchronized void send(SocketBuffer message) {      // if we're dead, we go ahead and just checkDead on the direct route      if (liveness == SocketNodeHandle.LIVENESS_DEAD) {        getRouteManager(SourceRoute.build(address.eaddress)).checkLiveness();        this.updated = spn.getEnvironment().getTimeSource().currentTimeMillis();      }      // and in any case, we either send if we have a best route or add the message      // to the queue      if (best == null) {        queue.addLast(message);        hardLinks.add(this);      } else if (!getRouteManager(best).isOpen()) {        queue.addLast(message);        hardLinks.add(this);        getRouteManager(best).checkLiveness();        this.best = null;        this.updated = spn.getEnvironment().getTimeSource().currentTimeMillis();      } else {        getRouteManager(best).send(message);      }    }    /**     * Method which opens an app socket to this address     *     * @param appAddress DESCRIBE THE PARAMETER     * @param receiver DESCRIBE THE PARAMETER     * @param timeout DESCRIBE THE PARAMETER     */    public synchronized void connect(int appAddress, AppSocketReceiver receiver, int timeout) {      // if we're dead, we go ahead and just checkDead on the direct route      if (liveness == SocketNodeHandle.LIVENESS_DEAD) {        getRouteManager(SourceRoute.build(address.eaddress)).checkLiveness();        this.updated = spn.getEnvironment().getTimeSource().currentTimeMillis();      }      // and in any case, we either send if we have a best route or add the message      // to the queue      if (best == null) {        pendingAppSockets.addLast(new PendingAppSocket(appAddress, receiver));        hardLinks.add(this);      } else if (!getRouteManager(best).isOpen()) {        pendingAppSockets.addLast(new PendingAppSocket(appAddress, receiver));        hardLinks.add(this);        getRouteManager(best).checkLiveness();        this.best = null;        this.updated = spn.getEnvironment().getTimeSource().currentTimeMillis();      } else {        getRouteManager(best).connect(appAddress, receiver, timeout);      }    }    /**     * Method which suggests a ping to the remote node.     */    public void ping() {      if (spn.getEnvironment().getTimeSource().currentTimeMillis() - updated > PING_THROTTLE) {        this.updated = spn.getEnvironment().getTimeSource().currentTimeMillis();        switch (liveness) {          case SocketNodeHandle.LIVENESS_DEAD_FOREVER:            return;          case SocketNodeHandle.LIVENESS_DEAD:            if (logger.level <= Logger.FINE) {              logger.log("(SSRM) PING: PINGING DEAD ADDRESS " + address + " - JUST IN CASE, NO HARM ANYWAY");            }            getRouteManager(SourceRoute.build(address.eaddress)).ping();            break;          default:            if (best != null) {              getRouteManager(best).ping();              // check to see if the direct route is available              if (!best.isDirect()) {                getRouteManager(SourceRoute.build(address.eaddress)).ping();              }            }            break;        }      }    }    /**     * Method which suggests a ping to the remote node.     */    public void checkLiveness() {      this.updated = spn.getEnvironment().getTimeSource().currentTimeMillis();      switch (liveness) {        case SocketNodeHandle.LIVENESS_DEAD_FOREVER:          return;        case SocketNodeHandle.LIVENESS_DEAD:          if (logger.level <= Logger.FINE) {            logger.log("(SSRM) CHECKLIVENESS: CHECKING DEAD ON DEAD ADDRESS " + address + " - JUST IN CASE, NO HARM ANYWAY");          }          getRouteManager(SourceRoute.build(address.eaddress)).checkLiveness();          break;        default:          if (best != null) {            getRouteManager(best).checkLiveness();            // check to see if the direct route is available            if (!best.isDirect()) {              getRouteManager(SourceRoute.build(address.eaddress)).checkLiveness();            }          }          break;      }    }    /**     * DESCRIBE THE METHOD     */    protected void purgeQueue() {      // and finally we can now send any pending messages      while (!queue.isEmpty()) {        reroute(address.eaddress, (SocketBuffer) queue.removeFirst());      }      while (!pendingAppSockets.isEmpty()) {        PendingAppSocket pas = (PendingAppSocket) pendingAppSockets.removeFirst();        pas.receiver.receiveException(null, new NodeIsDeadException());      }      hardLinks.remove(this);    }    /**     * Internal class which is charges with managing the remote connection via a     * specific route     *     * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $     * @author jeffh     */    public class SourceRouteManager {      // the remote route of this manager      /**       * DESCRIBE THE FIELD       */      protected SourceRoute route;      // the current liveness of this route      /**       * DESCRIBE THE FIELD       */      protected int liveness;      // the current best-known proximity of this route      /**       * DESCRIBE THE FIELD       */      protected int proximity;      // the last time the liveness information was updated      /**       * DESCRIBE THE FIELD       */      protected long updated;      // whether or not a check dead is currently being carried out on this route      /**       * DESCRIBE THE FIELD       */      protected boolean pending;      /**       * Constructor - builds a route manager given the route       *       * @param route The route       */      public SourceRouteManager(SourceRoute route) {        if (route == null) {          throw new IllegalArgumentException("route is null");        }        this.route = route;        this.liveness = SocketNodeHandle.LIVENESS_SUSPECTED;        this.proximity = SocketNodeHandle.DEFAULT_PROXIMITY;        this.pending = false;        this.updated = 0L;      }      /**       * Returns whether or not a socket is currently open to this route       *       * @return Whether or not a socket is currently open to this route       */      public boolean isOpen() {        return manager.isOpen(route);      }      /**       * Method which returns the last cached proximity value for the given       * address. If there is no cached value, then DEFAULT_PROXIMITY is       * returned.       *       * @return The ping value to the remote address       */      public int proximity() {        return proximity;      }      /**       * This method should be called when this route is declared alive.       */      protected void markAlive() {        this.liveness = SocketNodeHandle.LIVENESS_ALIVE;        this.pending = false;      }      /**       * This method should be called when this route is declared suspected.       */      protected void markSuspected() {        this.liveness = SocketNodeHandle.LIVENESS_SUSPECTED;      }      /**       * This method should be called when this route is declared dead.       */      protected void markDead() {        this.liveness = SocketNodeHandle.LIVENESS_DEAD;        this.pending = false;      }      /**       * This method should be called when this route has its proximity updated       *       * @param proximity The proximity       */      protected void markProximity(int proximity) {        if (this.proximity > proximity) {          this.proximity = proximity;        }      }      /**       * Method which checks to see this route is dead. If this address has been       * checked within the past CHECK_DEAD_THROTTLE millis, then this method       * does not actually do a check.       *       * @return Whether or not a check will actually be carried out       */      protected boolean checkLiveness() {        if (this.pending) {          return true;        }        if ((this.liveness < SocketNodeHandle.LIVENESS_DEAD) ||          (this.updated < spn.getEnvironment().getTimeSource().currentTimeMillis() - CHECK_DEAD_THROTTLE)) {          this.updated = spn.getEnvironment().getTimeSource().currentTimeMillis();          this.pending = true;          manager.checkLiveness(route);          return true;        }        return false;      }      /**       * Method which enqueues a message along this route       *       * @param message The message to send       */      public synchronized void send(SocketBuffer message) {        manager.send(route, message, AddressManager.this);      }      /**       * Method which opens a socket along this route       *       * @param appId DESCRIBE THE PARAMETER       * @param receiver DESCRIBE THE PARAMETER       * @param timeout DESCRIBE THE PARAMETER       */      public synchronized void connect(int appId, AppSocketReceiver receiver, int timeout) {        manager.connect(route, appId, receiver, timeout);      }      /**       * Method which suggests a ping to the remote node.       */      public void ping() {        manager.ping(route);      }    }  }  /**   * DESCRIBE THE CLASS   *   * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $   * @author jeffh   */  static class HardLinkTimerTask extends TimerTask {    AddressManager manager;    /**     * Constructor for HardLinkTimerTask.     *     * @param manager DESCRIBE THE PARAMETER     */    public HardLinkTimerTask(AddressManager manager) {      this.manager = manager;    }    /**     * Main processing method for the HardLinkTimerTask object     */    public void run() {      // do nothing, just expire    }  }}

⌨️ 快捷键说明

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