Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
ElyesKhama committed Dec 14, 2017
1 parent 68e3124 commit 093f5fd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 34 deletions.
5 changes: 2 additions & 3 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
public class Main {
public static void main(String[] args){
User user1 = new User("elyes");
System.out.println("Test pull 1 ");
System.out.println("Test pull 1 2");


}
}
}
2 changes: 1 addition & 1 deletion src/SendMessage.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import java.io.IOException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
Expand Down
40 changes: 28 additions & 12 deletions src/ThreadReceive.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import java.io.IOException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.util.HashMap;

public class ThreadReceive extends Thread {

public ThreadReceive(String name){
super(name);
}

private static User user;
public ThreadReceive(String name, User user1){
super(name);
user = user1;
}

public void run(){
try {
receiveMessage();
} catch (IOException e) {
e.printStackTrace();
public void run(){
try {
receiveMessage();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void receiveMessage() throws IOException {

Expand All @@ -35,11 +39,23 @@ public static void receiveMessage() throws IOException {
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("nouveau client : " + recvStr + "\n voici son adresse IP : " + addr + " et voici son port : " + port);
System.out.println("pseudo recu : " + recvStr);



ajoutUserListe(recvStr, addr.toString());

}

}

public static void ajoutUserListe(String pseudo, String ip){
user.setListUser(pseudo,ip);

System.out.println(user.getListUser().toString());

}


}
14 changes: 8 additions & 6 deletions src/ThreadSend.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import java.io.IOException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;

public class ThreadSend extends Thread{

public ThreadSend(String name){

private User user ;
public ThreadSend(String name, User user){
super(name);
this.user = user;
}

public void run(){
try {
for(int i=0;i<10;i++) {
sendMessageBroadcast();
sendMessageBroadcast(user);
try {
sleep(3000);
} catch (InterruptedException e) {
Expand All @@ -23,9 +25,9 @@ public void run(){
}
}

public static void sendMessageBroadcast() throws IOException {
public static void sendMessageBroadcast(User user) throws IOException {

String data = "Easy" ;
String data = user.getPseudo() ;
User.getSocketEnvoi().setBroadcast(true);
InetAddress address = InetAddress.getByName("255.255.255.255"); //mettre l'adresse de broadcast directement
DatagramPacket packet = new DatagramPacket(data.getBytes(),
Expand Down
36 changes: 24 additions & 12 deletions src/User.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import java.io.IOException;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Date;

import static java.lang.Thread.sleep;
import java.util.HashMap;
import java.util.Map;

public class User {
private String pseudo;
private static DatagramSocket socketEnvoi;
private static DatagramSocket socketEcoute;
private ArrayList<String> listUser;
private static boolean firstMsg = false;

private HashMap<String, String> listUser ;


public User(String pseudo){
listUser = new ArrayList<>();
listUser = new HashMap<>();

try {
this.socketEnvoi = new DatagramSocket();

Expand All @@ -24,14 +28,14 @@ public User(String pseudo){
e.printStackTrace();
}
this.pseudo = pseudo;

System.out.println("J'ai crée un utilisateur ; son pseudo est : " + pseudo );
Thread threadSend = new ThreadSend("thread send");
Thread threadSend = new ThreadSend("thread send", this);
threadSend.start();

Thread threadReceive = new ThreadReceive("thread receive");
Thread threadReceive = new ThreadReceive("thread receive", this);
threadReceive.start();

}

public String getPseudo(){
Expand All @@ -45,16 +49,24 @@ public void setPseudo(String pseudo){
public String toString() {
return this.pseudo;
}

public static DatagramSocket getSocketEnvoi(){
return socketEnvoi;
}

public static DatagramSocket getSocketEcoute(){
return socketEcoute;
}



public HashMap getListUser(){
return this.listUser;
}

public void setListUser(String pseudo, String ip){
this.listUser.put(pseudo, ip);
}




}

0 comments on commit 093f5fd

Please sign in to comment.