Пример #1
0
  /**
   * Given a RouteMessage and a Node as input, onMessage's method do execution steps for the
   * behaviour. Node should be casted to Node's class in order to manage data structures and
   * protocol of designed overlay.
   *
   * @param msg RouteMessage taken as input.
   * @param node Node taken as input.
   */
  public void onMessage(RouteMessage msg, Node node) {
    symphony = (SymphonyNode) node;

    outcommingSet = symphony.getOutcommingSet();
    incommingSet = symphony.getIncommingSet();

    /* We suppose long links are bidireccional TCP connections.
     */
    longHandle = msg.getSource();
    alreadyInOutcommingSet = outcommingSet.contains(longHandle);

    /* If this new long distance is accepted, the remote node only may contain
     * this local node in its incomming set (this local node only may contain the
     * remote node in the neighbour set or outcomming set).
     * If this new long distance already exists in the neighbour set,
     * the long distance is closed and attempt to find a new long distance.
     */
    if (symphony.neighbourSetContains(longHandle)) {
      symphony.sendMessage(
          symphony.buildMessage(
              symphony.getLocalHandle(),
              longHandle,
              SymphonyNode.CLOSE_LONG_CONNECT,
              SymphonyNode.REFRESH,
              null));
      getNewLongDistance();
      return;
    } else {
      if (incommingSet.contains(longHandle)) {
        // this case only occurs when two nodes simultaneously send
        // a QUERY_CONNECT to the other one in the same step.
        if (symphony.getId().compareTo(longHandle.getId()) < 0) {
          // do'nt require a CLOSE_CONNECT message
          incommingSet.remove(longHandle);
          if (!alreadyInOutcommingSet) outcommingSet.add(longHandle);
        } else {
          getNewLongDistance();
          return;
        }
      } else {
        if (!alreadyInOutcommingSet) outcommingSet.add(longHandle);
      }
    }

    /*
     *  If enough long link's aspirants, the new outcommingSet is the new  long distances set
     *  and the old outcomming's Set is now stale. Therefore, the peers must close the stale
     *  long connections. Otherwise, we call getNewLongDistance() method.
     *
     */
    if (outcommingSet.size() > SymphonyNode.getLongDistanceNumber()) {
      // the stalest node is the first in the outcomming set (the vector adds at the end)
      stale = (NodeHandle) outcommingSet.remove(0);

      close =
          symphony.buildMessage(
              symphony.getLocalHandle(),
              stale,
              SymphonyNode.CLOSE_LONG_CONNECT,
              SymphonyNode.REFRESH,
              null);
      symphony.sendMessage(close);
    } else if (outcommingSet.size() < SymphonyNode.getLongDistanceNumber()) getNewLongDistance();
  }