fix plusieurs connexion au me^me utilisateur
This commit is contained in:
parent
f496eb89b1
commit
d318c5d34f
7 changed files with 183 additions and 95 deletions
|
@ -31,9 +31,10 @@ public class Controller {
|
|||
final static int portUDPlistening_remoteUsr2 = 31002; // TO REMOVE when we use broadcast
|
||||
final static int portUDPlistening_remoteUsr3 = 31003; // TO REMOVE when we use broadcast
|
||||
|
||||
public Boolean interfaceRunning;
|
||||
/*** ATTRIBUTS ***/
|
||||
protected LocalUser myUser;
|
||||
protected Interface hisView;
|
||||
protected Interface view;
|
||||
private ListeningThreadUDP udp_connect_thread;
|
||||
private ListeningThreadTCPConnection tcp_connect_thread;
|
||||
private Historique histoire;
|
||||
|
@ -126,8 +127,8 @@ public class Controller {
|
|||
public LocalUser getMyUser() {
|
||||
return myUser;
|
||||
}
|
||||
public Interface getHisView() {
|
||||
return hisView;
|
||||
public Interface getview() {
|
||||
return view;
|
||||
}
|
||||
public ListeningThreadUDP getUdp_connect_thread() {
|
||||
return udp_connect_thread;
|
||||
|
@ -143,8 +144,8 @@ public class Controller {
|
|||
public void setMyUser(LocalUser myUser) {
|
||||
this.myUser = myUser;
|
||||
}
|
||||
public void setHisView(Interface hisView) {
|
||||
this.hisView = hisView;
|
||||
public void setview(Interface view) {
|
||||
this.view = view;
|
||||
}
|
||||
public void setUdp_connect_thread(ListeningThreadUDP udp_connect_thread) {
|
||||
this.udp_connect_thread = udp_connect_thread;
|
||||
|
@ -186,11 +187,11 @@ public class Controller {
|
|||
public void changePseudo() throws IOException {
|
||||
String oldPseudo = this.myUser.getPseudo(); //Saves the old one for comparison
|
||||
|
||||
String tmpPseudo = hisView.PseudotextField.getText(); // Read user input
|
||||
String tmpPseudo = view.PseudotextField.getText(); // Read user input
|
||||
|
||||
while(!(this.validatePseudo(tmpPseudo)) || tmpPseudo.equals(oldPseudo)) {
|
||||
hisView.Pseudolabel.setText("Already exists, enter another nickname. Your current username is: " + oldPseudo); // Read user input
|
||||
tmpPseudo = hisView.PseudotextField.getText(); // Read user input
|
||||
view.Pseudolabel.setText("Already exists, enter another nickname. Your current username is: " + oldPseudo); // Read user input
|
||||
tmpPseudo = view.PseudotextField.getText(); // Read user input
|
||||
}
|
||||
|
||||
this.myUser.setPseudo(tmpPseudo);
|
||||
|
@ -328,29 +329,48 @@ public class Controller {
|
|||
dgramSocket.close();
|
||||
}
|
||||
|
||||
public void openSession(Chat c) {
|
||||
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());
|
||||
}catch(IOException e) {
|
||||
System.out.println("Error linking to TCP server of "+ c.getRemoteUser().getPortTCP());
|
||||
/**
|
||||
* @parameters
|
||||
* @param chat : Chat
|
||||
* <p>
|
||||
* Ajout ou mise à jour l'utilisateur distant dans notre liste d'utilisateur distant
|
||||
* </p>
|
||||
*/
|
||||
|
||||
|
||||
public void openSession(RemoteUser rm) {
|
||||
myUser.addChats(rm);
|
||||
Chat c = myUser.getChats().get(myUser.getChatIndexOf(rm));
|
||||
|
||||
if(c.getActive()) {
|
||||
JOptionPane.showMessageDialog(null ,"Already a session with "+c.getRemoteUser().getPseudo());
|
||||
}
|
||||
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());
|
||||
// TODO Récupération de la conversation (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);
|
||||
JOptionPane.showMessageDialog(null ,"New chat with "+c.getRemoteUser().getPseudo());
|
||||
try {
|
||||
System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
|
||||
} catch (SQLException e) {
|
||||
System.out.println("souci avec le retrieveMsg");
|
||||
e.printStackTrace();
|
||||
}
|
||||
// TODO Récupération de la conversation (historique)
|
||||
}
|
||||
|
||||
public void closeSession(Chat c) {
|
||||
c.closeSocket();
|
||||
// TODO Serait mieux d'enlever le chat de la liste de myUser
|
||||
sendMsg(new Msg_Text(myUser.getAddIP(),"end"), c);
|
||||
this.myUser.closeChat(c);
|
||||
}
|
||||
|
||||
public void sendMsg(Message msg,Chat c) {
|
||||
|
@ -377,8 +397,11 @@ public class Controller {
|
|||
//System.out.println(message);
|
||||
out.println(dateFormat.format(date));
|
||||
out.println(message);
|
||||
c.addMessage(msg);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Plus utilisé
|
||||
public void TCPmessage(int index) throws IOException {
|
||||
Socket link=null;
|
||||
|
@ -468,12 +491,11 @@ public class Controller {
|
|||
|
||||
if (index >= 0 && index<this.myUser.getRemoteUsersList().size()) {
|
||||
|
||||
if(this.myUser.getIndexOf(this.myUser.getRemoteUsersList().get(index))==-1){
|
||||
if(this.myUser.getChatIndexOf(this.myUser.getRemoteUsersList().get(index))==-1){
|
||||
JOptionPane.showMessageDialog(null ,"User "+this.myUser.getRemoteUsersList().get(index).getPseudo()+" is already in chat with you");
|
||||
}
|
||||
else {
|
||||
Chat chat=this.myUser.addChats(this.myUser.getRemoteUsersList().get(index));
|
||||
this.openSession(chat);
|
||||
this.openSession(myUser.getRemoteUsersList().get(index));
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -499,21 +521,23 @@ public class Controller {
|
|||
Controller ctr1 = new Controller(31011,31001,31021,histoire);
|
||||
|
||||
/** Création de l'interface graphique **/
|
||||
Boolean interfaceRunning = true;
|
||||
ctr1.hisView=Interface.createAndShowGUI(ctr1);
|
||||
ctr1.interfaceRunning =true;
|
||||
ctr1.view=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());
|
||||
// SELECTION DE L UTILISATEUR
|
||||
RemoteUser rm0 = ctr1.myUser.getRemoteUsersList().get(0);
|
||||
// OPEN SESSION
|
||||
ctr1.openSession(chatwithrm0);
|
||||
ctr1.openSession(rm0);
|
||||
// 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);
|
||||
// CLOSE SESSION
|
||||
ctr1.sendMsg(new Msg_Text(ctr1.myUser.getAddIP(),"end"), chatwithrm0);
|
||||
ctr1.closeSession(chatwithrm0);
|
||||
|
||||
|
||||
|
@ -524,26 +548,24 @@ public class Controller {
|
|||
//ctr1.changePseudo();
|
||||
|
||||
|
||||
/*
|
||||
while(interfaceRunning) {
|
||||
|
||||
}*/
|
||||
|
||||
while(ctr1.interfaceRunning) {
|
||||
//ctr1.view
|
||||
}
|
||||
|
||||
System.out.println("Fin de la boucle");
|
||||
|
||||
/** 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.myUser.closeAllChat();
|
||||
ctr2.tcp_connect_thread.close();
|
||||
ctr2.udp_connect_thread.close();
|
||||
// REMOTEUSER_2 - ALICE
|
||||
ctr3.myUser.closeAllRemainingChatSocket();
|
||||
ctr3.myUser.closeAllChat();
|
||||
ctr3.tcp_connect_thread.close();
|
||||
ctr3.udp_connect_thread.close();
|
||||
// LOCAL USER
|
||||
ctr1.myUser.closeAllRemainingChatSocket();
|
||||
ctr1.myUser.closeAllChat();
|
||||
ctr1.tcp_connect_thread.close();
|
||||
ctr1.udp_connect_thread.close();
|
||||
// AFFICHAGE
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
package controller;
|
||||
|
||||
import model.Chat;
|
||||
import model.LocalUser;
|
||||
import model.Msg_Text;
|
||||
import model.RemoteUser;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
@ -37,29 +41,51 @@ public class ListeningThreadTCPChat extends ListeningThread{
|
|||
* </p>
|
||||
*/
|
||||
public void run() {
|
||||
/**** function variables ****/
|
||||
BufferedReader in = null;
|
||||
String msg = null;
|
||||
String input;
|
||||
String dateString;
|
||||
Date date;
|
||||
Calendar date1=Calendar.getInstance();
|
||||
// TODO retrouver le chat lié à ce thread
|
||||
// Chat c =
|
||||
// RemoteUser rm=c.getRemoteUser();
|
||||
|
||||
|
||||
Chat c = null;
|
||||
/************ Check rm user information **********/
|
||||
/*
|
||||
RemoteUser rm = new RemoteUser("Unknown",this.socket.getInetAddress(),this.socket.getPort());
|
||||
int indexRM = controller.myUser.getActiveUserIndexOf(rm);
|
||||
// Check if rm is identifiable
|
||||
System.out.println(rm);
|
||||
if(indexRM!=-1) {
|
||||
rm = controller.myUser.getRemoteUsersList().get(indexRM);
|
||||
// Check if chat already created
|
||||
int indexChat = controller.myUser.getChatIndexOf(rm);
|
||||
if(indexChat!=-1) {
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") Session déjà créer, on recupère le chat associé");
|
||||
c = controller.myUser.getChats().get(indexChat);
|
||||
}
|
||||
else {
|
||||
c = this.controller.myUser.addChats(rm);
|
||||
this.controller.openSession(c);
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") Remote User unidentifiable => CLOSING CONNECTION");
|
||||
}
|
||||
*/
|
||||
/*** listening tcp message from rm until session is close ***/
|
||||
try {
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE ");
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE FROM ");//+rm.getPseudo());
|
||||
in =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
while (!(input=in.readLine()).equals("end")) {
|
||||
while (!(input=in.readLine()).equals("end")/* && c.getActive()*/) {
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") recoit : "+input);
|
||||
// TODO save history (On doit choisir entre sauvegarder à la reception ou a l'emission) même appel dans controller.sendMsg()
|
||||
//this.controller.getHistory().saveMessage(this.controller.myUser, rm,input ,dateString );
|
||||
// TODO Update le chat avec ce nouveau message
|
||||
//c.addMessage(new Msg_Text(rm.getAddIP(),input));
|
||||
// TODO Prévenir l'interface de la réception d'un nouveau message
|
||||
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ public class Chat {
|
|||
private RemoteUser remoteUser;
|
||||
private ArrayList<Message> messages = new ArrayList<Message>();
|
||||
private Socket userSocket;
|
||||
private Boolean active = false;
|
||||
|
||||
/**
|
||||
* Constructor of Chat (local)
|
||||
|
@ -34,6 +35,9 @@ public class Chat {
|
|||
public Socket getUserSocket() {
|
||||
return userSocket;
|
||||
}
|
||||
public Boolean getActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
/*** SETTERS ***/
|
||||
public void setRemoteUser(RemoteUser rm) {
|
||||
|
@ -45,8 +49,12 @@ public class Chat {
|
|||
public void addMessage(Message msg) {
|
||||
this.messages.add(msg);
|
||||
}
|
||||
public void activate() {
|
||||
this.active = true;
|
||||
}
|
||||
|
||||
public void closeSocket() {
|
||||
public void close() {
|
||||
this.active = false;
|
||||
try {
|
||||
this.userSocket.close();
|
||||
} catch (IOException e) {
|
||||
|
@ -54,4 +62,5 @@ public class Chat {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -78,40 +78,59 @@ public class LocalUser extends User{
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @parameters
|
||||
* @param remoteUser : remoteUser => référence de l'utilisateur distant
|
||||
* @param localUser_portTCP : int => le numéro de port TCP d'écoute de la conversation de l'utilisateur local
|
||||
* @param remoteUser_portTCP : int => le numéro de port TCP d'écoute de la conversation de l'utilisateur distant
|
||||
* <p>
|
||||
* Ajout ou mise à jour l'utilisateur distant dans notre liste d'utilisateur distant
|
||||
* </p>
|
||||
*/
|
||||
public Chat addChats(RemoteUser rm) {
|
||||
public void addChats(RemoteUser rm) {
|
||||
Chat newChat= new Chat(rm);
|
||||
this.chats.add(newChat);
|
||||
return newChat;
|
||||
}
|
||||
|
||||
public int getIndexOf(RemoteUser remoteUser) {
|
||||
int i=0;
|
||||
Boolean found = (this.chats.get(i).getRemoteUser() == remoteUser);
|
||||
while(i<this.chats.size() && !found) {
|
||||
i++;
|
||||
found = (this.chats.get(i).getRemoteUser() == remoteUser);
|
||||
if(getChatIndexOf(rm)==-1) {
|
||||
this.getChats().add(newChat);
|
||||
}/*
|
||||
else {
|
||||
System.out.println("pb add chat, déjà un chat avec cet rm user");
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
public int getChatIndexOf(RemoteUser rm) {
|
||||
int index=0;
|
||||
|
||||
Boolean found = false;
|
||||
while(index<this.chats.size() && !found) {
|
||||
|
||||
found = (this.chats.get(index).getRemoteUser() == rm);
|
||||
index++;
|
||||
}
|
||||
if(found) {
|
||||
return i;
|
||||
return index-1;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public void closeAllRemainingChatSocket() {
|
||||
for(int i=0;i<this.chats.size();i++) {
|
||||
this.chats.get(i).closeSocket();
|
||||
public int getActiveUserIndexOf(RemoteUser rm) {
|
||||
int index = 0;
|
||||
Boolean found = false;
|
||||
while(index<this.remoteUsersList.size() && !found) {
|
||||
found = (this.remoteUsersList.get(index) == rm);
|
||||
index++;
|
||||
}
|
||||
if(found) {
|
||||
return index-1;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public void closeChat(Chat c) {
|
||||
this.chats.get(getChatIndexOf(c.getRemoteUser())).close();
|
||||
this.chats.remove(c);
|
||||
}
|
||||
|
||||
public void closeAllChat() {
|
||||
int length =this.chats.size();
|
||||
for(int i=0;i<length;i++) {
|
||||
this.closeChat(this.chats.get(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,4 +8,11 @@ public class RemoteUser extends User{
|
|||
super(pseudo, addIP, portTCP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RemoteUser [pseudo=" + pseudo + ", addIP=" + addIP + ", portTCP=" + portTCP + ", IPcode=" + IPcode
|
||||
+ "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -57,6 +57,7 @@ public abstract class User {
|
|||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
|
|
|
@ -15,7 +15,7 @@ public class Interface extends JFrame implements ActionListener, WindowListener
|
|||
*/
|
||||
private static final long serialVersionUID = 1L; //JFrame stuff
|
||||
|
||||
public static Controller hisController;
|
||||
public Controller controller;
|
||||
|
||||
public JLabel Pseudolabel;
|
||||
public JTextField PseudotextField;
|
||||
|
@ -31,7 +31,7 @@ public class Interface extends JFrame implements ActionListener, WindowListener
|
|||
final static String LOOKANDFEEL = "System";
|
||||
|
||||
public Interface(Controller controller) {
|
||||
this.hisController = controller;
|
||||
this.controller = controller;
|
||||
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
||||
this.addWindowListener(new java.awt.event.WindowAdapter() {
|
||||
@Override
|
||||
|
@ -46,15 +46,15 @@ public class Interface extends JFrame implements ActionListener, WindowListener
|
|||
PseudotextField = new JTextField(); //Pseudo setup
|
||||
PseudotextField.setColumns(10);
|
||||
PseudotextField.addActionListener(this);
|
||||
Pseudolabel = new JLabel("Your current username is: " + hisController.getMyUser().getPseudo());
|
||||
Pseudolabel = new JLabel("Your current username is: " + controller.getMyUser().getPseudo());
|
||||
Pseudolabel.setLabelFor(PseudotextField);
|
||||
|
||||
RemoteUserButton = new JButton("Click here to get Remote User list"); //Remote user list setup
|
||||
RemoteUserButton.addActionListener(this);
|
||||
//Convert the Userlist to Pseudotab
|
||||
String[] pseudotab = new String[hisController.getMyUser().getRemoteUsersList().size()];
|
||||
for(int i=0; i < hisController.getMyUser().getRemoteUsersList().size(); i++) {
|
||||
pseudotab[i] = "(" + Integer.toString(i) + "): " + hisController.getMyUser().getRemoteUsersList().get(i).getPseudo();
|
||||
String[] pseudotab = new String[controller.getMyUser().getRemoteUsersList().size()];
|
||||
for(int i=0; i < controller.getMyUser().getRemoteUsersList().size(); i++) {
|
||||
pseudotab[i] = "(" + Integer.toString(i) + "): " + controller.getMyUser().getRemoteUsersList().get(i).getPseudo();
|
||||
}
|
||||
RemoteUserbox = new JComboBox(pseudotab);
|
||||
RemoteUserbox.setEditable(true);
|
||||
|
@ -98,7 +98,7 @@ public class Interface extends JFrame implements ActionListener, WindowListener
|
|||
MessagetextField.setVisible(true);
|
||||
Messagelabel.setVisible(true);
|
||||
try {
|
||||
hisController.changePseudo();
|
||||
controller.changePseudo();
|
||||
} catch (IOException e1) {
|
||||
System.out.println("Error in change pseudo\n");
|
||||
e1.printStackTrace();
|
||||
|
@ -113,7 +113,7 @@ public class Interface extends JFrame implements ActionListener, WindowListener
|
|||
}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)));
|
||||
hisController.openSession(hisController.getMyUser().addChats(hisController.getMyUser().getRemoteUsersList().get(selectedUsernb)));
|
||||
controller.openSession(controller.getMyUser().getRemoteUsersList().get(selectedUsernb));
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -169,19 +169,23 @@ public class Interface extends JFrame implements ActionListener, WindowListener
|
|||
JFrame frame = new JFrame("Clavardage App");
|
||||
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
||||
|
||||
frame.addWindowListener(new java.awt.event.WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
|
||||
System.out.println("GUI has been closed");
|
||||
hisController.interfaceRunning = false;
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Interface app = new Interface(controller);
|
||||
Component contents = app.createComponents();
|
||||
frame.getContentPane().add(contents, BorderLayout.CENTER);
|
||||
|
||||
|
||||
frame.addWindowListener(new java.awt.event.WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
|
||||
System.out.println("GUI has been closed");
|
||||
app.controller.interfaceRunning = false;
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
|
||||
//Display the window.
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
|
|
Loading…
Reference in a new issue