Suite connexion() 3
This commit is contained in:
parent
00cedc86d8
commit
1d04a9000d
6 changed files with 89 additions and 37 deletions
Binary file not shown.
|
@ -1,11 +1,9 @@
|
|||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class ChatApp {
|
||||
|
||||
|
@ -28,20 +26,24 @@ public class ChatApp {
|
|||
this.me = new Utilisateur(pseudo,port,ip);
|
||||
this.actifUsers.add(me);
|
||||
}
|
||||
public void addList(Utilisateur u) {
|
||||
this.actifUsers.add(u);
|
||||
}
|
||||
public void afficherListeUtilisateurs() {
|
||||
System.out.println ("Liste des utilisateurs actifs : ");
|
||||
for(Utilisateur elem: this.actifUsers)
|
||||
{
|
||||
System.out.println (elem.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void connexion() throws IOException {
|
||||
// Envoie en broadcast à tous les utilsateurs
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
socket.setBroadcast(true);
|
||||
// @ de broadcast du réseau de l'utilisateur me
|
||||
InetAddress broadcastAdress = this.me.getIp(); // A MODIFIER
|
||||
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
||||
// Message que l'on envoie à tous les utilisateurs actifs
|
||||
String broadcastMessage = this.me.toString() ;
|
||||
byte[]buffer = broadcastMessage.getBytes();
|
||||
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, InetAddress.getLoopbackAddress(), 1234 );
|
||||
socket.send(packet);
|
||||
socket.close();
|
||||
System.out.println(this.me.getPseudo() + " envoie : " + broadcastMessage);
|
||||
Integer port = 1234 ;
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, port);
|
||||
}
|
||||
|
||||
|
||||
|
@ -54,29 +56,19 @@ public class ChatApp {
|
|||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
DatagramSocket socket = null;
|
||||
try {
|
||||
socket = new DatagramSocket(1234);
|
||||
} catch (SocketException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
byte buffer[] = new byte[1024];
|
||||
while(true)
|
||||
{
|
||||
DatagramPacket data = new DatagramPacket(buffer,buffer.length);
|
||||
try {
|
||||
socket.receive(data);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String received = new String(data.getData(), 0, data.getLength());
|
||||
System.out.println(app.me.getPseudo() + " reçoit : " + received);
|
||||
app.actifUsers.add(Utilisateur.stringToUtilisateur(received));
|
||||
for(Utilisateur elem: app.actifUsers)
|
||||
{
|
||||
System.out.println (elem.toString());
|
||||
} }
|
||||
|
||||
ExecutorService exec = Executors.newFixedThreadPool(1000);
|
||||
exec.submit(new Runner(app));
|
||||
}
|
||||
|
||||
}
|
||||
class Runner implements Runnable {
|
||||
ChatApp app ;
|
||||
public Runner(ChatApp app) {
|
||||
this.app = app ;
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
UDPEchange.ecouteUDP(app);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
BIN
Implementation/src/Runner.class
Normal file
BIN
Implementation/src/Runner.class
Normal file
Binary file not shown.
BIN
Implementation/src/RunnerUDP.class
Normal file
BIN
Implementation/src/RunnerUDP.class
Normal file
Binary file not shown.
BIN
Implementation/src/UDPEchange.class
Normal file
BIN
Implementation/src/UDPEchange.class
Normal file
Binary file not shown.
60
Implementation/src/UDPEchange.java
Normal file
60
Implementation/src/UDPEchange.java
Normal file
|
@ -0,0 +1,60 @@
|
|||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class UDPEchange {
|
||||
|
||||
public static void connexion( InetAddress broadcastAdress , String broadcastMessage , Integer port) throws IOException {
|
||||
// Envoie en broadcast à tous les utilsateurs
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
socket.setBroadcast(true);
|
||||
byte[]buffer = broadcastMessage.getBytes();
|
||||
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, InetAddress.getLoopbackAddress(), port );
|
||||
socket.send(packet);
|
||||
socket.close();
|
||||
System.out.println(broadcastMessage);
|
||||
}
|
||||
|
||||
public static void ecouteUDP(ChatApp app)
|
||||
{
|
||||
DatagramSocket socket = null;
|
||||
ExecutorService exec = Executors.newFixedThreadPool(1000);
|
||||
try {
|
||||
socket = new DatagramSocket(1234);
|
||||
} catch (SocketException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
byte buffer[] = new byte[1024];
|
||||
while(true)
|
||||
{
|
||||
DatagramPacket data = new DatagramPacket(buffer,buffer.length);
|
||||
try {
|
||||
socket.receive(data);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
exec.submit(new RunnerUDP(data,app));
|
||||
}
|
||||
}
|
||||
}
|
||||
class RunnerUDP implements Runnable {
|
||||
final DatagramPacket data ;
|
||||
ChatApp app ;
|
||||
|
||||
public RunnerUDP(DatagramPacket data, ChatApp app) {
|
||||
this.data= data;
|
||||
this.app = app ;
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Thread started");
|
||||
String received = new String(data.getData(), 0, data.getLength());
|
||||
System.out.println(received);
|
||||
app.addList(Utilisateur.stringToUtilisateur(received));
|
||||
app.afficherListeUtilisateurs();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue