interface semi-fonc

This commit is contained in:
Alexandre Gonzalvez 2020-12-17 21:03:42 +01:00
parent eababcf7c2
commit e3cc932bf5
3 changed files with 158 additions and 128 deletions

View file

@ -201,7 +201,6 @@ public class Controller {
this.notify_remote_users(); this.notify_remote_users();
} }
/** /**
* <p> * <p>
* *tmpPseudo : String => Le pseudo à valider * *tmpPseudo : String => Le pseudo à valider
@ -286,8 +285,7 @@ public class Controller {
return valid; return valid;
} }
/** notify_remote_users
/**
* <p> * <p>
* En utilisant le port UDP d'envoi, on envoie en broadcast les informations nous concernant * En utilisant le port UDP d'envoi, on envoie en broadcast les informations nous concernant
* </p> * </p>
@ -330,88 +328,178 @@ public class Controller {
dgramSocket.close(); dgramSocket.close();
} }
/**
* @parameters public Chat openSession(RemoteUser rm) {
* @param chat : Chat Chat c = myUser.addChats(rm); // Create chat and add it to myUser
* <p>
* Ajout ou mise à jour l'utilisateur distant dans notre liste d'utilisateur distant /*** Create socket send => ask connection to server of rm ***/
* </p> 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()*/);
public void openSession(RemoteUser rm) { }catch(IOException e) {
myUser.addChats(rm); System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP());
Chat c = myUser.getChats().get(myUser.getChatIndexOf(rm));
this.activeChat = c;
if(c.getActive()) {
JOptionPane.showMessageDialog(null ,"Already a session with "+c.getRemoteUser().getPseudo());
} }
else { c.setSocket(link);
Socket link=null;
try { /*** recup history and put it in model ***/
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()*/); try {
}catch(IOException e) { System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP()); String history = this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser());
} } catch (SQLException e) {
System.out.println("souci avec le retrieveMsg");
c.setSocket(link); e.printStackTrace();
JOptionPane.showMessageDialog(null ,"New session with "+c.getRemoteUser().getPseudo()); }
*/
// RECUPERATION HISTORIQUE return c;
try { }
System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser())); public String askOpenSession(int index) {
} catch (SQLException e) { String history="";
System.out.println("souci avec le retrieveMsg"); RemoteUser rm = myUser.getRemoteUsersList().get(index);
e.printStackTrace(); Chat c;
}
/*** Recup or create chat ***/
c.activate(); // 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<nbMessage;i++) {
history+="<br>"+c.getMessages().get(i).getMessage();
}
this.activeChat = c;
return history;
} }
public void closeSession(Chat c) { 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); 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) { public void sendMessage(Message msg,Chat c) {
/*** Init send process ***/
PrintWriter out=null; PrintWriter out=null;
try { try {
out = new PrintWriter(c.getUserSocket().getOutputStream(),true); out = new PrintWriter(c.getUserSocket().getOutputStream(),true);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
c.addMessage(msg);
String message = (String) msg.getMessage(); // TODO check if instance of Msg_Text or anything else and threat it given a type
/*** 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 // Date l'envoie du message
DateFormat dateFormat = new SimpleDateFormat("yyyy MM dd HH mm ss"); DateFormat dateFormat = new SimpleDateFormat("yyyy MM dd HH mm ss");
Date date=new Date(); Date date=new Date();
// Sauvegarde dans la base de données // 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)); //this.getHistory().saveMessage(getMyUser(), c.getRemoteUser(),message ,dateFormat.format(date));
// Envoie du message (avec la date) // Send message
//System.out.println(dateFormat.format(date));
//System.out.println(message);
out.println(dateFormat.format(date)); out.println(dateFormat.format(date));
out.println(message); 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 ******************************************/ /********************* 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");
}
} }

View file

@ -78,7 +78,7 @@ public class LocalUser extends User{
} }
} }
public void addChats(RemoteUser rm) { public Chat addChats(RemoteUser rm) {
Chat newChat= new Chat(rm); Chat newChat= new Chat(rm);
if(getChatIndexOf(rm)==-1) { if(getChatIndexOf(rm)==-1) {
@ -87,6 +87,8 @@ public class LocalUser extends User{
else { else {
System.out.println("pb add chat, déjà un chat avec cet rm user"); System.out.println("pb add chat, déjà un chat avec cet rm user");
}*/ }*/
return newChat;
} }

View file

@ -95,8 +95,7 @@ public class Interface extends JFrame implements ActionListener, WindowListener
if(e.getSource() == PseudotextField) { //Changing pseudo if(e.getSource() == PseudotextField) { //Changing pseudo
String Textinput = PseudotextField.getText(); String Textinput = PseudotextField.getText();
Pseudolabel.setText("Your current username is: " + Textinput); Pseudolabel.setText("Your current username is: " + Textinput);
MessagetextField.setVisible(true);
Messagelabel.setVisible(true);
try { try {
controller.changePseudo(); controller.changePseudo();
} catch (IOException e1) { } catch (IOException e1) {
@ -105,17 +104,23 @@ public class Interface extends JFrame implements ActionListener, WindowListener
} }
}else if(e.getSource() == MessagetextField){ //Messages }else if(e.getSource() == MessagetextField){ //Messages
String Textinput = MessagetextField.getText(); String Textinput = MessagetextField.getText();
Messagelabel.setText("Message: " + Textinput); Messagelabel.setText(Messagelabel.getText()+"<br>test" + Textinput);
//TODO mettre Textinput dans la database
controller.askToSend(Textinput);
MessagetextField.setText(""); MessagetextField.setText("");
controller.askToSend(Textinput); // ask to send to controller
}else if(e.getSource() == RemoteUserButton) { //Shows remote user list }else if(e.getSource() == RemoteUserButton) { //Shows remote user list
RemoteUserbox.setVisible(true); 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 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))); 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:<br>"+history);
MessagetextField.setVisible(true);
Messagelabel.setVisible(true);
} }
} }