Confronta commit
20 commit
Autore | SHA1 | Data | |
---|---|---|---|
82f3cbd91f | |||
42f8d92b9d | |||
0c7bcb7b51 | |||
a33aeb87ed | |||
6917a454e7 | |||
a9fa3e3ef4 | |||
0ca69de2c3 | |||
d8557a852f | |||
772c039b51 | |||
|
00b3ad3038 | ||
|
94031cc356 | ||
9633bf0f5d | |||
|
72d6f53020 | ||
|
fb7c74480c | ||
2d48a2b0d4 | |||
3ba80fc0a7 | |||
|
31d1638917 | ||
|
21780d4c4c | ||
|
26243f1363 | ||
09dce244b5 |
39 ha cambiato i file con 750 aggiunte e 279 eliminazioni
|
@ -1,6 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="lib" path="C:/Program Files (x86)/MySQL/Connector J 8.0/mysql-connector-java-8.0.22.jar"/>
|
||||
<classpathentry kind="lib" path="C:/Program Files/tyrus-standalone-client-1.9.jar"/>
|
||||
<classpathentry kind="lib" path="C:/Program Files/javax.websocket-client-api-1.1.jar"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
3
Application/Clavardage/bin/.gitignore
esterno
3
Application/Clavardage/bin/.gitignore
esterno
|
@ -1,3 +1,2 @@
|
|||
/websocket/
|
||||
/controller/
|
||||
/model/
|
||||
/view/
|
||||
|
|
BIN
Application/Clavardage/bin/model/Chat.class
File normale
BIN
Application/Clavardage/bin/model/Chat.class
File normale
File binario non mostrato.
BIN
Application/Clavardage/bin/model/LocalUser.class
File normale
BIN
Application/Clavardage/bin/model/LocalUser.class
File normale
File binario non mostrato.
BIN
Application/Clavardage/bin/model/Message.class
File normale
BIN
Application/Clavardage/bin/model/Message.class
File normale
File binario non mostrato.
BIN
Application/Clavardage/bin/model/Msg_Text.class
File normale
BIN
Application/Clavardage/bin/model/Msg_Text.class
File normale
File binario non mostrato.
BIN
Application/Clavardage/bin/model/RemoteUser.class
File normale
BIN
Application/Clavardage/bin/model/RemoteUser.class
File normale
File binario non mostrato.
BIN
Application/Clavardage/bin/model/User.class
File normale
BIN
Application/Clavardage/bin/model/User.class
File normale
File binario non mostrato.
BIN
Application/Clavardage/bin/view/Interface$1.class
File normale
BIN
Application/Clavardage/bin/view/Interface$1.class
File normale
File binario non mostrato.
BIN
Application/Clavardage/bin/view/Interface$2.class
File normale
BIN
Application/Clavardage/bin/view/Interface$2.class
File normale
File binario non mostrato.
BIN
Application/Clavardage/bin/view/Interface$3.class
File normale
BIN
Application/Clavardage/bin/view/Interface$3.class
File normale
File binario non mostrato.
BIN
Application/Clavardage/bin/view/Interface.class
File normale
BIN
Application/Clavardage/bin/view/Interface.class
File normale
File binario non mostrato.
|
@ -8,6 +8,7 @@ import java.net.DatagramPacket;
|
|||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.sql.SQLException;
|
||||
import java.text.DateFormat;
|
||||
|
@ -17,6 +18,7 @@ import java.util.Calendar;
|
|||
import java.util.Date;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
//import javax.websocket.DeploymentException;
|
||||
|
||||
import model.Chat;
|
||||
import model.LocalUser;
|
||||
|
@ -24,33 +26,36 @@ import model.Message;
|
|||
import model.Msg_Text;
|
||||
import model.RemoteUser;
|
||||
import view.Interface;
|
||||
//import websocket.Appel;
|
||||
|
||||
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_usr1 = 31001;
|
||||
final static int portUDPlistening_remoteUsr1 = 31001;
|
||||
final static int portUDPlistening_remoteUsr2 = 31002;
|
||||
final static int portUDPlistening_remoteUsr3 = 31003;
|
||||
final static int [] tabBroadcast = {portUDPlistening_usr1,portUDPlistening_remoteUsr2,portUDPlistening_remoteUsr3};
|
||||
final static int portUDPlistening_local = 31004;
|
||||
final static int [] tabBroadcast = {portUDPlistening_remoteUsr1,portUDPlistening_remoteUsr2,portUDPlistening_remoteUsr3,portUDPlistening_local};
|
||||
|
||||
public Boolean interfaceRunning = false;
|
||||
/*** 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
|
||||
* @descrition
|
||||
* <p>
|
||||
* On récupère l'adresse de la machine, on demande un pseudo à l'utilisateur que l'on vérifie
|
||||
|
@ -60,9 +65,10 @@ 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,Historique histoire) throws IOException {
|
||||
this.histoire= histoire;
|
||||
|
||||
// Récupération de l'adresse IP local
|
||||
InetAddress addIP = null;
|
||||
try
|
||||
{
|
||||
|
@ -71,22 +77,26 @@ public class Controller {
|
|||
catch(UnknownHostException e) {
|
||||
JOptionPane.showMessageDialog(null ,"Could not find local address!");
|
||||
}
|
||||
|
||||
// Création de l'utilisateur
|
||||
this.myUser = new LocalUser("Unknown",addIP,portUDPsend,portUDPlistening,portTCP);
|
||||
|
||||
try {
|
||||
this.myUser.setPseudo(this.initPseudo());
|
||||
this.myUser.setPseudo(this.initPseudo()); // Initialisation du pseudo manuel
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
|
||||
// Création des threads d'écoutes
|
||||
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
|
||||
notify_remote_users();
|
||||
|
||||
// Création de l'interface
|
||||
interfaceRunning =true;
|
||||
view=Interface.createAndShowGUI(this);
|
||||
}
|
||||
|
@ -106,28 +116,38 @@ public class Controller {
|
|||
* - notification aux autres utilisateurs actifs
|
||||
* </p>
|
||||
*/
|
||||
private Controller(int portUDPsend,int portUDPlistening,int portTCP,String pseudo,Historique histoire) {
|
||||
private Controller(int portUDPsend,int portUDPlistening,int portTCP,String pseudo,Historique histoire) throws IOException{
|
||||
|
||||
this.histoire=histoire;
|
||||
|
||||
// Récupération de l'adresse IP local
|
||||
InetAddress addIP = null;
|
||||
try
|
||||
{
|
||||
addIP = InetAddress.getLocalHost();
|
||||
|
||||
}
|
||||
catch(UnknownHostException e) {
|
||||
JOptionPane.showMessageDialog(null ,"Could not find local address!");
|
||||
}
|
||||
|
||||
// Création de l'utilisateur
|
||||
this.myUser = new LocalUser(pseudo,addIP,portUDPsend,portUDPlistening,portTCP);
|
||||
try {
|
||||
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();
|
||||
|
||||
|
||||
// Notification des utilisateurs distants
|
||||
notify_remote_users();
|
||||
|
||||
// Création de l'interface
|
||||
interfaceRunning =true;
|
||||
view=Interface.createAndShowGUI(this);
|
||||
}
|
||||
|
@ -137,18 +157,9 @@ public class Controller {
|
|||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
notify_remote_users();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*** GETTERS ***/
|
||||
public LocalUser getMyUser() {
|
||||
return myUser;
|
||||
|
@ -156,10 +167,10 @@ public class Controller {
|
|||
public Interface getview() {
|
||||
return view;
|
||||
}
|
||||
public ListeningThreadUDP getUdp_connect_thread() {
|
||||
public TListeningUDP getUdp_connect_thread() {
|
||||
return udp_connect_thread;
|
||||
}
|
||||
public ListeningThreadTCPConnection getTcp_connect_thread() {
|
||||
public TListeningTCPConnection getTcp_connect_thread() {
|
||||
return tcp_connect_thread;
|
||||
}
|
||||
public Historique getHistory() {
|
||||
|
@ -173,18 +184,19 @@ public class Controller {
|
|||
public void setview(Interface view) {
|
||||
this.view = view;
|
||||
}
|
||||
public void setUdp_connect_thread(ListeningThreadUDP udp_connect_thread) {
|
||||
public void setUdp_connect_thread(TListeningUDP udp_connect_thread) {
|
||||
this.udp_connect_thread = udp_connect_thread;
|
||||
}
|
||||
public void setTcp_connect_thread(ListeningThreadTCPConnection tcp_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) **********************************/
|
||||
|
||||
/**
|
||||
/** initPseudo
|
||||
* <p>
|
||||
* Demande à l'utilisateur de rentrer un pseudo et valide de ce dernier en demandant aux
|
||||
* utilisateurs distants leurs informations
|
||||
|
@ -203,7 +215,7 @@ public class Controller {
|
|||
return tmpPseudo;
|
||||
}
|
||||
|
||||
/**
|
||||
/** changePseudo
|
||||
* <p>
|
||||
* Demande à l'utilisateur de rentrer un pseudo et valide de ce dernier en demandant aux
|
||||
* utilisateurs distants leurs informations.
|
||||
|
@ -223,12 +235,11 @@ public class Controller {
|
|||
this.myUser.setPseudo(tmpPseudo);
|
||||
JOptionPane.showMessageDialog(null ,"Your new nickname : " + tmpPseudo + " is valid !");
|
||||
this.notify_remote_users();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/**
|
||||
|
||||
|
||||
/** validatePseudo
|
||||
* <p>
|
||||
* *tmpPseudo : String => Le pseudo à valider
|
||||
*</p><p>
|
||||
|
@ -242,29 +253,18 @@ public class Controller {
|
|||
* </p>
|
||||
*/
|
||||
public Boolean validatePseudo(String tmpPseudo) throws IOException {
|
||||
Boolean valid = true;
|
||||
|
||||
|
||||
// Call broadcast
|
||||
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
|
||||
//System.out.println(this.myUser.getPortUDPsend());
|
||||
DatagramSocket dgramSocket = new DatagramSocket(this.myUser.getPortUDPsend(),this.myUser.getAddIP());
|
||||
byte[] buffer = new byte[256];
|
||||
|
||||
// Création du message à envoyer
|
||||
String toSend = this.myUser.getAddIP()+":"+this.myUser.getPortUDPsend()+":info";
|
||||
String toSend = this.myUser.getAddIP().getHostAddress()+":"+this.myUser.getPortUDPsend()+":info";
|
||||
|
||||
// Send to other active user (simulation of broadcast)
|
||||
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(),broadcastIP, tabBroadcast[i]);
|
||||
dgramSocket.send(outPacket);
|
||||
}
|
||||
}
|
||||
// Broadcast du message
|
||||
broadcast(dgramSocket,toSend);
|
||||
|
||||
/*** For 5 seconds wait for answer : validate pseudo & add to userlist ***/
|
||||
Boolean valid = true;
|
||||
byte[] buffer = new byte[256];
|
||||
DatagramPacket inPacket;
|
||||
String response = null;
|
||||
String[] tabresponse= new String [4];
|
||||
|
@ -287,60 +287,68 @@ public class Controller {
|
|||
|
||||
buffer = inPacket.getData();
|
||||
response = new String(buffer);
|
||||
|
||||
if(arecu) {
|
||||
// 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]);
|
||||
valid= (tmpPseudo.compareTo(tabresponse[2])!=0); // On regarde la différence entre notre pseudo et le pseudo reçu
|
||||
|
||||
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();
|
||||
}
|
||||
dgramSocket.close();
|
||||
|
||||
|
||||
dgramSocket.close();
|
||||
if(!valid) {
|
||||
JOptionPane.showMessageDialog(null ,"Nickname : "+tmpPseudo +" is taken !");
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
/***broadcast
|
||||
*
|
||||
* @param dgramSocket
|
||||
* @param toSend
|
||||
* @param broadcastIP
|
||||
* @throws IOException
|
||||
* <p>
|
||||
* Simulation of broadcast with port given (tabBroadcast)
|
||||
* <p>
|
||||
*/
|
||||
public void broadcast(DatagramSocket dgramSocket,String toSend) throws IOException {
|
||||
InetAddress broadcastIP = InetAddress.getLocalHost();
|
||||
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(),broadcastIP, tabBroadcast[i]);
|
||||
dgramSocket.send(outPacket);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** notify_remote_users
|
||||
* <p>
|
||||
* En utilisant le port UDP d'envoi, on envoie en broadcast les informations nous concernant
|
||||
* </p>
|
||||
* @throws IOException
|
||||
*/
|
||||
public void notify_remote_users() {
|
||||
public void notify_remote_users() throws IOException {
|
||||
DatagramSocket dgramSocket= null;
|
||||
try {
|
||||
dgramSocket= new DatagramSocket(this.myUser.getPortUDPsend(),this.myUser.getAddIP());
|
||||
} 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();
|
||||
}
|
||||
|
||||
|
||||
/**************************** Gestion des sessions **********************************/
|
||||
|
||||
public Chat openSession(RemoteUser rm) {
|
||||
Chat c = myUser.addChats(rm); // Create chat and add it to myUser
|
||||
|
||||
|
@ -359,17 +367,21 @@ public class Controller {
|
|||
|
||||
|
||||
/*** recup history and put it in model ***/
|
||||
/*
|
||||
try {
|
||||
System.out.println(this.getHistory().retrieveMessage(getMyUser(), c.getRemoteUser()));
|
||||
String history = 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();
|
||||
}
|
||||
*/
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String askOpenSession(int index) {
|
||||
String history="";
|
||||
RemoteUser rm = myUser.getRemoteUsersList().get(index);
|
||||
|
@ -382,7 +394,7 @@ public class Controller {
|
|||
}
|
||||
else { // else create it
|
||||
c=openSession(rm);
|
||||
JOptionPane.showMessageDialog(null ,"New session with "+c.getRemoteUser().getPseudo());
|
||||
JOptionPane.showMessageDialog(null ,"New session with "+rm.getPseudo());
|
||||
}
|
||||
|
||||
// Look for history
|
||||
|
@ -396,6 +408,10 @@ public class Controller {
|
|||
return history;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void askCloseSession() {
|
||||
closeSession(this.activeChat);
|
||||
this.activeChat = null;
|
||||
|
@ -406,6 +422,8 @@ public class Controller {
|
|||
this.myUser.closeChat(c);
|
||||
}
|
||||
|
||||
/**************************** Gestion des messages **********************************/
|
||||
|
||||
public void askToSend(String textMessage){
|
||||
sendMessage(new Msg_Text(myUser,textMessage), this.activeChat);
|
||||
}
|
||||
|
@ -425,7 +443,7 @@ public class Controller {
|
|||
// Sauvegarde dans la base de données
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy MM dd HH mm ss");
|
||||
Date date=new Date();
|
||||
//this.getHistory().saveMessage(getMyUser(), c.getRemoteUser(),message ,dateFormat.format(date));
|
||||
this.getHistory().saveMessage(getMyUser(), c.getRemoteUser(),message ,dateFormat.format(date));
|
||||
|
||||
// Send message
|
||||
out.println(message);
|
||||
|
@ -443,12 +461,13 @@ public class Controller {
|
|||
out.println(msg);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
public String askNewMessage() {
|
||||
String message = "";
|
||||
return message;
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
public String [] askUpdateActiveUsers() {
|
||||
String[] pseudotab = new String[myUser.getRemoteUsersList().size()];
|
||||
int size = myUser.getRemoteUsersList().size();
|
||||
|
@ -477,120 +496,55 @@ public class Controller {
|
|||
|
||||
Historique histoire=new Historique();
|
||||
|
||||
/************************* INIT *******************************/
|
||||
/** Création des utilisateurs **/
|
||||
// LOCAL USER
|
||||
Controller ctr1 = new Controller(31011,portUDPlistening_usr1,31021,"Theau",histoire);
|
||||
|
||||
// REMOTEUSER_1 - MIKE
|
||||
Controller ctr2 = new Controller(31012,portUDPlistening_remoteUsr2,31022,"Leonie",histoire);
|
||||
|
||||
// REMOTEUSER_2 - ALICE
|
||||
Controller ctr3 = new Controller(31013,portUDPlistening_remoteUsr3,31023,"Alexandre",histoire);
|
||||
|
||||
|
||||
|
||||
|
||||
/*********************** TEST AREA ********************/
|
||||
/** Simulation of a session 1 message **/
|
||||
/*
|
||||
// AFFICHAGE REMOTE USER CHOISIE
|
||||
System.out.println("("+ctr1.myUser.getPseudo()+" ) OPEN SESSION WITH "+ctr1.myUser.getRemoteUsersList().get(0).getPseudo());
|
||||
// SELECTION DE L UTILISATEUR
|
||||
RemoteUser rm0 = ctr1.myUser.getRemoteUsersList().get(0);
|
||||
// OPEN SESSION
|
||||
ctr1.openSession(rm0);
|
||||
// RECUPERATION DE LA CONVERSATION
|
||||
Chat chatwithrm0 = ctr1.myUser.getChats().get(ctr1.myUser.getChatIndexOf(rm0));
|
||||
// SEND MESSAGE
|
||||
ctr1.sendMessage(new Msg_Text(ctr1.myUser.getAddIP(),"test"), chatwithrm0);
|
||||
// CLOSE SESSION
|
||||
ctr1.closeSession(chatwithrm0);
|
||||
*/
|
||||
|
||||
/** Unused function **/
|
||||
// MANUAL SELECTION OF ACTIVE USER
|
||||
//ctr1.selectActiveUser();
|
||||
// CHANGE USER NICKNAME
|
||||
//ctr1.changePseudo();
|
||||
|
||||
|
||||
|
||||
/************************* LOOP *******************************/
|
||||
ArrayList<Controller> lstCtr = new ArrayList<Controller>();
|
||||
|
||||
|
||||
// REMOTE USERS
|
||||
/*
|
||||
Controller ctr1 = new Controller(31011,portUDPlistening_remoteUsr1,31021,"Theau",histoire);
|
||||
lstCtr.add(ctr1);
|
||||
|
||||
/** loop **/
|
||||
Boolean running = ctr1.interfaceRunning || ctr2.interfaceRunning || ctr3.interfaceRunning;
|
||||
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);
|
||||
|
||||
//Appel app=new Appel();
|
||||
//app.test();
|
||||
|
||||
/** Loop **/
|
||||
Boolean running = isRunning(lstCtr);
|
||||
while(running) {
|
||||
running = ctr1.interfaceRunning || ctr2.interfaceRunning || ctr3.interfaceRunning;
|
||||
running = isRunning(lstCtr);
|
||||
}
|
||||
|
||||
System.out.println("Fin de la boucle");
|
||||
System.out.println("Fin de la boucle");//TOREMOVE
|
||||
|
||||
|
||||
|
||||
/************************ END *****************************/
|
||||
|
||||
/** Close thread and socket **/
|
||||
// REMOTEUSER_1 - MIKE
|
||||
ctr2.close();
|
||||
|
||||
// REMOTEUSER_2 - ALICE
|
||||
ctr3.close();
|
||||
|
||||
// LOCAL USER
|
||||
ctr1.close();
|
||||
|
||||
// AFFICHAGE
|
||||
/** End - Close thread and socket for every controller**/
|
||||
closeThreads(lstCtr);
|
||||
System.out.println("end program");
|
||||
JOptionPane.showMessageDialog(null ,"END");
|
||||
}
|
||||
|
||||
static boolean isRunning(ArrayList<Controller> lstCtr){
|
||||
boolean isRunning=false;
|
||||
for(Controller ctr : lstCtr) {
|
||||
isRunning|=ctr.interfaceRunning;
|
||||
}
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
static void closeThreads(ArrayList<Controller> lstCtr) {
|
||||
for(Controller ctr : lstCtr) {
|
||||
ctr.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/********************* Fonction debug console => A mettre dans l'interface ******************************************/
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Affichage de la liste d'utilisateurs actifs avec leurs index dans la liste
|
||||
* </p>
|
||||
*/
|
||||
public void printRemoteUserList() {
|
||||
System.out.println("Internal list of active remote users:");
|
||||
|
||||
for(int i=0; i<this.myUser.getRemoteUsersList().size(); i++) {
|
||||
System.out.println("- ("+i+") Username: " + this.myUser.getRemoteUsersList().get(i).getPseudo());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Laisse l'utilisateur choisir parmis la liste d'utilisateurs actifs celui avec lequel il souhaite échanger via un chat
|
||||
*</p>
|
||||
*/
|
||||
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(this.myUser.getChatIndexOf(this.myUser.getRemoteUsersList().get(index))==-1){
|
||||
JOptionPane.showMessageDialog(null ,"User "+this.myUser.getRemoteUsersList().get(index).getPseudo()+" is already in chat with you");
|
||||
}
|
||||
else {
|
||||
this.openSession(myUser.getRemoteUsersList().get(index));
|
||||
}
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null ,"Wrong index (no active at index number "+index+" )");
|
||||
this.selectActiveUser();
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************************************/
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -8,13 +8,17 @@ import java.sql.ResultSet;
|
|||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
|
||||
import javax.sql.rowset.JdbcRowSet;
|
||||
|
||||
import model.Message;
|
||||
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) {
|
||||
|
@ -42,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) {
|
||||
|
@ -50,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();
|
||||
|
@ -78,82 +82,52 @@ public class Historique {
|
|||
|
||||
}
|
||||
|
||||
public String 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();
|
||||
}
|
||||
Connection con=DriverManager.getConnection("jdbc:mysql://srv-bdens.insa-toulouse.fr:3306","tp_servlet_003","povu3Ma2");
|
||||
Statement stat=con.createStatement();
|
||||
Statement stat2=con.createStatement();
|
||||
int nb_changed_rows = stat.executeUpdate("USE tp_servlet_003");
|
||||
|
||||
try {
|
||||
nb_changed_rows = stat.executeUpdate("CREATE 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();
|
||||
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(" ");
|
||||
System.out.println(RSSent.getString(4));
|
||||
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) {
|
||||
message=message+"\n"+RSReceived.getString(3);
|
||||
message=message+"\n"+RSSent.getString(3);
|
||||
}else {
|
||||
message=message+"\n"+RSSent.getString(3);
|
||||
message=message+"\n"+RSReceived.getString(3);
|
||||
}
|
||||
encore1=RSSent.next();
|
||||
encore2=RSReceived.next();
|
||||
|
||||
}
|
||||
RSSent.close();
|
||||
RSReceived.close();
|
||||
stat.close();
|
||||
stat2.close();
|
||||
con.close();
|
||||
public ArrayList<Message> retrieveMessage(model.LocalUser usr, model.RemoteUser rmusr) throws SQLException, ClassNotFoundException {
|
||||
ArrayList<Message> messages=new ArrayList<Message>();
|
||||
|
||||
return 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;
|
||||
stat = con.createStatement();
|
||||
stat.executeUpdate("USE tp_servlet_003");
|
||||
/*
|
||||
Statement stat2 = null;
|
||||
stat2 = con.createStatement();
|
||||
stat.executeUpdate("USE tp_servlet_003");
|
||||
*/
|
||||
|
||||
//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");
|
||||
//
|
||||
//
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
|
141
Application/Clavardage/src/controller/TListeningTCPChat.java
File normale
141
Application/Clavardage/src/controller/TListeningTCPChat.java
File normale
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
125
Application/Clavardage/src/controller/TListeningUDP.java
File normale
125
Application/Clavardage/src/controller/TListeningUDP.java
File normale
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -49,6 +49,9 @@ public class Chat {
|
|||
public void addMessage(Message msg) {
|
||||
this.messages.add(msg);
|
||||
}
|
||||
public void addListMessage( ArrayList<Message> msg) {
|
||||
this.messages=msg;
|
||||
}
|
||||
public void activate() {
|
||||
this.active = true;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,10 @@ public abstract class Message {
|
|||
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
||||
this.date = dateFormat.format(new Date());
|
||||
}
|
||||
public Message(User autor, String date) {
|
||||
this.autor = autor;
|
||||
this.date = date;
|
||||
}
|
||||
/*** GETTERS ***/
|
||||
public String getDate() {
|
||||
return date;
|
||||
|
@ -36,6 +40,9 @@ public abstract class Message {
|
|||
public void setAutorIP(User autor) {
|
||||
this.autor = autor;
|
||||
}
|
||||
public void setDate(String date) {
|
||||
this.date=date;
|
||||
}
|
||||
|
||||
|
||||
public abstract Object getMessage();
|
||||
|
|
|
@ -11,7 +11,11 @@ public class Msg_Text extends Message{
|
|||
super(autor);
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public Msg_Text(User autor,String date, String text) {
|
||||
super(autor,date);
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return this.text;
|
||||
|
|
|
@ -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 + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
39
Application/Clavardage/src/websocket/Appel.java
File normale
39
Application/Clavardage/src/websocket/Appel.java
File normale
|
@ -0,0 +1,39 @@
|
|||
package websocket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import javax.websocket.ContainerProvider;
|
||||
import javax.websocket.DeploymentException;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.WebSocketContainer;
|
||||
|
||||
|
||||
public class Appel {
|
||||
|
||||
private WebSocketContainer container;
|
||||
private Client client;
|
||||
|
||||
public void test() {
|
||||
this.container= ContainerProvider.getWebSocketContainer();
|
||||
this.client=new Client();
|
||||
|
||||
Session session=null;
|
||||
try {
|
||||
session=this.container.connectToServer(this.client, new URI ("ws://localhost:8082/WebSocket2/hello"));
|
||||
} catch (DeploymentException | IOException | URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("pb avec le connect");
|
||||
}
|
||||
try {
|
||||
this.client.onOpen(session, null);
|
||||
this.client.sendMessage("hello from client");
|
||||
} catch (IOException e) {
|
||||
System.out.println("pb avec le sendMessage");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
5
Application/Clavardage/src/websocket/Centralized.java
File normale
5
Application/Clavardage/src/websocket/Centralized.java
File normale
|
@ -0,0 +1,5 @@
|
|||
package websocket;
|
||||
|
||||
public class Centralized {
|
||||
|
||||
}
|
26
Application/Clavardage/src/websocket/Client.java
File normale
26
Application/Clavardage/src/websocket/Client.java
File normale
|
@ -0,0 +1,26 @@
|
|||
package websocket;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.websocket.*;
|
||||
|
||||
public class Client extends Endpoint{
|
||||
private Session session;
|
||||
|
||||
@Override
|
||||
public void onOpen (Session session, EndpointConfig config) {
|
||||
this.session=session;
|
||||
this.session.addMessageHandler(new MessageHandler.Whole<String>() {
|
||||
@Override
|
||||
public void onMessage (String msg) {
|
||||
System.out.println("msg recu:"+msg);
|
||||
};}
|
||||
);
|
||||
}
|
||||
|
||||
public void sendMessage(String msg) throws IOException {
|
||||
this.session.getBasicRemote().sendText(msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
File binario non mostrato.
Prima Larghezza: | Altezza: | Dimensione: 59 KiB |
BIN
Executable/Alexandre_31023.jar
File normale
BIN
Executable/Alexandre_31023.jar
File normale
File binario non mostrato.
BIN
Executable/Client_31024.jar
File normale
BIN
Executable/Client_31024.jar
File normale
File binario non mostrato.
BIN
Executable/Leonie_31022.jar
File normale
BIN
Executable/Leonie_31022.jar
File normale
File binario non mostrato.
BIN
Executable/Theau_31021.jar
File normale
BIN
Executable/Theau_31021.jar
File normale
File binario non mostrato.
BIN
Final_Composite_Structure_Diagram.jpg
File normale
BIN
Final_Composite_Structure_Diagram.jpg
File normale
File binario non mostrato.
Dopo Larghezza: | Altezza: | Dimensione: 31 KiB |
BIN
Final_Sequence_Diagram.png
File normale
BIN
Final_Sequence_Diagram.png
File normale
File binario non mostrato.
Dopo Larghezza: | Altezza: | Dimensione: 213 KiB |
BIN
Final_State_Machine_Diagram.jpg
File normale
BIN
Final_State_Machine_Diagram.jpg
File normale
File binario non mostrato.
Dopo Larghezza: | Altezza: | Dimensione: 61 KiB |
47
README.md
47
README.md
|
@ -4,4 +4,49 @@ SYSTEME DE CLAVARDAGE DISTRIBUE INTERACTIF MULTI-UTILISATEUR TEMPS REEL
|
|||
4IR A2
|
||||
Alexandre Gonzalvez
|
||||
Théau Giraud
|
||||
Léonie Gallois
|
||||
Léonie Gallois
|
||||
|
||||
|
||||
|
||||
|
||||
*************Manuel d’installation & d’utilisation de l’application*************
|
||||
|
||||
Ce manuel est aussi disponible dans le rapport, notamment avec des images pour la partie utilisation. Nous vous conseillons aussi vivement de regarder la vidéo explicative se trouvant dans le même repository que ce fichier.
|
||||
|
||||
Etape 1: Télécharger le projet via GitEtud
|
||||
|
||||
Tout d’abord, allez sur le GitEtud de L’INSA Toulouse à cette adresse: https://git.etud.insa-toulouse.fr/gallois/Clavardage. Notre projet s’appelle Clavardage, vous pouvez effectuer un pull à partir de la branche master. (D’autres versions du projet sont également disponibles, par exemple la branche v1 ne nécessite aucune installation supplémentaire mais ne gère pas la récupération de l’historique).
|
||||
|
||||
Etape 2: Télécharger MySQL Workbench
|
||||
|
||||
Ensuite, pour faire le lien avec notre base de données, il vous faudra installer MySQL Workbench et ConnectorJ. MySQL est gratuit et répond bien à nos besoins pour la base de données. Voici un lien vers la page de téléchargement: https://dev.mysql.com/downloads/workbench/.
|
||||
|
||||
Vous remarquerez que la page web suivante vous demande un login. Il n’est pas obligatoire. Vous pouvez directement cliquer sur “No thanks, just start my download” en bas de la page et télécharger MySQL sans compte. Dans le setup MySQL il vous faudra télécharger seulement le Workbench et le ConnectorJ.
|
||||
|
||||
Il n’y a ensuite pas de manipulations à faire avec MySQL. La connexion avec la base de données a déjà été mise en place dans le code de l’application et du côté du serveur.
|
||||
|
||||
Etape 3: Connexion au VPN de L’INSA
|
||||
|
||||
Si vous utilisez une machine se situant à l’INSA vous pouvez passer cette étape. Sinon, il vous faudra vous connecter au réseau de l’INSA en utilisant un VPN pour faire marcher la base de données qui utilise ce réseau. Voici le lien vers la page du CSN expliquant sa mise en place: http://csn.insa-toulouse.fr/fr/services/services-transverses/connexion-par-vpn.html.
|
||||
|
||||
Etape 4: Lancement et utilisation de l’application
|
||||
|
||||
Vérifiez bien d’être connecté au VPN avant de tester l’application. Comme dans la vidéo explicative (que nous vous conseillons et qui est elle aussi sur le git), vous avez juste à ouvrir le fichier Client.jar dans le répertoire Executable ainsi qu’un ou plusieurs des .jar utilisateurs distants.
|
||||
|
||||
Quand vous ouvrez l’application, une fenêtre apparaît vous demandant votre Pseudo. Vous recevrez un message de confirmation avec votre Pseudo affiché ou un message d’erreur s’il n’est pas valide.
|
||||
|
||||
L’interface s’ouvrira ensuite. Vous pourrez changer votre Pseudo dans la barre de texte à côté de “Change nickname:”. Les mêmes messages de confirmation/d’erreur apparaîtront.
|
||||
|
||||
Ensuite, pour commencer un chat avec un des autres utilisateurs, cliquez sur la flèche du menu déroulant répertoriant leurs noms. Double-cliquez sur un utilisateur et un message de confirmation s’ouvrira.
|
||||
|
||||
Une fenêtre de chat vide s’ouvre. Vous pouvez y taper vos messages et les envoyer.
|
||||
|
||||
Pour visualiser l’arrivée des messages, établissez la connexion en faisant la même manip sur l’interface de l’utilisateur distant avec lequel vous communiquez. Vous verrez ici qu’un chat existant déjà, une fenêtre s’ouvre en disant que l’on va chercher les messages envoyés qui ont été stockés dans la base de données.
|
||||
|
||||
Les messages envoyés sont récupérés et sont affichés dans la fenêtre chat de l’utilisateur distant. Vous pouvez aussi changer encore une fois les pseudos et voir que les messages envoyés après le changement de pseudo sont marqués avec celui-ci.
|
||||
|
||||
Pour fermer le chat, cliquez sur le bouton “Close session”.
|
||||
|
||||
Vous pouvez aussi fermer les applications entièrement en cliquant sur la croix rouge en haut à droite. En rouvrant une nouvelle instance de l’application, vous pouvez (notamment avec un pseudo différent si cela vous chante, votre adresse sera quand même reconnue) relancer un chat avec l’utilisateur précédent. Les messages ayant été stockés dans la base de données, ils seront récupérés et affichés tels qu'ils avaient été envoyés.
|
||||
|
||||
*************Fin du manuel d’installation & d’utilisation de l’application*************
|
||||
|
|
BIN
Rapport COO.pdf
File normale
BIN
Rapport COO.pdf
File normale
File binario non mostrato.
BIN
Rapport POO.pdf
File normale
BIN
Rapport POO.pdf
File normale
File binario non mostrato.
|
@ -1,12 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>RemoteSystemsTempFiles</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.rse.ui.remoteSystemsTempNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
34
Server.java
File normale
34
Server.java
File normale
|
@ -0,0 +1,34 @@
|
|||
package websocket;
|
||||
|
||||
import javax.websocket.server.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.websocket.*;
|
||||
|
||||
@ServerEndpoint("/hello/")
|
||||
public class Server {
|
||||
private Session session;
|
||||
|
||||
@OnOpen
|
||||
public void connect(Session session) {
|
||||
this.session= session;
|
||||
System.out.println("session :"+ session);
|
||||
}
|
||||
|
||||
@OnClose
|
||||
public void close() {
|
||||
this.session=null;
|
||||
System.out.println("session closed");
|
||||
}
|
||||
|
||||
@OnMessage
|
||||
public void onMessage(String msg) throws IOException {
|
||||
System.out.println("msg recu:"+msg);
|
||||
this.session.getAsyncRemote().sendText("server:"+msg);
|
||||
//if (this.session != null && this.session.isOpen()) {
|
||||
// this.session.getBasicRemote().sendText("server:"+msg);
|
||||
//}
|
||||
}
|
||||
|
||||
}
|
File binario non mostrato.
Prima Larghezza: | Altezza: | Dimensione: 60 KiB |
BIN
démonstration_projet_clavardage.mkv
File normale
BIN
démonstration_projet_clavardage.mkv
File normale
File binario non mostrato.
Caricamento…
Crea riferimento in una nuova segnalazione