Package serveur

Source Code of serveur.ServeurChat

package serveur;

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.UnknownHostException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import client.InterfaceClientChat;

import util.Constantes;

/**
* Implemente l'interface InterfaceServeurChat pour fournir un serveur de chat RMI.<br/>
* Ce serveur est un "annuaire" liant les utilisateurs connectés à l'adresse à laquelle il se situent
* @author Meryeme AIT-MAMA, Yohann CALLEA, Babacar CISSE, Antoine DALLE-RIVE, Kevin FRANCES, Tarek JAOUANI
* @see java.rmi.server.UnicastRemoteObject
* @see InterfaceServeurChat
*/

public class ServeurChat extends UnicastRemoteObject implements InterfaceServeurChat {

    /**  Serial UID au 20-04-2011 */
  private static final long serialVersionUID = 20042011L;
 
  /** Adresse du serveur. */
    protected String adresse;
    /** Numero du port. */
    protected int port;
   
  /**
   * Table de hash contenant les pseudos et les URL des clients disponibles pour "chatter" <br/>
   * L'url est au format "rmi://adresse:port/pseudo
   */
  protected Map<String, String> clients;

 
 
    /**
     * Initialise l'ensemble contenant l'URL des clients distants, l'adresse du serveur et le numero du
     * port par defaut.
     * @throws RemoteException
     */
    public ServeurChat() throws RemoteException {
        super();
   
        System.out.println("Démarrage du serveur de chat...");
   
        this.port = Constantes.PORT;
        this.clients = new HashMap<String, String>();
       
        try {
      this.adresse = InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
      System.err.println("L'adresse IP de l'hôte ne peut être déterminée, le serveur n'a pas été lancé.");
      System.exit(1);
    }
       
        // Création du registre RMI, tant que le port choisi est utilisé, on incrémente le port (jusqu'à 500 tentatives)
    for(int i=500; i>0; i--) {
      try {
        LocateRegistry.createRegistry(port);
        break;
      } catch (RemoteException e) {
        port++;
      }
    }
   
    if (port == Constantes.PORT + 500) {
      System.err.println("Une erreur est survenue durant la création du registre");
      System.exit(1);
    }
   
    System.out.println("Serveur enregistré au registre sur le port " + port + "...");
       
        try {
            // Lie le serveur de chat à l'adresse indiquée
            Naming.rebind("rmi://" + adresse + ":" + port + "/" + Constantes.SERVER_NAME, this);
        System.out.println("Serveur de chat démarré");
        System.out.println("-------------------------------");
        System.out.println("URL de l'hôte : " + "rmi://" + adresse + ":" + port + "/" + Constantes.SERVER_NAME);
        System.out.println("-------------------------------");
    } catch (MalformedURLException e) {
      System.err.println("Erreur : L'URL du serveur est invalide");
      System.exit(1);
    }
    }


    /**
     * Initialise l'ensemble contenant l'adresse des clients distants, l'adresse du serveur et le numero du port.
     * @param adresse L'adresse du serveur.
     * @param port Le numéro du port.
     * @throws RemoteException
     */
    public ServeurChat(String adresse, int port) throws RemoteException {
        super();

        this.adresse = adresse;
        this.port = port;
        this.clients = new HashMap<String, String>();
       
        // Création du registre RMI.
        LocateRegistry.createRegistry(port);

        try {
            // Lie le serveur de chat à l'adresse indiquée
            Naming.rebind("rmi://" + adresse + ":" + port + "/" + Constantes.SERVER_NAME, this);
    } catch (MalformedURLException e) {
      System.err.println("L'adresse entrée en paramètre est mal formée.");
      System.exit(1);
    }
    }
   

   
  @Override
  public boolean enregistrer(String url) throws RemoteException {
        try {
            // On vérifie que l'url correspond à un client
          InterfaceClientChat client = (InterfaceClientChat) Naming.lookup(url);

          String pseudo = client.getUtilisateur();
         
          if (clients.get(pseudo) != null)
            return false;
         
          // Ajout du client à l'annuaire
            clients.put(pseudo, url);
           
            System.out.println(client.getUtilisateur() + " vient de se connecter au serveur de chat.");
        } catch(Exception ex) {
          return false;
        }
       
        return true;
    }   
 
  @Override
  public void desenregistrer(String user) throws RemoteException {
    if (clients.remove(user) != null)
      System.out.println(user + " est maintenant hors-ligne.");
  }

  @Override
  public String getAdresse(String pseudo) throws RemoteException {
    return clients.get(pseudo);
  }

  @Override
  public List<String> getConnectes() throws RemoteException {
    return new ArrayList<String>(clients.keySet());
  }
 
  @Override
    public List<String> getConnectes(String pseudo) throws RemoteException {
      return getConnectes();
    }

  @Override
  public String getUrl() throws RemoteException {
    return adresse;
  }
 
}
TOP

Related Classes of serveur.ServeurChat

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.