diff --git a/Application/Clavardage/bin/.gitignore b/Application/Clavardage/bin/.gitignore deleted file mode 100644 index 2d535ad..0000000 --- a/Application/Clavardage/bin/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/RemoteUser.class diff --git a/Application/Clavardage/bin/RemoteUser.class b/Application/Clavardage/bin/RemoteUser.class new file mode 100644 index 0000000..cc1ce98 Binary files /dev/null and b/Application/Clavardage/bin/RemoteUser.class differ diff --git a/Application/Clavardage/bin/User.class b/Application/Clavardage/bin/User.class index e4996a5..3048524 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 8e6ce10..499b0d2 100644 Binary files a/Application/Clavardage/bin/UserListeningThread.class and b/Application/Clavardage/bin/UserListeningThread.class differ diff --git a/Application/Clavardage/src/User.java b/Application/Clavardage/src/User.java index 8a1a57c..a6ac7f1 100644 --- a/Application/Clavardage/src/User.java +++ b/Application/Clavardage/src/User.java @@ -4,14 +4,23 @@ import java.util.Date; import java.util.Scanner; // Import the Scanner class import java.util.ArrayList; // import the ArrayList class + public class User{ - + final static int portUDPlistening_remoteUsr1 = 20001; + protected InetAddress addIP; protected String pseudo; - protected int nport; + + protected int portUDPsend; + protected int portUDPlistening; + protected int portTCP; + protected boolean actif; protected ArrayList remoteUserList = new ArrayList(); + protected ArrayList userChatList = new ArrayList(); + + protected UserListeningThread threadListeningUDP; public Boolean stopListeningUDPThread=false; /* @@ -21,7 +30,7 @@ public class User{ * 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 nport) throws IOException{ + public User(int portUDPsend,int portUDPlistening,int portTCP) throws IOException{ //localUser try { @@ -31,37 +40,23 @@ public class User{ System.out.println("Could not find local address!"); } - this.nport = nport; + this.portUDPsend = portUDPsend; + this.portUDPlistening = portUDPlistening; + this.portTCP = portTCP; this.setPseudo(); - this.actif = true; + this.setActif(true); + - UserListeningThread th = new UserListeningThread("UDP Listening thread",this); - th.start(); - //notify_remote_user(); //ask_change_pseudo(); + /* boolean wantstochange = true; //A faire avec un bouton dans SWING if(wantstochange) { //changePseudo(); } - - DatagramSocket dgramSocket= null; - try { - dgramSocket= new DatagramSocket(this.nport,this.addIP); - } catch (IOException e) { - e.printStackTrace(); - } - - String toSend = this.addIP.toString()+":"+this.nport+":"+this.pseudo+":test"; - //System.out.println("Message avant envoi " +toSend); - DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, 12229); - - try { - dgramSocket.send(outPacket); - } catch (IOException e) { - e.printStackTrace(); - } + */ + //this.waitProgramtoStop() } /* remoteUser @@ -72,7 +67,7 @@ public class User{ * 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 nport,String pseudo) { + public User(int portUDPsend,int portUDPlistening, int portTCP,String pseudo) { try { this.addIP = InetAddress.getLocalHost(); @@ -81,10 +76,10 @@ public class User{ System.out.println("Could not find local address!"); } this.pseudo = pseudo; - this.nport = nport; - this.actif = true; - UserListeningThread th = new UserListeningThread("UDP Listening thread",this); - th.start(); + this.portUDPsend = portUDPsend; + this.portUDPlistening = portUDPlistening; + this.portTCP = portTCP; + this.setActif(true); } @@ -104,37 +99,78 @@ public class User{ return pseudo; } - public int getNport() { - return nport; + public int getPortTCP() { + return portTCP; } - public void setNport(int nport) { - this.nport = nport; + public void setPortTCP(int portTCP) { + this.portTCP = portTCP; + } + + public int getPortUDPlistening() { + return portUDPlistening; } + 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) { - this.actif = actif; + notify_remote_user(); + this.threadListeningUDP = new UserListeningThread("UDP Listening thread",this); + this.threadListeningUDP.start(); + + this.actif=true; + } + + public void notify_remote_user() { + DatagramSocket dgramSocket= null; + try { + dgramSocket= new DatagramSocket(portUDPsend,this.addIP); + } catch (IOException e) { + e.printStackTrace(); + } + + 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); + + try { + dgramSocket.send(outPacket); + } catch (IOException e) { + e.printStackTrace(); + } + dgramSocket.close(); } public void setPseudo() throws IOException { - Scanner myObj = new Scanner(System.in); // Create a Scanner object + Scanner sc1 = new Scanner(System.in); // Create a Scanner object System.out.println("Enter nickname :"); - String tmpPseudo = myObj.nextLine(); // Read user input + String tmpPseudo = sc1.nextLine(); // Read user input while(!(this.validatePseudo(tmpPseudo))) { System.out.println("Enter another nickname :"); - tmpPseudo = myObj.nextLine(); // Read user input + tmpPseudo = sc1.nextLine(); // Read user input } this.pseudo = tmpPseudo; - myObj.close(); + //sc1.close(); System.out.println("Your nickname : " + tmpPseudo + " is valid !"); } @@ -143,17 +179,18 @@ public class User{ // Call broadcast InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast - - DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP); + //System.out.println(broadcastIP); + DatagramSocket dgramSocket = new DatagramSocket(this.portUDPsend,this.addIP); byte[] buffer = new byte[256]; - DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228); + String toSend = this.getAddIP()+":"+this.portUDPsend+":test"; + DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, portUDPlistening_remoteUsr1); dgramSocket.send(outPacket); Boolean valid = true; - + Date oldDate = new Date(); Date newDate = new Date(); @@ -164,7 +201,6 @@ public class User{ Boolean arecu; System.out.print("Wait for pseudo validation ...\n"); - System.out.println(".............."); int nbReponse =0; while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) { @@ -198,7 +234,6 @@ public class User{ newDate = new Date(); } - System.out.println(".............."); dgramSocket.close(); if(!valid) { @@ -212,50 +247,88 @@ public class User{ RemoteUser tmpRemoteUser = new RemoteUser(remoteUserIP,remoteUserPort,remoteUserPseudo); int index = this.remoteUserList.indexOf(tmpRemoteUser); if(index!=-1) { - System.out.println("Mise a jour, l'adresse IP(port) de cet utilisateur est déjà présente :"+remoteUserIP+"("+remoteUserPort+")"); + System.out.println("("+this.pseudo+") - "+"MAJ, IP(port) of user : "+remoteUserPseudo+" => "+remoteUserIP+"("+remoteUserPort+")"); this.remoteUserList.set(index,tmpRemoteUser); } else { - System.out.println("Ajout de l'utilisateur ayant pour IP(port) :"+remoteUserIP+"("+remoteUserPort+")"); + System.out.println("("+this.pseudo+") - "+"Add new user IP(port) of user : "+remoteUserPseudo+" => "+remoteUserIP+"("+remoteUserPort+")"); this.remoteUserList.add(tmpRemoteUser); } } // change pseudo - /* + private void changePseudo() 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 myObj = new Scanner(System.in); // Create a Scanner object + Scanner sc3 = new Scanner(System.in); // Create a Scanner object System.out.println("Enter new nickname :"); - String tmpPseudo = myObj.nextLine(); // Read user input + String tmpPseudo = sc3.nextLine(); // Read user input - while(!(this.validatePseudo(tmpPseudo)) & tmpPseudo.compareTo(oldPseudo) == 0) { + while(!(this.validatePseudo(tmpPseudo)) || tmpPseudo.equals(oldPseudo)) { System.out.println("Enter another nickname :"); - tmpPseudo = myObj.nextLine(); // Read user input + tmpPseudo = sc3.nextLine(); // Read user input } this.pseudo = tmpPseudo; - myObj.close(); + //myObj.close(); System.out.println("Your new nickname : " + tmpPseudo + " is valid !"); - //notifyothers(); //jsp comment elle s'appelle -}*/ + notify_remote_user(); +} + public void printRemoteUserList() { + System.out.println("Internal list of remote users:\n"); - public static void main(String[] args) throws IOException { - User usr1 = new User(12228,"Mike"); + for(int i=0; i= 0 && index