From d318c5d34fbb344f4c5765527f3a36034d56a549 Mon Sep 17 00:00:00 2001 From: Alexandre Gonzalvez Date: Thu, 17 Dec 2020 14:06:35 +0100 Subject: [PATCH] fix plusieurs connexion au me^me utilisateur --- .../Clavardage/src/controller/Controller.java | 114 +++++++++++------- .../controller/ListeningThreadTCPChat.java | 42 +++++-- Application/Clavardage/src/model/Chat.java | 11 +- .../Clavardage/src/model/LocalUser.java | 67 ++++++---- .../Clavardage/src/model/RemoteUser.java | 7 ++ Application/Clavardage/src/model/User.java | 1 + .../Clavardage/src/view/Interface.java | 36 +++--- 7 files changed, 183 insertions(+), 95 deletions(-) diff --git a/Application/Clavardage/src/controller/Controller.java b/Application/Clavardage/src/controller/Controller.java index 2d7b320..656333b 100644 --- a/Application/Clavardage/src/controller/Controller.java +++ b/Application/Clavardage/src/controller/Controller.java @@ -31,9 +31,10 @@ public class Controller { final static int portUDPlistening_remoteUsr2 = 31002; // TO REMOVE when we use broadcast final static int portUDPlistening_remoteUsr3 = 31003; // TO REMOVE when we use broadcast + public Boolean interfaceRunning; /*** ATTRIBUTS ***/ protected LocalUser myUser; - protected Interface hisView; + protected Interface view; private ListeningThreadUDP udp_connect_thread; private ListeningThreadTCPConnection tcp_connect_thread; private Historique histoire; @@ -126,8 +127,8 @@ public class Controller { public LocalUser getMyUser() { return myUser; } - public Interface getHisView() { - return hisView; + public Interface getview() { + return view; } public ListeningThreadUDP getUdp_connect_thread() { return udp_connect_thread; @@ -143,8 +144,8 @@ public class Controller { public void setMyUser(LocalUser myUser) { this.myUser = myUser; } - public void setHisView(Interface hisView) { - this.hisView = hisView; + public void setview(Interface view) { + this.view = view; } public void setUdp_connect_thread(ListeningThreadUDP udp_connect_thread) { this.udp_connect_thread = udp_connect_thread; @@ -186,11 +187,11 @@ public class Controller { public void changePseudo() throws IOException { String oldPseudo = this.myUser.getPseudo(); //Saves the old one for comparison - String tmpPseudo = hisView.PseudotextField.getText(); // Read user input + String tmpPseudo = view.PseudotextField.getText(); // Read user input while(!(this.validatePseudo(tmpPseudo)) || tmpPseudo.equals(oldPseudo)) { - hisView.Pseudolabel.setText("Already exists, enter another nickname. Your current username is: " + oldPseudo); // Read user input - tmpPseudo = hisView.PseudotextField.getText(); // Read user input + view.Pseudolabel.setText("Already exists, enter another nickname. Your current username is: " + oldPseudo); // Read user input + tmpPseudo = view.PseudotextField.getText(); // Read user input } this.myUser.setPseudo(tmpPseudo); @@ -328,29 +329,48 @@ public class Controller { dgramSocket.close(); } - public void openSession(Chat c) { - Socket link=null; - try { - System.out.println("("+this.myUser.getPseudo()+") Connecting to "+c.getRemoteUser().getPortTCP()+" of " + c.getRemoteUser().getPseudo()); - link=new Socket(c.getRemoteUser().getAddIP(),c.getRemoteUser().getPortTCP()); - }catch(IOException e) { - System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP()); + /** + * @parameters + * @param chat : Chat + *

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

+ */ + + + public void openSession(RemoteUser rm) { + myUser.addChats(rm); + Chat c = myUser.getChats().get(myUser.getChatIndexOf(rm)); + + if(c.getActive()) { + JOptionPane.showMessageDialog(null ,"Already a session with "+c.getRemoteUser().getPseudo()); + } + else { + Socket link=null; + try { + System.out.println("("+this.myUser.getPseudo()+") Connecting to "+c.getRemoteUser().getPortTCP()+" of " + c.getRemoteUser().getPseudo()); + link=new Socket(c.getRemoteUser().getAddIP(),c.getRemoteUser().getPortTCP()/*, InetAddress.getLocalHost() ,myUser.getPortTCP()*/); + }catch(IOException e) { + System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP()); + } + + c.setSocket(link); + JOptionPane.showMessageDialog(null ,"New session with "+c.getRemoteUser().getPseudo()); + // TODO Récupération de la conversation (historique) + try { + System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser())); + } catch (SQLException e) { + System.out.println("souci avec le retrieveMsg"); + e.printStackTrace(); + } + c.activate(); } - c.setSocket(link); - JOptionPane.showMessageDialog(null ,"New chat with "+c.getRemoteUser().getPseudo()); - try { - System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser())); - } catch (SQLException e) { - System.out.println("souci avec le retrieveMsg"); - e.printStackTrace(); - } - // TODO Récupération de la conversation (historique) } public void closeSession(Chat c) { - c.closeSocket(); - // TODO Serait mieux d'enlever le chat de la liste de myUser + sendMsg(new Msg_Text(myUser.getAddIP(),"end"), c); + this.myUser.closeChat(c); } public void sendMsg(Message msg,Chat c) { @@ -377,8 +397,11 @@ public class Controller { //System.out.println(message); out.println(dateFormat.format(date)); out.println(message); + c.addMessage(msg); + } + // Plus utilisé public void TCPmessage(int index) throws IOException { Socket link=null; @@ -468,12 +491,11 @@ public class Controller { if (index >= 0 && index */ public void run() { + /**** function variables ****/ BufferedReader in = null; String msg = null; String input; String dateString; Date date; Calendar date1=Calendar.getInstance(); - // TODO retrouver le chat lié à ce thread - // Chat c = - // RemoteUser rm=c.getRemoteUser(); + + Chat c = null; + /************ Check rm user information **********/ + /* + RemoteUser rm = new RemoteUser("Unknown",this.socket.getInetAddress(),this.socket.getPort()); + int indexRM = controller.myUser.getActiveUserIndexOf(rm); + // Check if rm is identifiable + System.out.println(rm); + if(indexRM!=-1) { + rm = controller.myUser.getRemoteUsersList().get(indexRM); + // Check if chat already created + int indexChat = controller.myUser.getChatIndexOf(rm); + if(indexChat!=-1) { + System.out.println("("+this.controller.myUser.getPseudo()+") Session déjà créer, on recupère le chat associé"); + c = controller.myUser.getChats().get(indexChat); + } + else { + c = this.controller.myUser.addChats(rm); + this.controller.openSession(c); + } + } + else { + System.out.println("("+this.controller.myUser.getPseudo()+") Remote User unidentifiable => CLOSING CONNECTION"); + } + */ + /*** listening tcp message from rm until session is close ***/ try { - System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE "); + System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE FROM ");//+rm.getPseudo()); in =new BufferedReader(new InputStreamReader(this.socket.getInputStream())); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { - while (!(input=in.readLine()).equals("end")) { + while (!(input=in.readLine()).equals("end")/* && c.getActive()*/) { System.out.println("("+this.controller.myUser.getPseudo()+") recoit : "+input); - // TODO save history (On doit choisir entre sauvegarder à la reception ou a l'emission) même appel dans controller.sendMsg() - //this.controller.getHistory().saveMessage(this.controller.myUser, rm,input ,dateString ); - // TODO Update le chat avec ce nouveau message + //c.addMessage(new Msg_Text(rm.getAddIP(),input)); // TODO Prévenir l'interface de la réception d'un nouveau message } diff --git a/Application/Clavardage/src/model/Chat.java b/Application/Clavardage/src/model/Chat.java index baba4a8..9e85b8f 100644 --- a/Application/Clavardage/src/model/Chat.java +++ b/Application/Clavardage/src/model/Chat.java @@ -10,6 +10,7 @@ public class Chat { private RemoteUser remoteUser; private ArrayList messages = new ArrayList(); private Socket userSocket; + private Boolean active = false; /** * Constructor of Chat (local) @@ -34,6 +35,9 @@ public class Chat { public Socket getUserSocket() { return userSocket; } + public Boolean getActive() { + return active; + } /*** SETTERS ***/ public void setRemoteUser(RemoteUser rm) { @@ -45,8 +49,12 @@ public class Chat { public void addMessage(Message msg) { this.messages.add(msg); } + public void activate() { + this.active = true; + } - public void closeSocket() { + public void close() { + this.active = false; try { this.userSocket.close(); } catch (IOException e) { @@ -54,4 +62,5 @@ public class Chat { } } + } diff --git a/Application/Clavardage/src/model/LocalUser.java b/Application/Clavardage/src/model/LocalUser.java index 8f8f6a0..2830ec3 100644 --- a/Application/Clavardage/src/model/LocalUser.java +++ b/Application/Clavardage/src/model/LocalUser.java @@ -78,40 +78,59 @@ public class LocalUser extends User{ } } - - /** - * @parameters - * @param remoteUser : remoteUser => référence de l'utilisateur distant - * @param localUser_portTCP : int => le numéro de port TCP d'écoute de la conversation de l'utilisateur local - * @param remoteUser_portTCP : int => le numéro de port TCP d'écoute de la conversation de l'utilisateur distant - *

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

- */ - public Chat addChats(RemoteUser rm) { + public void addChats(RemoteUser rm) { Chat newChat= new Chat(rm); - this.chats.add(newChat); - return newChat; - } - public int getIndexOf(RemoteUser remoteUser) { - int i=0; - Boolean found = (this.chats.get(i).getRemoteUser() == remoteUser); - while(i