fix connection tcp, send message + reception

This commit is contained in:
Alexandre Gonzalvez 2020-12-16 06:08:40 +01:00
parent e19bc4b45c
commit c9e317bd27
9 changed files with 236 additions and 161 deletions

View file

@ -9,28 +9,34 @@ import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import model.Chat;
import model.LocalUser; import model.LocalUser;
import model.Message;
import model.Msg_Text;
import model.RemoteUser;
import view.Interface; import view.Interface;
public class Controller { public class Controller {
/*** CONSTANTES ***/ /*** CONSTANTES ***/
final static int portUDPlistening_remoteUsr2 = 20002; // TO REMOVE when we use broadcast final static int portUDPlistening_remoteUsr2 = 31002; // TO REMOVE when we use broadcast
final static int portUDPlistening_remoteUsr3 = 20003; // TO REMOVE when we use broadcast final static int portUDPlistening_remoteUsr3 = 31003; // TO REMOVE when we use broadcast
/*** ATTRIBUTS ***/ /*** ATTRIBUTS ***/
private LocalUser myUser; protected LocalUser myUser;
private Interface hisView; protected Interface hisView;
private ListeningThreadUDP udp_connect_thread; private ListeningThreadUDP udp_connect_thread;
private ListeningThreadTCPConnection tcp_connect_thread; private ListeningThreadTCPConnection tcp_connect_thread;
private ArrayList<ListeningThreadTCPChat> tcp_chats_threads = new ArrayList<ListeningThreadTCPChat>(); // listes des utilisateurs actifs
private Historique histoire; private Historique histoire;
/** /**
* Constructor of Controller * Constructor of Controller
* @parametres * @parametres
@ -46,7 +52,7 @@ public class Controller {
* - notification aux autres utilisateurs actifs * - notification aux autres utilisateurs actifs
* </p> * </p>
*/ */
private Controller(int portUDPsend,int portUDPlistening,int portTCP,Historique histoire) { private Controller(int portUDPsend,int portUDPlistening,int portTCP) {
this.histoire=histoire; this.histoire=histoire;
InetAddress addIP = null; InetAddress addIP = null;
try try
@ -64,10 +70,10 @@ public class Controller {
e.printStackTrace(); 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.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(); this.tcp_connect_thread.start();
notify_remote_users(); notify_remote_users();
@ -90,8 +96,7 @@ public class Controller {
* - notification aux autres utilisateurs actifs * - notification aux autres utilisateurs actifs
* </p> * </p>
*/ */
private Controller(int portUDPsend,int portUDPlistening,int portTCP,String pseudo,Historique histoire) { private Controller(int portUDPsend,int portUDPlistening,int portTCP,String pseudo) {
this.histoire=histoire;
InetAddress addIP = null; InetAddress addIP = null;
try try
{ {
@ -102,10 +107,10 @@ public class Controller {
} }
this.myUser = new LocalUser(pseudo,addIP,portUDPsend,portUDPlistening,portTCP); 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.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(); this.tcp_connect_thread.start();
notify_remote_users(); notify_remote_users();
@ -127,12 +132,10 @@ public class Controller {
public ListeningThreadTCPConnection getTcp_connect_thread() { public ListeningThreadTCPConnection getTcp_connect_thread() {
return tcp_connect_thread; return tcp_connect_thread;
} }
public ArrayList<ListeningThreadTCPChat> getTcp_chats_threads() {
return tcp_chats_threads;
}
public Historique getHistory() { public Historique getHistory() {
return histoire; return histoire;
} }
/*** SETTERS ***/ /*** SETTERS ***/
public void setMyUser(LocalUser myUser) { public void setMyUser(LocalUser myUser) {
this.myUser = myUser; this.myUser = myUser;
@ -146,9 +149,6 @@ public class Controller {
public void setTcp_connect_thread(ListeningThreadTCPConnection tcp_connect_thread) { public void setTcp_connect_thread(ListeningThreadTCPConnection tcp_connect_thread) {
this.tcp_connect_thread = 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) { public void setHistory(Historique histoire) {
this.histoire=histoire; this.histoire=histoire;
} }
@ -169,7 +169,7 @@ public class Controller {
//sc1.close(); //sc1.close();
JOptionPane.showMessageDialog(null ,"Your nickname : " + tmpPseudo + " is valid !"); 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; return tmpPseudo;
} }
@ -263,10 +263,7 @@ public class Controller {
tabresponse = response.split(":"); tabresponse = response.split(":");
// Affichage de la réponse // Affichage de la réponse
System.out.println("Remote user n°"+nbReponse+ //System.out.println("Remote user n°"+nbReponse+"\nIP : "+tabresponse[0]+"\nn°Port : "+tabresponse[1]+"\npseudo : "+tabresponse[2]);
"\nIP : "+tabresponse[0]+
"\nn°Port : "+tabresponse[1]+
"\npseudo : "+tabresponse[2]);
// Si reception on ajoute l'utilisateur à notre liste d'utilisateur distant // 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]); this.myUser.addRemoteUser(InetAddress.getByName(tabresponse[0].split("/")[1]),Integer.parseInt(tabresponse[1]),tabresponse[2]);
@ -328,6 +325,41 @@ public class Controller {
dgramSocket.close(); 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 { public void TCPmessage(int index) throws IOException {
Socket link=null; Socket link=null;
String s1; String s1;
@ -390,35 +422,27 @@ public class Controller {
* Laisse l'utilisateur choisir parmis la liste d'utilisateurs actifs celui avec lequel il souhaite échanger via un chat * Laisse l'utilisateur choisir parmis la liste d'utilisateurs actifs celui avec lequel il souhaite échanger via un chat
*</p> *</p>
*/ */
public void getOneActiveUser() throws IOException { public void selectActiveUser() throws IOException {
this.printRemoteUserList(); 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:")); 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 (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"); JOptionPane.showMessageDialog(null ,"User "+this.myUser.getRemoteUsersList().get(index).getPseudo()+" is already in chat with you");
} }
else { 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 { else {
JOptionPane.showMessageDialog(null ,"Wrong index (no active at index number "+index+" )"); JOptionPane.showMessageDialog(null ,"Wrong index (no active at index number "+index+" )");
this.selectActiveUser();
}
} }
this.TCPmessage(index);
//sc2.close();
}
/********************************** A mettre dans l'interface ******************************************/ /********************************** A mettre dans l'interface ******************************************/
@ -439,31 +463,60 @@ public class Controller {
/*************************************************************************************************************************/ /*************************************************************************************************************************/
public static void main(String[] args) throws IOException, InterruptedException { 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); ctr1.hisView=Interface.createAndShowGUI(ctr1);
// Fonction appelé par notre utilisateur local /** Simulation of a session **/
ctr1.getOneActiveUser(); // SELECT REMOTE USER
ctr1.changePseudo(); 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 /** Unused function **/
System.out.println("Sleep mode for 5 seconds"); // 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); Thread.sleep(5000);
// REMOTEUSER_1 - MIKE
// On ferme les différents threads et socket d'écoute ctr2.myUser.closeAllRemainingChatSocket();
ctr1.udp_connect_thread.close();
ctr2.udp_connect_thread.close();
ctr3.udp_connect_thread.close();
ctr1.tcp_connect_thread.close();
ctr2.tcp_connect_thread.close(); ctr2.tcp_connect_thread.close();
ctr2.udp_connect_thread.close();
// REMOTEUSER_2 - ALICE
ctr3.myUser.closeAllRemainingChatSocket();
ctr3.tcp_connect_thread.close(); 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");
} }
} }

View file

@ -1,14 +1,13 @@
package controller; package controller;
import model.LocalUser;
public abstract class ListeningThread extends Thread{ 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); super(s);
this.myUser = myUser; this.controller = controller;
} }
public abstract void run(); public abstract void run();

View file

@ -23,8 +23,8 @@ public class ListeningThreadTCPChat extends ListeningThread{
* <p> * <p>
* </p> * </p>
*/ */
public ListeningThreadTCPChat(String s,LocalUser myUser,Socket socket) { public ListeningThreadTCPChat(String s,Controller controller,Socket socket) {
super(s,myUser); super(s,controller);
this.socket=socket; this.socket=socket;
} }
@ -36,35 +36,28 @@ public class ListeningThreadTCPChat extends ListeningThread{
* </p> * </p>
*/ */
public void run() { public void run() {
Socket link = this.socket; BufferedReader in = null;
String msg = null;
try { try {
BufferedReader in =new BufferedReader(new InputStreamReader(link.getInputStream())); System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE ");
in =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); 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; String input;
/*while (!(input=in.readLine()).equals("end")) {
System.out.print("server_recoit:"+input);
}*/
try { try {
link.close(); while (!(input=in.readLine()).equals("end")) {
System.out.println("("+this.controller.myUser.getPseudo()+") recoit : "+input);
}
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
/* close /* close
@ -80,7 +73,7 @@ public class ListeningThreadTCPChat extends ListeningThread{
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); 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 { try {
this.interrupt(); this.interrupt();
}catch (Exception e){ }catch (Exception e){

View file

@ -18,14 +18,14 @@ public class ListeningThreadTCPConnection extends ListeningThread{
* <p> * <p>
* </p> * </p>
*/ */
public ListeningThreadTCPConnection(String s,LocalUser myUser) { public ListeningThreadTCPConnection(String s,Controller controller) {
super(s,myUser); super(s,controller);
} }
public void accept(ServerSocket servSocket) throws IOException { public void accept(ServerSocket servSocket) throws IOException {
Socket socket_tcp= servSocket.accept(); 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.start();
threadtcpchat.interrupt(); threadtcpchat.interrupt();
@ -42,18 +42,21 @@ public class ListeningThreadTCPConnection extends ListeningThread{
public void run(){ public void run(){
// Tant que l'utilisateur est actif on attends la demande de nouvelle conversation // Tant que l'utilisateur est actif on attends la demande de nouvelle conversation
while(true) {
ServerSocket servSocket=null; ServerSocket servSocket=null;
try { try {
servSocket = new ServerSocket(myUser.getPortTCP()); 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) { } catch (IOException e) {
// TODO Auto-generated catch block System.out.println("("+this.controller.myUser.getPseudo()+") Server is not listening on port "+this.controller.myUser.getPortTCP());
e.printStackTrace(); e.printStackTrace();
} }
// TODO changer le true
while(true) {
System.out.println("("+this.controller.myUser.getPseudo()+") WAITING FOR NEW CONNECTION");
try { try {
this.accept(servSocket); this.accept(servSocket);
System.out.println("("+this.controller.myUser.getPseudo()+") NEW CONNECTION");
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -77,7 +80,7 @@ public class ListeningThreadTCPConnection extends ListeningThread{
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); 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 { try {
this.interrupt(); this.interrupt();
}catch (Exception e){ }catch (Exception e){

View file

@ -23,10 +23,10 @@ public class ListeningThreadUDP extends ListeningThread{
* Création d'un socket d'écoute UDP * Création d'un socket d'écoute UDP
* </p> * </p>
*/ */
public ListeningThreadUDP(String s,LocalUser myUser) { public ListeningThreadUDP(String s,Controller controller) {
super(s,myUser); super(s,controller);
try { 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) { } catch (IOException e) {
e.printStackTrace(); 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 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); DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, senderUDPport);
try { try {
@ -86,7 +86,7 @@ public class ListeningThreadUDP extends ListeningThread{
else { else {
try { 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 // 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) { } catch (NumberFormatException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -111,7 +111,7 @@ public class ListeningThreadUDP extends ListeningThread{
*/ */
public void close() { public void close() {
this.dgramSocket.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 { try {
this.interrupt(); this.interrupt();
}catch (Exception e){ }catch (Exception e){

View file

@ -1,54 +1,57 @@
package model; package model;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
public class Chat { public class Chat {
/*** ATTRIBUTS ***/ /*** ATTRIBUTS ***/
private int localUser_portTCP; private RemoteUser remoteUser;
private int remoteUser_portTCP;
private ArrayList<RemoteUser> remoteUsersChatList = new ArrayList<RemoteUser>(); // listes des utilisateurs sur ce chat
private ArrayList<Message> messages = new ArrayList<Message>(); private ArrayList<Message> messages = new ArrayList<Message>();
private Socket userSocket;
/** /**
* Constructor of Chat (local) * Constructor of Chat (local)
* @parametres * @parametres
* @param remoteUser : remoteUser => référence de l'utilisateur distant * @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>
* *
* </p> * </p>
*/ */
public Chat(RemoteUser rm,int localUser_portTCP,int remoteUser_portTCP) { public Chat(RemoteUser rm) {
this.localUser_portTCP = localUser_portTCP; this.remoteUser=rm;
this.remoteUser_portTCP = remoteUser_portTCP;
remoteUsersChatList.add(rm);
} }
/*** GETTERS ***/ /*** GETTERS ***/
public int getLocalUser_portTCP() { public RemoteUser getRemoteUser() {
return localUser_portTCP; return remoteUser;
}
public int getRemoteUser_portTCP() {
return remoteUser_portTCP;
}
public ArrayList<RemoteUser> getRemoteUsersChatList() {
return remoteUsersChatList;
} }
public ArrayList<Message> getMessages() { public ArrayList<Message> getMessages() {
return messages; return messages;
} }
public Socket getUserSocket() {
return userSocket;
}
/*** SETTERS ***/ /*** SETTERS ***/
public void setLocalUser_portTCP(int localUser_portTCP) { public void setRemoteUser(RemoteUser rm) {
this.localUser_portTCP = localUser_portTCP; this.remoteUser = rm;
} }
public void setRemoteUser_portTCP(int remoteUser_portTCP) { public void setSocket(Socket link) {
this.remoteUser_portTCP = remoteUser_portTCP; this.userSocket = link;
} }
public void setRemoteUsersChatList(ArrayList<RemoteUser> remoteUsersChatList) { public void addMessage(Message msg) {
this.remoteUsersChatList = remoteUsersChatList; this.messages.add(msg);
}
public void closeSocket() {
try {
this.userSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
} }

View file

@ -88,8 +88,30 @@ public class LocalUser extends User{
* Ajout ou mise à jour l'utilisateur distant dans notre liste d'utilisateur distant * Ajout ou mise à jour l'utilisateur distant dans notre liste d'utilisateur distant
* </p> * </p>
*/ */
public void addChats(RemoteUser rm,int localUser_portTCP,int remoteUser_portTCP) { public Chat addChats(RemoteUser rm) {
Chat newChat= new Chat(rm,localUser_portTCP,remoteUser_portTCP); Chat newChat= new Chat(rm);
this.chats.add(newChat); 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();
}
} }
} }

View file

@ -4,24 +4,7 @@ import java.net.*;
public abstract class User { 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 ***/ /*** ATTRIBUTS ***/
protected String pseudo; protected String pseudo;
protected InetAddress addIP; protected InetAddress addIP;
@ -73,4 +56,23 @@ public abstract class User {
this.portTCP = portTCP; 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;
}
} }

View file

@ -24,7 +24,7 @@ public class Interface implements ActionListener {
final static String LOOKANDFEEL = "System"; final static String LOOKANDFEEL = "System";
public Interface(Controller controller) { public Interface(Controller controller) {
this.controller = controller; this.hisController = controller;
} }
public Component createComponents() { public Component createComponents() {