version 1 - Chat tcp

This commit is contained in:
Alexandre Gonzalvez 2020-12-17 19:49:19 +01:00
parent d318c5d34f
commit eababcf7c2
2 changed files with 16 additions and 69 deletions

View file

@ -38,6 +38,7 @@ public class Controller {
private ListeningThreadUDP udp_connect_thread;
private ListeningThreadTCPConnection tcp_connect_thread;
private Historique histoire;
private Chat activeChat;
/**
* Constructor of Controller
@ -341,7 +342,7 @@ public class Controller {
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());
}
@ -356,24 +357,26 @@ public class Controller {
c.setSocket(link);
JOptionPane.showMessageDialog(null ,"New session with "+c.getRemoteUser().getPseudo());
// TODO Récupération de la conversation (historique)
// 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.activate(); //
}
}
public void closeSession(Chat c) {
sendMsg(new Msg_Text(myUser.getAddIP(),"end"), c);
sendMessage(new Msg_Text(myUser.getAddIP(),"end"), c);
this.myUser.closeChat(c);
}
public void sendMsg(Message msg,Chat c) {
public void sendMessage(Message msg,Chat c) {
PrintWriter out=null;
try {
out = new PrintWriter(c.getUserSocket().getOutputStream(),true);
@ -402,69 +405,13 @@ public class Controller {
}
// Plus utilisé
public void TCPmessage(int index) throws IOException {
Socket link=null;
String s1;
try {
link=new Socket(this.myUser.getRemoteUsersList().get(index).getAddIP(),this.myUser.getRemoteUsersList().get(index).getPortTCP());
public void askToSend(String textMessage){
sendMessage(new Msg_Text(myUser.getAddIP(),textMessage), this.activeChat);
System.out.println("Server is listening on port"+this.myUser.getPortTCP()+"of localhost");
}catch(IOException e) {
System.out.println("Server is not listening on port"+this.myUser.getPortTCP()+" of localhost");
}
BufferedReader in=null;
try {
in = new BufferedReader(new InputStreamReader(link.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
PrintWriter out = new PrintWriter(link.getOutputStream(),true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String input;
String dateString;
Date date;
Calendar date1=Calendar.getInstance();
String s2 = (this.myUser.getPseudo()+" reçoit un message");
try {
while (!(input=in.readLine()).equals("end")) {
System.out.print("client_recoit:"+input);
date=new Date();
date1.setTime(date);
dateString=date1.toString();
this.getHistory().saveMessage(this.getMyUser(), getMyUser().getRemoteUsersList().get(index),input ,dateString );
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*or (int i=0; i<4; i++) {
System.out.println("client envoie");
out.println("coucou \n");
}
out.println("end");*/
try {
link.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/********************* Fonction debug console => A mettre dans l'interface ******************************************/
/**
@ -524,9 +471,8 @@ public class Controller {
ctr1.interfaceRunning =true;
ctr1.view=Interface.createAndShowGUI(ctr1);
/** Simulation of a session **/
/** 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
@ -536,10 +482,10 @@ public class Controller {
// RECUPERATION DE LA CONVERSATION
Chat chatwithrm0 = ctr1.myUser.getChats().get(ctr1.myUser.getChatIndexOf(rm0));
// SEND MESSAGE
ctr1.sendMsg(new Msg_Text(ctr1.myUser.getAddIP(),"test"), chatwithrm0);
ctr1.sendMessage(new Msg_Text(ctr1.myUser.getAddIP(),"test"), chatwithrm0);
// CLOSE SESSION
ctr1.closeSession(chatwithrm0);
*/
/** Unused function **/
// MANUAL SELECTION OF ACTIVE USER

View file

@ -107,6 +107,7 @@ public class Interface extends JFrame implements ActionListener, WindowListener
String Textinput = MessagetextField.getText();
Messagelabel.setText("Message: " + Textinput);
//TODO mettre Textinput dans la database
controller.askToSend(Textinput);
MessagetextField.setText("");
}else if(e.getSource() == RemoteUserButton) { //Shows remote user list
RemoteUserbox.setVisible(true);