diff --git a/Application/Clavardage/src/controller/Controller.java b/Application/Clavardage/src/controller/Controller.java
index 8f39d6b..ac08439 100644
--- a/Application/Clavardage/src/controller/Controller.java
+++ b/Application/Clavardage/src/controller/Controller.java
@@ -201,7 +201,6 @@ public class Controller {
this.notify_remote_users();
}
-
/**
*
* *tmpPseudo : String => Le pseudo à valider
@@ -286,8 +285,7 @@ public class Controller {
return valid;
}
-
- /**
+ /** notify_remote_users
*
* En utilisant le port UDP d'envoi, on envoie en broadcast les informations nous concernant
*
@@ -330,88 +328,178 @@ public class Controller {
dgramSocket.close();
}
- /**
- * @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));
- this.activeChat = c;
- if(c.getActive()) {
- JOptionPane.showMessageDialog(null ,"Already a session with "+c.getRemoteUser().getPseudo());
+
+ public Chat openSession(RemoteUser rm) {
+ Chat c = myUser.addChats(rm); // Create chat and add it to myUser
+
+ /*** Create socket send => ask connection to server of rm ***/
+ 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());
}
- 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());
-
- // RECUPERATION 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);
+
+ /*** recup history and put it in model ***/
+ /*
+ try {
+ System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
+ String history = this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser());
+ } catch (SQLException e) {
+ System.out.println("souci avec le retrieveMsg");
+ e.printStackTrace();
+ }
+ */
+ return c;
+ }
+ public String askOpenSession(int index) {
+ String history="";
+ RemoteUser rm = myUser.getRemoteUsersList().get(index);
+ Chat c;
+
+ /*** Recup or create chat ***/
+ if(myUser.getChatIndexOf(rm)!=-1) { // if already a chat open
+ c = myUser.getChats().get(myUser.getChatIndexOf(rm));
+ JOptionPane.showMessageDialog(null ,"Active session with "+c.getRemoteUser().getPseudo()+" loading history");
+ }
+ else { // else create it
+ c=openSession(rm);
+ JOptionPane.showMessageDialog(null ,"New session with "+c.getRemoteUser().getPseudo());
}
+ // Look for history
+ int nbMessage = c.getMessages().size();
+ for(int i=0;i"+c.getMessages().get(i).getMessage();
+ }
+ this.activeChat = c;
+
+ return history;
}
public void closeSession(Chat c) {
- sendMessage(new Msg_Text(myUser.getAddIP(),"end"), c);
+ sendMessage(new Msg_Text(myUser.getAddIP(),"end"), c); // tell rm session STOP
+ JOptionPane.showMessageDialog(null ,"Close session with "+c.getRemoteUser().getPseudo()+" loading history");
this.myUser.closeChat(c);
}
+ public void askToSend(String textMessage){
+ sendMessage(new Msg_Text(myUser.getAddIP(),textMessage), this.activeChat);
+ }
public void sendMessage(Message msg,Chat c) {
+ /*** Init send process ***/
PrintWriter out=null;
try {
out = new PrintWriter(c.getUserSocket().getOutputStream(),true);
} catch (IOException e) {
e.printStackTrace();
}
-
- String message = (String) msg.getMessage(); // TODO check if instance of Msg_Text or anything else and threat it given a type
-
+ c.addMessage(msg);
+
+ /*** Gestion des différentes instance de Message ***/
+ // TODO check if instance of Msg_Text or anything else and threat it given a type
+ String message = (String) msg.getMessage();
+
// Date l'envoie du message
DateFormat dateFormat = new SimpleDateFormat("yyyy MM dd HH mm ss");
-
Date date=new Date();
// Sauvegarde dans la base de données
- // TODO save history (On doit choisir entre sauvegarder à la reception ou a l'emission) même appel dans ListeningThreadTCPChat.run()
//this.getHistory().saveMessage(getMyUser(), c.getRemoteUser(),message ,dateFormat.format(date));
- // Envoie du message (avec la date)
- //System.out.println(dateFormat.format(date));
- //System.out.println(message);
+ // Send message
out.println(dateFormat.format(date));
out.println(message);
- c.addMessage(msg);
-
- }
-
-
- public void askToSend(String textMessage){
- sendMessage(new Msg_Text(myUser.getAddIP(),textMessage), this.activeChat);
}
+
+
+ public static void main(String[] args) throws IOException, InterruptedException {
+
+ System.out.println("start program");
+
+ Historique histoire=new Historique();
+
+ /************************* INIT *******************************/
+ /** 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);
+
+
+
+ /*********************** TEST AREA ********************/
+ /** Simulation of a session 1 message **/
+ /*
+ // AFFICHAGE REMOTE USER CHOISIE
+ System.out.println("("+ctr1.myUser.getPseudo()+" ) OPEN SESSION WITH "+ctr1.myUser.getRemoteUsersList().get(0).getPseudo());
+ // SELECTION DE L UTILISATEUR
+ RemoteUser rm0 = ctr1.myUser.getRemoteUsersList().get(0);
+ // OPEN SESSION
+ ctr1.openSession(rm0);
+ // RECUPERATION DE LA CONVERSATION
+ Chat chatwithrm0 = ctr1.myUser.getChats().get(ctr1.myUser.getChatIndexOf(rm0));
+ // SEND MESSAGE
+ ctr1.sendMessage(new Msg_Text(ctr1.myUser.getAddIP(),"test"), chatwithrm0);
+ // CLOSE SESSION
+ ctr1.closeSession(chatwithrm0);
+ */
+
+ /** Unused function **/
+ // MANUAL SELECTION OF ACTIVE USER
+ //ctr1.selectActiveUser();
+ // CHANGE USER NICKNAME
+ //ctr1.changePseudo();
+
+
+
+ /************************* LOOP *******************************/
+
+ /** Création de l'interface graphique **/
+ ctr1.interfaceRunning =true;
+ ctr1.view=Interface.createAndShowGUI(ctr1);
+
+ /** loop **/
+ while(ctr1.interfaceRunning) {
+ //ctr1.view.update(info)
+ }
+
+ System.out.println("Fin de la boucle");
+
+
+
+ /************************ END *****************************/
+
+ /** Close thread and socket **/
+ // REMOTEUSER_1 - MIKE
+ ctr2.myUser.closeAllChat();
+ ctr2.tcp_connect_thread.close();
+ ctr2.udp_connect_thread.close();
+ // REMOTEUSER_2 - ALICE
+ ctr3.myUser.closeAllChat();
+ ctr3.tcp_connect_thread.close();
+ ctr3.udp_connect_thread.close();
+ // LOCAL USER
+ ctr1.myUser.closeAllChat();
+ ctr1.tcp_connect_thread.close();
+ ctr1.udp_connect_thread.close();
+ // AFFICHAGE
+ System.out.println("end program");
+ JOptionPane.showMessageDialog(null ,"END");
+ }
+
+
+
+
+
/********************* Fonction debug console => A mettre dans l'interface ******************************************/
/**
@@ -452,71 +540,6 @@ public class Controller {
}
/*************************************************************************************************************************/
- public static void main(String[] args) throws IOException, InterruptedException {
-
- System.out.println("start program");
- 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 de l'interface graphique **/
- ctr1.interfaceRunning =true;
- ctr1.view=Interface.createAndShowGUI(ctr1);
-
- /** Simulation of a session 1 message **/
- /*
- // AFFICHAGE REMOTE USER CHOISIE
- System.out.println("("+ctr1.myUser.getPseudo()+" ) OPEN SESSION WITH "+ctr1.myUser.getRemoteUsersList().get(0).getPseudo());
- // SELECTION DE L UTILISATEUR
- RemoteUser rm0 = ctr1.myUser.getRemoteUsersList().get(0);
- // OPEN SESSION
- ctr1.openSession(rm0);
- // RECUPERATION DE LA CONVERSATION
- Chat chatwithrm0 = ctr1.myUser.getChats().get(ctr1.myUser.getChatIndexOf(rm0));
- // SEND MESSAGE
- ctr1.sendMessage(new Msg_Text(ctr1.myUser.getAddIP(),"test"), chatwithrm0);
- // CLOSE SESSION
- ctr1.closeSession(chatwithrm0);
- */
-
- /** Unused function **/
- // MANUAL SELECTION OF ACTIVE USER
- //ctr1.selectActiveUser();
- // CHANGE USER NICKNAME
- //ctr1.changePseudo();
-
-
-
- while(ctr1.interfaceRunning) {
- //ctr1.view
- }
-
- System.out.println("Fin de la boucle");
-
- /** Close thread and socket **/
- // REMOTEUSER_1 - MIKE
- ctr2.myUser.closeAllChat();
- ctr2.tcp_connect_thread.close();
- ctr2.udp_connect_thread.close();
- // REMOTEUSER_2 - ALICE
- ctr3.myUser.closeAllChat();
- ctr3.tcp_connect_thread.close();
- ctr3.udp_connect_thread.close();
- // LOCAL USER
- ctr1.myUser.closeAllChat();
- 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/model/LocalUser.java b/Application/Clavardage/src/model/LocalUser.java
index 2830ec3..afd79c7 100644
--- a/Application/Clavardage/src/model/LocalUser.java
+++ b/Application/Clavardage/src/model/LocalUser.java
@@ -78,7 +78,7 @@ public class LocalUser extends User{
}
}
- public void addChats(RemoteUser rm) {
+ public Chat addChats(RemoteUser rm) {
Chat newChat= new Chat(rm);
if(getChatIndexOf(rm)==-1) {
@@ -87,6 +87,8 @@ public class LocalUser extends User{
else {
System.out.println("pb add chat, déjà un chat avec cet rm user");
}*/
+
+ return newChat;
}
diff --git a/Application/Clavardage/src/view/Interface.java b/Application/Clavardage/src/view/Interface.java
index 56cec7f..7fd07d7 100644
--- a/Application/Clavardage/src/view/Interface.java
+++ b/Application/Clavardage/src/view/Interface.java
@@ -95,8 +95,7 @@ public class Interface extends JFrame implements ActionListener, WindowListener
if(e.getSource() == PseudotextField) { //Changing pseudo
String Textinput = PseudotextField.getText();
Pseudolabel.setText("Your current username is: " + Textinput);
- MessagetextField.setVisible(true);
- Messagelabel.setVisible(true);
+
try {
controller.changePseudo();
} catch (IOException e1) {
@@ -105,17 +104,23 @@ public class Interface extends JFrame implements ActionListener, WindowListener
}
}else if(e.getSource() == MessagetextField){ //Messages
String Textinput = MessagetextField.getText();
- Messagelabel.setText("Message: " + Textinput);
- //TODO mettre Textinput dans la database
- controller.askToSend(Textinput);
+ Messagelabel.setText(Messagelabel.getText()+"
test" + Textinput);
MessagetextField.setText("");
+ controller.askToSend(Textinput); // ask to send to controller
+
}else if(e.getSource() == RemoteUserButton) { //Shows remote user list
RemoteUserbox.setVisible(true);
- }else {
+ }else { // Choice in remote user list
+
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)));
- controller.openSession(controller.getMyUser().getRemoteUsersList().get(selectedUsernb));
+ // start or switch chat => get history
+ String history = controller.askOpenSession(selectedUsernb);
+ // display it
+ Messagelabel.setText("Message:
"+history);
+ MessagetextField.setVisible(true);
+ Messagelabel.setVisible(true);
}
}