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
| package fr.cnamp.factory;
import java.lang.Thread.*;
import java.rmi.Remote.*;
import java.rmi.server.*;
import fr.cnamp.agent.*;
import fr.cnamp.factory.*;
import java.rmi.registry.*;
import java.rmi.*;
public class FactoryJeuAgentOD extends UnicastRemoteObject implements FactoryJeuAgentODInt
{
// Attributs de FactoryJeuAgentOD
private String name;
private int nb;
private int color;
private int port;
private AgentOD[] listAgent; // tableau des agents
//constructeur FactoryJeuAgentOD
public FactoryJeuAgentOD(String reqname,int reqnb, int reqcolor, int reqport) throws Exception{
name=reqname;
nb=reqnb;
color=reqcolor;
port=reqport;
listAgent=new AgentOD[reqnb+1];
System.out.println("Execution de FactoryJeuAgentOD :");
try
{
// création des agents dans le tableau.
for (int i=1;i<=reqnb;i++)
{
System.out.println("Creation de l'Agent "+ i+"/"+reqnb);
listAgent[i]=new AgentOD(10,10, reqcolor);
System.out.println("Creation de l'Agent "+i+" termine ds factory");
//enregistrement de l'agent dans le registry
Naming.rebind("rmi://localhost:"+port+ "/"+"factory"+i ,this);
}
// pour faire bouger les agents dans le factory et pas dans l'AgentOD
moveAgent();
}
catch(Exception e){System.out.println(e.getMessage());}
}
// la fonction qui appellera le thread de deplacement de chaque agent.
public void moveAgent()throws RemoteException{
System.out.println("demarage de moveAgent() pour "+this.nb+" agents: ");
for (int i=1; i<=this.nb;i++)
{
System.out.println("demarage du tread " + i);
listAgent[i].th.start();
}
}
// afficher les coordonnées en appellant les fonction getX() et getY() dans AgentOD
public void printXY()throws RemoteException{
try{
//boucle d'affichage et de deplacement.
System.out.print("\n");
for (int i=1; i<=this.nb;i++){System.out.print("agent " + i+" \t");}System.out.println();
System.out.println();
while(true){
for (int i=1; i<=nb;i++){
System.out.print(listAgent[i].x + " - " + listAgent[i].y+"\t\t" );
}
try{ // pour attendre une seconde
Thread.sleep(1000); // avant de charger les valeur
} // de x et y,
catch (InterruptedException e){
System.out.println(e.getMessage());
}System.out.println();
}
}catch (Exception e){e.getMessage();}
}
public void creatFactory(String reqname,int rnb, int rcolor, int rport)throws RemoteException{
try{
FactoryJeuAgentOD fact=new FactoryJeuAgentOD( reqname, rnb, rcolor, rport);
fact.printXY();
}catch (Exception e){e.getMessage();}}
public static void main(String argv[]) throws Exception
{
try
{
// Creation de l'adptateur
LocateRegistry.createRegistry(9101);
}
catch(Exception e){
// si il existe deja alors inutile de le creer
}
try{
FactoryJeuAgentOD fact=new FactoryJeuAgentOD( "bux",5,1,9103);
fact.printXY();
}catch (Exception e){e.getMessage();}
}
} |
Partager