210 lines
5.6 KiB
Java
210 lines
5.6 KiB
Java
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.Enumeration;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
|
|
/**
|
|
* <p>
|
|
* Classe récapitulant toutes les actions possibles pour un utilisateur
|
|
* </p>
|
|
*/
|
|
|
|
public class ChatApp {
|
|
|
|
/* Liste des utilisateurs actifs */
|
|
private ListUtilisateurs actifUsers ;
|
|
private static ArrayList<Integer> ListPort = new ArrayList<Integer>();
|
|
private Map<String,Historique> mapHistorique ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static ArrayList<Integer> getListPort() {
|
|
return ListPort;
|
|
}
|
|
|
|
/* ChatApp est associé à un utilisateur */
|
|
private Utilisateur me;
|
|
|
|
/**
|
|
* Constructeur de l'application de chat
|
|
*
|
|
* @param pseudo Pseudo de l'utilisateur
|
|
* @param port Port de communication
|
|
*/
|
|
public ChatApp(String pseudo, Integer port){
|
|
this.actifUsers = new ListUtilisateurs() ;
|
|
// 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();
|
|
}
|
|
//ip.getHostAddress();
|
|
this.me = new Utilisateur(pseudo,port,ip);
|
|
this.actifUsers.addList(getMe());
|
|
this.mapHistorique = new HashMap<String,Historique>() ;
|
|
}
|
|
|
|
public void majHistorique(String pseudo,Historique h) {
|
|
getMapHistorique().put(h.getUser2().getPseudo(),h);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Modification du pseudo de l'utilisateur
|
|
* Envoie en broadcast son ancien pseudo et son nouveau
|
|
*
|
|
* @param nouveau correspond au nouveau pseudo
|
|
*/
|
|
public void modifierPseudo(String nouveau) throws IOException {
|
|
// @ de broadcast du réseau de l'utilisateur me
|
|
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 ) {
|
|
if(!(p.equals(this.getMe().getPort())))
|
|
{
|
|
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
|
}
|
|
}*/
|
|
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
|
}
|
|
|
|
|
|
/**
|
|
* Methode appelée lors de la connexion d'un nouvel utilisateur.
|
|
* Il va prévenir les utilisateurs du réseau de son arrivée.
|
|
*
|
|
*/
|
|
public void connexion() throws IOException {
|
|
// @ de broadcast du réseau de l'utilisateur me
|
|
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 ) {
|
|
if(!(p.equals(this.getMe().getPort())))
|
|
{
|
|
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
|
}
|
|
}*/
|
|
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
|
}
|
|
|
|
/**
|
|
* Methode appelée lors de la déconnexion de l'utilisateur.
|
|
* Il va prévenir les utilisateurs du réseau de son départ.
|
|
*
|
|
*/
|
|
public void deconnexion() throws IOException {
|
|
// @ de broadcast du réseau de l'utilisateur me
|
|
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 ) {
|
|
if(!(p.equals(this.getMe().getPort())))
|
|
{
|
|
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
|
}
|
|
}*/
|
|
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
|
}
|
|
|
|
public static void main (String[] args) throws IOException {
|
|
//Integer p = 2345 ;
|
|
ChatApp app = new ChatApp(args[0],Integer.parseInt(args[1])) ;
|
|
InetAddress localHost = InetAddress.getLocalHost() ;
|
|
System.out.println("Mon adress: "+ localHost.toString());
|
|
ListPort.add(1234);
|
|
ListPort.add(3000);
|
|
ListPort.add(4000);
|
|
/*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 {
|
|
app.connexion();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
if (app.getMe().getPort() == 4000) {
|
|
try {
|
|
Thread.sleep(2000);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
app.modifierPseudo("Doudou");
|
|
try {
|
|
Thread.sleep(2000);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
app.modifierPseudo("Eliot");
|
|
/*try {
|
|
Thread.sleep(2000);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
app.deconnexion(); */
|
|
}
|
|
}
|
|
public Utilisateur getMe() {
|
|
return me;
|
|
}
|
|
|
|
public ListUtilisateurs getActifUsers() {
|
|
return actifUsers;
|
|
}
|
|
|
|
public Map<String,Historique> getMapHistorique() {
|
|
return mapHistorique;
|
|
}
|
|
|
|
public Historique getHist(String pseudo) {
|
|
Historique h = this.mapHistorique.get(pseudo);
|
|
if( h != null) {
|
|
return h ;
|
|
}
|
|
else {
|
|
h = new Historique(this.me, this.getActifUsers().getPseudoList(pseudo));
|
|
return h ;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
class Runner implements Runnable {
|
|
ChatApp app ;
|
|
public Runner(ChatApp app) {
|
|
this.app = app ;
|
|
}
|
|
@Override
|
|
public void run() {
|
|
|
|
UDPEchange.ecouteUDP(app);
|
|
|
|
}
|
|
}
|