Ajout des utilisateurs distants dans une liste + les notifier + changement de pseudo

This commit is contained in:
Alexandre Gonzalvez 2020-11-30 18:29:22 +01:00
parent 5902f67382
commit 77f2746e17
7 changed files with 132 additions and 5 deletions

1
Application/Clavardage/bin/.gitignore vendored Normal file
View file

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

View file

@ -0,0 +1,58 @@
import java.net.InetAddress;
public class RemoteUser {
InetAddress addIP;
int nPort;
String pseudo;
public RemoteUser(InetAddress remoteUserIP, int remoteUserPort,String pseudo) {
this.addIP=remoteUserIP;
this.nPort=remoteUserPort;
this.pseudo=pseudo;
}
public InetAddress getRemoteUserIP() {
return addIP;
}
public void setRemoteUserIP(InetAddress remoteUserIP) {
this.addIP = remoteUserIP;
}
public int getRemoteUserPort() {
return nPort;
}
public void setRemoteUserPort(int remoteUserPort) {
this.nPort = remoteUserPort;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo(String pseudo) {
this.pseudo = pseudo;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RemoteUser other = (RemoteUser) obj;
if (addIP == null) {
if (other.addIP != null)
return false;
} else if (!addIP.equals(other.addIP))
return false;
return true;
}
}

View file

@ -2,6 +2,7 @@ import java.io.IOException;
import java.net.*;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
import java.util.ArrayList; // import the ArrayList class
public class User{
@ -10,7 +11,7 @@ public class User{
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
protected ArrayList<RemoteUser> remoteUserList = new ArrayList<RemoteUser>();
public Boolean stopListeningUDPThread=false;
/*
@ -38,6 +39,29 @@ public class User{
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();
}
}
/* remoteUser
@ -61,9 +85,11 @@ public class User{
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
@ -165,7 +191,7 @@ public class User{
System.out.println("\tn°Port : "+lstresponse[1]);
System.out.println("\tpseudo : "+lstresponse[2]);
this.addRemoteUser(InetAddress.getByName(lstresponse[0].split("/")[1]),Integer.parseInt(lstresponse[1]),lstresponse[2]);
valid= (tmpPseudo.compareTo(lstresponse[2])!=0);
}
@ -182,9 +208,38 @@ public class User{
return valid;
}
public void addRemoteUser(InetAddress remoteUserIP, int remoteUserPort,String remoteUserPseudo) {
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+")");
this.remoteUserList.set(index,tmpRemoteUser);
}
else {
System.out.println("Ajout de l'utilisateur ayant pour IP(port) :"+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
System.out.println("Enter new nickname :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo)) & tmpPseudo.compareTo(oldPseudo) == 0) {
System.out.println("Enter another nickname :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
myObj.close();
System.out.println("Your new nickname : " + tmpPseudo + " is valid !");
//notifyothers(); //jsp comment elle s'appelle
}*/
public static void main(String[] args) throws IOException {
@ -192,6 +247,7 @@ public class User{
User usr2 = new User(12229);
//User usr3 = new User(12229);
// Attend une réponse pour fermer l'écoute UDP

View file

@ -32,9 +32,9 @@ 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();

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>RemoteSystemsTempFiles</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>org.eclipse.rse.ui.remoteSystemsTempNature</nature>
</natures>
</projectDescription>