Changement pseudo, notif other user, pick a chat

This commit is contained in:
Alexandre Gonzalvez 2020-12-02 12:06:39 +01:00
parent 77f2746e17
commit 39b24741a3
6 changed files with 193 additions and 84 deletions

View file

@ -1 +0,0 @@
/RemoteUser.class

Binary file not shown.

View file

@ -4,14 +4,23 @@ import java.util.Date;
import java.util.Scanner; // Import the Scanner class
import java.util.ArrayList; // import the ArrayList class
public class User{
final static int portUDPlistening_remoteUsr1 = 20001;
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected int portUDPsend;
protected int portUDPlistening;
protected int portTCP;
protected boolean actif;
protected ArrayList<RemoteUser> remoteUserList = new ArrayList<RemoteUser>();
protected ArrayList<RemoteUser> userChatList = new ArrayList<RemoteUser>();
protected UserListeningThread threadListeningUDP;
public Boolean stopListeningUDPThread=false;
/*
@ -21,7 +30,7 @@ public class User{
* 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{
public User(int portUDPsend,int portUDPlistening,int portTCP) throws IOException{
//localUser
try
{
@ -31,37 +40,23 @@ public class User{
System.out.println("Could not find local address!");
}
this.nport = nport;
this.portUDPsend = portUDPsend;
this.portUDPlistening = portUDPlistening;
this.portTCP = portTCP;
this.setPseudo();
this.actif = true;
this.setActif(true);
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
//notify_remote_user();
//ask_change_pseudo();
/*
boolean wantstochange = true; //A faire avec un bouton dans SWING
if(wantstochange) {
//changePseudo();
}
DatagramSocket dgramSocket= null;
try {
dgramSocket= new DatagramSocket(this.nport,this.addIP);
} catch (IOException e) {
e.printStackTrace();
}
String toSend = this.addIP.toString()+":"+this.nport+":"+this.pseudo+":test";
//System.out.println("Message avant envoi " +toSend);
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, 12229);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
*/
//this.waitProgramtoStop()
}
/* remoteUser
@ -72,7 +67,7 @@ public class User{
* 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) {
public User(int portUDPsend,int portUDPlistening, int portTCP,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
@ -81,10 +76,10 @@ public class User{
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
this.portUDPsend = portUDPsend;
this.portUDPlistening = portUDPlistening;
this.portTCP = portTCP;
this.setActif(true);
}
@ -104,15 +99,32 @@ public class User{
return pseudo;
}
public int getNport() {
return nport;
public int getPortTCP() {
return portTCP;
}
public void setNport(int nport) {
this.nport = nport;
public void setPortTCP(int portTCP) {
this.portTCP = portTCP;
}
public int getPortUDPlistening() {
return portUDPlistening;
}
public void setPortUDPlistening(int portUDPlistening) {
this.portUDPlistening = portUDPlistening;
}
public int getPortUDPsend() {
return portUDPsend;
}
public void setPortUDPsend(int portUDPsend) {
this.portUDPsend = portUDPsend;
}
public boolean isActif() {
return actif;
@ -120,21 +132,45 @@ public class User{
public void setActif(boolean actif) {
this.actif = actif;
notify_remote_user();
this.threadListeningUDP = new UserListeningThread("UDP Listening thread",this);
this.threadListeningUDP.start();
this.actif=true;
}
public void notify_remote_user() {
DatagramSocket dgramSocket= null;
try {
dgramSocket= new DatagramSocket(portUDPsend,this.addIP);
} catch (IOException e) {
e.printStackTrace();
}
String toSend = this.addIP.toString()+":"+this.portTCP+":"+this.pseudo+":test";
//System.out.println("Message avant envoi " +toSend);
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),this.addIP, portUDPlistening_remoteUsr1);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
dgramSocket.close();
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
Scanner sc1 = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter nickname :");
String tmpPseudo = myObj.nextLine(); // Read user input
String tmpPseudo = sc1.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Enter another nickname :");
tmpPseudo = myObj.nextLine(); // Read user input
tmpPseudo = sc1.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
myObj.close();
//sc1.close();
System.out.println("Your nickname : " + tmpPseudo + " is valid !");
}
@ -143,12 +179,13 @@ public class User{
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
//System.out.println(broadcastIP);
DatagramSocket dgramSocket = new DatagramSocket(this.portUDPsend,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
String toSend = this.getAddIP()+":"+this.portUDPsend+":test";
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),broadcastIP, portUDPlistening_remoteUsr1);
dgramSocket.send(outPacket);
@ -164,7 +201,6 @@ public class User{
Boolean arecu;
System.out.print("Wait for pseudo validation ...\n");
System.out.println("..............");
int nbReponse =0;
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
@ -198,7 +234,6 @@ public class User{
newDate = new Date();
}
System.out.println("..............");
dgramSocket.close();
if(!valid) {
@ -212,49 +247,87 @@ public class User{
RemoteUser tmpRemoteUser = new RemoteUser(remoteUserIP,remoteUserPort,remoteUserPseudo);
int index = this.remoteUserList.indexOf(tmpRemoteUser);
if(index!=-1) {
System.out.println("Mise a jour, l'adresse IP(port) de cet utilisateur est déjà présente :"+remoteUserIP+"("+remoteUserPort+")");
System.out.println("("+this.pseudo+") - "+"MAJ, IP(port) of user : "+remoteUserPseudo+" => "+remoteUserIP+"("+remoteUserPort+")");
this.remoteUserList.set(index,tmpRemoteUser);
}
else {
System.out.println("Ajout de l'utilisateur ayant pour IP(port) :"+remoteUserIP+"("+remoteUserPort+")");
System.out.println("("+this.pseudo+") - "+"Add new user IP(port) of user : "+remoteUserPseudo+" => "+remoteUserIP+"("+remoteUserPort+")");
this.remoteUserList.add(tmpRemoteUser);
}
}
// change pseudo
/*
private void changePseudo() 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 myObj = new Scanner(System.in); // Create a Scanner object
Scanner sc3 = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter new nickname :");
String tmpPseudo = myObj.nextLine(); // Read user input
String tmpPseudo = sc3.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo)) & tmpPseudo.compareTo(oldPseudo) == 0) {
while(!(this.validatePseudo(tmpPseudo)) || tmpPseudo.equals(oldPseudo)) {
System.out.println("Enter another nickname :");
tmpPseudo = myObj.nextLine(); // Read user input
tmpPseudo = sc3.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
myObj.close();
//myObj.close();
System.out.println("Your new nickname : " + tmpPseudo + " is valid !");
//notifyothers(); //jsp comment elle s'appelle
}*/
notify_remote_user();
}
public void printRemoteUserList() {
System.out.println("Internal list of remote users:\n");
public static void main(String[] args) throws IOException {
User usr1 = new User(12228,"Mike");
for(int i=0; i<this.remoteUserList.size(); i++) {
String pseudo = this.remoteUserList.get(i).getPseudo();
int Port = this.remoteUserList.get(i).getRemoteUserPort();
User usr2 = new User(12229);
System.out.println("("+i+")"
+"Username: " + pseudo + "\n"
+ "IP Adress: " + this.remoteUserList.get(i).getRemoteUserIP() + "\n"
+ "Port: " + Port + "\n" + "\n");
}
}
public void getOneActiveUser() throws UnknownHostException {
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");
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");
}
else {
userChatList.add(remoteUserList.get(index));
System.out.println("New chat with "+remoteUserList.get(index).getPseudo());
}
}
//sc2.close();
}
public static void main(String[] args) throws IOException, InterruptedException {
User usr1 = new User(12221,portUDPlistening_remoteUsr1,22221,"Mike");
User usr2 = new User(12222,20002,22222);
//User usr3 = new User(12229);
// Attend une réponse pour fermer l'écoute UDP
usr2.printRemoteUserList();
usr2.getOneActiveUser();
//sleep(1000);
//usr1.stopListeningUDPThread=true;
usr2.stopListeningUDPThread=true;
usr2.changePseudo();
System.out.println("Sleep mode for 5 seconds");
Thread.sleep(5000);
usr1.threadListeningUDP.close();
usr2.threadListeningUDP.close();
System.out.println("Fin du programme");
}

