package client;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
import com.Message;
import serveur.InterfaceServeurChat;
import util.Constantes;
import util.ProfilException;
/**
* Implemente l'interface InterfaceClientChat pour fournir un client du service de chat RMI.
* @author Meryeme AIT-MAMA, Yohann CALLEA, Babacar CISSE, Antoine DALLE-RIVE, Kevin FRANCES, Tarek JAOUANI
* @see java.rmi.server.UnicastRemoteObject
* @see InterfaceClientChat
*/
public abstract class ClientChat extends UnicastRemoteObject implements InterfaceClientChat {
/** Serial UID au 20-04-2011 */
private static final long serialVersionUID = 20042011L;
/** Pseudo de l'utilisateur du client */
protected String pseudo;
/** Adresse du client */
protected String adresse;
/** Numéro du port à utiliser */
protected int port;
/** Le serveur de chat */
protected InterfaceServeurChat serveur;
/**
* Initialise l'adresse du serveur, le numero du port et le nom de l'utilisateur.
* @param serveur Adresse du serveur.
* @param port Numero du port.
* @param pseudo Nom de l'utilsateur.
* @throws RemoteException
*/
public ClientChat(String serveur, int port, String pseudo) throws RemoteException {
super();
this.port = port;
this.pseudo = pseudo;
try {
// Récupération de l'adresse locale
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);
}
try {
// Récupération des informations concernant le serveur et connexion du client
this.serveur = (InterfaceServeurChat)Naming.lookup("rmi://" + serveur + ":" + port + "/" + Constantes.SERVER_NAME);
// Définition de l'adresse correspondant à l'objet courant
Naming.rebind("rmi://" + adresse + ":" + port + "/" + pseudo, this);
} catch (MalformedURLException e) {
System.err.println("L'adresse entrée en paramètre est mal formée.");
System.exit(1);
} catch (NotBoundException e) {
System.err.println("Une erreur s'est produite durant le binding du client.");
System.exit(1);
}
}
/**
* Connecte ce client au serveur de chat.
* @throws RemoteException
* @throws ProfilException
*/
@Override
public void connexion() throws RemoteException, ProfilException {
if (!serveur.enregistrer("rmi://" + adresse + ":" + port + "/" + pseudo)) {
throw new ProfilException(ProfilException.EXISTING_USERNAME);
}
}
/**
* Déconnecte ce client du serveur de chat.<br/>
* Cette méthode doit obligatoirement être appellée lorsque le client quitte le chat.
* @throws RemoteException
*/
@Override
public void deconnexion() throws RemoteException {
// Désenregistre le client distant du serveur de chat.
serveur.desenregistrer(pseudo);
try {
// Détruit la liaison entre l'objet courant et l'url
Naming.unbind("rmi://" + adresse + ":" + port + "/" + pseudo);
} catch(Exception e) {
return;
}
}
@Override
public boolean envoyerMessage(Message message, String pseudo) throws RemoteException {
String url = serveur.getAdresse(pseudo);
InterfaceClientChat interlocuteur;
try {
interlocuteur = (InterfaceClientChat)Naming.lookup(url);
} catch (Exception e) {
return false;
}
interlocuteur.afficherMessage(message);
this.afficherMessage(message);
return true;
}
@Override
public abstract void afficherMessage(Message message) throws RemoteException;
@Override
public String getUtilisateur() throws RemoteException {
return pseudo;
}
@Override
public List<String> getConnectes() throws RemoteException {
return serveur.getConnectes();
}
@Override
public List<String> getConnectes(String pseudo)throws RemoteException {
return getConnectes();
}
// Lors de la destruction de l'objet, on déconnecte, si possible, le client.
public void finalize() {
try {
deconnexion();
} catch (Exception e) {
return;
}
}
}