fix notify for remote user
This commit is contained in:
parent
82fdc9bacb
commit
f1898738c1
4 changed files with 145 additions and 77 deletions
Binary file not shown.
|
@ -114,83 +114,33 @@ public class User{
|
|||
public void setPortUDPsend(int portUDPsend) {
|
||||
this.portUDPsend = portUDPsend;
|
||||
}
|
||||
/*active
|
||||
* $ 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();
|
||||
this.threadListeningUDP = new UserListeningThreadUDP("UDP Listening thread",this);
|
||||
this.threadListeningUDP.start();
|
||||
|
||||
//this.threadListeningTCP = new UserListeningThreadTCP("TCP main Listening thread",this);
|
||||
//this.threadListeningTCP.start();
|
||||
|
||||
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
|
||||
*/
|
||||
* $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
|
||||
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
|
||||
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
|
||||
}
|
||||
while(!(this.validatePseudo(tmpPseudo)) || tmpPseudo.equals(oldPseudo)) {
|
||||
System.out.println("Already exist, enter another nickname :");
|
||||
tmpPseudo = sc3.nextLine(); // Read user input
|
||||
}
|
||||
|
||||
this.pseudo = tmpPseudo;
|
||||
//myObj.close();
|
||||
System.out.println("Your new nickname : " + tmpPseudo + " is valid !");
|
||||
this.pseudo = tmpPseudo;
|
||||
//myObj.close();
|
||||
System.out.println("Your new nickname : " + tmpPseudo + " is valid !");
|
||||
|
||||
notify_remote_user();
|
||||
}
|
||||
|
||||
notify_remote_user();
|
||||
}
|
||||
|
||||
|
||||
/*** PROCEDURES ***/
|
||||
|
||||
/* notify_remote_user
|
||||
* $description
|
||||
* En utilisant le port UDP d'envoi, on envoi en broadcast les informations nous concernant
|
||||
*/
|
||||
public void notify_remote_user() {
|
||||
// Création du socket d'envoi d'information
|
||||
DatagramSocket dgramSocket= null;
|
||||
try {
|
||||
dgramSocket= new DatagramSocket(portUDPsend,this.addIP);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Construction du message à envoyer
|
||||
String toSend = this.addIP.toString()+":"+this.portTCP+":"+this.pseudo+":test";
|
||||
|
||||
|
||||
// Send information to usr2
|
||||
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, portUDPlistening_remoteUsr2);
|
||||
try {
|
||||
dgramSocket.send(outPacket);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Send information to usr3
|
||||
outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, portUDPlistening_remoteUsr3);
|
||||
try {
|
||||
dgramSocket.send(outPacket);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
dgramSocket.close();
|
||||
}
|
||||
|
||||
/*initPseudo
|
||||
* $description
|
||||
|
@ -316,12 +266,72 @@ public class User{
|
|||
}
|
||||
}
|
||||
|
||||
/* notify_remote_user
|
||||
* $description
|
||||
* En utilisant le port UDP d'envoi, on envoi en broadcast les informations nous concernant
|
||||
*/
|
||||
public void notify_remote_user() {
|
||||
// Création du socket d'envoi d'information
|
||||
DatagramSocket dgramSocket= null;
|
||||
try {
|
||||
dgramSocket= new DatagramSocket(portUDPsend,this.addIP);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Construction du message à envoyer
|
||||
String toSend = this.addIP.toString()+":"+this.portTCP+":"+this.pseudo+":test";
|
||||
|
||||
DatagramPacket outPacket =null;
|
||||
|
||||
// Send information to usr2
|
||||
if(this.portUDPlistening!=portUDPlistening_remoteUsr2) {
|
||||
outPacket = new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, portUDPlistening_remoteUsr2);
|
||||
try {
|
||||
dgramSocket.send(outPacket);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Send information to usr3
|
||||
if(this.portUDPlistening!=portUDPlistening_remoteUsr3) {
|
||||
|
||||
outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, portUDPlistening_remoteUsr3);
|
||||
try {
|
||||
dgramSocket.send(outPacket);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
dgramSocket.close();
|
||||
}
|
||||
|
||||
/*active
|
||||
* $ description
|
||||
* On démarre un thread d'écoute UDP pour répondre au nouveaux utilisateurs
|
||||
* On démarre un thread d'écoute TCP pour commencer une conversation avec les utilisateurs actifs
|
||||
* On notifie les utilisateurs distants actifs de notre passage en mode actif (envoi de nos informations)
|
||||
*/
|
||||
private void active() {
|
||||
this.threadListeningUDP = new UserListeningThreadUDP("UDP Listening thread",this);
|
||||
this.threadListeningUDP.start();
|
||||
|
||||
this.threadListeningTCP = new UserListeningThreadTCP("TCP main Listening thread",this);
|
||||
this.threadListeningTCP.start();
|
||||
|
||||
notify_remote_user();
|
||||
|
||||
this.actif=true;
|
||||
}
|
||||
|
||||
/*printRemoteUserList
|
||||
* $descrition
|
||||
* Affichage de la liste d'utilisateur actif avec leurs index dans la liste
|
||||
*/
|
||||
public void printRemoteUserList() {
|
||||
System.out.println("Internal list of active remote users:\n");
|
||||
System.out.println("\nInternal list of active remote users:");
|
||||
|
||||
for(int i=0; i<this.remoteUserList.size(); i++) {
|
||||
System.out.println("- ("+i+") Username: " + this.remoteUserList.get(i).getPseudo());
|
||||
|
@ -335,11 +345,10 @@ public class User{
|
|||
public void getOneActiveUser() throws UnknownHostException {
|
||||
this.printRemoteUserList();
|
||||
Scanner sc2= new Scanner(System.in);
|
||||
System.out.println("Please, enter one active user that you saw on the list to start a conversation with");
|
||||
System.out.println("Please, enter index of one active user that you saw on the list to start a conversation with:");
|
||||
int index=Integer.parseInt(sc2.nextLine());
|
||||
|
||||
if (index >= 0 && index<remoteUserList.size()) {
|
||||
System.out.println("This user is active, you can start to exchange messages");
|
||||
if(userChatList.contains(remoteUserList.get(index))) {
|
||||
System.out.println("User "+remoteUserList.get(index).getPseudo()+" is already in chat with you");
|
||||
}
|
||||
|
@ -348,6 +357,9 @@ public class User{
|
|||
System.out.println("New chat with "+remoteUserList.get(index).getPseudo());
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Wrong index (no active at index number "+index+" )");
|
||||
}
|
||||
//sc2.close();
|
||||
}
|
||||
|
||||
|
@ -359,7 +371,6 @@ public class User{
|
|||
User usr1 = new User(12221,20001,22221); // Notre utilisateur local
|
||||
|
||||
// Fonction appelé par notre utilisateur local
|
||||
usr1.printRemoteUserList();
|
||||
usr1.getOneActiveUser();
|
||||
usr1.setPseudo();
|
||||
|
||||
|
@ -370,7 +381,10 @@ public class User{
|
|||
usr1.threadListeningUDP.close();
|
||||
usr2.threadListeningUDP.close();
|
||||
usr3.threadListeningUDP.close();
|
||||
|
||||
|
||||
usr1.threadListeningTCP.close();
|
||||
usr2.threadListeningTCP.close();
|
||||
usr3.threadListeningTCP.close();
|
||||
System.out.println("End");
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,58 @@
|
|||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class UserListeningThreadTCP {
|
||||
public class UserListeningThreadTCP extends Thread{
|
||||
|
||||
private User myUser;
|
||||
private DatagramSocket dgramSocket=null;
|
||||
|
||||
/* CONSTRUCTOR OF UserListeningThreadTCP
|
||||
* $parametres
|
||||
* * s : String => nom du thread
|
||||
* * user : User => utilisateur utilisant ce thread
|
||||
* $description
|
||||
* Création d'un socket d'écoute
|
||||
*/
|
||||
public UserListeningThreadTCP(String s,User user) {
|
||||
super(s);
|
||||
this.myUser = user;
|
||||
try {
|
||||
this.dgramSocket = new DatagramSocket(this.myUser.getPortTCP(),this.myUser.getAddIP());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/* run
|
||||
* $description
|
||||
* écoutes les messages TCP tant que l'utilisateur est actif
|
||||
* Traitement
|
||||
* Si réception d'une demande, créer un
|
||||
*/
|
||||
public void run(){
|
||||
|
||||
// Tant que l'utilisateur est actif on attends la demande de nouvelle conversation
|
||||
while(true) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* close
|
||||
* $description
|
||||
* ferme le socket d'écoute UDP
|
||||
* interrupt UDP listening threadS
|
||||
*/
|
||||
public void close() {
|
||||
this.dgramSocket.close();
|
||||
System.out.println("End of listing thread TCP ("+this.myUser.getPseudo()+")");
|
||||
try {
|
||||
this.interrupt();
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -101,7 +101,7 @@ public class UserListeningThreadUDP extends Thread{
|
|||
*/
|
||||
public void close() {
|
||||
this.dgramSocket.close();
|
||||
System.out.println("End of listing thread ("+this.myUser.getPseudo()+")");
|
||||
System.out.println("End of listing thread UDP ("+this.myUser.getPseudo()+")");
|
||||
try {
|
||||
this.interrupt();
|
||||
}catch (Exception e){
|
||||
|
|
Loading…
Reference in a new issue