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)); }
public int starSynchro(boolean finishedNode[]) { int arite = this.getArity(); int[] answer = new int[arite]; /* random */ int choosenNumber = Math.abs(SynchronizedRandom.nextInt()); /* Send to all neighbours */ for (int i = 0; i < arite; i++) { if (!finishedNode[i]) { this.sendTo( i, new IntegerMessage(new Integer(choosenNumber), Mazurkiewicz_Election.synchronization)); } } /* receive all numbers from neighbours */ for (int i = 0; i < arite; i++) { if (!finishedNode[i]) { Message msg = this.receiveFrom(i); answer[i] = ((IntegerMessage) msg).value(); if (answer[i] == -1) { finishedNode[i] = true; } } } /* get the max */ int max = choosenNumber; for (int i = 0; i < arite; i++) { if (!finishedNode[i]) { if (answer[i] >= max) { max = answer[i]; } } } for (int i = 0; i < arite; i++) { if (!finishedNode[i]) { this.sendTo(i, new IntegerMessage(new Integer(max), Mazurkiewicz_Election.synchronization)); } } /* get alla answers from neighbours */ for (int i = 0; i < arite; i++) { if (!finishedNode[i]) { Message msg = this.receiveFrom(i); answer[i] = ((IntegerMessage) msg).value(); } } /* get the max */ max = choosenNumber; for (int i = 0; i < arite; i++) { if (answer[i] >= max) { max = answer[i]; } } if (choosenNumber >= max) { for (int door = 0; door < arite; door++) { if (!finishedNode[door]) { this.setDoorState(new SyncState(true), door); } } for (int i = 0; i < arite; i++) { if (!finishedNode[i]) { this.sendTo(i, new IntegerMessage(new Integer(1), Mazurkiewicz_Election.synchronization)); } } for (int i = 0; i < arite; i++) { if (!finishedNode[i]) { /* Message msg= */ this.receiveFrom(i); } } Mazurkiewicz_Election.synchroNumber++; return this.starCenter; } else { int inTheStar = this.notInTheStar; for (int i = 0; i < arite; i++) { if (!finishedNode[i]) { this.sendTo(i, new IntegerMessage(new Integer(0), Mazurkiewicz_Election.synchronization)); } } for (int i = 0; i < arite; i++) { if (!finishedNode[i]) { Message msg = this.receiveFrom(i); if (((IntegerMessage) msg).value() == 1) { inTheStar = i; } } } return inTheStar; } }