fix connection tcp, send message + reception
This commit is contained in:
parent
e19bc4b45c
commit
c9e317bd27
9 changed files with 236 additions and 161 deletions
|
@ -9,28 +9,34 @@ import java.net.DatagramSocket;
|
|||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
import model.Chat;
|
||||
import model.LocalUser;
|
||||
import model.Message;
|
||||
import model.Msg_Text;
|
||||
import model.RemoteUser;
|
||||
import view.Interface;
|
||||
|
||||
public class Controller {
|
||||
|
||||
/*** CONSTANTES ***/
|
||||
final static int portUDPlistening_remoteUsr2 = 20002; // TO REMOVE when we use broadcast
|
||||
final static int portUDPlistening_remoteUsr3 = 20003; // TO REMOVE when we use broadcast
|
||||
final static int portUDPlistening_remoteUsr2 = 31002; // TO REMOVE when we use broadcast
|
||||
final static int portUDPlistening_remoteUsr3 = 31003; // TO REMOVE when we use broadcast
|
||||
|
||||
/*** ATTRIBUTS ***/
|
||||
private LocalUser myUser;
|
||||
private Interface hisView;
|
||||
protected LocalUser myUser;
|
||||
protected Interface hisView;
|
||||
private ListeningThreadUDP udp_connect_thread;
|
||||
private ListeningThreadTCPConnection tcp_connect_thread;
|
||||
private ArrayList<ListeningThreadTCPChat> tcp_chats_threads = new ArrayList<ListeningThreadTCPChat>(); // listes des utilisateurs actifs
|
||||
private Historique histoire;
|
||||
|
||||
/**
|
||||
* Constructor of Controller
|
||||
* @parametres
|
||||
|
@ -46,7 +52,7 @@ public class Controller {
|
|||
* - notification aux autres utilisateurs actifs
|
||||
* </p>
|
||||
*/
|
||||
private Controller(int portUDPsend,int portUDPlistening,int portTCP,Historique histoire) {
|
||||
private Controller(int portUDPsend,int portUDPlistening,int portTCP) {
|
||||
this.histoire=histoire;
|
||||
InetAddress addIP = null;
|
||||
try
|
||||
|
@ -64,10 +70,10 @@ public class Controller {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this.myUser);
|
||||
this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
|
||||
this.udp_connect_thread.start();
|
||||
|
||||
this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this.myUser);
|
||||
this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this);
|
||||
this.tcp_connect_thread.start();
|
||||
|
||||
notify_remote_users();
|
||||
|
@ -90,8 +96,7 @@ public class Controller {
|
|||
* - notification aux autres utilisateurs actifs
|
||||
* </p>
|
||||
*/
|
||||
private Controller(int portUDPsend,int portUDPlistening,int portTCP,String pseudo,Historique histoire) {
|
||||
this.histoire=histoire;
|
||||
private Controller(int portUDPsend,int portUDPlistening,int portTCP,String pseudo) {
|
||||
InetAddress addIP = null;
|
||||
try
|
||||
{
|
||||
|
@ -102,10 +107,10 @@ public class Controller {
|
|||
}
|
||||
this.myUser = new LocalUser(pseudo,addIP,portUDPsend,portUDPlistening,portTCP);
|
||||
|
||||
this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this.myUser);
|
||||
this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
|
||||
this.udp_connect_thread.start();
|
||||
|
||||
this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this.myUser);
|
||||
this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this);
|
||||
this.tcp_connect_thread.start();
|
||||
|
||||
notify_remote_users();
|
||||
|
@ -127,12 +132,10 @@ public class Controller {
|
|||
public ListeningThreadTCPConnection getTcp_connect_thread() {
|
||||
return tcp_connect_thread;
|
||||
}
|
||||
public ArrayList<ListeningThreadTCPChat> getTcp_chats_threads() {
|
||||
return tcp_chats_threads;
|
||||
}
|
||||
public Historique getHistory() {
|
||||
return histoire;
|
||||
}
|
||||
|
||||
/*** SETTERS ***/
|
||||
public void setMyUser(LocalUser myUser) {
|
||||
this.myUser = myUser;
|
||||
|
@ -146,9 +149,6 @@ public class Controller {
|
|||
public void setTcp_connect_thread(ListeningThreadTCPConnection tcp_connect_thread) {
|
||||
this.tcp_connect_thread = tcp_connect_thread;
|
||||
}
|
||||
public void setTcp_chats_threads(ArrayList<ListeningThreadTCPChat> tcp_chats_threads) {
|
||||
this.tcp_chats_threads = tcp_chats_threads;
|
||||
}
|
||||
public void setHistory(Historique histoire) {
|
||||
this.histoire=histoire;
|
||||
}
|
||||
|
@ -169,7 +169,7 @@ public class Controller {
|
|||
|
||||
//sc1.close();
|
||||
JOptionPane.showMessageDialog(null ,"Your nickname : " + tmpPseudo + " is valid !");
|
||||
hisView.Pseudolabel.setText("Your current username is: " + tmpPseudo);
|
||||
//hisView.Pseudolabel.setText("Your current username is: " + tmpPseudo);
|
||||
|
||||
return tmpPseudo;
|
||||
}
|
||||
|
@ -263,10 +263,7 @@ public class Controller {
|
|||
tabresponse = response.split(":");
|
||||
|
||||
// Affichage de la réponse
|
||||
System.out.println("Remote user n°"+nbReponse+
|
||||
"\nIP : "+tabresponse[0]+
|
||||
"\nn°Port : "+tabresponse[1]+
|
||||
"\npseudo : "+tabresponse[2]);
|
||||
//System.out.println("Remote user n°"+nbReponse+"\nIP : "+tabresponse[0]+"\nn°Port : "+tabresponse[1]+"\npseudo : "+tabresponse[2]);
|
||||
|
||||
// Si reception on ajoute l'utilisateur à notre liste d'utilisateur distant
|
||||
this.myUser.addRemoteUser(InetAddress.getByName(tabresponse[0].split("/")[1]),Integer.parseInt(tabresponse[1]),tabresponse[2]);
|
||||
|
@ -328,6 +325,41 @@ 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());
|
||||
}
|
||||
c.setSocket(link);
|
||||
JOptionPane.showMessageDialog(null ,"New chat with "+c.getRemoteUser().getPseudo());
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
public void sendMsg(Message msg,Chat c) {
|
||||
PrintWriter out=null;
|
||||
try {
|
||||
out = new PrintWriter(c.getUserSocket().getOutputStream(),true);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
||||
Date date = new Date();
|
||||
out.println(dateFormat.format(date));
|
||||
|
||||
out.println(msg.getMessage());
|
||||
}
|
||||
|
||||
// Plus utilisé
|
||||
public void TCPmessage(int index) throws IOException {
|
||||
Socket link=null;
|
||||
String s1;
|
||||
|
@ -390,36 +422,28 @@ public class Controller {
|
|||
* Laisse l'utilisateur choisir parmis la liste d'utilisateurs actifs celui avec lequel il souhaite échanger via un chat
|
||||
*</p>
|
||||
*/
|
||||
public void getOneActiveUser() throws IOException {
|
||||
public void selectActiveUser() throws IOException {
|
||||
this.printRemoteUserList();
|
||||
int index=Integer.parseInt(JOptionPane.showInputDialog(null, "Please, enter index of one active user that you saw on the list to start a conversation with:"));
|
||||
|
||||
if (index >= 0 && index<this.myUser.getRemoteUsersList().size()) {
|
||||
/*
|
||||
if(userChatList.contains(this.myUser.getRemoteUsersList().get(index))) {
|
||||
|
||||
if(this.myUser.getIndexOf(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);
|
||||
}
|
||||
*/
|
||||
// IF
|
||||
// TODO regarder si ils sont déjà en conversation
|
||||
// ELSE
|
||||
// TODO Créer un nouveau thread tcp
|
||||
int localUser_portTCP;
|
||||
int remoteUser_portTCP;
|
||||
//this.myUser.addChats(this.myUser.getRemoteUsersList().get(index), localUser_portTCP, remoteUser_portTCP);
|
||||
JOptionPane.showMessageDialog(null ,"New chat with "+this.myUser.getRemoteUsersList().get(index).getPseudo());
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null ,"Wrong index (no active at index number "+index+" )");
|
||||
this.selectActiveUser();
|
||||
}
|
||||
|
||||
this.TCPmessage(index);
|
||||
//sc2.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/********************************** A mettre dans l'interface ******************************************/
|
||||
|
||||
/**
|
||||
|
@ -439,31 +463,60 @@ public class Controller {
|
|||
|
||||
/*************************************************************************************************************************/
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
Historique histoire=new Historique();
|
||||
Controller ctr2 = new Controller(12226,portUDPlistening_remoteUsr2,22222,"Mike",histoire);
|
||||
Controller ctr3 = new Controller(12224,portUDPlistening_remoteUsr3,22223,"Alice",histoire);
|
||||
Controller ctr1 = new Controller(12225,20001,22221,histoire); // Notre utilisateur local
|
||||
|
||||
// Création de l'interface
|
||||
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");
|
||||
// REMOTEUSER_2 - ALICE
|
||||
Controller ctr3 = new Controller(31013,portUDPlistening_remoteUsr3,31023,"Alice");
|
||||
// LOCAL USER
|
||||
Controller ctr1 = new Controller(31011,31001,31021);
|
||||
|
||||
/** Création de l'interface graphique **/
|
||||
ctr1.hisView=Interface.createAndShowGUI(ctr1);
|
||||
|
||||
// Fonction appelé par notre utilisateur local
|
||||
ctr1.getOneActiveUser();
|
||||
ctr1.changePseudo();
|
||||
/** 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());
|
||||
// OPEN SESSION
|
||||
ctr1.openSession(chatwithrm0);
|
||||
// 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);
|
||||
|
||||
// On attends 5 secondes
|
||||
System.out.println("Sleep mode for 5 seconds");
|
||||
/** Unused function **/
|
||||
// MANUAL SELECTION OF ACTIVE USER
|
||||
//ctr1.selectActiveUser();
|
||||
// CHANGE USER NICKNAME
|
||||
//ctr1.changePseudo();
|
||||
|
||||
/** Close thread and socket **/
|
||||
// SLEEP 5 SEC
|
||||
System.out.println("Sleep mode for 5 seconds ...");
|
||||
Thread.sleep(5000);
|
||||
|
||||
// On ferme les différents threads et socket d'écoute
|
||||
ctr1.udp_connect_thread.close();
|
||||
ctr2.udp_connect_thread.close();
|
||||
ctr3.udp_connect_thread.close();
|
||||
|
||||
ctr1.tcp_connect_thread.close();
|
||||
// REMOTEUSER_1 - MIKE
|
||||
ctr2.myUser.closeAllRemainingChatSocket();
|
||||
ctr2.tcp_connect_thread.close();
|
||||
ctr2.udp_connect_thread.close();
|
||||
// REMOTEUSER_2 - ALICE
|
||||
ctr3.myUser.closeAllRemainingChatSocket();
|
||||
ctr3.tcp_connect_thread.close();
|
||||
JOptionPane.showMessageDialog(null ,"End");
|
||||
ctr3.udp_connect_thread.close();
|
||||
// LOCAL USER
|
||||
ctr1.myUser.closeAllRemainingChatSocket();
|
||||
ctr1.tcp_connect_thread.close();
|
||||
ctr1.udp_connect_thread.close();
|
||||
// AFFICHAGE
|
||||
System.out.println("end program");
|
||||
JOptionPane.showMessageDialog(null ,"END");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
package controller;
|
||||
|
||||
import model.LocalUser;
|
||||
|
||||
public abstract class ListeningThread extends Thread{
|
||||
|
||||
protected LocalUser myUser;
|
||||
protected Controller controller;
|
||||
|
||||
public ListeningThread(String s,LocalUser myUser) {
|
||||
public ListeningThread(String s,Controller controller) {
|
||||
super(s);
|
||||
this.myUser = myUser;
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
public abstract void run();
|
||||
|
|
|
@ -23,8 +23,8 @@ public class ListeningThreadTCPChat extends ListeningThread{
|
|||
* <p>
|
||||
* </p>
|
||||
*/
|
||||
public ListeningThreadTCPChat(String s,LocalUser myUser,Socket socket) {
|
||||
super(s,myUser);
|
||||
public ListeningThreadTCPChat(String s,Controller controller,Socket socket) {
|
||||
super(s,controller);
|
||||
this.socket=socket;
|
||||
|
||||
}
|
||||
|
@ -36,35 +36,28 @@ public class ListeningThreadTCPChat extends ListeningThread{
|
|||
* </p>
|
||||
*/
|
||||
public void run() {
|
||||
Socket link = this.socket;
|
||||
try {
|
||||
BufferedReader in =new BufferedReader(new InputStreamReader(link.getInputStream()));
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
PrintWriter out=null;
|
||||
try {
|
||||
out = new PrintWriter(link.getOutputStream(),true);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
||||
Date date = new Date();
|
||||
System.out.println(myUser.getPseudo()+" envoie un message");
|
||||
out.println(dateFormat.format(date));
|
||||
out.println("end");
|
||||
String input;
|
||||
/*while (!(input=in.readLine()).equals("end")) {
|
||||
System.out.print("server_recoit:"+input);
|
||||
}*/
|
||||
try {
|
||||
link.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
BufferedReader in = null;
|
||||
String msg = null;
|
||||
try {
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE ");
|
||||
in =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
String input;
|
||||
try {
|
||||
while (!(input=in.readLine()).equals("end")) {
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") recoit : "+input);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* close
|
||||
|
@ -80,7 +73,7 @@ public class ListeningThreadTCPChat extends ListeningThread{
|
|||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
System.out.println("End of listing thread UDP ("+this.myUser.getPseudo()+")");
|
||||
System.out.println("End of listing thread UDP ("+this.controller.myUser.getPseudo()+")");
|
||||
try {
|
||||
this.interrupt();
|
||||
}catch (Exception e){
|
||||
|
|
|
@ -18,14 +18,14 @@ public class ListeningThreadTCPConnection extends ListeningThread{
|
|||
* <p>
|
||||
* </p>
|
||||
*/
|
||||
public ListeningThreadTCPConnection(String s,LocalUser myUser) {
|
||||
super(s,myUser);
|
||||
public ListeningThreadTCPConnection(String s,Controller controller) {
|
||||
super(s,controller);
|
||||
|
||||
}
|
||||
|
||||
public void accept(ServerSocket servSocket) throws IOException {
|
||||
Socket socket_tcp= servSocket.accept();
|
||||
ListeningThreadTCPChat threadtcpchat = new ListeningThreadTCPChat("Chat_with_"+myUser.getPseudo(),myUser,socket_tcp);
|
||||
ListeningThreadTCPChat threadtcpchat = new ListeningThreadTCPChat("Chat_with_"+controller.myUser.getPseudo(),controller,socket_tcp);
|
||||
threadtcpchat.start();
|
||||
threadtcpchat.interrupt();
|
||||
|
||||
|
@ -42,18 +42,21 @@ public class ListeningThreadTCPConnection extends ListeningThread{
|
|||
public void run(){
|
||||
|
||||
// Tant que l'utilisateur est actif on attends la demande de nouvelle conversation
|
||||
ServerSocket servSocket=null;
|
||||
try {
|
||||
servSocket = new ServerSocket(controller.myUser.getPortTCP());
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") Server is listening on port "+this.controller.myUser.getPortTCP());
|
||||
} catch (IOException e) {
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") Server is not listening on port "+this.controller.myUser.getPortTCP());
|
||||
e.printStackTrace();
|
||||
}
|
||||
// TODO changer le true
|
||||
while(true) {
|
||||
|
||||
ServerSocket servSocket=null;
|
||||
try {
|
||||
servSocket = new ServerSocket(myUser.getPortTCP());
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") WAITING FOR NEW CONNECTION");
|
||||
|
||||
try {
|
||||
this.accept(servSocket);
|
||||
System.out.println("("+this.controller.myUser.getPseudo()+") NEW CONNECTION");
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
@ -77,7 +80,7 @@ public class ListeningThreadTCPConnection extends ListeningThread{
|
|||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
System.out.println("End of listing thread TCP ("+this.myUser.getPseudo()+")");
|
||||
System.out.println("End of listing thread TCP ("+this.controller.myUser.getPseudo()+")");
|
||||
try {
|
||||
this.interrupt();
|
||||
}catch (Exception e){
|
||||
|
|
|
@ -23,10 +23,10 @@ public class ListeningThreadUDP extends ListeningThread{
|
|||
* Création d'un socket d'écoute UDP
|
||||
* </p>
|
||||
*/
|
||||
public ListeningThreadUDP(String s,LocalUser myUser) {
|
||||
super(s,myUser);
|
||||
public ListeningThreadUDP(String s,Controller controller) {
|
||||
super(s,controller);
|
||||
try {
|
||||
this.dgramSocket = new DatagramSocket(this.myUser.getPortUDPlistening(),this.myUser.getAddIP());
|
||||
this.dgramSocket = new DatagramSocket(this.controller.myUser.getPortUDPlistening(),this.controller.myUser.getAddIP());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ public class ListeningThreadUDP extends ListeningThread{
|
|||
}
|
||||
int senderUDPport = Integer.parseInt(tabMsg[1]); // On récupère le port UDP de l'utilisateur distant
|
||||
|
||||
String toSend = myUser.getAddIP().toString()+":"+myUser.getPortTCP()+":"+myUser.getPseudo()+":test";
|
||||
String toSend = controller.myUser.getAddIP().toString()+":"+controller.myUser.getPortTCP()+":"+controller.myUser.getPseudo()+":test";
|
||||
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, senderUDPport);
|
||||
|
||||
try {
|
||||
|
@ -86,7 +86,7 @@ public class ListeningThreadUDP extends ListeningThread{
|
|||
else {
|
||||
try {
|
||||
// On récupère l'adresse IP et le port TCP de l'utilisateur distant et ajout à la liste de l'utilisateur utilisant ce thread
|
||||
this.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0].split("/")[1]),Integer.parseInt(tabMsg[1]),tabMsg[2]);
|
||||
this.controller.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0].split("/")[1]),Integer.parseInt(tabMsg[1]),tabMsg[2]);
|
||||
} catch (NumberFormatException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
@ -111,7 +111,7 @@ public class ListeningThreadUDP extends ListeningThread{
|
|||
*/
|
||||
public void close() {
|
||||
this.dgramSocket.close();
|
||||
System.out.println("End of listing thread UDP ("+this.myUser.getPseudo()+")");
|
||||
System.out.println("End of listing thread UDP ("+this.controller.myUser.getPseudo()+")");
|
||||
try {
|
||||
this.interrupt();
|
||||
}catch (Exception e){
|
||||
|
|
|
@ -1,54 +1,57 @@
|
|||
package model;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Chat {
|
||||
|
||||
/*** ATTRIBUTS ***/
|
||||
private int localUser_portTCP;
|
||||
private int remoteUser_portTCP;
|
||||
private ArrayList<RemoteUser> remoteUsersChatList = new ArrayList<RemoteUser>(); // listes des utilisateurs sur ce chat
|
||||
private RemoteUser remoteUser;
|
||||
private ArrayList<Message> messages = new ArrayList<Message>();
|
||||
private Socket userSocket;
|
||||
|
||||
/**
|
||||
* Constructor of Chat (local)
|
||||
* @parametres
|
||||
* @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
|
||||
* @description
|
||||
* @description
|
||||
* <p>
|
||||
*
|
||||
* </p>
|
||||
*/
|
||||
public Chat(RemoteUser rm,int localUser_portTCP,int remoteUser_portTCP) {
|
||||
this.localUser_portTCP = localUser_portTCP;
|
||||
this.remoteUser_portTCP = remoteUser_portTCP;
|
||||
remoteUsersChatList.add(rm);
|
||||
public Chat(RemoteUser rm) {
|
||||
this.remoteUser=rm;
|
||||
}
|
||||
|
||||
/*** GETTERS ***/
|
||||
public int getLocalUser_portTCP() {
|
||||
return localUser_portTCP;
|
||||
}
|
||||
public int getRemoteUser_portTCP() {
|
||||
return remoteUser_portTCP;
|
||||
}
|
||||
public ArrayList<RemoteUser> getRemoteUsersChatList() {
|
||||
return remoteUsersChatList;
|
||||
public RemoteUser getRemoteUser() {
|
||||
return remoteUser;
|
||||
}
|
||||
public ArrayList<Message> getMessages() {
|
||||
return messages;
|
||||
}
|
||||
public Socket getUserSocket() {
|
||||
return userSocket;
|
||||
}
|
||||
|
||||
/*** SETTERS ***/
|
||||
public void setLocalUser_portTCP(int localUser_portTCP) {
|
||||
this.localUser_portTCP = localUser_portTCP;
|
||||
public void setRemoteUser(RemoteUser rm) {
|
||||
this.remoteUser = rm;
|
||||
}
|
||||
public void setRemoteUser_portTCP(int remoteUser_portTCP) {
|
||||
this.remoteUser_portTCP = remoteUser_portTCP;
|
||||
public void setSocket(Socket link) {
|
||||
this.userSocket = link;
|
||||
}
|
||||
public void setRemoteUsersChatList(ArrayList<RemoteUser> remoteUsersChatList) {
|
||||
this.remoteUsersChatList = remoteUsersChatList;
|
||||
public void addMessage(Message msg) {
|
||||
this.messages.add(msg);
|
||||
}
|
||||
|
||||
public void closeSocket() {
|
||||
try {
|
||||
this.userSocket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -88,8 +88,30 @@ public class LocalUser extends User{
|
|||
* Ajout ou mise à jour l'utilisateur distant dans notre liste d'utilisateur distant
|
||||
* </p>
|
||||
*/
|
||||
public void addChats(RemoteUser rm,int localUser_portTCP,int remoteUser_portTCP) {
|
||||
Chat newChat= new Chat(rm,localUser_portTCP,remoteUser_portTCP);
|
||||
public Chat 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(found) {
|
||||
return i;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public void closeAllRemainingChatSocket() {
|
||||
for(int i=0;i<this.chats.size();i++) {
|
||||
this.chats.get(i).closeSocket();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,24 +4,7 @@ import java.net.*;
|
|||
|
||||
public abstract class User {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
User other = (User) obj;
|
||||
if (addIP == null) {
|
||||
if (other.addIP != null)
|
||||
return false;
|
||||
} else if (!addIP.equals(other.addIP))
|
||||
return false;
|
||||
if (portTCP != other.portTCP)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*** ATTRIBUTS ***/
|
||||
protected String pseudo;
|
||||
protected InetAddress addIP;
|
||||
|
@ -73,4 +56,23 @@ public abstract class User {
|
|||
this.portTCP = portTCP;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
User other = (User) obj;
|
||||
if (addIP == null) {
|
||||
if (other.addIP != null)
|
||||
return false;
|
||||
} else if (!addIP.equals(other.addIP))
|
||||
return false;
|
||||
if (portTCP != other.portTCP)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ public class Interface implements ActionListener {
|
|||
final static String LOOKANDFEEL = "System";
|
||||
|
||||
public Interface(Controller controller) {
|
||||
this.controller = controller;
|
||||
this.hisController = controller;
|
||||
}
|
||||
|
||||
public Component createComponents() {
|
||||
|
|
Loading…
Reference in a new issue