View file

@ -2,26 +2,26 @@ import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class UserListeningThread extends Thread{
private User myUser;
private DatagramSocket dgramSocket=null;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
DatagramSocket dgramSocket= null;
try {
dgramSocket= new DatagramSocket(this.myUser.getNport(),this.myUser.getAddIP());
this.dgramSocket = new DatagramSocket(this.myUser.getPortUDPlistening(),this.myUser.getAddIP());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
while(!this.myUser.stopListeningUDPThread) {
while(true) {
byte[] buffer = new byte[256];
@ -33,25 +33,62 @@ public class UserListeningThread extends Thread{
}
buffer = inPacket.getData();
System.out.println("Listening thread UDP : "+new String(buffer));
String receiveMsg = new String(buffer);
String [] tabMsg = receiveMsg.split(":");
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
if(tabMsg.length==3) { // si demande d'information d'un nouvelle utilisateur
InetAddress itsIP = null;
try {
itsIP = InetAddress.getByName(tabMsg[0].split("/")[1]);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int senderUDPport = Integer.parseInt(tabMsg[1]);
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);
//int itsPort=inPacket.getPort();
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
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);
// System.out.println("message avant envoi :"+toSend);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
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]);
try {
this.myUser.addRemoteUser(InetAddress.getByName(tabMsg[0].split("/")[1]),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();
}
}
}
dgramSocket.close();
System.out.println("Fin du thread "+this.myUser.getNport());
}
}
public void close() {
this.dgramSocket.close();
System.out.println("Fin du thread "+this.myUser.getPseudo());
try {
this.interrupt();
}catch (Exception e){
e.printStackTrace();
}
}
}