validation du pseudo correction

This commit is contained in:
Alexandre Gonzalvez 2020-11-30 16:30:39 +01:00
parent 9f4164d29c
commit 5902f67382
2 changed files with 75 additions and 59 deletions

View file

@ -1,10 +1,5 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
@ -16,8 +11,15 @@ public class User{
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public Boolean stopListeningUDPThread=false;
/*
* Constructor of User
* parameters
* #nport
* description : On récupère l'adresse de la machine, le numéro de port à utiliser(TODO), 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)
*/
public User(int nport) throws IOException{
//localUser
try
@ -34,10 +36,19 @@ public class User{
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
/* remoteUser
* Constructor of User simuled a remote user (Already log into our system)
* parameters
* #nport
* #pseudo
* description : On récupère l'adresse de la machine, le numéro de port à utiliser(TODO), 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)
*/
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
@ -67,18 +78,38 @@ public class User{
return pseudo;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
System.out.println("Enter nickname :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
System.out.println("Enter another nickname :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
myObj.close();
System.out.println("Your nickname : " + tmpPseudo + " is valid !");
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
@ -106,10 +137,13 @@ public class User{
dgramSocket.setSoTimeout(1000);
Boolean arecu;
System.out.println("Wait for pseudo validation ...");
System.out.print("Wait for pseudo validation ...\n");
System.out.println("..............");
int nbReponse =0;
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
nbReponse++;
inPacket= new DatagramPacket(buffer, buffer.length);
arecu=true;
@ -122,72 +156,50 @@ public class User{
response = new String(buffer);
if(arecu) {
System.out.println("Reponse : "+response);
//System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("RemoteUser");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println("Remote user n°"+nbReponse);
System.out.println("\tIP : "+lstresponse[0]);
System.out.println("\tn°Port : "+lstresponse[1]);
System.out.println("\tpseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
//System.out.println(tmpPseudo==lstresponse[2]);
valid= (tmpPseudo.compareTo(lstresponse[2])!=0);
if(!valid) {
System.out.println("Déjà pris");
}
}
newDate = new Date();
}
System.out.println(valid);
System.out.println("..............");
dgramSocket.close();
if(!valid) {
System.out.println("Nickname : "+tmpPseudo +" is taken !");
}
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12228,"Mike");
User usr1 = new User(12229);
User usr2 = new User(12229);
//User usr3 = new User(12229);
// Attend une réponse pour fermer l'écoute UDP
//sleep(1000);
//usr1.stopListeningUDPThread=true;
usr2.stopListeningUDPThread=true;
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}
/*
public Boolean compare(String str1, String str2) {
}
*/

View file

@ -21,7 +21,7 @@ public class UserListeningThread extends Thread{
}
while(!this.myUser.haveToStopThread) {
while(!this.myUser.stopListeningUDPThread) {
byte[] buffer = new byte[256];
@ -32,11 +32,14 @@ public class UserListeningThread extends Thread{
e.printStackTrace();
}
buffer = inPacket.getData();
System.out.println("Listening thread UDP : "+new String(buffer));
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo()+":testS";
System.out.println(toSend);
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo()+":test";
//System.out.println("Message avant envoi " +toSend);
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12229);
try {
@ -47,6 +50,7 @@ public class UserListeningThread extends Thread{
}
dgramSocket.close();
System.out.println("Fin du thread "+this.myUser.getNport());
}
}