1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
| import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.net.*;
import java.io.*;
public class NOEUD extends _iNOEUDStub {
/**Attributs*/
public String nom;
public int numero;
private int capacite;
private iNOEUD suivant;
/**Implantation de l'interface*/
public iNOEUD RechercheConnection(String RmiR) {
iNOEUD mdj = null;
try {
String dummy[] = RmiR.split(" ");
ORB orb = ORB.init(dummy, null);
orb.connect(this);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
NameComponent nc = new NameComponent(dummy[0], "");
NameComponent[] path = { nc };
mdj = (iNOEUD) ncRef.resolve(path);
} catch (Exception e) {
e.printStackTrace();
}
return mdj;
}
public iNOEUD DemandeConnection(iNOEUD n) {
iNOEUD suivant = this.GetSuivant();
this.SetSuivant(n);
return suivant;
}
public void CloseSite(int i) {
iNOEUD temp_suivant = suivant;
if(suivant.GetNumSuivant() == i) {
this.SetSuivant(suivant.GetSuivant());
System.out.println("Le noeud suivant (" + temp_suivant.GetNumSuivant() + ") va être fermé. Le nouveau suivant est le site numéro " + suivant.GetNumSuivant() + ".");
}
if(this.numero != 1) {
if(this.numero == i) {
System.out.println("Le noeud est exclu de l'anneau.");
}
temp_suivant.CloseSite(i);
} else {
this.GetSuivant().Election(this.capacite, this.capacite, this.numero, this.numero);
}
}
public void Numerote(int i) {
if(this.numero != 1) {
this.numero = i + 1;
this.capacite = (int) (Math.random() * 1024);
System.out.println("Numéro : " + this.numero + "\tCapacité : " + this.capacite);
if(suivant.GetNomSuivant().equals("MaitreDesJeux")) {
System.out.println("Suivant : 1");
} else {
int num_suivant = i + 2;
System.out.println("Suivant : " + num_suivant);
}
this.GetSuivant().Numerote(this.numero);
} else {
this.GetSuivant().Election(this.capacite, this.capacite, this.numero, this.numero);
}
}
public void Election(int capaciteInit, int capaciteGagnante, int siteGagnant, int siteInitiateur) {
if(this.numero == siteInitiateur) {
if(siteGagnant == this.numero) {
System.out.println("Tous les sites sont fermés, le programme va se terminer.");
System.exit(2);
} else {
System.out.println("Le site gagnant est le site numéro " + siteGagnant + ".");
iNOEUD temp_suivant = suivant;
if(suivant.GetNumSuivant() == siteGagnant) {
this.SetSuivant(suivant.GetSuivant());
System.out.println("Le noeud suivant (" + temp_suivant.GetNumSuivant() + ") va être fermé. Le nouveau suivant est le site numéro " + suivant.GetNumSuivant() + ".");
}
temp_suivant.CloseSite(siteGagnant);
}
} else {
if((siteGagnant == siteInitiateur) || (Math.abs(capaciteGagnante - capaciteInit) > Math.abs(this.capacite - capaciteInit))) {
this.GetSuivant().Election(capaciteInit, this.capacite, this.numero, siteInitiateur);
} else {
this.GetSuivant().Election(capaciteInit, capaciteGagnante, siteGagnant, siteInitiateur);
}
}
}
public String GetNomSuivant() {
return this.nom;
}
public iNOEUD GetSuivant() {
return this.suivant;
}
public int GetNumSuivant() {
return this.numero;
}
/**Méthodes additionnelles*/
public NOEUD(String S) {
this.nom = S;
this.suivant = null;
}
public void SetNomSuivant(String s) {
}
public void SetSuivant(iNOEUD s) {
this.suivant = s;
}
public int GetNum() {
return this.numero;
}
public static void main(String[] args) {
if(args.length > 10 || args.length < 1) {
System.out.println("Arguments : nomdunoeud");
}
else {
try {
NOEUD n = new NOEUD(args[0]);
if(args[0].equals("MaitreDesJeux")) {
// Crée l'ORB et l'initialise:
ORB orb = ORB.init(args, null);
// Crée l'objet servant et l'enregistre :
orb.connect(n);
// Obtient la racine du contexte de nommage :
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// Associe un nom à la référence de l'objet (binding)
NameComponent nc = new NameComponent(args[0], "");
NameComponent[] path = { nc };
ncRef.rebind(path, n);
System.out.println(args[0] + " est enregistré.");
System.out.println();
System.out.println("Appuyez sur \"Entrée\" pour lancer l'élection.");
BufferedReader entree = new BufferedReader(new InputStreamReader(System.in));
String ligne = entree.readLine();
if(ligne != null) {
n.Numerote(0);
}
} else {
String nom = args[0];
args[0] = "MaitreDesJeux";
String dummy = "";
for (int i = 0; i < args.length; i++) {
if(i == 0) {
dummy = dummy + args[i];
} else {
dummy = dummy + " " + args[i];
}
}
iNOEUD mdj = n.RechercheConnection(dummy);
if(mdj == null) {
System.out.println("Erreur : créez d'abord le noeud maître du jeu.");
} else {
iNOEUD suivant = mdj.DemandeConnection(n);
if(suivant == null) {
suivant = mdj;
}
n.SetSuivant(suivant);
System.out.println("Le noeud " + nom + " est implanté.");
java.lang.Object sync = new java.lang.Object();
synchronized(sync){
sync.wait();
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
} |
Partager