Création de la classe threadTCP, + quelques modifs/commentaires
This commit is contained in:
parent
0bb9c560b1
commit
82fdc9bacb
8 changed files with 152 additions and 103 deletions
2
Application/Clavardage/bin/.gitignore
vendored
Normal file
2
Application/Clavardage/bin/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/UserListeningThreadTCP.class
|
||||||
|
/UserListeningThreadUDP.class
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -6,37 +6,41 @@ public class RemoteUser {
|
||||||
int portTCP;
|
int portTCP;
|
||||||
String pseudo;
|
String pseudo;
|
||||||
|
|
||||||
|
/*** CONSTRUCTOR of RemoteUser ***/
|
||||||
public RemoteUser(InetAddress remoteUserIP, int remoteUserPortTCP,String pseudo) {
|
public RemoteUser(InetAddress remoteUserIP, int remoteUserPortTCP,String pseudo) {
|
||||||
this.addIP=remoteUserIP;
|
this.addIP=remoteUserIP;
|
||||||
this.portTCP=remoteUserPortTCP;
|
this.portTCP=remoteUserPortTCP;
|
||||||
this.pseudo=pseudo;
|
this.pseudo=pseudo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*** GETTERS ***/
|
||||||
public InetAddress getRemoteUserIP() {
|
public InetAddress getRemoteUserIP() {
|
||||||
return addIP;
|
return addIP;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRemoteUserIP(InetAddress remoteUserIP) {
|
|
||||||
this.addIP = remoteUserIP;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getRemoteUserPort() {
|
public int getRemoteUserPort() {
|
||||||
return portTCP;
|
return portTCP;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRemoteUserPort(int remoteUserPort) {
|
|
||||||
this.portTCP = remoteUserPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPseudo() {
|
public String getPseudo() {
|
||||||
return pseudo;
|
return pseudo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPseudo(String pseudo) {
|
|
||||||
this.pseudo = pseudo;
|
/*** SETTERS ***/
|
||||||
|
public void setRemoteUserIP(InetAddress remoteUserIP) {
|
||||||
|
this.addIP = remoteUserIP;
|
||||||
|
}
|
||||||
|
public void setRemoteUserPort(int remoteUserPort) {
|
||||||
|
this.portTCP = remoteUserPort;
|
||||||
|
}
|
||||||
|
public void setRemotePseudo(String remoteUserpseudo) {
|
||||||
|
this.pseudo = remoteUserpseudo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*** OVERRIDE METHODS ***/
|
||||||
|
/*
|
||||||
|
* equals
|
||||||
|
* @see java.lang.Object#equals(java.lang.Object)
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj)
|
if (this == obj)
|
||||||
|
@ -49,10 +53,15 @@ public class RemoteUser {
|
||||||
if (addIP == null) {
|
if (addIP == null) {
|
||||||
if (other.addIP != null)
|
if (other.addIP != null)
|
||||||
return false;
|
return false;
|
||||||
} else if (!(addIP.equals(other.addIP) && portTCP==other.portTCP))
|
} else if (!addIP.equals(other.addIP))
|
||||||
|
return false;
|
||||||
|
if (portTCP != other.portTCP)
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@ import java.util.ArrayList; // import the ArrayList class
|
||||||
public class User{
|
public class User{
|
||||||
|
|
||||||
/*** CONSTANTES ***/
|
/*** CONSTANTES ***/
|
||||||
final static int portUDPlistening_remoteUsr2 = 20002;
|
final static int portUDPlistening_remoteUsr2 = 20002; // TO REMOVE when we use broadcast
|
||||||
final static int portUDPlistening_remoteUsr3 = 20003;
|
final static int portUDPlistening_remoteUsr3 = 20003; // TO REMOVE when we use broadcast
|
||||||
|
|
||||||
/*** VARIABLES ***/
|
/*** VARIABLES ***/
|
||||||
protected InetAddress addIP;
|
protected InetAddress addIP;
|
||||||
|
@ -22,19 +22,19 @@ public class User{
|
||||||
protected ArrayList<RemoteUser> remoteUserList = new ArrayList<RemoteUser>(); // listes des utilisateurs actifs
|
protected ArrayList<RemoteUser> remoteUserList = new ArrayList<RemoteUser>(); // listes des utilisateurs actifs
|
||||||
protected ArrayList<RemoteUser> userChatList = new ArrayList<RemoteUser>(); // listes des utilisateurs avec qui un chat est actif
|
protected ArrayList<RemoteUser> userChatList = new ArrayList<RemoteUser>(); // listes des utilisateurs avec qui un chat est actif
|
||||||
|
|
||||||
protected UserListeningThread threadListeningUDP; // UDP listening thread (pour répondre aux nouveaux utilisateurs)
|
protected UserListeningThreadUDP threadListeningUDP; // UDP listening thread (pour répondre aux nouveaux utilisateurs)
|
||||||
|
protected UserListeningThreadTCP threadListeningTCP; // TCP listening thread (pour commencer une nouvelle conversation)
|
||||||
|
|
||||||
/*
|
/* Constructor of User (local)
|
||||||
* Constructor of User
|
* $parametres
|
||||||
* $parameters
|
* * portUDPsend : int => le numéro de port pour envoyé ces informations lors d'un changements ou d'une nouvelle connexion
|
||||||
* *
|
* * portUDPlistening : int => le numéro de port pour recevoir les informations des nouveaux utilisateurs ou les changements
|
||||||
* *
|
* * portTCP : int => le numéro de port pour commencer une nouvelle conversation
|
||||||
* *
|
*
|
||||||
* $description : On récupère l'adresse de la machine, le numéro de port à utiliser(TODO), on demande un pseudo à l'utilisateur
|
* $description : On récupère l'adresse de la machine, le numéro de port à utiliser, on demande un pseudo à l'utilisateur
|
||||||
* que l'on vérifie, une fois validé, l'utilisateur devient actif (Début de l'écoute UDP pour les pseudos)
|
* que l'on vérifie, une fois validé, l'utilisateur devient actif (Début de l'écoute UDP pour les pseudos et notification aux autres utilisateurs actifs)
|
||||||
*/
|
*/
|
||||||
public User(int portUDPsend,int portUDPlistening,int portTCP) throws IOException{
|
public User(int portUDPsend,int portUDPlistening,int portTCP) throws IOException{
|
||||||
//localUser
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
this.addIP = InetAddress.getLocalHost();
|
this.addIP = InetAddress.getLocalHost();
|
||||||
|
@ -49,18 +49,19 @@ public class User{
|
||||||
|
|
||||||
this.initPseudo();
|
this.initPseudo();
|
||||||
|
|
||||||
this.setActif(true);
|
this.active();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Constructor of User (simulation des utilisateurs distants)
|
* Constructor of User (simulation of remote user)
|
||||||
* $parameters
|
* $parametres
|
||||||
* *
|
* * portUDPsend : int => le numéro de port pour envoyé ces informations lors d'un changements ou d'une nouvelle connexion
|
||||||
* *
|
* * portUDPlistening : int => le numéro de port pour recevoir les informations des nouveaux utilisateurs ou les changements
|
||||||
* *
|
* * portTCP : int => le numéro de port pour commencer une nouvelle conversation
|
||||||
* $description : On récupère l'adresse de la machine, le numéro de port à utiliser(TODO), on demande un pseudo à l'utilisateur
|
* * pseudo : String => le pseudo avec lequel on initie l'utilisateur distant (! pas de vérification)
|
||||||
* que l'on vérifie, une fois validé, l'utilisateur devient actif (Début de l'écoute UDP pour les pseudos)
|
*
|
||||||
|
* $description : On récupère l'adresse de la machine, le numéro de port à utiliser, on demande un pseudo à l'utilisateur
|
||||||
|
* que l'on vérifie, une fois validé, l'utilisateur devient actif (Début de l'écoute UDP pour les pseudos et notification aux autres utilisateurs actifs)
|
||||||
*/
|
*/
|
||||||
public User(int portUDPsend,int portUDPlistening, int portTCP,String pseudo) {
|
public User(int portUDPsend,int portUDPlistening, int portTCP,String pseudo) {
|
||||||
try
|
try
|
||||||
|
@ -70,31 +71,29 @@ public class User{
|
||||||
catch(UnknownHostException e) {
|
catch(UnknownHostException e) {
|
||||||
System.out.println("Could not find local address!");
|
System.out.println("Could not find local address!");
|
||||||
}
|
}
|
||||||
this.pseudo = pseudo;
|
|
||||||
this.portUDPsend = portUDPsend;
|
this.portUDPsend = portUDPsend;
|
||||||
this.portUDPlistening = portUDPlistening;
|
this.portUDPlistening = portUDPlistening;
|
||||||
this.portTCP = portTCP;
|
this.portTCP = portTCP;
|
||||||
this.setActif(true);
|
this.pseudo = pseudo;
|
||||||
|
|
||||||
|
this.active();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*** GETTER ***/
|
/*** GETTERS ***/
|
||||||
public InetAddress getAddIP() {
|
public InetAddress getAddIP() {
|
||||||
return addIP;
|
return addIP;
|
||||||
}
|
}
|
||||||
public String getPseudo() {
|
public String getPseudo() {
|
||||||
return pseudo;
|
return pseudo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPortTCP() {
|
public int getPortTCP() {
|
||||||
return portTCP;
|
return portTCP;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPortUDPlistening() {
|
public int getPortUDPlistening() {
|
||||||
return portUDPlistening;
|
return portUDPlistening;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPortUDPsend() {
|
public int getPortUDPsend() {
|
||||||
return portUDPsend;
|
return portUDPsend;
|
||||||
}
|
}
|
||||||
|
@ -102,34 +101,63 @@ public class User{
|
||||||
return actif;
|
return actif;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** SETTER ***/
|
/*** SETTERS ***/
|
||||||
public void setAddIP(InetAddress addIP) {
|
public void setAddIP(InetAddress addIP) {
|
||||||
this.addIP = addIP;
|
this.addIP = addIP;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPortTCP(int portTCP) {
|
public void setPortTCP(int portTCP) {
|
||||||
this.portTCP = portTCP;
|
this.portTCP = portTCP;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPortUDPlistening(int portUDPlistening) {
|
public void setPortUDPlistening(int portUDPlistening) {
|
||||||
this.portUDPlistening = portUDPlistening;
|
this.portUDPlistening = portUDPlistening;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPortUDPsend(int portUDPsend) {
|
public void setPortUDPsend(int portUDPsend) {
|
||||||
this.portUDPsend = portUDPsend;
|
this.portUDPsend = portUDPsend;
|
||||||
}
|
}
|
||||||
|
/*active
|
||||||
public void setActif(boolean actif) {
|
* $ description
|
||||||
|
* On notifie les utilisateurs distants actifs de notre passage en mode actif (envoi de nos informations)
|
||||||
|
* On démarre un thread d'écoute UDP pour répondre au nouveaux utilisateurs
|
||||||
|
* (TODO)On démarre un thread d'écoute TCP pour commencer une conversation avec les utilisateurs actifs
|
||||||
|
*/
|
||||||
|
private void active() {
|
||||||
notify_remote_user();
|
notify_remote_user();
|
||||||
this.threadListeningUDP = new UserListeningThread("UDP Listening thread",this);
|
this.threadListeningUDP = new UserListeningThreadUDP("UDP Listening thread",this);
|
||||||
this.threadListeningUDP.start();
|
this.threadListeningUDP.start();
|
||||||
|
|
||||||
|
//this.threadListeningTCP = new UserListeningThreadTCP("TCP main Listening thread",this);
|
||||||
|
//this.threadListeningTCP.start();
|
||||||
|
|
||||||
this.actif=true;
|
this.actif=true;
|
||||||
}
|
}
|
||||||
|
/*setPseudo
|
||||||
|
* $description
|
||||||
|
* Demande à l'utilisateur de rentrer un pseudo et validation de ce dernier en demandant aux utilisateurs distants leurs informations
|
||||||
|
* On regarde si le pseudo est bien différent de l'ancien
|
||||||
|
*/
|
||||||
|
private void setPseudo() throws IOException {
|
||||||
|
String oldPseudo = this.pseudo; //Saves the old one for comparison
|
||||||
|
|
||||||
|
Scanner sc3 = new Scanner(System.in); // Create a Scanner object
|
||||||
|
System.out.println("Enter new nickname :");
|
||||||
|
String tmpPseudo = sc3.nextLine(); // Read user input
|
||||||
|
|
||||||
|
while(!(this.validatePseudo(tmpPseudo)) || tmpPseudo.equals(oldPseudo)) {
|
||||||
|
System.out.println("Enter another nickname :");
|
||||||
|
tmpPseudo = sc3.nextLine(); // Read user input
|
||||||
|
}
|
||||||
|
|
||||||
|
this.pseudo = tmpPseudo;
|
||||||
|
//myObj.close();
|
||||||
|
System.out.println("Your new nickname : " + tmpPseudo + " is valid !");
|
||||||
|
|
||||||
|
notify_remote_user();
|
||||||
|
}
|
||||||
|
|
||||||
/*** PROCEDURES ***/
|
/*** PROCEDURES ***/
|
||||||
|
|
||||||
/* notify_remote_user
|
/* notify_remote_user
|
||||||
|
* $description
|
||||||
* En utilisant le port UDP d'envoi, on envoi en broadcast les informations nous concernant
|
* En utilisant le port UDP d'envoi, on envoi en broadcast les informations nous concernant
|
||||||
*/
|
*/
|
||||||
public void notify_remote_user() {
|
public void notify_remote_user() {
|
||||||
|
@ -165,6 +193,7 @@ public class User{
|
||||||
}
|
}
|
||||||
|
|
||||||
/*initPseudo
|
/*initPseudo
|
||||||
|
* $description
|
||||||
* Demande à l'utilisateur de rentrer un pseudo et validation de ce dernier en demandant aux utilisateurs distants leurs informations
|
* Demande à l'utilisateur de rentrer un pseudo et validation de ce dernier en demandant aux utilisateurs distants leurs informations
|
||||||
*/
|
*/
|
||||||
public void initPseudo() throws IOException {
|
public void initPseudo() throws IOException {
|
||||||
|
@ -211,14 +240,12 @@ public class User{
|
||||||
outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, portUDPlistening_remoteUsr3);
|
outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, portUDPlistening_remoteUsr3);
|
||||||
dgramSocket.send(outPacket);
|
dgramSocket.send(outPacket);
|
||||||
|
|
||||||
// Initialisation des paramètres de la boucles
|
// Initialisation des variables de la boucle
|
||||||
Boolean valid = true;
|
Boolean valid = true;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
DatagramPacket inPacket;
|
DatagramPacket inPacket;
|
||||||
String response = null;
|
String response = null;
|
||||||
String[] lstresponse= new String [4];
|
String[] tabresponse= new String [4];
|
||||||
dgramSocket.setSoTimeout(1000);
|
dgramSocket.setSoTimeout(1000);
|
||||||
Boolean arecu;
|
Boolean arecu;
|
||||||
|
|
||||||
|
@ -243,17 +270,17 @@ public class User{
|
||||||
|
|
||||||
if(arecu) {
|
if(arecu) {
|
||||||
// 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])
|
||||||
lstresponse = response.split(":");
|
tabresponse = response.split(":");
|
||||||
|
|
||||||
// Affichage de la réponse
|
// Affichage de la réponse
|
||||||
System.out.println("Remote user n°"+nbReponse);
|
System.out.println("Remote user n°"+nbReponse);
|
||||||
System.out.println("\tIP : "+lstresponse[0]);
|
System.out.println("\tIP : "+tabresponse[0]);
|
||||||
System.out.println("\tn°Port : "+lstresponse[1]);
|
System.out.println("\tn°Port : "+tabresponse[1]);
|
||||||
System.out.println("\tpseudo : "+lstresponse[2]);
|
System.out.println("\tpseudo : "+tabresponse[2]);
|
||||||
|
|
||||||
// Si reception on ajoute l'utilisateur à notre liste d'utilisateur distant
|
// Si reception on ajoute l'utilisateur à notre liste d'utilisateur distant
|
||||||
this.addRemoteUser(InetAddress.getByName(lstresponse[0].split("/")[1]),Integer.parseInt(lstresponse[1]),lstresponse[2]);
|
this.addRemoteUser(InetAddress.getByName(tabresponse[0].split("/")[1]),Integer.parseInt(tabresponse[1]),tabresponse[2]);
|
||||||
valid= (tmpPseudo.compareTo(lstresponse[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
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -268,38 +295,27 @@ public class User{
|
||||||
return valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addRemoteUser(InetAddress remoteUserIP, int remoteUserPort,String remoteUserPseudo) {
|
/*addRemoteUser
|
||||||
RemoteUser tmpRemoteUser = new RemoteUser(remoteUserIP,remoteUserPort,remoteUserPseudo);
|
* $parametres
|
||||||
|
* *remoteUserIP : InetAddress => l'adresse IP de l'utilisateur distant
|
||||||
|
* *remoteUserPortTCP : int => le numéro de port TCP d'écoute de l'utilisateur distant
|
||||||
|
* *remoteUserPseudo : String => le pseudo de l'utilisateur distant
|
||||||
|
* $description
|
||||||
|
* On ajoute ou mise à jour l'utilisateur distant dans notre liste d'utilisateur distant
|
||||||
|
*/
|
||||||
|
public void addRemoteUser(InetAddress remoteUserIP, int remoteUserPortTCP,String remoteUserPseudo) {
|
||||||
|
RemoteUser tmpRemoteUser = new RemoteUser(remoteUserIP,remoteUserPortTCP,remoteUserPseudo);
|
||||||
int index = this.remoteUserList.indexOf(tmpRemoteUser);
|
int index = this.remoteUserList.indexOf(tmpRemoteUser);
|
||||||
if(index!=-1) {
|
if(index!=-1) {
|
||||||
System.out.println("("+this.pseudo+") - "+"MAJ, IP(port) of user : "+remoteUserPseudo+" => "+remoteUserIP+"("+remoteUserPort+")");
|
System.out.println("("+this.pseudo+") - "+"MAJ, IP(port) of user : "+remoteUserPseudo+" => "+remoteUserIP+"("+remoteUserPortTCP+")");
|
||||||
this.remoteUserList.set(index,tmpRemoteUser);
|
this.remoteUserList.set(index,tmpRemoteUser);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
System.out.println("("+this.pseudo+") - "+"Add new user IP(port) of user : "+remoteUserPseudo+" => "+remoteUserIP+"("+remoteUserPort+")");
|
System.out.println("("+this.pseudo+") - "+"Add new user IP(port) of user : "+remoteUserPseudo+" => "+remoteUserIP+"("+remoteUserPortTCP+")");
|
||||||
this.remoteUserList.add(tmpRemoteUser);
|
this.remoteUserList.add(tmpRemoteUser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// change pseudo
|
|
||||||
|
|
||||||
private void setPseudo() throws IOException { //seule différence avec setPseudo c'est qu'on check si on remet pas le même
|
|
||||||
String oldPseudo = this.pseudo; //Saves the old one for comparison
|
|
||||||
|
|
||||||
Scanner sc3 = new Scanner(System.in); // Create a Scanner object
|
|
||||||
System.out.println("Enter new nickname :");
|
|
||||||
String tmpPseudo = sc3.nextLine(); // Read user input
|
|
||||||
|
|
||||||
while(!(this.validatePseudo(tmpPseudo)) || tmpPseudo.equals(oldPseudo)) {
|
|
||||||
System.out.println("Enter another nickname :");
|
|
||||||
tmpPseudo = sc3.nextLine(); // Read user input
|
|
||||||
}
|
|
||||||
|
|
||||||
this.pseudo = tmpPseudo;
|
|
||||||
//myObj.close();
|
|
||||||
System.out.println("Your new nickname : " + tmpPseudo + " is valid !");
|
|
||||||
|
|
||||||
notify_remote_user();
|
|
||||||
}
|
|
||||||
/*printRemoteUserList
|
/*printRemoteUserList
|
||||||
* $descrition
|
* $descrition
|
||||||
* Affichage de la liste d'utilisateur actif avec leurs index dans la liste
|
* Affichage de la liste d'utilisateur actif avec leurs index dans la liste
|
||||||
|
@ -340,9 +356,9 @@ private void setPseudo() throws IOException { //seule diff
|
||||||
// Création des utilisateurs
|
// Création des utilisateurs
|
||||||
User usr2 = new User(12222,portUDPlistening_remoteUsr2,22222,"Mike"); // simulation d'un utilisateur distant n1
|
User usr2 = new User(12222,portUDPlistening_remoteUsr2,22222,"Mike"); // simulation d'un utilisateur distant n1
|
||||||
User usr3 = new User(12223,portUDPlistening_remoteUsr3,22223,"Alice"); // simulation d'un utilisateur distant n2
|
User usr3 = new User(12223,portUDPlistening_remoteUsr3,22223,"Alice"); // simulation d'un utilisateur distant n2
|
||||||
User usr1 = new User(12221,20001,22221); // Notre utilisateur locale
|
User usr1 = new User(12221,20001,22221); // Notre utilisateur local
|
||||||
|
|
||||||
// Fonction appelé par notre utilisateur locale
|
// Fonction appelé par notre utilisateur local
|
||||||
usr1.printRemoteUserList();
|
usr1.printRemoteUserList();
|
||||||
usr1.getOneActiveUser();
|
usr1.getOneActiveUser();
|
||||||
usr1.setPseudo();
|
usr1.setPseudo();
|
||||||
|
|
4
Application/Clavardage/src/UserListeningThreadTCP.java
Normal file
4
Application/Clavardage/src/UserListeningThreadTCP.java
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
public class UserListeningThreadTCP {
|
||||||
|
|
||||||
|
}
|
|
@ -4,12 +4,19 @@ import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
public class UserListeningThread extends Thread{
|
public class UserListeningThreadUDP extends Thread{
|
||||||
|
|
||||||
private User myUser;
|
private User myUser;
|
||||||
private DatagramSocket dgramSocket=null;
|
private DatagramSocket dgramSocket=null;
|
||||||
|
|
||||||
public UserListeningThread(String s,User user) {
|
/* CONSTRUCTOR OF UserListeningThreadUDP
|
||||||
|
* $parametres
|
||||||
|
* * s : String => nom du thread
|
||||||
|
* * user : User => utilisateur utilisant ce thread
|
||||||
|
* $description
|
||||||
|
* Création d'un socket d'écoute
|
||||||
|
*/
|
||||||
|
public UserListeningThreadUDP(String s,User user) {
|
||||||
super(s);
|
super(s);
|
||||||
this.myUser = user;
|
this.myUser = user;
|
||||||
try {
|
try {
|
||||||
|
@ -19,11 +26,19 @@ public class UserListeningThread extends Thread{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* run
|
||||||
|
* $description
|
||||||
|
* é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
|
||||||
|
*/
|
||||||
public void run(){
|
public void run(){
|
||||||
|
|
||||||
|
// Tant que l'utilisateur est actif on attends les messages des nouveaux utilisateurs et changements des utilisateurs actifs
|
||||||
while(true) {
|
while(true) {
|
||||||
|
|
||||||
|
// Réception du message
|
||||||
byte[] buffer = new byte[256];
|
byte[] buffer = new byte[256];
|
||||||
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
|
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
|
||||||
try {
|
try {
|
||||||
|
@ -33,38 +48,36 @@ public class UserListeningThread extends Thread{
|
||||||
}
|
}
|
||||||
buffer = inPacket.getData();
|
buffer = inPacket.getData();
|
||||||
|
|
||||||
|
// Traitement en fonction du message reçu
|
||||||
String receiveMsg = new String(buffer);
|
String receiveMsg = new String(buffer);
|
||||||
String [] tabMsg = receiveMsg.split(":");
|
String [] tabMsg = receiveMsg.split(":");
|
||||||
|
|
||||||
if(tabMsg.length==3) { // si demande d'information d'un nouvelle utilisateur
|
|
||||||
|
// si demande d'information d'un nouvel utilisateur
|
||||||
|
if(tabMsg.length==3) {
|
||||||
InetAddress itsIP = null;
|
InetAddress itsIP = null;
|
||||||
try {
|
try {
|
||||||
itsIP = InetAddress.getByName(tabMsg[0].split("/")[1]);
|
itsIP = InetAddress.getByName(tabMsg[0].split("/")[1]); // 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]);
|
int senderUDPport = Integer.parseInt(tabMsg[1]); // On récupère le port UDP de l'utilisateur distant
|
||||||
|
|
||||||
//int itsPort=inPacket.getPort();
|
|
||||||
|
|
||||||
String toSend = myUser.getAddIP().toString()+":"+myUser.getPortTCP()+":"+myUser.getPseudo()+":test";
|
String toSend = myUser.getAddIP().toString()+":"+myUser.getPortTCP()+":"+myUser.getPseudo()+":test";
|
||||||
//System.out.println("Message avant envoi " +toSend);
|
|
||||||
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, senderUDPport);
|
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, senderUDPport);
|
||||||
|
|
||||||
// System.out.println("message avant envoi :"+toSend);
|
|
||||||
try {
|
try {
|
||||||
dgramSocket.send(outPacket);
|
dgramSocket.send(outPacket);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { // Si un nouvelle utilisateur passe en mode actif
|
|
||||||
//System.out.println("\tIP : "+tabMsg[0]);
|
|
||||||
//System.out.println("\tn°Port : "+tabMsg[1]);
|
|
||||||
//System.out.println("\tpseudo : "+tabMsg[2]);
|
|
||||||
|
|
||||||
|
// Si un nouvel utilisateur passe en mode actif ou changement d'information
|
||||||
|
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
|
||||||
this.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0].split("/")[1]),Integer.parseInt(tabMsg[1]),tabMsg[2]);
|
this.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0].split("/")[1]),Integer.parseInt(tabMsg[1]),tabMsg[2]);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
|
@ -81,6 +94,11 @@ public class UserListeningThread extends Thread{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* close
|
||||||
|
* $description
|
||||||
|
* ferme le socket d'écoute UDP
|
||||||
|
* interrupt UDP listening threadS
|
||||||
|
*/
|
||||||
public void close() {
|
public void close() {
|
||||||
this.dgramSocket.close();
|
this.dgramSocket.close();
|
||||||
System.out.println("End of listing thread ("+this.myUser.getPseudo()+")");
|
System.out.println("End of listing thread ("+this.myUser.getPseudo()+")");
|
Loading…
Reference in a new issue