コード例 #1
0
  public void init() {

    int graphS = getNetSize(); /* la taille du graphe */
    int synchro;
    boolean run = true; /* booleen de fin  de l'algorithme */
    String neighbourLabel;
    boolean finishedNode[] = new boolean[getArity()];
    String lastName;
    int nb;
    Vector name;

    for (int i = 0; i < getArity(); i++) finishedNode[i] = false;

    while (run) {

      synchro = synchronization(finishedNode);

      nb = getArity();
      for (int i = 0; i < getArity(); i++) if (finishedNode[i]) nb--;
      if (nb == 0) {
        putProperty("label", new String(eNode));
        break;
      }

      sendTo(synchro, new StringMessage((String) getProperty("label"), labels));
      neighbourLabel = ((StringMessage) receiveFrom(synchro)).data();

      if ((((String) getProperty("label")).compareTo(nNode) == 0)
          && (neighbourLabel.compareTo(nNode) == 0)) {

        int choosenNumber = Math.abs(SynchronizedRandom.nextInt());
        sendTo(synchro, new IntegerMessage(new Integer(choosenNumber)));
        Message msg = receiveFrom(synchro);
        int answer = ((IntegerMessage) msg).value();

        if (choosenNumber < answer) {
          putProperty("label", new String(fNode));
          run = false;
        }
        /*else
        if (choosenNumber>answer) {
            finishedNode[synchro]=true;
            nbr_arity--;
            }*/
      }

      // System.out.println(nbr_arity);

    }

    for (int i = 0; i < getArity(); i++)
      if (!finishedNode[i]) sendTo(i, new IntegerMessage(new Integer(-1), synchronization));
  }
コード例 #2
0
  /** Un round de la synchronisation. */
  private int trySynchronize(boolean finishedNode[]) {
    int arite = getArity();
    int[] answer = new int[arite];
    int nb;

    /*choice of the neighbour*/
    Random generator = new Random();
    int choosenNeighbour = Math.abs((generator.nextInt())) % arite;

    nb = arite;
    for (int i = 0; i < arite; i++) if (finishedNode[i]) nb--;

    if (nb == 0) return -1;

    while (finishedNode[choosenNeighbour]) {
      generator = new Random();
      choosenNeighbour = Math.abs((generator.nextInt())) % arite;
    }

    sendTo(choosenNeighbour, new IntegerMessage(new Integer(1), synchronization));
    for (int i = 0; i < arite; i++) {
      if (i != choosenNeighbour)
        if (!finishedNode[i]) sendTo(i, new IntegerMessage(new Integer(0), synchronization));
    }

    for (int i = 0; i < arite; i++) {
      if (!finishedNode[i]) {
        Message msg = receiveFrom(i, new IntegerMessageCriterion());
        IntegerMessage smsg = (IntegerMessage) msg;

        answer[i] = smsg.value();
        if (answer[i] == -1) finishedNode[i] = true;
      }
    }
    if (answer[choosenNeighbour] == 1) {
      return choosenNeighbour;
    } else return -2;
  }