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- * 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