131 satır
3,8 KiB
Java
131 satır
3,8 KiB
Java
package liste;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
import nom.GestionnaireNom;
|
|
|
|
public class TraitementCmdListe implements Runnable{
|
|
|
|
private String[] cmd;
|
|
private Semaphore semaphore;
|
|
|
|
public TraitementCmdListe(String message, Semaphore semaphore) {
|
|
this.cmd = message.split("\\$\\$\\$", 0);
|
|
this.semaphore = semaphore;
|
|
if(cmd.length != 4) {
|
|
System.out.println("Erreur nombre d'arguments");
|
|
this.cmd = new String[] {"", "", "", ""};
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
try {
|
|
|
|
String ordre = cmd[0];
|
|
String id = cmd[1];
|
|
String nom = cmd[2];
|
|
String ip = cmd[3];
|
|
|
|
if (ordre.equals("listRequest")) {
|
|
envoyerNom();
|
|
}
|
|
|
|
semaphore.acquireUninterruptibly();
|
|
System.out.println("bloqué");
|
|
|
|
if (ordre.equals("delete")) {
|
|
GestionnaireListeUtilisateur.instance().setListeUtilisateur(supprimeUtilisateur(GestionnaireListeUtilisateur.instance().getListeUtilisateur(), id));
|
|
}
|
|
|
|
if (ordre.equals("add")) {
|
|
if (nom.equals("null")) {
|
|
//System.out.println("NULLLLLLLL");
|
|
//on ne fait rien (utilisateur non encore configuré)
|
|
} else {
|
|
//GestionnaireListeUtilisateur.instance().setListeUtilisateur(
|
|
ajouteUtilisateur(GestionnaireListeUtilisateur.instance().getListeUtilisateur(), id, nom, ip);//);
|
|
for(int i=0; i<GestionnaireListeUtilisateur.instance().getListeUtilisateur().size(); i++) {
|
|
System.out.println("ici : " + GestionnaireListeUtilisateur.instance().getListeUtilisateur().get(i).nom + " "
|
|
+ GestionnaireListeUtilisateur.instance().getListeUtilisateur().get(i).id );
|
|
}
|
|
}
|
|
|
|
}
|
|
System.out.println("libéré");
|
|
semaphore.release();
|
|
|
|
|
|
//Thread.sleep(1000);
|
|
//System.out.println("fini " + id);
|
|
//System.out.println(GestionnaireListeUtilisateur.getListeUtilisateur().get(0).nom);
|
|
|
|
} catch (Exception e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
//envoie la liste courrante d'utilisateur
|
|
public void envoyerNom() { //localHost TODO (gerer IP, unhandle exception ...)
|
|
UdpBroadcastClient client = new UdpBroadcastClient(ConstanteListeUtilisateur.NUM_PORT_SERVER,
|
|
"add"+ "$$$" + GestionnaireNom.instance().getId() + "$$$" + GestionnaireNom.instance().getNom() + "$$$" + "LocalHost");
|
|
Thread t = new Thread(client);
|
|
t.start();
|
|
}
|
|
|
|
public void envoyerNom2(int port) { //localHost TODO (gerer IP, unhandle exception ...)
|
|
UdpUnicastClient client = new UdpUnicastClient(port, "LocalHost",
|
|
"add"+ "$$$" + GestionnaireNom.instance().getId() + "$$$" + GestionnaireNom.instance().getNom() + "$$$" + "LocalHost");
|
|
Thread t = new Thread(client);
|
|
t.start();
|
|
}
|
|
|
|
|
|
|
|
|
|
// detecte les nouveaux utilisateurs entrant et les ajoute à la liste
|
|
private ArrayList<TypeListeUtilisateur> ajouteUtilisateur(ArrayList<TypeListeUtilisateur> listeUtilisateur, String id, String nom, String ip) {
|
|
|
|
boolean inListe = false;
|
|
|
|
//si il est dans la liste on le renomme
|
|
for (int i=0; i<listeUtilisateur.size(); i++) {
|
|
if (listeUtilisateur.get(i).id.equals(id)) {
|
|
inListe = true;
|
|
TypeListeUtilisateur utilisateur = listeUtilisateur.get(i);
|
|
utilisateur.nom = nom;
|
|
|
|
listeUtilisateur.set(i, utilisateur);
|
|
}
|
|
}
|
|
|
|
//s'il n'est pas dans la liste on la rajoute
|
|
if (!inListe) {
|
|
listeUtilisateur.add(new TypeListeUtilisateur(id, nom, ip));
|
|
|
|
}
|
|
|
|
return listeUtilisateur;
|
|
|
|
}
|
|
|
|
|
|
|
|
// detecte les utilisateurs sortant et les supprime de la liste
|
|
private ArrayList<TypeListeUtilisateur> supprimeUtilisateur(ArrayList<TypeListeUtilisateur> listeUtilisateur, String id) {
|
|
|
|
for (int i=0; i<listeUtilisateur.size(); i++) {
|
|
if (listeUtilisateur.get(i).id.equals(id)) {
|
|
|
|
listeUtilisateur.remove(i);
|
|
}
|
|
}
|
|
|
|
return listeUtilisateur;
|
|
|
|
}
|
|
}
|