From e748951c79c5e8d5e5b1740968d540140bff5547 Mon Sep 17 00:00:00 2001 From: Alexandre Gonzalvez Date: Wed, 16 Dec 2020 12:22:58 +0100 Subject: [PATCH] ajout selection remoteuser --- .../Clavardage/src/controller/Controller.java | 114 ++++++++++-------- .../Clavardage/src/view/Interface.java | 9 +- 2 files changed, 70 insertions(+), 53 deletions(-) diff --git a/Application/Clavardage/src/controller/Controller.java b/Application/Clavardage/src/controller/Controller.java index 3d5c875..f04f1fe 100644 --- a/Application/Clavardage/src/controller/Controller.java +++ b/Application/Clavardage/src/controller/Controller.java @@ -363,7 +363,7 @@ public class Controller { date = new Date(); date1.setTime(date); dateString=date1.toString(); - this.getHistory().saveMessage(getMyUser(), c.getRemoteUser(),message ,dateString ); + //this.getHistory().saveMessage(getMyUser(), c.getRemoteUser(),message ,dateString ); /* System.out.println(this.getHistory()); @@ -378,7 +378,7 @@ public class Controller { //this.getHistory().saveMessage(this.myUser, c.getRemoteUser(),message ,dateString); // Envoie du message (avec la date) - //DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); out.println(dateString); out.println(message); } @@ -489,60 +489,70 @@ public class Controller { /*************************************************************************************************************************/ public static void main(String[] args) throws IOException, InterruptedException { - System.out.println("start program"); + System.out.println("start program"); - Historique histoire=new Historique(); - + Historique histoire=new Historique(); + - /** Création des utilisateurs **/ - // REMOTEUSER_1 - MIKE - Controller ctr2 = new Controller(31012,portUDPlistening_remoteUsr2,31022,"Mike",histoire); - // REMOTEUSER_2 - ALICE - Controller ctr3 = new Controller(31013,portUDPlistening_remoteUsr3,31023,"Alice",histoire); - // LOCAL USER - Controller ctr1 = new Controller(31011,31001,31021,histoire); + /** Création des utilisateurs **/ + // REMOTEUSER_1 - MIKE + Controller ctr2 = new Controller(31012,portUDPlistening_remoteUsr2,31022,"Mike",histoire); + // REMOTEUSER_2 - ALICE + Controller ctr3 = new Controller(31013,portUDPlistening_remoteUsr3,31023,"Alice",histoire); + // LOCAL USER + Controller ctr1 = new Controller(31011,31001,31021,histoire); - /** Création de l'interface graphique **/ - ctr1.hisView=Interface.createAndShowGUI(ctr1); + /** Création de l'interface graphique **/ + Boolean interfaceRunning = true; + ctr1.hisView=Interface.createAndShowGUI(ctr1); + + /** Simulation of a session **/ + + // SELECT REMOTE USER + Chat chatwithrm0 = ctr1.myUser.addChats(ctr1.myUser.getRemoteUsersList().get(0)); + // AFFICHAGE REMOTE USER CHOISIE + System.out.println("("+ctr1.myUser.getPseudo()+" ) OPEN SESSION WITH "+ctr1.myUser.getRemoteUsersList().get(0).getPseudo()); + // OPEN SESSION + ctr1.openSession(chatwithrm0); + // SEND MESSAGE + ctr1.sendMsg(new Msg_Text(ctr1.myUser.getAddIP(),"test"), chatwithrm0); + // CLOSE SESSION + ctr1.sendMsg(new Msg_Text(ctr1.myUser.getAddIP(),"end"), chatwithrm0); + ctr1.closeSession(chatwithrm0); + + + /** Unused function **/ + // MANUAL SELECTION OF ACTIVE USER + //ctr1.selectActiveUser(); + // CHANGE USER NICKNAME + //ctr1.changePseudo(); + + + /* + while(interfaceRunning) { - /** Simulation of a session **/ - // SELECT REMOTE USER - Chat chatwithrm0 = ctr1.myUser.addChats(ctr1.myUser.getRemoteUsersList().get(0)); - // AFFICHAGE REMOTE USER CHOISIE - System.out.println("("+ctr1.myUser.getPseudo()+" ) OPEN SESSION WITH "+ctr1.myUser.getRemoteUsersList().get(0).getPseudo()); - // OPEN SESSION - ctr1.openSession(chatwithrm0); - // SEND MESSAGE - ctr1.sendMsg(new Msg_Text(ctr1.myUser.getAddIP(),"test"), chatwithrm0); - // CLOSE SESSION - ctr1.sendMsg(new Msg_Text(ctr1.myUser.getAddIP(),"end"), chatwithrm0); - ctr1.closeSession(chatwithrm0); - - /** Unused function **/ - // MANUAL SELECTION OF ACTIVE USER - //ctr1.selectActiveUser(); - // CHANGE USER NICKNAME - //ctr1.changePseudo(); - - /** Close thread and socket **/ - // SLEEP 5 SEC - System.out.println("Sleep mode for 5 seconds ..."); - Thread.sleep(5000); - // REMOTEUSER_1 - MIKE - ctr2.myUser.closeAllRemainingChatSocket(); - ctr2.tcp_connect_thread.close(); - ctr2.udp_connect_thread.close(); - // REMOTEUSER_2 - ALICE - ctr3.myUser.closeAllRemainingChatSocket(); - ctr3.tcp_connect_thread.close(); - ctr3.udp_connect_thread.close(); - // LOCAL USER - ctr1.myUser.closeAllRemainingChatSocket(); - ctr1.tcp_connect_thread.close(); - ctr1.udp_connect_thread.close(); - // AFFICHAGE - System.out.println("end program"); - JOptionPane.showMessageDialog(null ,"END"); + }*/ + + /** Close thread and socket **/ + // SLEEP 20 SEC + System.out.println("Sleep mode for 20 seconds ..."); + Thread.sleep(20000); + + // REMOTEUSER_1 - MIKE + ctr2.myUser.closeAllRemainingChatSocket(); + ctr2.tcp_connect_thread.close(); + ctr2.udp_connect_thread.close(); + // REMOTEUSER_2 - ALICE + ctr3.myUser.closeAllRemainingChatSocket(); + ctr3.tcp_connect_thread.close(); + ctr3.udp_connect_thread.close(); + // LOCAL USER + ctr1.myUser.closeAllRemainingChatSocket(); + ctr1.tcp_connect_thread.close(); + ctr1.udp_connect_thread.close(); + // AFFICHAGE + System.out.println("end program"); + JOptionPane.showMessageDialog(null ,"END"); } } diff --git a/Application/Clavardage/src/view/Interface.java b/Application/Clavardage/src/view/Interface.java index 6b3e5e4..7d6669d 100644 --- a/Application/Clavardage/src/view/Interface.java +++ b/Application/Clavardage/src/view/Interface.java @@ -101,8 +101,10 @@ public class Interface implements ActionListener { }else { JComboBox cb = (JComboBox)e.getSource(); //Casts obscurs pour récupérer le numéro du user dans la liste int selectedUsernb = Integer.parseInt(String.valueOf(((String) cb.getSelectedItem()).charAt(1))); - System.out.println("selectedUsernb: " + selectedUsernb); //Test pour voir si ça marche + //System.out.println("selectedUsernb: " + selectedUsernb); //Test pour voir si ça marche //TODO Utiliser ce selectedUsernb pour faire le lien avec openSession de Controller + hisController.openSession(hisController.getMyUser().addChats(hisController.getMyUser().getRemoteUsersList().get(selectedUsernb))); + } } @@ -157,6 +159,8 @@ public class Interface implements ActionListener { JFrame frame = new JFrame("SwingApplication"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + Interface app = new Interface(controller); Component contents = app.createComponents(); frame.getContentPane().add(contents, BorderLayout.CENTER); @@ -167,6 +171,9 @@ public class Interface implements ActionListener { return app; } + + + /* public static void main(String[] args) throws IOException { //Schedule a job for the event-dispatching thread: