Skip to content

Commit

Permalink
avant de foutre le zbeul avec user
Browse files Browse the repository at this point in the history
  • Loading branch information
ElyesKhama committed Dec 14, 2017
1 parent 4023549 commit 931def8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

import java.util.Scanner;

public class Main {
public static void main(String[] args){
User user1 = new User("elyes2");

}
}
2 changes: 1 addition & 1 deletion src/SendMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions src/ThreadMenu.java
Original file line number Diff line number Diff line change
@@ -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 ;
}
}
}
}
21 changes: 12 additions & 9 deletions src/ThreadReceive.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());

}

Expand Down
3 changes: 1 addition & 2 deletions src/ThreadSend.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down
16 changes: 16 additions & 0 deletions src/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 931def8

Please sign in to comment.