From 09dce244b5ce77fe264a75e4b35aed011c163665 Mon Sep 17 00:00:00 2001
From: Alexandre Gonzalvez
Date: Mon, 4 Jan 2021 12:27:26 +0100
Subject: [PATCH] Ajout d'une fonction simulant le broadcast + new comments +
fix
---
.../Clavardage/src/controller/Controller.java | 287 +++++++-----------
1 file changed, 118 insertions(+), 169 deletions(-)
diff --git a/Application/Clavardage/src/controller/Controller.java b/Application/Clavardage/src/controller/Controller.java
index 1998196..8726335 100644
--- a/Application/Clavardage/src/controller/Controller.java
+++ b/Application/Clavardage/src/controller/Controller.java
@@ -31,10 +31,11 @@ public class Controller {
int NB_SECOND_WAITING_RESPONSE_BROADCAST = 2;
// 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 ***/
@@ -51,6 +52,7 @@ public class Controller {
* @param portUDPsend : int => le numéro de port pour envoyé 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
*
* On récupère l'adresse de la machine, on demande un pseudo à l'utilisateur que l'on vérifie
@@ -60,9 +62,10 @@ public class Controller {
* - notification aux autres utilisateurs actifs
*
*/
- 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 +74,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();
}
+ // Création des threads d'écoutes
this.udp_connect_thread = new ListeningThreadUDP("UDP Listening thread",this);
this.udp_connect_thread.start();
-
this.tcp_connect_thread = new ListeningThreadTCPConnection("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,8 +113,11 @@ public class Controller {
* - notification aux autres utilisateurs actifs
*
*/
- 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
{
@@ -117,6 +127,7 @@ public class Controller {
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)) {
@@ -128,6 +139,11 @@ public class Controller {
this.tcp_connect_thread = new ListeningThreadTCPConnection("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,54 +153,14 @@ public class Controller {
} catch (IOException e) {
e.printStackTrace();
}
-
-
-
-
-
-
- notify_remote_users();
}
- /*** GETTERS ***/
- public LocalUser getMyUser() {
- return myUser;
- }
- public Interface getview() {
- return view;
- }
- public ListeningThreadUDP getUdp_connect_thread() {
- return udp_connect_thread;
- }
- public ListeningThreadTCPConnection getTcp_connect_thread() {
- return tcp_connect_thread;
- }
- public Historique getHistory() {
- return histoire;
- }
+ /**************************** Initialisation pseudo et découverte utilisateur distant (+notification utilisateurs distants) **********************************/
- /*** SETTERS ***/
- public void setMyUser(LocalUser myUser) {
- this.myUser = myUser;
- }
- public void setview(Interface view) {
- this.view = view;
- }
- public void setUdp_connect_thread(ListeningThreadUDP udp_connect_thread) {
- this.udp_connect_thread = udp_connect_thread;
- }
- public void setTcp_connect_thread(ListeningThreadTCPConnection tcp_connect_thread) {
- this.tcp_connect_thread = tcp_connect_thread;
- }
- public void setHistory(Historique histoire) {
- this.histoire=histoire;
- }
-
-
- /**
+ /** initPseudo
*
* Demande à l'utilisateur de rentrer un pseudo et valide de ce dernier en demandant aux
* utilisateurs distants leurs informations
@@ -203,7 +179,7 @@ public class Controller {
return tmpPseudo;
}
- /**
+ /** changePseudo
*
* Demande à l'utilisateur de rentrer un pseudo et valide de ce dernier en demandant aux
* utilisateurs distants leurs informations.
@@ -223,12 +199,11 @@ public class Controller {
this.myUser.setPseudo(tmpPseudo);
JOptionPane.showMessageDialog(null ,"Your new nickname : " + tmpPseudo + " is valid !");
this.notify_remote_users();
-
}
-
-
}
- /**
+
+
+ /** validatePseudo
*
* *tmpPseudo : String => Le pseudo à valider
*
@@ -242,29 +217,18 @@ public class Controller {
*
*/
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";
- // Send to other active user (simulation of broadcast)
- DatagramPacket outPacket = null;
- int tabBroadcastSize = tabBroadcast.length;
- for(int i=0;i
+ * Simulation of broadcast with port given (tabBroadcast)
+ *
+ */
+ 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
* En utilisant le port UDP d'envoi, on envoie en broadcast les informations nous concernant
*
+ * @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());
@@ -324,6 +307,7 @@ public class Controller {
// 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 A mettre dans l'interface ******************************************/
-
- /**
- *
- * Affichage de la liste d'utilisateurs actifs avec leurs index dans la liste
- *
- */
- public void printRemoteUserList() {
- System.out.println("Internal list of active remote users:");
-
- for(int i=0; i
- * Laisse l'utilisateur choisir parmis la liste d'utilisateurs actifs celui avec lequel il souhaite échanger via un chat
- *
- */
- 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