Ajout des utilisateurs distants dans une liste + les notifier + changement de pseudo
This commit is contained in:
parent
5902f67382
commit
77f2746e17
7 changed files with 132 additions and 5 deletions
1
Application/Clavardage/bin/.gitignore
vendored
Normal file
1
Application/Clavardage/bin/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/RemoteUser.class
|
Binary file not shown.
Binary file not shown.
58
Application/Clavardage/src/RemoteUser.java
Normal file
58
Application/Clavardage/src/RemoteUser.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ import java.io.IOException;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Scanner; // Import the Scanner class
|
import java.util.Scanner; // Import the Scanner class
|
||||||
|
import java.util.ArrayList; // import the ArrayList class
|
||||||
|
|
||||||
public class User{
|
public class User{
|
||||||
|
|
||||||
|
@ -10,7 +11,7 @@ public class User{
|
||||||
protected int nport;
|
protected int nport;
|
||||||
protected boolean actif;
|
protected boolean actif;
|
||||||
|
|
||||||
protected User[] remoteUserList;
|
protected ArrayList<RemoteUser> remoteUserList = new ArrayList<RemoteUser>();
|
||||||
public Boolean stopListeningUDPThread=false;
|
public Boolean stopListeningUDPThread=false;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -38,6 +39,29 @@ public class User{
|
||||||
|
|
||||||
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
|
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
|
||||||
th.start();
|
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
|
/* remoteUser
|
||||||
|
@ -61,9 +85,11 @@ public class User{
|
||||||
this.actif = true;
|
this.actif = true;
|
||||||
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
|
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
|
||||||
th.start();
|
th.start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public InetAddress getAddIP() {
|
public InetAddress getAddIP() {
|
||||||
return addIP;
|
return addIP;
|
||||||
}
|
}
|
||||||
|
@ -165,7 +191,7 @@ public class User{
|
||||||
System.out.println("\tn°Port : "+lstresponse[1]);
|
System.out.println("\tn°Port : "+lstresponse[1]);
|
||||||
System.out.println("\tpseudo : "+lstresponse[2]);
|
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);
|
valid= (tmpPseudo.compareTo(lstresponse[2])!=0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -182,9 +208,38 @@ public class User{
|
||||||
return valid;
|
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 {
|
public static void main(String[] args) throws IOException {
|
||||||
|
@ -192,6 +247,7 @@ public class User{
|
||||||
|
|
||||||
User usr2 = new User(12229);
|
User usr2 = new User(12229);
|
||||||
|
|
||||||
|
|
||||||
//User usr3 = new User(12229);
|
//User usr3 = new User(12229);
|
||||||
|
|
||||||
// Attend une réponse pour fermer l'écoute UDP
|
// Attend une réponse pour fermer l'écoute UDP
|
||||||
|
|
12
RemoteSystemsTempFiles/.project
Normal file
12
RemoteSystemsTempFiles/.project
Normal 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>
|
Loading…
Reference in a new issue