257 lines
9.1 KiB
Java
257 lines
9.1 KiB
Java
|
import java.io.BufferedReader;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStreamReader;
|
||
|
import java.io.Serializable;
|
||
|
|
||
|
/*
|
||
|
* To change this template, choose Tools | Templates
|
||
|
* and open the template in the editor.
|
||
|
*/
|
||
|
import java.net.MalformedURLException;
|
||
|
import java.rmi.*;
|
||
|
import java.rmi.server.UnicastRemoteObject;
|
||
|
import java.util.StringTokenizer;
|
||
|
import java.util.logging.Level;
|
||
|
import java.util.logging.Logger;
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @author Smea
|
||
|
*/
|
||
|
|
||
|
|
||
|
public class NOEUDImpl extends UnicastRemoteObject implements NOEUD, Serializable{
|
||
|
public NOEUD suivant = null;
|
||
|
public String nom = "";
|
||
|
public String rmi = "";
|
||
|
public int numero = 0;
|
||
|
public int survivance = 0;
|
||
|
|
||
|
public NOEUDImpl(String n, String r) throws RemoteException {
|
||
|
this.nom = n;
|
||
|
this.rmi = r;
|
||
|
if(n.equals("INIT")) {
|
||
|
this.numero = 1;
|
||
|
this.survivance = (int)(Math.random()*1024)+1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public NOEUD ConnectSuivant() throws RemoteException {
|
||
|
NOEUD suiv = null;
|
||
|
try {
|
||
|
NOEUD init = (NOEUD) Naming.lookup(this.rmi + "/INIT" );
|
||
|
if(init.GetTonSuivant() == null) {
|
||
|
suiv = init;
|
||
|
}
|
||
|
else {
|
||
|
suiv = init.GetTonSuivant();
|
||
|
init.SetTonSuivant(this);
|
||
|
}
|
||
|
} catch (NotBoundException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
} catch (MalformedURLException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
}
|
||
|
finally {
|
||
|
return suiv;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public NOEUD GetTonSuivant() throws RemoteException {
|
||
|
return this.suivant;
|
||
|
}
|
||
|
|
||
|
public String GetInfo() throws RemoteException {
|
||
|
return this.nom +"(" + this.numero + ")" +"-"+this.survivance ;
|
||
|
}
|
||
|
|
||
|
public void SetTonSuivant(NOEUD n) throws RemoteException {
|
||
|
this.suivant = n;
|
||
|
}
|
||
|
|
||
|
public void CloseSite(NOEUD n) throws RemoteException {
|
||
|
if(this.GetTonSuivant().equals(n)) {
|
||
|
this.SetTonSuivant(n.GetTonSuivant());
|
||
|
}
|
||
|
else {
|
||
|
this.GetTonSuivant().CloseSite(n);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void Numerote(int n) throws RemoteException {
|
||
|
if(this.numero != 1) {
|
||
|
this.numero = n+1;
|
||
|
this.survivance = (int)(Math.random()*1024)+1;
|
||
|
this.GetTonSuivant().Numerote(n+1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void KillSite(int n) throws RemoteException {
|
||
|
if(this.numero == n) {
|
||
|
System.out.println("J'ai gagné, je m'en vais!");
|
||
|
this.CloseSite(this);
|
||
|
System.exit(1);
|
||
|
}
|
||
|
else {
|
||
|
|
||
|
this.GetTonSuivant().KillSite(n);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void Election(int capaciteInit, int capaciteGagnante, int siteGagnant, int siteInitiateur) throws RemoteException {
|
||
|
int dif = this.survivance-capaciteInit;
|
||
|
if(this.numero == siteInitiateur) {
|
||
|
new Thread() {
|
||
|
private int gagnant;
|
||
|
private int initiat;
|
||
|
public void start(int g, int i) {
|
||
|
this.gagnant = g;
|
||
|
this.initiat = i;
|
||
|
this.start();
|
||
|
}
|
||
|
public void run() {
|
||
|
try {
|
||
|
sleep(1000);
|
||
|
ProclamationElection(gagnant, initiat);
|
||
|
} catch (InterruptedException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
} catch (RemoteException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
}
|
||
|
}
|
||
|
}.start(siteGagnant,siteInitiateur);
|
||
|
}
|
||
|
else {
|
||
|
if(dif <= capaciteGagnante) {
|
||
|
this.GetTonSuivant().Election(capaciteInit, Math.abs(this.survivance-capaciteGagnante), this.numero, siteInitiateur);
|
||
|
}
|
||
|
else {
|
||
|
this.GetTonSuivant().Election(capaciteInit, capaciteGagnante, siteGagnant, siteInitiateur);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public void ProclamationElection(int siteGagnant, int siteInitiateur) throws RemoteException {
|
||
|
System.out.println("Gagnant noeud numero " + siteGagnant);
|
||
|
this.KillSite(siteGagnant);
|
||
|
}
|
||
|
|
||
|
public static void afficheMenu() {
|
||
|
System.out.println('\t' + "K: Tuer ce noeud");
|
||
|
System.out.println('\t' + "I: Infos de ce noeud");
|
||
|
System.out.println('\t' + "N: Numeroter ");
|
||
|
System.out.println('\t' + "E: Election ");
|
||
|
System.out.println('\t' + "END: Quitter ");
|
||
|
}
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
String rmi = "rmi://" + args[0];
|
||
|
String nom = args[1];
|
||
|
NOEUD n = null;
|
||
|
boolean cont = true;
|
||
|
String cmd = "";
|
||
|
BufferedReader bfr = new BufferedReader( new InputStreamReader( System.in) );
|
||
|
if(nom.equals("INIT")) {
|
||
|
try {
|
||
|
n = new NOEUDImpl(nom, rmi);
|
||
|
Naming.bind(rmi + "/" + nom, n);
|
||
|
|
||
|
} catch (AlreadyBoundException ex) {
|
||
|
try {
|
||
|
Naming.rebind(rmi + "/" + nom, n);
|
||
|
} catch (RemoteException ex1) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex1);
|
||
|
} catch (MalformedURLException ex1) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex1);
|
||
|
}
|
||
|
} catch (MalformedURLException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
} catch (RemoteException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
}
|
||
|
finally {
|
||
|
try {
|
||
|
n.SetTonSuivant(n.ConnectSuivant());
|
||
|
} catch (RemoteException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
try {
|
||
|
n = new NOEUDImpl(nom, rmi);
|
||
|
n.SetTonSuivant(n.ConnectSuivant());
|
||
|
} catch (RemoteException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
}
|
||
|
}
|
||
|
System.out.println("Menu de " + nom);
|
||
|
System.out.println("Saisie commande: ");
|
||
|
|
||
|
while(cont) {
|
||
|
afficheMenu();
|
||
|
try {
|
||
|
cmd = bfr.readLine();
|
||
|
} catch (IOException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
}
|
||
|
if(cmd.equals("K")) {
|
||
|
try {
|
||
|
n.CloseSite(n);
|
||
|
System.exit(0);
|
||
|
} catch (RemoteException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
}
|
||
|
}
|
||
|
else if(cmd.equals("I")) {
|
||
|
try {
|
||
|
String tmp = "NOEUD " + n.GetInfo() + " suivant " + n.GetTonSuivant().GetInfo();
|
||
|
System.out.println(tmp);
|
||
|
} catch (RemoteException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
}
|
||
|
}
|
||
|
else if(cmd.equals("N")) {
|
||
|
try {
|
||
|
NOEUD init = (NOEUD) Naming.lookup(rmi +"/INIT");
|
||
|
init.GetTonSuivant().Numerote(1);
|
||
|
init = null;
|
||
|
} catch (NotBoundException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
} catch (MalformedURLException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
} catch (RemoteException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
}
|
||
|
}
|
||
|
else if(cmd.equals("E")) {
|
||
|
try {
|
||
|
|
||
|
NOEUD init = (NOEUD) Naming.lookup(rmi +"/INIT");
|
||
|
StringTokenizer stSurvi = new StringTokenizer(init.GetInfo(),"-");
|
||
|
stSurvi.nextToken();
|
||
|
String survie = stSurvi.nextToken();
|
||
|
init.GetTonSuivant().Election(new Integer(survie),1024,0,1);
|
||
|
init = null;
|
||
|
} catch (NotBoundException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
} catch (MalformedURLException ex) {
|
||
|
Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
} catch (RemoteException ex) {
|
||
|
//Logger.getLogger(NOEUDImpl.class.getName()).log(Level.SEVERE, null, ex);
|
||
|
} catch (Exception e) {
|
||
|
|
||
|
}
|
||
|
}
|
||
|
else if(cmd.equals("END")) {
|
||
|
cont = false;
|
||
|
}
|
||
|
cmd = "";
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|