ChatApp-AL-NM/Implementation/chatapp/src/main/java/chatapp/Model/Utilisateur.java
2021-01-02 12:59:34 +04:00

94 wiersze
2,1 KiB
Java

package chatapp.Model;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* <p>
* Classe representant un Utilisateur
* Un Utilisateur est associe avec un pseudo, un numero de port, une addresse IP ainsi qu'un ID.
* </p>
*/
public class Utilisateur {
private String pseudo ;
private Integer port;
private final InetAddress ip ;
private final String id ;
Integer TAILLE_MAX = 5;
/**
* Constructeur : Utilisateur
* @param pseudo Le pseudo associe
* @param port
* @param ip
*/
public Utilisateur(String pseudo,Integer port, InetAddress ip ){
this.setPseudo(pseudo) ;
this.setPort(port);
this.ip = ip ;
String aux= ip.getHostName() ;
if(aux.length() > TAILLE_MAX)
aux = aux.substring(0, TAILLE_MAX);
this.id = aux ;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo(String pseudo) {
this.pseudo = pseudo;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public InetAddress getIp() {
return ip;
}
public String getId() {
return id;
}
@Override
public String toString(){
String s = "";
s+="pseudo " + this.pseudo + " | ";
s+="port " + (this.port).toString() + " | ";
s+="ip " + (this.ip).toString() + " | ";
s+="id " + (this.id).toString() + " | ";
return s;
}
public static Utilisateur stringToUtilisateur(String s) {
String name;
Integer port = 0;
String ip = "" ;
String id = "";
String mots[] = s.split(" ");
name=mots[1];
port=Integer.parseInt(mots[4]);
ip=mots[7];
id=mots[10];
Utilisateur user = null;
try {
user = new Utilisateur(name,port,InetAddress.getByName(ip.split("/")[1]));
} catch (UnknownHostException e) {
e.printStackTrace();
}
return user;
}
public Boolean equals(Utilisateur u) {
return this.getId().equals( u.getId() ) ;
}
}