merge broadcast
This commit is contained in:
commit
8d70aa09e7
8 changed files with 126 additions and 62 deletions
|
@ -1,9 +1,14 @@
|
|||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InterfaceAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
|
@ -43,6 +48,7 @@ public class ChatApp {
|
|||
// Recuperer adresse IP de l'utilisateur
|
||||
InetAddress ip = null ;
|
||||
try {
|
||||
//ip = InetAddress.getByName("192.168.1.72");
|
||||
ip = InetAddress.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -70,12 +76,13 @@ public class ChatApp {
|
|||
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
||||
// Message que l'on envoie à tous les utilisateurs actifs
|
||||
String broadcastMessage = "Modification Pseudo\n" + this.getMe().getPseudo() + "\n" + nouveau + "\n";
|
||||
for(Integer p : ListPort ) {
|
||||
/*for(Integer p : ListPort ) {
|
||||
if(!(p.equals(this.getMe().getPort())))
|
||||
{
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
||||
}
|
||||
|
||||
|
||||
|
@ -89,12 +96,13 @@ public class ChatApp {
|
|||
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
||||
// Message que l'on envoie à tous les utilisateurs actifs
|
||||
String broadcastMessage = "Connexion\n" + this.getMe().toString() ;
|
||||
for(Integer p : ListPort ) {
|
||||
/*for(Integer p : ListPort ) {
|
||||
if(!(p.equals(this.getMe().getPort())))
|
||||
{
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -107,12 +115,13 @@ public class ChatApp {
|
|||
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
||||
// Message que l'on envoie à tous les utilisateurs actifs
|
||||
String broadcastMessage = "Deconnexion\n" + this.getMe().toString() ;
|
||||
for(Integer p : ListPort ) {
|
||||
/*for(Integer p : ListPort ) {
|
||||
if(!(p.equals(this.getMe().getPort())))
|
||||
{
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
||||
}
|
||||
|
||||
public static void main (String[] args) throws IOException {
|
||||
|
@ -123,6 +132,13 @@ public class ChatApp {
|
|||
ListPort.add(1234);
|
||||
ListPort.add(3000);
|
||||
ListPort.add(4000);
|
||||
InetAddress localHost = InetAddress.getLocalHost();
|
||||
System.out.println("Mon adresse:" + localHost.toString());
|
||||
/*for(InetAddress broadcastAddr : UDPEchange.listAllBroadcastAddresses()) {
|
||||
System.out.println("Broadcast sent with address " + broadcastAddr.toString());
|
||||
}*/
|
||||
//InetAddress broadcastAddress = InterfaceAddress.getBroadcast();
|
||||
//System.out.println("Mon adresse:" + localHost.toString());
|
||||
ExecutorService exec = Executors.newFixedThreadPool(1000);
|
||||
exec.submit(new Runner(app));
|
||||
try {
|
||||
|
@ -176,7 +192,10 @@ public class ChatApp {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
class Runner implements Runnable {
|
||||
ChatApp app ;
|
||||
public Runner(ChatApp app) {
|
||||
|
@ -189,4 +208,3 @@ class Runner implements Runnable {
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
@ -36,6 +34,7 @@ public class MessageHorodate {
|
|||
* @return Les differents attributs de la classe sous forme de string
|
||||
* </p>
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
String Msg = "";
|
||||
Msg += ("Destinataire: " + this.destinataire + "\n") ;
|
||||
|
|
Binary file not shown.
|
@ -1,5 +1,4 @@
|
|||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
|
Binary file not shown.
|
@ -2,7 +2,12 @@ import java.io.IOException;
|
|||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
/**
|
||||
|
@ -26,12 +31,24 @@ public class UDPEchange {
|
|||
*/
|
||||
public static void connexion( InetAddress broadcastAdress , String broadcastMessage , Integer port) throws IOException {
|
||||
// Envoie en broadcast à tous les utilsateurs
|
||||
for (InetAddress broadcastAddr : listAllBroadcastAddresses()) {
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
socket.setBroadcast(true);
|
||||
byte[]buffer = broadcastMessage.getBytes();
|
||||
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, InetAddress.getLoopbackAddress(), port );
|
||||
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, broadcastAddr, port );
|
||||
socket.send(packet);
|
||||
socket.close();
|
||||
System.out.println("Broadcast sent with address " + broadcastAddr.toString());
|
||||
}
|
||||
/*
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
socket.setBroadcast(true);
|
||||
byte[]buffer = broadcastMessage.getBytes();
|
||||
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, broadcastAdress, port );
|
||||
socket.send(packet);
|
||||
socket.close();
|
||||
*/
|
||||
|
||||
System.out.println("***********Message envoye***********");
|
||||
System.out.println("Dest Ip: " + broadcastAdress.toString());
|
||||
System.out.println("Dest port: " + port.toString());
|
||||
|
@ -89,8 +106,31 @@ public class UDPEchange {
|
|||
System.out.println(broadcastMessage);
|
||||
System.out.println("************************************");
|
||||
}
|
||||
|
||||
|
||||
static List<InetAddress> listAllBroadcastAddresses() throws SocketException {
|
||||
List<InetAddress> broadcastList = new ArrayList<>();
|
||||
Enumeration<NetworkInterface> interfaces
|
||||
= NetworkInterface.getNetworkInterfaces();
|
||||
while (interfaces.hasMoreElements()) {
|
||||
NetworkInterface networkInterface = interfaces.nextElement();
|
||||
|
||||
if (networkInterface.isLoopback() || !networkInterface.isUp()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
networkInterface.getInterfaceAddresses().stream()
|
||||
.map(a -> a.getBroadcast())
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(broadcastList::add);
|
||||
}
|
||||
return broadcastList;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Classe implémentant l'interface Runnable.
|
||||
|
@ -241,4 +281,8 @@ class RunnerUDP implements Runnable {
|
|||
( app.getActifUsers() ).supprimerList(Utilisateur.stringToUtilisateur(received.split("\n")[1]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ public class Utilisateur extends Object {
|
|||
return ip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
String s = "";
|
||||
s+="pseudo " + this.pseudo + " | ";
|
||||
|
|
3
Implementation/src/View.java
Normal file
3
Implementation/src/View.java
Normal file
|
@ -0,0 +1,3 @@
|
|||
public class View{
|
||||
|
||||
}
|
Loading…
Reference in a new issue