diff --git a/Application/Clavardage/bin/.gitignore b/Application/Clavardage/bin/.gitignore new file mode 100644 index 0000000..2d535ad --- /dev/null +++ b/Application/Clavardage/bin/.gitignore @@ -0,0 +1 @@ +/RemoteUser.class diff --git a/Application/Clavardage/bin/User.class b/Application/Clavardage/bin/User.class index f74a1c2..e4996a5 100644 Binary files a/Application/Clavardage/bin/User.class and b/Application/Clavardage/bin/User.class differ diff --git a/Application/Clavardage/bin/UserListeningThread.class b/Application/Clavardage/bin/UserListeningThread.class index 1416ea4..8e6ce10 100644 Binary files a/Application/Clavardage/bin/UserListeningThread.class and b/Application/Clavardage/bin/UserListeningThread.class differ diff --git a/Application/Clavardage/src/RemoteUser.java b/Application/Clavardage/src/RemoteUser.java new file mode 100644 index 0000000..c3dbb5f --- /dev/null +++ b/Application/Clavardage/src/RemoteUser.java @@ -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; + } + + +} diff --git a/Application/Clavardage/src/User.java b/Application/Clavardage/src/User.java index 8a78a7c..8a1a57c 100644 --- a/Application/Clavardage/src/User.java +++ b/Application/Clavardage/src/User.java @@ -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 remoteUserList = new ArrayList(); 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 diff --git a/Application/Clavardage/src/UserListeningThread.java b/Application/Clavardage/src/UserListeningThread.java index e7e5d78..3d1e1b1 100644 --- a/Application/Clavardage/src/UserListeningThread.java +++ b/Application/Clavardage/src/UserListeningThread.java @@ -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(); diff --git a/RemoteSystemsTempFiles/.project b/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000..5447a64 --- /dev/null +++ b/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + +