historique fix & pollisage du package controller

This commit is contained in:
Alexandre Gonzalvez 2021-01-08 21:54:17 +01:00
parent 3ba80fc0a7
commit 2d48a2b0d4
6 changed files with 518 additions and 189 deletions

View file

@ -28,7 +28,7 @@ import view.Interface;
public class Controller {
/*** CONSTANTES ***/
int NB_SECOND_WAITING_RESPONSE_BROADCAST = 2;
final int NB_SECOND_WAITING_RESPONSE_BROADCAST = 1;
// TO REMOVE when we use broadcast
final static int portUDPlistening_remoteUsr1 = 31001;
@ -41,15 +41,15 @@ public class Controller {
/*** ATTRIBUTS ***/
protected LocalUser myUser;
protected Interface view;
private ListeningThreadUDP udp_connect_thread;
private ListeningThreadTCPConnection tcp_connect_thread;
private TListeningUDP udp_connect_thread;
private TListeningTCPConnection tcp_connect_thread;
private Historique histoire;
protected Chat activeChat;
/**
* Constructor of Controller
* @parametres
* @param portUDPsend : int => le numéro de port pour envoyé ces informations lors d'un changements ou d'une nouvelle connexion
* @param portUDPsend : int => le numéro de port pour envoyer ces informations lors d'un changements ou d'une nouvelle connexion
* @param portUDPlistening : int => le numéro de port pour recevoir les informations des nouveaux utilisateurs ou les changements
* @param portTCP : int => le numéro de port pour commencer une nouvelle conversation
* @throws IOException
@ -85,9 +85,9 @@ public class Controller {
}
// Création des threads d'écoutes
this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
this.udp_connect_thread = new TListeningUDP("UDP Listening thread",this);
this.udp_connect_thread.start();
this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this);
this.tcp_connect_thread = new TListeningTCPConnection("TCP main Listening thread",this);
this.tcp_connect_thread.start();
// Notification des utilisateurs distants
@ -122,6 +122,7 @@ public class Controller {
try
{
addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
JOptionPane.showMessageDialog(null ,"Could not find local address!");
@ -133,10 +134,10 @@ public class Controller {
if(this.validatePseudo(pseudo)) {
this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
this.udp_connect_thread = new TListeningUDP("UDP Listening thread",this);
this.udp_connect_thread.start();
this.tcp_connect_thread = new ListeningThreadTCPConnection("TCP main Listening thread",this);
this.tcp_connect_thread = new TListeningTCPConnection("TCP main Listening thread",this);
this.tcp_connect_thread.start();
@ -156,7 +157,39 @@ public class Controller {
}
/*** GETTERS ***/
public LocalUser getMyUser() {
return myUser;
}
public Interface getview() {
return view;
}
public TListeningUDP getUdp_connect_thread() {
return udp_connect_thread;
}
public TListeningTCPConnection getTcp_connect_thread() {
return tcp_connect_thread;
}
public Historique getHistory() {
return histoire;
}
/*** SETTERS ***/
public void setMyUser(LocalUser myUser) {
this.myUser = myUser;
}
public void setview(Interface view) {
this.view = view;
}
public void setUdp_connect_thread(TListeningUDP udp_connect_thread) {
this.udp_connect_thread = udp_connect_thread;
}
public void setTcp_connect_thread(TListeningTCPConnection tcp_connect_thread) {
this.tcp_connect_thread = tcp_connect_thread;
}
public void setHistory(Historique histoire) {
this.histoire=histoire;
}
/**************************** Initialisation pseudo et découverte utilisateur distant (+notification utilisateurs distants) **********************************/
@ -222,7 +255,7 @@ public class Controller {
DatagramSocket dgramSocket = new DatagramSocket(this.myUser.getPortUDPsend(),this.myUser.getAddIP());
// Création du message à envoyer
String toSend = this.myUser.getAddIP()+":"+this.myUser.getPortUDPsend()+":info";
String toSend = this.myUser.getAddIP().getHostAddress()+":"+this.myUser.getPortUDPsend()+":info";
// Broadcast du message
broadcast(dgramSocket,toSend);
@ -255,7 +288,7 @@ public class Controller {
// On découpe la réponse en tableau de string ([adresseIP,tcpPort,nickname])
tabresponse = response.split(":");
// 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]),Integer.parseInt(tabresponse[1]),tabresponse[2]);
valid= (tmpPseudo.compareTo(tabresponse[2])!=0); // On regarde la différence entre notre pseudo et le pseudo reçu
}
newDate = new Date();
@ -304,23 +337,8 @@ public class Controller {
} catch (IOException e) {
e.printStackTrace();
}
// Send to other active user (simulation of broadcast)
String toSend = this.myUser.getAddIP().toString()+":"+this.myUser.getPortTCP()+":"+this.myUser.getPseudo()+":notify";
/*
DatagramPacket outPacket =null;
int tabBroadcastSize = tabBroadcast.length;
for(int i=0;i<tabBroadcastSize;i++) {
if(tabBroadcast[i]!=myUser.getPortUDPlistening()) {
outPacket = new DatagramPacket(toSend.getBytes(), toSend.length(),this.myUser.getAddIP(), tabBroadcast[i]);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
}*/
String toSend = this.myUser.getAddIP().getHostAddress()+":"+this.myUser.getPortTCP()+":"+this.myUser.getPseudo()+":notify";
broadcast(dgramSocket,toSend);
dgramSocket.close();
}
@ -347,15 +365,20 @@ public class Controller {
/*** recup history and put it in model ***/
try {
//System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
c.addListMessage(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
try {
c.addListMessage(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (SQLException e) {
System.out.println("souci avec le retrieveMsg");
System.out.println("souci avec le retrieveMsgSQL");
e.printStackTrace();
}
//System.out.println(myUser.getChats().get(myUser.getChatIndexOf(rm)));
return c;
}
public String askOpenSession(int index) {
String history="";
RemoteUser rm = myUser.getRemoteUsersList().get(index);
@ -382,6 +405,10 @@ public class Controller {
return history;
}
public void askCloseSession() {
closeSession(this.activeChat);
this.activeChat = null;
@ -431,13 +458,12 @@ public class Controller {
out.println(msg);
}
/*
public String askNewMessage() {
String message = "";
return message;
}
}*/
/**************************** Autre fonctions **********************************/
public String [] askUpdateActiveUsers() {
String[] pseudotab = new String[myUser.getRemoteUsersList().size()];
@ -468,75 +494,47 @@ public class Controller {
Historique histoire=new Historique();
/** Création des utilisateurs **/
// REMOTEUSER_1 - THEAU
Controller ctr1 = new Controller(31011,portUDPlistening_remoteUsr1,31021,"Theau",histoire);
// REMOTEUSER_2 - LEONIE
Controller ctr2 = new Controller(31012,portUDPlistening_remoteUsr2,31022,"Leonie",histoire);
// REMOTEUSER_3 - ALEXANDRE
Controller ctr3 = new Controller(31013,portUDPlistening_remoteUsr3,31023,"Alexandre",histoire);
// LOCAL USER - AS YOU WANT
Controller ctr = new Controller(31014,portUDPlistening_local,31024,histoire);
ArrayList<Controller> lstCtr = new ArrayList<Controller>();
/** loop **/
Boolean running = ctr1.interfaceRunning || ctr2.interfaceRunning || ctr3.interfaceRunning || ctr.interfaceRunning;
// REMOTE USERS
Controller ctr1 = new Controller(31011,portUDPlistening_remoteUsr1,31021,"Theau",histoire);
lstCtr.add(ctr1);
Controller ctr2 = new Controller(31012,portUDPlistening_remoteUsr2,31022,"Leonie",histoire);
lstCtr.add(ctr2);
Controller ctr3 = new Controller(31013,portUDPlistening_remoteUsr3,31023,"Alexandre",histoire);
lstCtr.add(ctr3);
// LOCAL USER
Controller ctr = new Controller(31014,portUDPlistening_local,31024,histoire);
lstCtr.add(ctr);
/** Loop **/
Boolean running = isRunning(lstCtr);
while(running) {
running = ctr1.interfaceRunning || ctr2.interfaceRunning || ctr3.interfaceRunning || ctr.interfaceRunning;
running = isRunning(lstCtr);
}
System.out.println("Fin de la boucle");
System.out.println("Fin de la boucle");//TOREMOVE
/** Close thread and socket **/
// REMOTEUSER_1 - THEAU
ctr1.close();
// REMOTEUSER_2 - LEONIE
ctr2.close();
// REMOTEUSER_3 - ALEXANDRE
ctr3.close();
// LOCAL USER
ctr.close();
// AFFICHAGE
/** End - Close thread and socket for every controller**/
closeThreads(lstCtr);
System.out.println("end program");
JOptionPane.showMessageDialog(null ,"END");
}
/*** GETTERS ***/
public LocalUser getMyUser() {
return myUser;
}
public Interface getview() {
return view;
}
public ListeningThreadUDP getUdp_connect_thread() {
return udp_connect_thread;
}
public ListeningThreadTCPConnection getTcp_connect_thread() {
return tcp_connect_thread;
}
public Historique getHistory() {
return histoire;
static boolean isRunning(ArrayList<Controller> lstCtr){
boolean isRunning=false;
for(Controller ctr : lstCtr) {
isRunning|=ctr.interfaceRunning;
}
return isRunning;
}
/*** SETTERS ***/
public void setMyUser(LocalUser myUser) {
this.myUser = myUser;
}
public void setview(Interface view) {
this.view = view;
}
public void setUdp_connect_thread(ListeningThreadUDP udp_connect_thread) {
this.udp_connect_thread = udp_connect_thread;
}
public void setTcp_connect_thread(ListeningThreadTCPConnection tcp_connect_thread) {
this.tcp_connect_thread = tcp_connect_thread;
}
public void setHistory(Historique histoire) {
this.histoire=histoire;
static void closeThreads(ArrayList<Controller> lstCtr) {
for(Controller ctr : lstCtr) {
ctr.close();
}
}
}

View file

@ -18,7 +18,7 @@ import model.Msg_Text;
public class Historique {
public void saveMessage(model.User ctr1, model.RemoteUser ctr2, String input, String date) {
public void saveMessage(model.User author, model.RemoteUser receiver, String input, String date) {
try {
Class.forName("java.sql.Driver");
} catch (ClassNotFoundException e) {
@ -46,7 +46,7 @@ public class Historique {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*
try {
nb_changed_rows = stat.executeUpdate("CREATE TABLE chat ( user_IPcode1 INTEGER, user_IPcode2 INTEGER ,Message VARCHAR(450) ,temps VARCHAR(450) )");
}catch (Exception e) {
@ -54,9 +54,9 @@ public class Historique {
}
System.out.println("coucou");
*/
try {
nb_changed_rows = stat.executeUpdate("INSERT INTO chat VALUES ('"+ctr1.getIPcode()+"','"+ctr2.getIPcode()+"','"+input+"','"+date+"')");
nb_changed_rows = stat.executeUpdate("INSERT INTO chat VALUES ('"+author.getIPcode()+"','"+receiver.getIPcode()+"','"+input+"','"+date+"')");
} catch (SQLException e) {
System.out.println("insertion pas établie");
e.printStackTrace();
@ -82,106 +82,52 @@ public class Historique {
}
public ArrayList<Message> retrieveMessage(model.LocalUser user1, model.RemoteUser user2) throws SQLException {
try {
Class.forName("java.sql.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public ArrayList<Message> retrieveMessage(model.LocalUser usr, model.RemoteUser rmusr) throws SQLException, ClassNotFoundException {
ArrayList<Message> messages=new ArrayList<Message>();
Class.forName("java.sql.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://srv-bdens.insa-toulouse.fr:3306","tp_servlet_003","povu3Ma2");
Statement stat = null;
try {
stat = con.createStatement();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
stat = con.createStatement();
stat.executeUpdate("USE tp_servlet_003");
/*
Statement stat2 = null;
try {
stat2 = con.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int nb_changed_rows = stat.executeUpdate("USE tp_servlet_003");
try {
nb_changed_rows = stat.executeUpdate("USE TABLE chat ( user_IPcode1 INTEGER, user_IPcode2 INTEGER ,Message VARCHAR(450) ,temps VARCHAR(450) )");
}catch (Exception e) {
e.printStackTrace();
}
nb_changed_rows = stat2.executeUpdate("USE tp_servlet_003");
try {
nb_changed_rows = stat2.executeUpdate("USE TABLE chat ( user_IPcode1 INTEGER, user_IPcode2 INTEGER ,Message VARCHAR(450) ,temps VARCHAR(450) )");
}catch (Exception e) {
e.printStackTrace();
}
ResultSet RSSent=(stat.executeQuery("SELECT user_IPcode1, user_IPcode2, Message, temps FROM chat WHERE user_IPcode1="+user1.getIPcode()));
//System.out.println(RSSent);
boolean encore1 = RSSent.next();
ResultSet RSReceived=(stat2.executeQuery("SELECT user_IPcode1, user_IPcode2, Message, temps FROM chat WHERE user_IPcode1="+user2.getIPcode()));
boolean encore2 = RSReceived.next();
ArrayList<Message> message=new ArrayList<Message>();
//String message=null;
String[] string1=null;
String[] string2=null;
Calendar date1=Calendar.getInstance();
Calendar date2=Calendar.getInstance();
while(encore1 || encore2) {
//RSSent=(stat.executeQuery("SELECT user_IPcode1, user_IPcode2, Message, temps FROM chat WHERE user_IPcode1="+user1.getIPcode()));
//boolean temp=RSSent.next();
while (RSSent.getInt(2)!=user2.getIPcode()) {
encore1=RSSent.next();
}
string1=RSSent.getString(4).split(" ");
date1.set(Integer.valueOf(string1[0]), Integer.valueOf(string1[1]), Integer.valueOf(string1[2]),Integer.valueOf(string1[3]), Integer.valueOf(string1[4]), Integer.valueOf(string1[5]));
//RSReceived=(stat.executeQuery("SELECT user_IPcode1, user_IPcode2, Message, temps FROM chat WHERE user_IPcode1="+user2.getIPcode()));
//boolean tempi = RSReceived.next();
while (RSReceived.getInt(1)!=user1.getIPcode()) {
encore2=RSReceived.next();
}
string2=RSReceived.getString(4).split(" ");
date2.set(Integer.valueOf(string2[0]), Integer.valueOf(string2[1]), Integer.valueOf(string2[2]),Integer.valueOf(string2[3]), Integer.valueOf(string2[4]), Integer.valueOf(string2[5]));
if (date1.compareTo(date2)>=0) {
Msg_Text msg =new Msg_Text(user2,RSReceived.getString(4),RSReceived.getString(3));
message.add(msg);
Msg_Text msg1 =new Msg_Text(user1,RSSent.getString(4),RSSent.getString(3));
message.add(msg1);
}else {
Msg_Text msg1 =new Msg_Text(user1,RSSent.getString(4),RSSent.getString(3));
message.add(msg1);
Msg_Text msg =new Msg_Text(user2,RSReceived.getString(4),RSReceived.getString(3));
message.add(msg);
}
encore1=RSSent.next();
encore2=RSReceived.next();
}
RSSent.close();
RSReceived.close();
stat.close();
stat2.close();
con.close();
stat2 = con.createStatement();
stat.executeUpdate("USE tp_servlet_003");
*/
return message;
//int nb_changed_rows;
String query = "SELECT user_IPcode1 ,Message, temps"
+" FROM chat WHERE (user_IPcode1="+usr.getIPcode()
+" AND user_IPcode2="+rmusr.getIPcode()+") "
+"OR (user_IPcode1="+rmusr.getIPcode()+" "
+ "AND user_IPcode2="+usr.getIPcode()+") "
+ "ORDER BY temps;";
ResultSet resultQueryRS=(stat.executeQuery(query));
int autorIPcode;
String message;
String strDate;
boolean encore = resultQueryRS.next();
while(encore) {
autorIPcode=resultQueryRS.getInt(1);
message=resultQueryRS.getString(2);;
strDate=resultQueryRS.getString(3);
if(usr.getIPcode()==autorIPcode) {
messages.add(new Msg_Text(usr,strDate,message));
}
else {
messages.add(new Msg_Text(rmusr,strDate,message));
}
encore=resultQueryRS.next();
}
return messages;
}
// //MAIN
// public Historique() {
//
// System.out.println("coucoudb");
//
//
// }
}

View file

@ -0,0 +1,141 @@
package controller;
import model.Chat;
import model.Msg_Text;
import model.RemoteUser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Calendar;
import java.util.Date;
public class TListeningTCPChat extends Thread{
protected Controller controller;
private Socket socket;
/* CONSTRUCTOR OF ListeningThreadTCPConnection
* @parametres
* @param s : String => nom du thread
* @param myUser : User => utilisateur utilisant ce thread
* @description
* <p>
* </p>
*/
public TListeningTCPChat(String s,Controller controller,Socket socket) {
this.controller = controller;
this.socket=socket;
}
/* run
* @description
* <p>
*
* </p>
*/
public void run() {
/**** function variables ****/
BufferedReader in = null;
String msg = null;
String input;
String dateString;
Date date;
Calendar date1=Calendar.getInstance();
Chat c = null;
try {
in =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/************ Check rm user information **********/
// check data identification from rm user | TO REMOVE IF !=IP
int rmPortTCP = -1;
try {
rmPortTCP = Integer.parseInt(in.readLine());
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
RemoteUser rm = new RemoteUser("Unknown",this.socket.getInetAddress(),rmPortTCP);
int indexRM = controller.myUser.getActiveUserIndexOf(rm);
// Check if rm is identifiable
/*System.out.println(rm);
for(int i=0;i<controller.myUser.getRemoteUsersList().size();i++) {
System.out.println(controller.myUser.getRemoteUsersList().get(i));
}
System.out.println(indexRM);*/
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.openSession(rm);
}
/*** listening tcp message from rm until session is close ***/
try {
String msgToprint;
while (!(input=in.readLine()).equals("end")/* && c.getActive()*/) {
System.out.println("("+this.controller.myUser.getPseudo()+") recoit => "+rm.getPseudo()+" : "+input);
Msg_Text message = new Msg_Text(rm,input);
c.addMessage(message);
if(controller.activeChat==c) {
controller.view.notifyNewMessage("("+message.getDate()+")\n"+c.getRemoteUser().getPseudo()+" : "+input);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
System.out.println("("+this.controller.myUser.getPseudo()+") Remote User unidentifiable => CLOSING CONNECTION");
}
}
/* close
* @description
* <p>
* ferme le socket d'écoute TCP pour le chat
* interrupt TCP listening thread pour le chat
* </p>
*/
public void close() {
try {
this.socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("End of listing thread UDP ("+this.controller.myUser.getPseudo()+")");
try {
this.interrupt();
}catch (Exception e){
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,92 @@
package controller;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TListeningTCPConnection extends Thread{
protected Controller controller;
private Socket socket_tcp=null;
private int nbChat = 0;
/* CONSTRUCTOR OF ListeningThreadTCPConnection
* @parametres
* @param s : String => nom du thread
* @param myUser : User => utilisateur utilisant ce thread
* @description
* <p>
* </p>
*/
public TListeningTCPConnection(String s,Controller controller) {
this.controller = controller;
}
public void accept(ServerSocket servSocket) throws IOException {
Socket socket_tcp= servSocket.accept();
this.nbChat++;
TListeningTCPChat threadtcpchat = new TListeningTCPChat("Chat_of_"+controller.myUser.getPseudo()+"_"+nbChat,controller,socket_tcp);
threadtcpchat.start();
threadtcpchat.interrupt();
}
/* run
* @description
* <p>
* écoutes les messages TCP tant que l'utilisateur est actif
* Traitement
* Si réception d'une demande, créer un thread pour la conversation
* </p>
*/
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();
}
while(true) {
System.out.println("("+this.controller.myUser.getPseudo()+") TCP Server 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();
}
}
}
/* close
* @description
* <p>
* ferme le socket d'écoute TCO
* interrupt UDP listening threadS
* </p>
*/
public void close() {
try {
this.socket_tcp.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("End of listing thread TCP ("+this.controller.myUser.getPseudo()+")");
try {
this.interrupt();
}catch (Exception e){
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,125 @@
package controller;
import model.LocalUser;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TListeningUDP extends Thread{
protected Controller controller;
private DatagramSocket dgramSocket = null;
/* CONSTRUCTOR OF UserListeningThreadUDP
* @parametres
* @param s : String => nom du thread
* @param user : User => utilisateur utilisant ce thread
* @description
* <p>
* Appel du constructeur de la classe hérité
* Création d'un socket d'écoute UDP
* </p>
*/
public TListeningUDP(String s,Controller controller) {
this.controller = controller;
try {
this.dgramSocket = new DatagramSocket(this.controller.myUser.getPortUDPlistening(),this.controller.myUser.getAddIP());
} catch (IOException e) {
e.printStackTrace();
}
}
/* run
* @description
* <p>
* écoutes les messages UDP tant que l'utilisateur est actif
* Traitement
* 1) Si demande d'information => envoi ses informations
* 2) Si réception d'information => ajoute les informations
* </p>
*/
public void run(){
// Tant que l'utilisateur est actif on attends les messages des nouveaux utilisateurs et changements des utilisateurs actifs
while(true) {
// Réception du message
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (Exception e) {
e.printStackTrace();
}
buffer = inPacket.getData();
// Traitement en fonction du message reçu
String receiveMsg = new String(buffer);
String [] tabMsg = receiveMsg.split(":");
// si demande d'information d'un nouvel utilisateur
if(tabMsg.length==3) {
InetAddress itsIP = null;
try {
itsIP = InetAddress.getByName(tabMsg[0]); // On récupère l'adresse IP de l'utilisateur distant
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int senderUDPport = Integer.parseInt(tabMsg[1]); // On récupère le port UDP de l'utilisateur distant
String toSend = controller.myUser.getAddIP().getHostAddress()+":"+controller.myUser.getPortTCP()+":"+controller.myUser.getPseudo()+":test";
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, senderUDPport);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
// Si un nouvel utilisateur passe en mode actif ou changement d'information
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
controller.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0]),Integer.parseInt(tabMsg[1]),tabMsg[2]);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(controller.interfaceRunning) {
controller.view.updateActiveUsers();
}
}
}
}
/* close
* @description
* <p>
* Ferme le socket d'écoute UDP
* Interrompt le thread d'écoute UDP
* </p>
*/
public void close() {
this.dgramSocket.close();
System.out.println("End of listing thread UDP ("+this.controller.myUser.getPseudo()+")");
try {
this.interrupt();
}catch (Exception e){
e.printStackTrace();
}
}
}

View file

@ -26,7 +26,7 @@ public abstract class User {
this.pseudo = pseudo;
this.addIP=addIP;
this.portTCP=portTCP;
this.IPcode=addIP.hashCode();
this.IPcode=hashCode();
}
@ -57,6 +57,15 @@ public abstract class User {
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
String ip=addIP.getHostAddress();
result = prime * result + ((ip == null) ? 0 : ip.hashCode());
result = prime * result + portTCP;
return result;
}
@Override
@ -68,11 +77,29 @@ public abstract class User {
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;
}
@Override
public String toString() {
return "User [pseudo=" + pseudo + ", addIP=" + addIP.getHostAddress() + ", portTCP=" + portTCP + ", IPcode=" + IPcode + "]";
}