package graphe; import java.io.*; import java.util.StringTokenizer; import javax.swing.*; /** * Classe ElectionJeton. * Cette classe permet de gérer les élections dans le graphe sur le * principe de la circulation de jeton. * @author Estelle Colin, Thomas Peclier, Fabrice Berna @ IUP GMI * @see Graphe, Application, Sommet * @version 1.0 */ public abstract class ElectionJeton extends Sommet { /*----------------------------- Variables -----------------------------*/ private boolean[][] Tvoisins; private int[] Tpere; private int prochain; int initiateurId; int chef; public String etat; String message; StringTokenizer strToken; /*----------------------------- Accesseurs -----------------------------*/ /*----------------------------- Méthodes -----------------------------*/ public ElectionJeton(int i, int nb, boolean[] v, JTextArea out) { super(i, nb, v, out); Tvoisins = new boolean[nb][nbvoisins]; Tpere = new int[nb]; for (int j = 0; j < nb; j++) { Tpere[j] = id; } nbSommet = nb; etat = "repos"; chef = id; } private final int extraire(int t) { for (int i = 0; i < nbvoisins; i++) { if (Tvoisins[t][i]) { Tvoisins[t][i] = false; return i; } } return -1; } private final void initier(String s) throws IOException { for (int i = 0; i < nbvoisins; i++) Tvoisins[id][i] = true; prochain = extraire(id); ecrit(prochain, s + " " + id); } final void sur_reception_de(int j, String s, int k) throws IOException { if (!faire_suivre(j, s, k)) { if (initiateurId == id) println("### J'ai recu mon message : " + id + " ###"); } } private final boolean faire_suivre(int j, String s, int k) throws IOException { if ((k != id) && (Tpere[k] == id)) { Tpere[k] = j; for (int i = 0; i < nbvoisins; i++) Tvoisins[k][i] = true; Tvoisins[k][j] = false; } prochain = extraire(k); if (prochain != -1) { ecrit(prochain, s + " " + initiateurId); return true; } else { if (k != id) { ecrit(Tpere[k], s + " " + initiateurId); Tpere[k] = id; return false; } } return false; } public final void candidature(int init, int couleurReçue) throws IOException { if (nbvoisins == 0) { chef = id; etat = "terminé"; initierColoration(); } else if (etat.equals("repos")) { etat = "en_cours"; chef = id; initier("Election"); } } final void sur_reception_req(int j, String s, int k) throws IOException { if (etat.equals("repos") || (k >= chef)) { etat = "en_cours"; chef = k; if (!faire_suivre(j, s, k)) { if (initiateurId == id) { etat = "terminé"; initier("Conf"); println( "##### Je suis élu " + id + " J'en informe tout le graphe #####"); } } } } final void sur_reception_conf(int j, String s, int k) throws IOException { etat = "terminé"; if (!faire_suivre(j, s, k)) if (id == k) { println("##### Je suis élu " + id + " #####"); initierColoration(); } } /** * Méthode d'interprétation des messages reçus */ public void interprete(String s, int i) throws IOException { strToken = new StringTokenizer(s); while (strToken.hasMoreTokens()) { message = strToken.nextToken(); initiateurId = Integer.parseInt(strToken.nextToken()); } if (message.equals("Election")) { sur_reception_req(i, message, initiateurId); } else if (message.equals("Conf")) { sur_reception_conf(i, message, initiateurId); } else { sur_reception_de(i, message, initiateurId); } } /** * Etat du sommet */ public void etat() { println(id + " | " + nbvoisins + " voisins | etat " + etat + " | chef " + chef); } /** * Méthode abstraite */ public abstract void initierColoration() throws IOException; }