diff --git a/Application/Clavardage/bin/RemoteUser.class b/Application/Clavardage/bin/RemoteUser.class index cc1ce98..8a456d3 100644 Binary files a/Application/Clavardage/bin/RemoteUser.class and b/Application/Clavardage/bin/RemoteUser.class differ diff --git a/Application/Clavardage/bin/User.class b/Application/Clavardage/bin/User.class index 3048524..510087c 100644 Binary files a/Application/Clavardage/bin/User.class and b/Application/Clavardage/bin/User.class differ diff --git a/Application/Clavardage/bin/UserListeningThread.class b/Application/Clavardage/bin/UserListeningThread.class index 499b0d2..8dafd7a 100644 Binary files a/Application/Clavardage/bin/UserListeningThread.class and b/Application/Clavardage/bin/UserListeningThread.class differ diff --git a/Application/Clavardage/src/RemoteUser.java b/Application/Clavardage/src/RemoteUser.java index c3dbb5f..6389019 100644 --- a/Application/Clavardage/src/RemoteUser.java +++ b/Application/Clavardage/src/RemoteUser.java @@ -3,12 +3,12 @@ import java.net.InetAddress; public class RemoteUser { InetAddress addIP; - int nPort; + int portTCP; String pseudo; - public RemoteUser(InetAddress remoteUserIP, int remoteUserPort,String pseudo) { + public RemoteUser(InetAddress remoteUserIP, int remoteUserPortTCP,String pseudo) { this.addIP=remoteUserIP; - this.nPort=remoteUserPort; + this.portTCP=remoteUserPortTCP; this.pseudo=pseudo; } @@ -21,11 +21,11 @@ public class RemoteUser { } public int getRemoteUserPort() { - return nPort; + return portTCP; } public void setRemoteUserPort(int remoteUserPort) { - this.nPort = remoteUserPort; + this.portTCP = remoteUserPort; } public String getPseudo() { @@ -49,7 +49,7 @@ public class RemoteUser { if (addIP == null) { if (other.addIP != null) return false; - } else if (!addIP.equals(other.addIP)) + } else if (!(addIP.equals(other.addIP) && portTCP==other.portTCP)) return false; return true; } diff --git a/Application/Clavardage/src/User.java b/Application/Clavardage/src/User.java index a6ac7f1..136a9be 100644 --- a/Application/Clavardage/src/User.java +++ b/Application/Clavardage/src/User.java @@ -6,28 +6,31 @@ import java.util.ArrayList; // import the ArrayList class public class User{ - final static int portUDPlistening_remoteUsr1 = 20001; - + + /*** CONSTANTES ***/ + final static int portUDPlistening_remoteUsr2 = 20002; + final static int portUDPlistening_remoteUsr3 = 20003; + + /*** VARIABLES ***/ protected InetAddress addIP; protected String pseudo; - protected int portUDPsend; protected int portUDPlistening; protected int portTCP; - protected boolean actif; - protected ArrayList remoteUserList = new ArrayList(); - protected ArrayList userChatList = new ArrayList(); + protected ArrayList remoteUserList = new ArrayList(); // listes des utilisateurs actifs + protected ArrayList userChatList = new ArrayList(); // listes des utilisateurs avec qui un chat est actif - protected UserListeningThread threadListeningUDP; - public Boolean stopListeningUDPThread=false; + protected UserListeningThread threadListeningUDP; // UDP listening thread (pour répondre aux nouveaux utilisateurs) /* * Constructor of User - * parameters - * #nport - * description : On récupère l'adresse de la machine, le numéro de port à utiliser(TODO), on demande un pseudo à l'utilisateur + * $parameters + * * + * * + * * + * $description : On récupère l'adresse de la machine, le numéro de port à utiliser(TODO), on demande un pseudo à l'utilisateur * que l'on vérifie, une fois validé, l'utilisateur devient actif (Début de l'écoute UDP pour les pseudos) */ public User(int portUDPsend,int portUDPlistening,int portTCP) throws IOException{ @@ -44,28 +47,20 @@ public class User{ this.portUDPlistening = portUDPlistening; this.portTCP = portTCP; - this.setPseudo(); + this.initPseudo(); this.setActif(true); - - //ask_change_pseudo(); - /* - boolean wantstochange = true; //A faire avec un bouton dans SWING - if(wantstochange) { - //changePseudo(); - } - */ - //this.waitProgramtoStop() } - /* remoteUser - * Constructor of User simuled a remote user (Already log into our system) - * parameters - * #nport - * #pseudo - * description : On récupère l'adresse de la machine, le numéro de port à utiliser(TODO), on demande un pseudo à l'utilisateur - * que l'on vérifie, une fois validé, l'utilisateur devient actif (Début de l'écoute UDP pour les pseudos) + /* + * Constructor of User (simulation des utilisateurs distants) + * $parameters + * * + * * + * * + * $description : On récupère l'adresse de la machine, le numéro de port à utiliser(TODO), on demande un pseudo à l'utilisateur + * que l'on vérifie, une fois validé, l'utilisateur devient actif (Début de l'écoute UDP pour les pseudos) */ public User(int portUDPsend,int portUDPlistening, int portTCP,String pseudo) { try @@ -84,53 +79,46 @@ public class User{ } - + /*** GETTER ***/ public InetAddress getAddIP() { return addIP; } - - - public void setAddIP(InetAddress addIP) { - this.addIP = addIP; - } - - public String getPseudo() { return pseudo; } - + public int getPortTCP() { return portTCP; } - - - public void setPortTCP(int portTCP) { - this.portTCP = portTCP; - } public int getPortUDPlistening() { return portUDPlistening; } + + public int getPortUDPsend() { + return portUDPsend; + } + public boolean isActif() { + return actif; + } + + /*** SETTER ***/ + public void setAddIP(InetAddress addIP) { + this.addIP = addIP; + } + public void setPortTCP(int portTCP) { + this.portTCP = portTCP; + } public void setPortUDPlistening(int portUDPlistening) { this.portUDPlistening = portUDPlistening; } - public int getPortUDPsend() { - return portUDPsend; - } - - public void setPortUDPsend(int portUDPsend) { this.portUDPsend = portUDPsend; } - public boolean isActif() { - return actif; - } - - public void setActif(boolean actif) { notify_remote_user(); this.threadListeningUDP = new UserListeningThread("UDP Listening thread",this); @@ -139,7 +127,13 @@ public class User{ this.actif=true; } + /*** PROCEDURES ***/ + + /* notify_remote_user + * En utilisant le port UDP d'envoi, on envoi en broadcast les informations nous concernant + */ public void notify_remote_user() { + // Création du socket d'envoi d'information DatagramSocket dgramSocket= null; try { dgramSocket= new DatagramSocket(portUDPsend,this.addIP); @@ -147,24 +141,38 @@ public class User{ e.printStackTrace(); } + // Construction du message à envoyer String toSend = this.addIP.toString()+":"+this.portTCP+":"+this.pseudo+":test"; - //System.out.println("Message avant envoi " +toSend); - DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, portUDPlistening_remoteUsr1); - + + + // Send information to usr2 + DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, portUDPlistening_remoteUsr2); try { dgramSocket.send(outPacket); } catch (IOException e) { e.printStackTrace(); } + + // Send information to usr3 + outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, portUDPlistening_remoteUsr3); + try { + dgramSocket.send(outPacket); + } catch (IOException e) { + e.printStackTrace(); + } + dgramSocket.close(); } - public void setPseudo() throws IOException { + /*initPseudo + * Demande à l'utilisateur de rentrer un pseudo et validation de ce dernier en demandant aux utilisateurs distants leurs informations + */ + public void initPseudo() throws IOException { Scanner sc1 = new Scanner(System.in); // Create a Scanner object System.out.println("Enter nickname :"); String tmpPseudo = sc1.nextLine(); // Read user input - while(!(this.validatePseudo(tmpPseudo))) { + while(!(this.validatePseudo(tmpPseudo))) { // On demande aux autres utilisateurs de nous envoyer leurs informations et on test si le pseudo est déjà utilisé System.out.println("Enter another nickname :"); tmpPseudo = sc1.nextLine(); // Read user input } @@ -174,6 +182,15 @@ public class User{ System.out.println("Your nickname : " + tmpPseudo + " is valid !"); } + /* validatePseudo + * $parametres + * *tmpPseudo : String => Le pseudo à valider + * $description + * On envoi en broadcast (pour l'instant envoi sur les ports de notre ordinateur) d'une demande d'information + * On attends les réponses pendant 5 secondes + * On valide le pseudo en fonction des pseudos reçu + * On en profites pour ajouter les utilisateurs répondant à notre liste d'utilisateur distant (RemoteUser) + */ public Boolean validatePseudo(String tmpPseudo) throws IOException { @@ -181,19 +198,24 @@ public class User{ InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast //System.out.println(broadcastIP); DatagramSocket dgramSocket = new DatagramSocket(this.portUDPsend,this.addIP); - byte[] buffer = new byte[256]; + // Création du message à envoyer String toSend = this.getAddIP()+":"+this.portUDPsend+":test"; - DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, portUDPlistening_remoteUsr1); - + + // Send to remote user 1 + DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, portUDPlistening_remoteUsr2); dgramSocket.send(outPacket); - Boolean valid = true; - - Date oldDate = new Date(); - Date newDate = new Date(); + // Send to remote user 2 + outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, portUDPlistening_remoteUsr3); + dgramSocket.send(outPacket); + // Initialisation des paramètres de la boucles + Boolean valid = true; + + + DatagramPacket inPacket; String response = null; String[] lstresponse= new String [4]; @@ -203,6 +225,8 @@ public class User{ System.out.print("Wait for pseudo validation ...\n"); int nbReponse =0; + Date oldDate = new Date(); + Date newDate = new Date(); while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) { nbReponse++; @@ -217,18 +241,19 @@ public class User{ buffer = inPacket.getData(); response = new String(buffer); - if(arecu) { - //System.out.println("Reponse : "+response); - + if(arecu) { + // On découpe la réponse en tableau de string ([adresseIP,tcpPort,nickname]) lstresponse = response.split(":"); + // Affichage de la réponse System.out.println("Remote user n°"+nbReponse); System.out.println("\tIP : "+lstresponse[0]); System.out.println("\tn°Port : "+lstresponse[1]); System.out.println("\tpseudo : "+lstresponse[2]); + // Si reception on ajoute l'utilisateur à notre liste d'utilisateur distant this.addRemoteUser(InetAddress.getByName(lstresponse[0].split("/")[1]),Integer.parseInt(lstresponse[1]),lstresponse[2]); - valid= (tmpPseudo.compareTo(lstresponse[2])!=0); + valid= (tmpPseudo.compareTo(lstresponse[2])!=0); // On regarde la différence entre notre pseudo et le pseudo reçu } @@ -257,7 +282,7 @@ public class User{ } // change pseudo -private void changePseudo() throws IOException { //seule différence avec setPseudo c'est qu'on check si on remet pas le même +private void setPseudo() throws IOException { //seule différence avec setPseudo c'est qu'on check si on remet pas le même String oldPseudo = this.pseudo; //Saves the old one for comparison Scanner sc3 = new Scanner(System.in); // Create a Scanner object @@ -275,23 +300,25 @@ private void changePseudo() throws IOException { //seule diff notify_remote_user(); } - + /*printRemoteUserList + * $descrition + * Affichage de la liste d'utilisateur actif avec leurs index dans la liste + */ public void printRemoteUserList() { - System.out.println("Internal list of remote users:\n"); + System.out.println("Internal list of active remote users:\n"); for(int i=0; i