diff --git a/src/Main.java b/src/Main.java index 6337567..57b7da3 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,9 @@ + +import java.util.Scanner; + public class Main { public static void main(String[] args){ User user1 = new User("elyes2"); + } } diff --git a/src/SendMessage.java b/src/SendMessage.java index 634aa86..cf59ccd 100644 --- a/src/SendMessage.java +++ b/src/SendMessage.java @@ -19,7 +19,7 @@ public static void sendMessage(InetAddress address, int port, User user) throws String data = user.getPseudo(); DatagramPacket packet = new DatagramPacket(data.getBytes(), data.getBytes().length, address, 45047); - System.out.println("J'ai renvoyé mon paquet apres l'ecoute sur le port : " + 45047); + //System.out.println("J'ai renvoyé mon paquet apres l'ecoute sur le port : " + 45047); User.getSocketEcoute().send(packet); diff --git a/src/ThreadMenu.java b/src/ThreadMenu.java new file mode 100644 index 0000000..b8f9c40 --- /dev/null +++ b/src/ThreadMenu.java @@ -0,0 +1,34 @@ + +import java.util.Scanner; + +public class ThreadMenu extends Thread { + + + private static User user; + + public ThreadMenu(String name, User user1){ + super(name); + user = user1; + } + + public void run(){ + int choix = 0; + System.out.println("MENU : \n 1 : Voir liste des utilisateurs présents \n 2 : Initialiser communication (TCP) \n 9 : Quitter"); + + + while(choix != 9) { + + Scanner sc = new Scanner(System.in); + choix = sc.nextInt(); + + switch(choix){ + case 1: user.getListUser().toString(); + System.out.println(">>>>>>"); + break; + // case 2: .. + //break; + case 9 : return ; + } + } + } +} \ No newline at end of file diff --git a/src/ThreadReceive.java b/src/ThreadReceive.java index fa08861..cedf7e2 100644 --- a/src/ThreadReceive.java +++ b/src/ThreadReceive.java @@ -24,7 +24,7 @@ public static void receiveMessage() throws IOException { while(true){ - System.out.println("Jattends de recevoir un message <<< bloqué" + "temps : " + System.currentTimeMillis()); + // System.out.println("Jattends de recevoir un message <<< bloqué" + "temps : " + System.currentTimeMillis()); byte[] recvBuf = new byte[1024]; DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length); @@ -34,28 +34,31 @@ public static void receiveMessage() throws IOException { } catch (IOException e) { e.printStackTrace(); } - System.out.println("Jai recu le message >>> non bloqué "); + // System.out.println("Jai recu le message >>> non bloqué "); String recvStr = new String(recvPacket.getData(), 0, recvPacket.getLength()); InetAddress addr = recvPacket.getAddress(); int port = recvPacket.getPort(); //System.out.println("nouveau client : " + recvStr + "\n voici son adresse IP : " + addr + " et voici son port : " + port); - System.out.println("pseudo recu : " + recvStr); - - - - ajoutUserListe(recvStr, addr.toString()); + // System.out.println("pseudo recu : " + recvStr); + + if( !user.belongList(recvStr)){ SendMessage.sendMessage(addr, port, user); + ajoutUserListe(recvStr, addr.toString()); + } + else{ + // System.out.println(recvStr + "est deja dans la liste >>>>>>>>>>> "); + } } } - public static void ajoutUserListe(String pseudo, String ip){ + public synchronized static void ajoutUserListe(String pseudo, String ip){ user.setListUser(pseudo,ip); - System.out.println(user.getListUser().toString()); + // System.out.println(user.getListUser().toString()); } diff --git a/src/ThreadSend.java b/src/ThreadSend.java index c13524b..aeb53f2 100644 --- a/src/ThreadSend.java +++ b/src/ThreadSend.java @@ -19,7 +19,6 @@ public void run(){ } catch (IOException ex) { Logger.getLogger(ThreadSend.class.getName()).log(Level.SEVERE, null, ex); } - } public static void sendMessageBroadcast(User user) throws IOException { @@ -32,7 +31,7 @@ public static void sendMessageBroadcast(User user) throws IOException { //DatagramPacket packet2 = new DatagramPacket(data.getBytes(), // data.getBytes().length, address, 45048); - System.out.println("J'ai envoyé mon paquet en broadcast"); + // System.out.println("J'ai envoyé mon paquet en broadcast"); User.getSocketEnvoi().send(packet); } diff --git a/src/User.java b/src/User.java index 82ef214..266f952 100644 --- a/src/User.java +++ b/src/User.java @@ -36,8 +36,17 @@ public User(String pseudo){ Thread threadReceive = new ThreadReceive("thread receive", this); threadReceive.start(); + ThreadMenu threadMenu = new ThreadMenu("thread menu", this); + threadMenu.start(); + + + } + public User getUser(){ + return this; + } + public String getPseudo(){ return this.pseudo; } @@ -65,4 +74,11 @@ public HashMap getListUser(){ public void setListUser(String pseudo, String ip){ this.listUser.put(pseudo, ip); } + + public boolean belongList(String pseudo){ + if (listUser.containsKey(pseudo)){ + return true; + } + return false; + } } \ No newline at end of file