From 601e040edd7c084781794548f102851cbab502b8 Mon Sep 17 00:00:00 2001 From: Alexandre Gonzalvez Date: Wed, 9 Dec 2020 12:34:15 +0100 Subject: [PATCH] lien entre chat et message --- Application/Clavardage/bin/.gitignore | 7 +- Application/Clavardage/bin/RemoteUser.class | Bin 1589 -> 0 bytes Application/Clavardage/bin/User.class | Bin 10575 -> 0 bytes Application/Clavardage/src/Database.java | 9 - Application/Clavardage/src/Interface.java | 124 ----- Application/Clavardage/src/RemoteUser.java | 67 --- Application/Clavardage/src/User.java | 467 ------------------ .../src/UserConnexionthreadTCP.java | 55 --- .../src/UserListeningThreadTCP.java | 95 ---- .../src/UserListeningThreadUDP.java | 114 ----- Application/Clavardage/src/model/Chat.java | 4 + 11 files changed, 7 insertions(+), 935 deletions(-) delete mode 100644 Application/Clavardage/bin/RemoteUser.class delete mode 100644 Application/Clavardage/bin/User.class delete mode 100644 Application/Clavardage/src/Database.java delete mode 100644 Application/Clavardage/src/Interface.java delete mode 100644 Application/Clavardage/src/RemoteUser.java delete mode 100644 Application/Clavardage/src/User.java delete mode 100644 Application/Clavardage/src/UserConnexionthreadTCP.java delete mode 100644 Application/Clavardage/src/UserListeningThreadTCP.java delete mode 100644 Application/Clavardage/src/UserListeningThreadUDP.java diff --git a/Application/Clavardage/bin/.gitignore b/Application/Clavardage/bin/.gitignore index b32d6f7..6a15f9e 100644 --- a/Application/Clavardage/bin/.gitignore +++ b/Application/Clavardage/bin/.gitignore @@ -1,4 +1,3 @@ -/UserListeningThreadTCP.class -/UserListeningThreadUDP.class -/UserConnexionthreadTCP.class -/Database.class +/controller/ +/model/ +/view/ diff --git a/Application/Clavardage/bin/RemoteUser.class b/Application/Clavardage/bin/RemoteUser.class deleted file mode 100644 index db140d3c372dad1a8a241347659348e609c8c722..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1589 zcmZ`(OHUJF6g^*`v<#Hehae~qLE9-%UV>7@7!s43f<%yoyJ%xf8T!qSVqPVmq759eq1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>(C4Df zyqZ^IUN+sp=Wzo!BHq73?_uVJaLXOI{#t{TIwYONP27yy@<~1uXWwS5c?ToQ`65)K zQe~2JxesD>ZyC!IKRO7YMX87r5Y>QR^qg;r81XZ4L^4V&Q%*V?33Otu<$0#<7 zo!wyl`|ngxgusCtLD+v3yuAuqk<4O@FZ?3L^d0EuK z*Fr50)w&Mm%|QQCd_CU)I1NS`mq>5LW0k&JHQc`)!_kH1D@a?lPpCPBu!6d*M>wr) zCs!5H8#3t@3A2-8e4~{y6CqV{cH+fSw;f-rxEf35Agb=9VhMZW5oz(?ErhICTRjeu zpnozkTk}V!oiE|H_xy2)BYPpU1K;QmL_Q7EdfJ5NE)+PACGzV6j1n)z&zGtwkE>}A zFQTDbLyHhnmhic_hp(eB9B@6H?@~S=F4utT8IyK#6TT*Grrqqv7nuPBixy>#`RWEB zW?RDnONk@PIo_!qCgsR-7BiJ2XW;AicoyBAXo(A!QdT#ZcPJM;sxl%xWtB>k z;*H7`Rd-gi%8Cy8QHW(al4@LFFxAt=Tk7)pCQm2d+`+eb_VaBqz5~5?qjwv6w|DRk z^zMl9PN|SRIQ6bc*7hSrBwk1Tcsud&UrPGP zAc#o7do}~#d_8`%a~&<<8x>2|F>9gCQVxEn^Ej}~DzxF}YX8B4iHIV&WdeUa&Le<< zRf)gmiSZLXtMPn((k{P|>1Gt(L)RACjP0UJ(HbXjh#I?Nymu>2Oo+MPiaBZs6|W^+ zKF1Oww$sYgoU# remoteUserList = new ArrayList(); // listes des utilisateurs actifs - protected ArrayList userChatList = new ArrayList(); // listes des utilisateurs avec qui un chat est actif - - protected UserListeningThreadUDP threadListeningUDP; // UDP listening thread (pour répondre aux nouveaux utilisateurs) - protected UserListeningThreadTCP threadListeningTCP; // TCP listening thread (pour commencer une nouvelle conversation) - - /** - * Constructor of User (local) - * $parametres - * * portUDPsend : int => le numéro de port pour envoyé ces informations lors d'un changements ou d'une nouvelle connexion - * * portUDPlistening : int => le numéro de port pour recevoir les informations des nouveaux utilisateurs ou les changements - * * portTCP : int => le numéro de port pour commencer une nouvelle conversation - *

- * On récupère l'adresse de la machine, le numéro de port à utiliser, 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 et notification aux autres utilisateurs actifs) - *

- */ - - public User(int portUDPsend,int portUDPlistening,int portTCP) throws IOException{ - try - { - this.addIP = InetAddress.getLocalHost(); - } - catch(UnknownHostException e) { - JOptionPane.showMessageDialog(null ,"Could not find local address!"); - } - - this.portUDPsend = portUDPsend; - this.portUDPlistening = portUDPlistening; - this.portTCP = portTCP; - - this.initPseudo(); - - this.active(); - } - - /** - * Constructor of User (simulation of remote user) - * $parametres - * * portUDPsend : int => le numéro de port pour envoyé ces informations lors d'un changements ou d'une nouvelle connexion - * * portUDPlistening : int => le numéro de port pour recevoir les informations des nouveaux utilisateurs ou les changements - * * portTCP : int => le numéro de port pour commencer une nouvelle conversation - * * pseudo : String => le pseudo avec lequel on initie l'utilisateur distant (! pas de vérification) - *

- * On récupère l'adresse de la machine, le numéro de port à utiliser, 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 - * et notification aux autres utilisateurs actifs) - *

- */ - public User(int portUDPsend,int portUDPlistening, int portTCP,String pseudo) { - try - { - this.addIP = InetAddress.getLocalHost(); - } - catch(UnknownHostException e) { - JOptionPane.showMessageDialog(null ,"Could not find local address!"); - } - this.portUDPsend = portUDPsend; - this.portUDPlistening = portUDPlistening; - this.portTCP = portTCP; - this.pseudo = pseudo; - - this.active(); - - } - - - /*** GETTERS ***/ - public InetAddress getAddIP() { - return addIP; - } - public String getPseudo() { - return pseudo; - } - public int getPortTCP() { - return portTCP; - } - public int getPortUDPlistening() { - return portUDPlistening; - } - public int getPortUDPsend() { - return portUDPsend; - } - public boolean isActif() { - return actif; - } - - /*** SETTERS ***/ - 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 void setPortUDPsend(int portUDPsend) { - this.portUDPsend = portUDPsend; - } - - /** - *

- * Demande à l'utilisateur de rentrer un pseudo et valide de ce dernier en demandant aux - * utilisateurs distants leurs informations. - * On regarde si le pseudo est bien différent de l'ancien - *

- */ - private void setPseudo() throws IOException { - String oldPseudo = this.pseudo; //Saves the old one for comparison - - String tmpPseudo = JOptionPane.showInputDialog(null, "Enter nickname:"); // Read user input - - while(!(this.validatePseudo(tmpPseudo)) || tmpPseudo.equals(oldPseudo)) { - tmpPseudo = JOptionPane.showInputDialog(null, "Already exist, enter another nickname :"); // Read user input - } - - this.pseudo = tmpPseudo; - //myObj.close(); - JOptionPane.showMessageDialog(null ,"Your new nickname : " + tmpPseudo + " is valid !"); - - notify_remote_user(); - } - - /** - *

- * Demande à l'utilisateur de rentrer un pseudo et valide de ce dernier en demandant aux - * utilisateurs distants leurs informations - *

- */ - public void initPseudo() throws IOException { - String tmpPseudo = JOptionPane.showInputDialog(null, "Enter nickname:"); // Read user input - - while(!(this.validatePseudo(tmpPseudo))) { // On demande aux autres utilisateurs de nous envoyer leurs informations et on test si le pseudo est déjà utilisé - tmpPseudo = JOptionPane.showInputDialog(null, "Enter another nickname:"); // Read user input - } - - this.pseudo = tmpPseudo; - //sc1.close(); - JOptionPane.showMessageDialog(null ,"Your nickname : " + tmpPseudo + " is valid !"); - } - - public void TCPmessage(int index) throws IOException { - Socket link=null; - String s1; - try { - link=new Socket(this.userChatList.get(index).addIP,this.userChatList.get(index).portTCP); - - System.out.println("Server is listening on port"+this.portTCP+"of localhost"); - }catch(IOException e) { - - System.out.println("Server is not listening on port"+this.portTCP+" of localhost"); - - } - BufferedReader in=null; - try { - in = new BufferedReader(new InputStreamReader(link.getInputStream())); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - try { - PrintWriter out = new PrintWriter(link.getOutputStream(),true); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - String input; - String s2 = (this.getPseudo()+" reçoit un message"); - try { - while (!(input=in.readLine()).equals("end")) { - System.out.print("client_recoit:"+input); - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - /*or (int i=0; i<4; i++) { - System.out.println("client envoie"); - out.println("coucou \n"); - } - out.println("end");*/ - try { - link.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - - /** - *

- * *tmpPseudo : String => Le pseudo à valider - *

- * Envoi en broadcast (pour l'instant envoi sur les ports de notre ordinateur) d'une demande d'information - *

- * On attend les réponses pendant 5 secondes - *

- * On valide le pseudo en fonction des pseudos reçu - *

- * On en profite pour ajouter les utilisateurs répondant à notre liste d'utilisateur distant (RemoteUser) - *

- */ - public Boolean validatePseudo(String tmpPseudo) throws IOException { - - - // Call broadcast - 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"; - - // Send to remote user 1 - DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, portUDPlistening_remoteUsr2); - dgramSocket.send(outPacket); - - // Send to remote user 2 - outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, portUDPlistening_remoteUsr3); - dgramSocket.send(outPacket); - - // Initialisation des variables de la boucle - Boolean valid = true; - - DatagramPacket inPacket; - String response = null; - String[] tabresponse= new String [4]; - dgramSocket.setSoTimeout(1000); - Boolean arecu; - - 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++; - inPacket= new DatagramPacket(buffer, buffer.length); - - arecu=true; - try{ - dgramSocket.receive(inPacket); - }catch (Exception e) { - arecu=false; - } - buffer = inPacket.getData(); - response = new String(buffer); - - if(arecu) { - // On découpe la réponse en tableau de string ([adresseIP,tcpPort,nickname]) - tabresponse = response.split(":"); - - // Affichage de la réponse - System.out.println("Remote user n°"+nbReponse+ - "\nIP : "+tabresponse[0]+ - "\nn°Port : "+tabresponse[1]+ - "\npseudo : "+tabresponse[2]); - - // Si reception on ajoute l'utilisateur à notre liste d'utilisateur distant - this.addRemoteUser(InetAddress.getByName(tabresponse[0].split("/")[1]),Integer.parseInt(tabresponse[1]),tabresponse[2]); - valid= (tmpPseudo.compareTo(tabresponse[2])!=0); // On regarde la différence entre notre pseudo et le pseudo reçu - - } - - newDate = new Date(); - } - dgramSocket.close(); - - if(!valid) { - JOptionPane.showMessageDialog(null ,"Nickname : "+tmpPseudo +" is taken !"); - } - - return valid; - } - - /** - *

- * *remoteUserIP : InetAddress => l'adresse IP de l'utilisateur distant - *

- * *remoteUserPortTCP : int => le numéro de port TCP d'écoute de l'utilisateur distant - *

- * *remoteUserPseudo : String => le pseudo de l'utilisateur distant - *

- * Ajout ou mise à jour l'utilisateur distant dans notre liste d'utilisateur distant - *

- */ - public void addRemoteUser(InetAddress remoteUserIP, int remoteUserPortTCP,String remoteUserPseudo) { - RemoteUser tmpRemoteUser = new RemoteUser(remoteUserIP,remoteUserPortTCP,remoteUserPseudo); - int index = this.remoteUserList.indexOf(tmpRemoteUser); - if(index!=-1) { - System.out.println("("+this.pseudo+") - "+"MAJ, IP(port) of user : "+remoteUserPseudo+" => "+remoteUserIP+"("+remoteUserPortTCP+")"); - this.remoteUserList.set(index,tmpRemoteUser); - } - else { - System.out.println("("+this.pseudo+") - "+"Add new user IP(port) of user : "+remoteUserPseudo+" => "+remoteUserIP+"("+remoteUserPortTCP+")"); - this.remoteUserList.add(tmpRemoteUser); - } - } - - /** - *

- * En utilisant le port UDP d'envoi, on envoie 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); - } catch (IOException e) { - e.printStackTrace(); - } - - // Construction du message à envoyer - String toSend = this.addIP.toString()+":"+this.portTCP+":"+this.pseudo+":test"; - - DatagramPacket outPacket =null; - - // Send information to usr2 - if(this.portUDPlistening!=portUDPlistening_remoteUsr2) { - outPacket = new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, portUDPlistening_remoteUsr2); - try { - dgramSocket.send(outPacket); - } catch (IOException e) { - e.printStackTrace(); - } - } - - - // Send information to usr3 - if(this.portUDPlistening!=portUDPlistening_remoteUsr3) { - - outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, portUDPlistening_remoteUsr3); - try { - dgramSocket.send(outPacket); - } catch (IOException e) { - e.printStackTrace(); - } - } - dgramSocket.close(); - } - - /** - *

- * On démarre un thread d'écoute UDP pour répondre au nouveaux utilisateurs, - *

- * On démarre un thread d'écoute TCP pour commencer une conversation avec les utilisateurs actifs, - *

- * On notifie les utilisateurs distants actifs de notre passage en mode actif (envoi de nos informations) - *

- */ - private void active() { - this.threadListeningUDP = new UserListeningThreadUDP("UDP Listening thread",this); - this.threadListeningUDP.start(); - - this.threadListeningTCP = new UserListeningThreadTCP("TCP main Listening thread",this); - this.threadListeningTCP.start(); - - notify_remote_user(); - - this.actif=true; - } - - /** - *

- * Affichage de la liste d'utilisateurs actifs avec leurs index dans la liste - *

- */ - public void printRemoteUserList() { - System.out.println("Internal list of active remote users:"); - - for(int i=0; i - * Laisse l'utilisateur choisir parmis la liste d'utilisateurs actifs celui avec lequel il souhaite échanger via un chat - *

- */ - public void getOneActiveUser() throws IOException { - this.printRemoteUserList(); - int index=Integer.parseInt(JOptionPane.showInputDialog(null, "Please, enter index of one active user that you saw on the list to start a conversation with:")); - - if (index >= 0 && index nom du thread - * * user : User => utilisateur utilisant ce thread - * $description - * Création d'un socket d'écoute - */ - public UserListeningThreadTCP(String s,User user) { - super(s); - this.myUser = user; - /* try { - this.dgramSocket = new DatagramSocket(this.myUser.getPortTCP(),this.myUser.getAddIP()); - } catch (IOException e) { - e.printStackTrace(); - } - - */ - - } - - public void accept(ServerSocket servSocket) throws IOException { - Socket socket_tcp= servSocket.accept(); - UserConnexionthreadTCP threadtcp= new UserConnexionthreadTCP("Chat_with_"+myUser.getPseudo(),myUser,socket_tcp); - threadtcp.start(); - threadtcp.interrupt(); - - - } - - /* run - * $description - * écoutes les messages TCP tant que l'utilisateur est actif - * Traitement - * Si réception d'une demande, créer un - */ - public void run(){ - - // Tant que l'utilisateur est actif on attends la demande de nouvelle conversation - while(true) { - - ServerSocket servSocket=null; - try { - servSocket = new ServerSocket(myUser.portTCP); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - try { - this.accept(servSocket); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - - } - - } - - /* close - * $description - * ferme le socket d'écoute UDP - * interrupt UDP listening threadS - */ - public void close() { - // this.dgramSocket.close(); - System.out.println("End of listing thread TCP ("+this.myUser.getPseudo()+")"); - try { - this.interrupt(); - }catch (Exception e){ - e.printStackTrace(); - } - } - -} \ No newline at end of file diff --git a/Application/Clavardage/src/UserListeningThreadUDP.java b/Application/Clavardage/src/UserListeningThreadUDP.java deleted file mode 100644 index f40e491..0000000 --- a/Application/Clavardage/src/UserListeningThreadUDP.java +++ /dev/null @@ -1,114 +0,0 @@ -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; -import java.net.UnknownHostException; - -public class UserListeningThreadUDP extends Thread{ - - private User myUser; - private DatagramSocket dgramSocket=null; - - /* CONSTRUCTOR OF UserListeningThreadUDP - * $parametres - * * s : String => nom du thread - * * user : User => utilisateur utilisant ce thread - * $description - * Création d'un socket d'écoute - */ - public UserListeningThreadUDP(String s,User user) { - super(s); - this.myUser = user; - try { - this.dgramSocket = new DatagramSocket(this.myUser.getPortUDPlistening(),this.myUser.getAddIP()); - } catch (IOException e) { - e.printStackTrace(); - } - } - - /* run - * $description - * écoutes les messages UDP tant que l'utilisateur est actif - * Traitement - * 1) Si demande d'information => envoi ses informations - * 2) Si réception d'information => ajoute les informations - */ - public void run(){ - - // Tant que l'utilisateur est actif on attends les messages des nouveaux utilisateurs et changements des utilisateurs actifs - while(true) { - - // Réception du message - byte[] buffer = new byte[256]; - DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length); - try { - dgramSocket.receive(inPacket); - } catch (Exception e) { - e.printStackTrace(); - } - buffer = inPacket.getData(); - - // Traitement en fonction du message reçu - String receiveMsg = new String(buffer); - String [] tabMsg = receiveMsg.split(":"); - - - // si demande d'information d'un nouvel utilisateur - if(tabMsg.length==3) { - InetAddress itsIP = null; - try { - itsIP = InetAddress.getByName(tabMsg[0].split("/")[1]); // On récupère l'adresse IP de l'utilisateur distant - } catch (UnknownHostException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } - int senderUDPport = Integer.parseInt(tabMsg[1]); // On récupère le port UDP de l'utilisateur distant - - String toSend = myUser.getAddIP().toString()+":"+myUser.getPortTCP()+":"+myUser.getPseudo()+":test"; - DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, senderUDPport); - - try { - dgramSocket.send(outPacket); - } catch (IOException e) { - e.printStackTrace(); - } - } - - // Si un nouvel utilisateur passe en mode actif ou changement d'information - else { - try { - // On récupère l'adresse IP et le port TCP de l'utilisateur distant et ajout à la liste de l'utilisateur utilisant ce thread - this.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0].split("/")[1]),Integer.parseInt(tabMsg[1]),tabMsg[2]); - } catch (NumberFormatException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (UnknownHostException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - - } - - - } - - /* close - * $description - * ferme le socket d'écoute UDP - * interrupt UDP listening threadS - */ - public void close() { - this.dgramSocket.close(); - System.out.println("End of listing thread UDP ("+this.myUser.getPseudo()+")"); - try { - this.interrupt(); - }catch (Exception e){ - e.printStackTrace(); - } - } -} - - - diff --git a/Application/Clavardage/src/model/Chat.java b/Application/Clavardage/src/model/Chat.java index e1c2064..9479352 100644 --- a/Application/Clavardage/src/model/Chat.java +++ b/Application/Clavardage/src/model/Chat.java @@ -8,6 +8,7 @@ public class Chat { private int localUser_portTCP; private int remoteUser_portTCP; private ArrayList remoteUsersChatList = new ArrayList(); // listes des utilisateurs sur ce chat + private ArrayList messages = new ArrayList(); /** * Constructor of Chat (local) @@ -36,6 +37,9 @@ public class Chat { public ArrayList getRemoteUsersChatList() { return remoteUsersChatList; } + public ArrayList getMessages() { + return messages; + } /*** SETTERS ***/ public void setLocalUser_portTCP(int localUser_portTCP) {