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 ListeningThreadUDP udp_connect_thread;
private ListeningThreadTCPConnection tcp_connect_thread; private ListeningThreadTCPConnection tcp_connect_thread;
private Historique histoire; private Historique histoire;
private Chat activeChat;
/** /**
* Constructor of Controller * Constructor of Controller
@ -341,7 +342,7 @@ public class Controller {
public void openSession(RemoteUser rm) { public void openSession(RemoteUser rm) {
myUser.addChats(rm); myUser.addChats(rm);
Chat c = myUser.getChats().get(myUser.getChatIndexOf(rm)); Chat c = myUser.getChats().get(myUser.getChatIndexOf(rm));
this.activeChat = c;
if(c.getActive()) { if(c.getActive()) {
JOptionPane.showMessageDialog(null ,"Already a session with "+c.getRemoteUser().getPseudo()); JOptionPane.showMessageDialog(null ,"Already a session with "+c.getRemoteUser().getPseudo());
} }
@ -356,24 +357,26 @@ public class Controller {
c.setSocket(link); c.setSocket(link);
JOptionPane.showMessageDialog(null ,"New session with "+c.getRemoteUser().getPseudo()); JOptionPane.showMessageDialog(null ,"New session with "+c.getRemoteUser().getPseudo());
// TODO Récupération de la conversation (historique)
// RECUPERATION HISTORIQUE
try { try {
System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser())); System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
} catch (SQLException e) { } catch (SQLException e) {
System.out.println("souci avec le retrieveMsg"); System.out.println("souci avec le retrieveMsg");
e.printStackTrace(); e.printStackTrace();
} }
c.activate();
c.activate(); //
} }
} }
public void closeSession(Chat c) { 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); this.myUser.closeChat(c);
} }
public void sendMsg(Message msg,Chat c) { public void sendMessage(Message msg,Chat c) {
PrintWriter out=null; PrintWriter out=null;
try { try {
out = new PrintWriter(c.getUserSocket().getOutputStream(),true); out = new PrintWriter(c.getUserSocket().getOutputStream(),true);
@ -402,66 +405,10 @@ public class Controller {
} }
// Plus utilisé public void askToSend(String textMessage){
public void TCPmessage(int index) throws IOException { sendMessage(new Msg_Text(myUser.getAddIP(),textMessage), this.activeChat);
Socket link=null;
String s1;
try {
link=new Socket(this.myUser.getRemoteUsersList().get(index).getAddIP(),this.myUser.getRemoteUsersList().get(index).getPortTCP());
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();
}
}
@ -524,9 +471,8 @@ public class Controller {
ctr1.interfaceRunning =true; ctr1.interfaceRunning =true;
ctr1.view=Interface.createAndShowGUI(ctr1); ctr1.view=Interface.createAndShowGUI(ctr1);
/** Simulation of a session **/ /** Simulation of a session 1 message **/
/*
// AFFICHAGE REMOTE USER CHOISIE // AFFICHAGE REMOTE USER CHOISIE
System.out.println("("+ctr1.myUser.getPseudo()+" ) OPEN SESSION WITH "+ctr1.myUser.getRemoteUsersList().get(0).getPseudo()); System.out.println("("+ctr1.myUser.getPseudo()+" ) OPEN SESSION WITH "+ctr1.myUser.getRemoteUsersList().get(0).getPseudo());
// SELECTION DE L UTILISATEUR // SELECTION DE L UTILISATEUR
@ -536,10 +482,10 @@ public class Controller {
// RECUPERATION DE LA CONVERSATION // RECUPERATION DE LA CONVERSATION
Chat chatwithrm0 = ctr1.myUser.getChats().get(ctr1.myUser.getChatIndexOf(rm0)); Chat chatwithrm0 = ctr1.myUser.getChats().get(ctr1.myUser.getChatIndexOf(rm0));
// SEND MESSAGE // SEND MESSAGE
ctr1.sendMsg(new Msg_Text(ctr1.myUser.getAddIP(),"test"), chatwithrm0); ctr1.sendMessage(new Msg_Text(ctr1.myUser.getAddIP(),"test"), chatwithrm0);
// CLOSE SESSION // CLOSE SESSION
ctr1.closeSession(chatwithrm0); ctr1.closeSession(chatwithrm0);
*/
/** Unused function **/ /** Unused function **/
// MANUAL SELECTION OF ACTIVE USER // MANUAL SELECTION OF ACTIVE USER

View file

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