version_2(broadcast+BD)
This commit is contained in:
parent
21780d4c4c
commit
34f15aefd2
7 changed files with 180 additions and 223 deletions
|
@ -28,7 +28,7 @@ import view.Interface;
|
||||||
public class Controller {
|
public class Controller {
|
||||||
|
|
||||||
/*** CONSTANTES ***/
|
/*** CONSTANTES ***/
|
||||||
int NB_SECOND_WAITING_RESPONSE_BROADCAST = 2;
|
final int NB_SECOND_WAITING_RESPONSE_BROADCAST = 1;
|
||||||
|
|
||||||
// TO REMOVE when we use broadcast
|
// TO REMOVE when we use broadcast
|
||||||
final static int portUDPlistening_remoteUsr1 = 31001;
|
final static int portUDPlistening_remoteUsr1 = 31001;
|
||||||
|
@ -41,15 +41,15 @@ public class Controller {
|
||||||
/*** ATTRIBUTS ***/
|
/*** ATTRIBUTS ***/
|
||||||
protected LocalUser myUser;
|
protected LocalUser myUser;
|
||||||
protected Interface view;
|
protected Interface view;
|
||||||
private ListeningThreadUDP udp_connect_thread;
|
private TListeningUDP udp_connect_thread;
|
||||||
private ListeningThreadTCPConnection tcp_connect_thread;
|
private TListeningTCPConnection tcp_connect_thread;
|
||||||
private Historique histoire;
|
private Historique histoire;
|
||||||
protected Chat activeChat;
|
protected Chat activeChat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor of Controller
|
* Constructor of Controller
|
||||||
* @parametres
|
* @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 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
|
* @param portTCP : int => le numéro de port pour commencer une nouvelle conversation
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
|
@ -85,9 +85,9 @@ public class Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Création des threads d'écoutes
|
// 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.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();
|
this.tcp_connect_thread.start();
|
||||||
|
|
||||||
// Notification des utilisateurs distants
|
// Notification des utilisateurs distants
|
||||||
|
@ -122,6 +122,7 @@ public class Controller {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
addIP = InetAddress.getLocalHost();
|
addIP = InetAddress.getLocalHost();
|
||||||
|
|
||||||
}
|
}
|
||||||
catch(UnknownHostException e) {
|
catch(UnknownHostException e) {
|
||||||
JOptionPane.showMessageDialog(null ,"Could not find local address!");
|
JOptionPane.showMessageDialog(null ,"Could not find local address!");
|
||||||
|
@ -133,10 +134,10 @@ public class Controller {
|
||||||
if(this.validatePseudo(pseudo)) {
|
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.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();
|
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) **********************************/
|
/**************************** 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());
|
DatagramSocket dgramSocket = new DatagramSocket(this.myUser.getPortUDPsend(),this.myUser.getAddIP());
|
||||||
|
|
||||||
// Création du message à envoyer
|
// 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 du message
|
||||||
broadcast(dgramSocket,toSend);
|
broadcast(dgramSocket,toSend);
|
||||||
|
@ -255,7 +288,7 @@ public class Controller {
|
||||||
// On découpe la réponse en tableau de string ([adresseIP,tcpPort,nickname])
|
// On découpe la réponse en tableau de string ([adresseIP,tcpPort,nickname])
|
||||||
tabresponse = response.split(":");
|
tabresponse = response.split(":");
|
||||||
// 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]),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
|
valid= (tmpPseudo.compareTo(tabresponse[2])!=0); // On regarde la différence entre notre pseudo et le pseudo reçu
|
||||||
}
|
}
|
||||||
newDate = new Date();
|
newDate = new Date();
|
||||||
|
@ -304,23 +337,8 @@ public class Controller {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send to other active user (simulation of broadcast)
|
// Send to other active user (simulation of broadcast)
|
||||||
String toSend = this.myUser.getAddIP().toString()+":"+this.myUser.getPortTCP()+":"+this.myUser.getPseudo()+":notify";
|
String toSend = this.myUser.getAddIP().getHostAddress()+":"+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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}*/
|
|
||||||
broadcast(dgramSocket,toSend);
|
broadcast(dgramSocket,toSend);
|
||||||
dgramSocket.close();
|
dgramSocket.close();
|
||||||
}
|
}
|
||||||
|
@ -347,15 +365,20 @@ public class Controller {
|
||||||
|
|
||||||
/*** recup history and put it in model ***/
|
/*** recup history and put it in model ***/
|
||||||
try {
|
try {
|
||||||
//System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
|
try {
|
||||||
c.addListMessage(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
|
c.addListMessage(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
|
||||||
} catch (SQLException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
System.out.println("souci avec le retrieveMsg");
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("souci avec le retrieveMsgSQL");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
//System.out.println(myUser.getChats().get(myUser.getChatIndexOf(rm)));
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public String askOpenSession(int index) {
|
public String askOpenSession(int index) {
|
||||||
String history="";
|
String history="";
|
||||||
RemoteUser rm = myUser.getRemoteUsersList().get(index);
|
RemoteUser rm = myUser.getRemoteUsersList().get(index);
|
||||||
|
@ -382,6 +405,10 @@ public class Controller {
|
||||||
return history;
|
return history;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void askCloseSession() {
|
public void askCloseSession() {
|
||||||
closeSession(this.activeChat);
|
closeSession(this.activeChat);
|
||||||
this.activeChat = null;
|
this.activeChat = null;
|
||||||
|
@ -431,13 +458,12 @@ public class Controller {
|
||||||
out.println(msg);
|
out.println(msg);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
public String askNewMessage() {
|
public String askNewMessage() {
|
||||||
String message = "";
|
String message = "";
|
||||||
return message;
|
return message;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
/**************************** Autre fonctions **********************************/
|
|
||||||
|
|
||||||
public String [] askUpdateActiveUsers() {
|
public String [] askUpdateActiveUsers() {
|
||||||
String[] pseudotab = new String[myUser.getRemoteUsersList().size()];
|
String[] pseudotab = new String[myUser.getRemoteUsersList().size()];
|
||||||
|
@ -468,75 +494,47 @@ public class Controller {
|
||||||
Historique histoire=new Historique();
|
Historique histoire=new Historique();
|
||||||
|
|
||||||
/** Création des utilisateurs **/
|
/** Création des utilisateurs **/
|
||||||
// REMOTEUSER_1 - THEAU
|
ArrayList<Controller> lstCtr = new ArrayList<Controller>();
|
||||||
|
|
||||||
|
// REMOTE USERS
|
||||||
Controller ctr1 = new Controller(31011,portUDPlistening_remoteUsr1,31021,"Theau",histoire);
|
Controller ctr1 = new Controller(31011,portUDPlistening_remoteUsr1,31021,"Theau",histoire);
|
||||||
// REMOTEUSER_2 - LEONIE
|
lstCtr.add(ctr1);
|
||||||
Controller ctr2 = new Controller(31012,portUDPlistening_remoteUsr2,31022,"Leonie",histoire);
|
Controller ctr2 = new Controller(31012,portUDPlistening_remoteUsr2,31022,"Leonie",histoire);
|
||||||
// REMOTEUSER_3 - ALEXANDRE
|
lstCtr.add(ctr2);
|
||||||
Controller ctr3 = new Controller(31013,portUDPlistening_remoteUsr3,31023,"Alexandre",histoire);
|
Controller ctr3 = new Controller(31013,portUDPlistening_remoteUsr3,31023,"Alexandre",histoire);
|
||||||
|
lstCtr.add(ctr3);
|
||||||
|
|
||||||
// LOCAL USER - AS YOU WANT
|
// LOCAL USER
|
||||||
Controller ctr = new Controller(31014,portUDPlistening_local,31024,histoire);
|
Controller ctr = new Controller(31014,portUDPlistening_local,31024,histoire);
|
||||||
|
lstCtr.add(ctr);
|
||||||
|
|
||||||
/** loop **/
|
|
||||||
Boolean running = ctr1.interfaceRunning || ctr2.interfaceRunning || ctr3.interfaceRunning || ctr.interfaceRunning;
|
/** Loop **/
|
||||||
|
Boolean running = isRunning(lstCtr);
|
||||||
while(running) {
|
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
|
||||||
|
|
||||||
|
/** End - Close thread and socket for every controller**/
|
||||||
|
closeThreads(lstCtr);
|
||||||
/** Close thread and socket **/
|
|
||||||
// REMOTEUSER_1 - THEAU
|
|
||||||
ctr1.close();
|
|
||||||
// REMOTEUSER_2 - LEONIE
|
|
||||||
ctr2.close();
|
|
||||||
// REMOTEUSER_3 - ALEXANDRE
|
|
||||||
ctr3.close();
|
|
||||||
// LOCAL USER
|
|
||||||
ctr.close();
|
|
||||||
|
|
||||||
// AFFICHAGE
|
|
||||||
System.out.println("end program");
|
System.out.println("end program");
|
||||||
JOptionPane.showMessageDialog(null ,"END");
|
JOptionPane.showMessageDialog(null ,"END");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static boolean isRunning(ArrayList<Controller> lstCtr){
|
||||||
|
boolean isRunning=false;
|
||||||
/*** GETTERS ***/
|
for(Controller ctr : lstCtr) {
|
||||||
public LocalUser getMyUser() {
|
isRunning|=ctr.interfaceRunning;
|
||||||
return myUser;
|
|
||||||
}
|
}
|
||||||
public Interface getview() {
|
return isRunning;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** SETTERS ***/
|
static void closeThreads(ArrayList<Controller> lstCtr) {
|
||||||
public void setMyUser(LocalUser myUser) {
|
for(Controller ctr : lstCtr) {
|
||||||
this.myUser = myUser;
|
ctr.close();
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ import model.Msg_Text;
|
||||||
|
|
||||||
public class Historique {
|
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 {
|
try {
|
||||||
Class.forName("java.sql.Driver");
|
Class.forName("java.sql.Driver");
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
|
@ -46,7 +46,7 @@ public class Historique {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
try {
|
try {
|
||||||
nb_changed_rows = stat.executeUpdate("CREATE TABLE chat ( user_IPcode1 INTEGER, user_IPcode2 INTEGER ,Message VARCHAR(450) ,temps VARCHAR(450) )");
|
nb_changed_rows = stat.executeUpdate("CREATE TABLE chat ( user_IPcode1 INTEGER, user_IPcode2 INTEGER ,Message VARCHAR(450) ,temps VARCHAR(450) )");
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
|
@ -54,9 +54,9 @@ public class Historique {
|
||||||
|
|
||||||
}
|
}
|
||||||
System.out.println("coucou");
|
System.out.println("coucou");
|
||||||
|
*/
|
||||||
try {
|
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) {
|
} catch (SQLException e) {
|
||||||
System.out.println("insertion pas établie");
|
System.out.println("insertion pas établie");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -82,106 +82,52 @@ public class Historique {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Message> retrieveMessage(model.LocalUser user1, model.RemoteUser user2) throws SQLException {
|
public ArrayList<Message> retrieveMessage(model.LocalUser usr, model.RemoteUser rmusr) throws SQLException, ClassNotFoundException {
|
||||||
try {
|
ArrayList<Message> messages=new ArrayList<Message>();
|
||||||
|
|
||||||
Class.forName("java.sql.Driver");
|
Class.forName("java.sql.Driver");
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
Connection con=DriverManager.getConnection("jdbc:mysql://srv-bdens.insa-toulouse.fr:3306","tp_servlet_003","povu3Ma2");
|
Connection con=DriverManager.getConnection("jdbc:mysql://srv-bdens.insa-toulouse.fr:3306","tp_servlet_003","povu3Ma2");
|
||||||
Statement stat = null;
|
Statement stat = null;
|
||||||
try {
|
|
||||||
stat = con.createStatement();
|
stat = con.createStatement();
|
||||||
} catch (SQLException e2) {
|
stat.executeUpdate("USE tp_servlet_003");
|
||||||
// TODO Auto-generated catch block
|
/*
|
||||||
e2.printStackTrace();
|
|
||||||
}
|
|
||||||
Statement stat2 = null;
|
Statement stat2 = null;
|
||||||
try {
|
|
||||||
stat2 = con.createStatement();
|
stat2 = con.createStatement();
|
||||||
} catch (SQLException e1) {
|
stat.executeUpdate("USE tp_servlet_003");
|
||||||
// TODO Auto-generated catch block
|
*/
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
int nb_changed_rows = stat.executeUpdate("USE tp_servlet_003");
|
|
||||||
|
|
||||||
try {
|
//int nb_changed_rows;
|
||||||
nb_changed_rows = stat.executeUpdate("USE TABLE chat ( user_IPcode1 INTEGER, user_IPcode2 INTEGER ,Message VARCHAR(450) ,temps VARCHAR(450) )");
|
|
||||||
}catch (Exception e) {
|
String query = "SELECT user_IPcode1 ,Message, temps"
|
||||||
e.printStackTrace();
|
+" 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();
|
||||||
|
|
||||||
}
|
}
|
||||||
nb_changed_rows = stat2.executeUpdate("USE tp_servlet_003");
|
return messages;
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// //MAIN
|
|
||||||
// public Historique() {
|
|
||||||
//
|
|
||||||
// System.out.println("coucoudb");
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
package controller;
|
|
||||||
|
|
||||||
|
|
||||||
public abstract class ListeningThread extends Thread{
|
|
||||||
|
|
||||||
protected Controller controller;
|
|
||||||
|
|
||||||
public ListeningThread(String s,Controller controller) {
|
|
||||||
super(s);
|
|
||||||
this.controller = controller;
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract void run();
|
|
||||||
|
|
||||||
public abstract void close();
|
|
||||||
}
|
|
|
@ -11,8 +11,9 @@ import java.net.Socket;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class ListeningThreadTCPChat extends ListeningThread{
|
public class TListeningTCPChat extends Thread{
|
||||||
|
|
||||||
|
protected Controller controller;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
|
|
||||||
/* CONSTRUCTOR OF ListeningThreadTCPConnection
|
/* CONSTRUCTOR OF ListeningThreadTCPConnection
|
||||||
|
@ -23,10 +24,9 @@ public class ListeningThreadTCPChat extends ListeningThread{
|
||||||
* <p>
|
* <p>
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public ListeningThreadTCPChat(String s,Controller controller,Socket socket) {
|
public TListeningTCPChat(String s,Controller controller,Socket socket) {
|
||||||
super(s,controller);
|
this.controller = controller;
|
||||||
this.socket=socket;
|
this.socket=socket;
|
||||||
// TODO OpenSession si ouverture exterieur
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* run
|
/* run
|
|
@ -4,10 +4,12 @@ import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
public class ListeningThreadTCPConnection extends ListeningThread{
|
public class TListeningTCPConnection extends Thread{
|
||||||
|
|
||||||
|
protected Controller controller;
|
||||||
private Socket socket_tcp=null;
|
private Socket socket_tcp=null;
|
||||||
private int nbChat = 0;
|
private int nbChat = 0;
|
||||||
|
|
||||||
/* CONSTRUCTOR OF ListeningThreadTCPConnection
|
/* CONSTRUCTOR OF ListeningThreadTCPConnection
|
||||||
* @parametres
|
* @parametres
|
||||||
* @param s : String => nom du thread
|
* @param s : String => nom du thread
|
||||||
|
@ -16,15 +18,14 @@ public class ListeningThreadTCPConnection extends ListeningThread{
|
||||||
* <p>
|
* <p>
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public ListeningThreadTCPConnection(String s,Controller controller) {
|
public TListeningTCPConnection(String s,Controller controller) {
|
||||||
super(s,controller);
|
this.controller = 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();
|
||||||
this.nbChat++;
|
this.nbChat++;
|
||||||
ListeningThreadTCPChat threadtcpchat = new ListeningThreadTCPChat("Chat_of_"+controller.myUser.getPseudo()+"_"+nbChat,controller,socket_tcp);
|
TListeningTCPChat threadtcpchat = new TListeningTCPChat("Chat_of_"+controller.myUser.getPseudo()+"_"+nbChat,controller,socket_tcp);
|
||||||
threadtcpchat.start();
|
threadtcpchat.start();
|
||||||
threadtcpchat.interrupt();
|
threadtcpchat.interrupt();
|
||||||
|
|
|
@ -8,8 +8,9 @@ import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
public class ListeningThreadUDP extends ListeningThread{
|
public class TListeningUDP extends Thread{
|
||||||
|
|
||||||
|
protected Controller controller;
|
||||||
private DatagramSocket dgramSocket = null;
|
private DatagramSocket dgramSocket = null;
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,13 +24,14 @@ 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,Controller controller) {
|
public TListeningUDP(String s,Controller controller) {
|
||||||
super(s,controller);
|
this.controller = controller;
|
||||||
try {
|
try {
|
||||||
this.dgramSocket = new DatagramSocket(this.controller.myUser.getPortUDPlistening(),this.controller.myUser.getAddIP());
|
this.dgramSocket = new DatagramSocket(this.controller.myUser.getPortUDPlistening(),this.controller.myUser.getAddIP());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* run
|
/* run
|
||||||
|
@ -60,19 +62,18 @@ public class ListeningThreadUDP extends ListeningThread{
|
||||||
String receiveMsg = new String(buffer);
|
String receiveMsg = new String(buffer);
|
||||||
String [] tabMsg = receiveMsg.split(":");
|
String [] tabMsg = receiveMsg.split(":");
|
||||||
|
|
||||||
|
|
||||||
// si demande d'information d'un nouvel utilisateur
|
// si demande d'information d'un nouvel utilisateur
|
||||||
if(tabMsg.length==3) {
|
if(tabMsg.length==3) {
|
||||||
InetAddress itsIP = null;
|
InetAddress itsIP = null;
|
||||||
try {
|
try {
|
||||||
itsIP = InetAddress.getByName(tabMsg[0].split("/")[1]); // On récupère l'adresse IP de l'utilisateur distant
|
itsIP = InetAddress.getByName(tabMsg[0]); // On récupère l'adresse IP de l'utilisateur distant
|
||||||
} catch (UnknownHostException e1) {
|
} catch (UnknownHostException e1) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
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 = controller.myUser.getAddIP().toString()+":"+controller.myUser.getPortTCP()+":"+controller.myUser.getPseudo()+":test";
|
String toSend = controller.myUser.getAddIP().getHostAddress()+":"+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 +87,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
|
||||||
controller.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0].split("/")[1]),Integer.parseInt(tabMsg[1]),tabMsg[2]);
|
controller.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0]),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();
|
|
@ -26,7 +26,7 @@ public abstract class User {
|
||||||
this.pseudo = pseudo;
|
this.pseudo = pseudo;
|
||||||
this.addIP=addIP;
|
this.addIP=addIP;
|
||||||
this.portTCP=portTCP;
|
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
|
@Override
|
||||||
|
@ -68,12 +77,30 @@ public abstract class User {
|
||||||
if (getClass() != obj.getClass())
|
if (getClass() != obj.getClass())
|
||||||
return false;
|
return false;
|
||||||
User other = (User) obj;
|
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)
|
if (portTCP != other.portTCP)
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User [pseudo=" + pseudo + ", addIP=" + addIP.getHostAddress() + ", portTCP=" + portTCP + ", IPcode=" + IPcode + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue