version finale du code serveur
This commit is contained in:
parent
dbd3861814
commit
3fe2faa785
11 changed files with 263 additions and 72 deletions
|
@ -34,7 +34,6 @@ class UDPServer extends Thread {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (this.running) {
|
while (this.running) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
//When a datagram is received, converts its data in a Message
|
//When a datagram is received, converts its data in a Message
|
||||||
|
@ -42,7 +41,7 @@ class UDPServer extends Thread {
|
||||||
this.sockUDP.receive(inPacket);
|
this.sockUDP.receive(inPacket);
|
||||||
String msgString = new String(inPacket.getData(), 0, inPacket.getLength());
|
String msgString = new String(inPacket.getData(), 0, inPacket.getLength());
|
||||||
Message msg = Message.stringToMessage(msgString);
|
Message msg = Message.stringToMessage(msgString);
|
||||||
|
|
||||||
//Depending on the type of the message
|
//Depending on the type of the message
|
||||||
switch (msg.getTypeMessage()) {
|
switch (msg.getTypeMessage()) {
|
||||||
case JE_SUIS_CONNECTE:
|
case JE_SUIS_CONNECTE:
|
||||||
|
|
|
@ -39,7 +39,7 @@ public class ControleurConnexion implements ActionListener{
|
||||||
this.sqlManager = new SQLiteManager(0);
|
this.sqlManager = new SQLiteManager(0);
|
||||||
this.vueStd = null;
|
this.vueStd = null;
|
||||||
|
|
||||||
int[] portServer = {2209, 2309, 2409, 2509};
|
int[] portServer = {2209, 2309, 2409, 2509, 3334};
|
||||||
try {
|
try {
|
||||||
switch(numtest) {
|
switch(numtest) {
|
||||||
case 0 :
|
case 0 :
|
||||||
|
|
|
@ -116,7 +116,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException e1) {
|
} catch (IOException e1) {
|
||||||
e1.printStackTrace();
|
vue.displayJOptionResponse("refusee");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -108,7 +108,6 @@ public class CommunicationUDP extends Thread {
|
||||||
Message message = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, idClient, pseudoClient, port);
|
Message message = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, idClient, pseudoClient, port);
|
||||||
observer.update(this, message);
|
observer.update(this, message);
|
||||||
} catch (MauvaisTypeMessageException e) {
|
} catch (MauvaisTypeMessageException e) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +118,6 @@ public class CommunicationUDP extends Thread {
|
||||||
Message message = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, idClient, pseudoClient, port);
|
Message message = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, idClient, pseudoClient, port);
|
||||||
observer.update(this, message);
|
observer.update(this, message);
|
||||||
} catch (MauvaisTypeMessageException e) {
|
} catch (MauvaisTypeMessageException e) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +131,6 @@ public class CommunicationUDP extends Thread {
|
||||||
Message message = new MessageSysteme(Message.TypeMessage.JE_SUIS_DECONNECTE, pseudoClient, idClient, port);
|
Message message = new MessageSysteme(Message.TypeMessage.JE_SUIS_DECONNECTE, pseudoClient, idClient, port);
|
||||||
observer.update(this, message);
|
observer.update(this, message);
|
||||||
} catch (MauvaisTypeMessageException e) {
|
} catch (MauvaisTypeMessageException e) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,7 +162,6 @@ public class CommunicationUDP extends Thread {
|
||||||
this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost());
|
this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,8 +184,6 @@ public class CommunicationUDP extends Thread {
|
||||||
this.client.sendMessageUDP_local(m, port, InetAddress.getLocalHost());
|
this.client.sendMessageUDP_local(m, port, InetAddress.getLocalHost());
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,8 +192,6 @@ public class CommunicationUDP extends Thread {
|
||||||
try {
|
try {
|
||||||
this.client.sendMessageUDP_local(m, port, InetAddress.getLocalHost());
|
this.client.sendMessageUDP_local(m, port, InetAddress.getLocalHost());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@ -33,20 +32,20 @@ public class ServletPresence extends HttpServlet implements Observer {
|
||||||
|
|
||||||
public ServletPresence() {
|
public ServletPresence() {
|
||||||
try {
|
try {
|
||||||
comUDP = new CommunicationUDP(3333, 3334, new int[] {2209, 2309, 2409, 3334});
|
comUDP = new CommunicationUDP(3333, 3334, new int[] {2209, 2309, 2409, 2509, 3334});
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
comUDP.setObserver(this);
|
comUDP.setObserver(this);
|
||||||
remoteUsers = new ArrayList<Utilisateur>();
|
remoteUsers = new ArrayList<Utilisateur>();
|
||||||
try {
|
try {
|
||||||
Utilisateur.setSelf("serv_p", "Serveur de presence", "localhost", 3334);
|
Utilisateur.setSelf("serv_p", "Serveur de presence", "localhost", 3334);
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----- REMOTE USER LIST MANAGEMENT ----- //
|
||||||
|
|
||||||
private int getIndexByID(String id) {
|
private int getIndexByID(String id) {
|
||||||
for(int i=0; i < remoteUsers.size() ; i++) {
|
for(int i=0; i < remoteUsers.size() ; i++) {
|
||||||
if(remoteUsers.get(i).getId().equals(id) ) {
|
if(remoteUsers.get(i).getId().equals(id) ) {
|
||||||
|
@ -65,7 +64,8 @@ public class ServletPresence extends HttpServlet implements Observer {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Fonctions d'affichage
|
|
||||||
|
// ----- HTML PRINT METHODS ----- //
|
||||||
|
|
||||||
//Affiche la liste des utilisateurs actifs
|
//Affiche la liste des utilisateurs actifs
|
||||||
private void printActiveUsers(PrintWriter out) {
|
private void printActiveUsers(PrintWriter out) {
|
||||||
|
@ -103,6 +103,7 @@ public class ServletPresence extends HttpServlet implements Observer {
|
||||||
out.println( "<H3>Se connecter : ?type=POST&id=[votre id]&pseudo=[pseudo voulu]&port=[port utilisé] </H3>" );
|
out.println( "<H3>Se connecter : ?type=POST&id=[votre id]&pseudo=[pseudo voulu]&port=[port utilisé] </H3>" );
|
||||||
out.println( "<H3>Se déconnecter : ?type=DELETE&id=[votre id] </H3>" );
|
out.println( "<H3>Se déconnecter : ?type=DELETE&id=[votre id] </H3>" );
|
||||||
out.println( "<H3>Changer de pseudo : ?type=PUT&id=[votre id]&pseudo=[pseudo voulu] </H3>" );
|
out.println( "<H3>Changer de pseudo : ?type=PUT&id=[votre id]&pseudo=[pseudo voulu] </H3>" );
|
||||||
|
out.println( "<H3>Rafraîchir la liste des utilisateurs : ?type=GET" );
|
||||||
out.println( "</BODY>" );
|
out.println( "</BODY>" );
|
||||||
out.println( "</HTML>" );
|
out.println( "</HTML>" );
|
||||||
}
|
}
|
||||||
|
@ -148,7 +149,9 @@ public class ServletPresence extends HttpServlet implements Observer {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Informe de la modification de la liste tous les utilisateurs internes et externes
|
// ----- NOTIFY METHOD ----- //
|
||||||
|
|
||||||
|
//Informe de la modification de la liste tous les utilisateurs internes et externes : appelée automatiquement à chaque modification de la liste
|
||||||
private void snotify(MessageSysteme message, Utilisateur user) {
|
private void snotify(MessageSysteme message, Utilisateur user) {
|
||||||
if (remoteUsers.contains(user)) {
|
if (remoteUsers.contains(user)) {
|
||||||
//diffuse le message localement, envoie la nouvelle liste des utilisateurs aux utilisateurs externes SAUF L'EXPEDITEUR
|
//diffuse le message localement, envoie la nouvelle liste des utilisateurs aux utilisateurs externes SAUF L'EXPEDITEUR
|
||||||
|
@ -167,6 +170,10 @@ public class ServletPresence extends HttpServlet implements Observer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ----- HTTP METHODS ----- //
|
||||||
|
|
||||||
// susbribe/unsubscribe : Permet a un utilisateur externe de s'ajouter/s'enlever à la liste des utilisateurs externes : au tout début de l'application
|
// susbribe/unsubscribe : Permet a un utilisateur externe de s'ajouter/s'enlever à la liste des utilisateurs externes : au tout début de l'application
|
||||||
//Note : le serveur agit comme un proxy pour le TCP et remplace le port de l'utilisateur par le sien
|
//Note : le serveur agit comme un proxy pour le TCP et remplace le port de l'utilisateur par le sien
|
||||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
@ -202,8 +209,6 @@ public class ServletPresence extends HttpServlet implements Observer {
|
||||||
printActiveUsersOnly(out);
|
printActiveUsersOnly(out);
|
||||||
}
|
}
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
@ -212,13 +217,11 @@ public class ServletPresence extends HttpServlet implements Observer {
|
||||||
String id = request.getParameter("id");
|
String id = request.getParameter("id");
|
||||||
int index = getIndexByID(id);
|
int index = getIndexByID(id);
|
||||||
Utilisateur user = remoteUsers.get(index);
|
Utilisateur user = remoteUsers.get(index);
|
||||||
remoteUsers.remove(index);
|
|
||||||
try {
|
try {
|
||||||
snotify(new MessageSysteme(Message.TypeMessage.JE_SUIS_DECONNECTE,"", id, -1), user);
|
snotify(new MessageSysteme(Message.TypeMessage.JE_SUIS_DECONNECTE,"", id, 3334), user);
|
||||||
} catch (MauvaisTypeMessageException e) {
|
} catch (MauvaisTypeMessageException e) {
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
remoteUsers.remove(index);
|
||||||
response.setContentType( "text/html" );
|
response.setContentType( "text/html" );
|
||||||
PrintWriter out = response.getWriter();
|
PrintWriter out = response.getWriter();
|
||||||
printHomePage(out);
|
printHomePage(out);
|
||||||
|
@ -245,14 +248,13 @@ public class ServletPresence extends HttpServlet implements Observer {
|
||||||
try {
|
try {
|
||||||
snotify(new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudo, id, 3334), user);
|
snotify(new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudo, id, 3334), user);
|
||||||
} catch (MauvaisTypeMessageException e) {
|
} catch (MauvaisTypeMessageException e) {
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
printActiveUsersOnly(out);
|
printActiveUsersOnly(out);
|
||||||
}
|
}
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Méthode générale, embranche vers l'une au l'autre des méthodes HTTP (à défaut d'une "vraie" interface)
|
||||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -271,6 +273,12 @@ public class ServletPresence extends HttpServlet implements Observer {
|
||||||
doPut(request, response);
|
doPut(request, response);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "GET" :
|
||||||
|
//Met à jour la liste des utilisateurs affichée
|
||||||
|
PrintWriter out1 = response.getWriter();
|
||||||
|
printActiveUsersOnly(out1);
|
||||||
|
out1.close();
|
||||||
|
|
||||||
//génère une jolie page
|
//génère une jolie page
|
||||||
default :
|
default :
|
||||||
response.setContentType( "text/html" );
|
response.setContentType( "text/html" );
|
||||||
|
@ -289,6 +297,7 @@ public class ServletPresence extends HttpServlet implements Observer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----- OBSERVER METHOD ----- //
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
//Note : on part du principe que pour les communications TCP et autres, le serveur agira comme un proxy et donc que les
|
//Note : on part du principe que pour les communications TCP et autres, le serveur agira comme un proxy et donc que les
|
||||||
|
@ -307,7 +316,6 @@ public class ServletPresence extends HttpServlet implements Observer {
|
||||||
try {
|
try {
|
||||||
comUDP.sendMessage(new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, u.getPseudo(), u.getId(), u.getPort()), port);
|
comUDP.sendMessage(new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, u.getPseudo(), u.getId(), u.getPort()), port);
|
||||||
} catch (MauvaisTypeMessageException e) {
|
} catch (MauvaisTypeMessageException e) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,7 @@ import java.net.*;
|
||||||
|
|
||||||
public class Utilisateur implements Serializable{
|
public class Utilisateur implements Serializable{
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private String id;
|
private String id;
|
||||||
|
@ -14,53 +12,104 @@ public class Utilisateur implements Serializable{
|
||||||
private InetAddress ip;
|
private InetAddress ip;
|
||||||
private int port;
|
private int port;
|
||||||
|
|
||||||
|
//Represents the user that is currently using the application
|
||||||
private static Utilisateur self;
|
private static Utilisateur self;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create and initialize an object representing an user
|
||||||
|
*
|
||||||
|
* @param id : user id as String
|
||||||
|
* @param pseudo : name under which other users can see this user as String
|
||||||
|
* @param ip : ip of the device this user is currently using as InetAddress
|
||||||
|
* @param port : on local mode, port used for the TCP listen socket as int
|
||||||
|
*
|
||||||
|
*/
|
||||||
public Utilisateur(String id, String pseudo, InetAddress ip, int port) throws UnknownHostException {
|
public Utilisateur(String id, String pseudo, InetAddress ip, int port) throws UnknownHostException {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.pseudo = pseudo;
|
this.pseudo = pseudo;
|
||||||
this.ip = ip;
|
this.ip = ip;
|
||||||
|
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----- GETTERS ----- //
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns user id as String
|
||||||
|
*
|
||||||
|
* @return user id as String
|
||||||
|
*/
|
||||||
public String getId() {
|
public String getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns user pseudo as String
|
||||||
|
*
|
||||||
|
* @return user pseudo as String
|
||||||
|
*/
|
||||||
public String getPseudo() {
|
public String getPseudo() {
|
||||||
return pseudo;
|
return pseudo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPseudo(String pseudo) {
|
/**
|
||||||
this.pseudo = pseudo;
|
* Returns user device's ip as String
|
||||||
}
|
*
|
||||||
|
* @return user device's ip as String
|
||||||
|
*/
|
||||||
public InetAddress getIp() {
|
public InetAddress getIp() {
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the port the user uses for their TCP listen socket as int
|
||||||
|
*
|
||||||
|
* @return TCP listen socket port as int
|
||||||
|
*/
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the user currently using this instance of the application as Utilisateur
|
||||||
|
*
|
||||||
|
* @return current user as Utilisateur
|
||||||
|
*/
|
||||||
|
public static Utilisateur getSelf() {
|
||||||
|
return Utilisateur.self;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----- SETTERS ----- //
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change the pseudo used by an user
|
||||||
|
*
|
||||||
|
* @param pseudo : new pseudo as String
|
||||||
|
*/
|
||||||
|
public void setPseudo(String pseudo) {
|
||||||
|
this.pseudo = pseudo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the self static attribute with a new Utilisateur
|
||||||
|
*
|
||||||
|
* @param id : user id as String
|
||||||
|
* @param pseudo : name under which other users can see this user as String
|
||||||
|
* @param ip : ip of the device this user is currently using as InetAddress
|
||||||
|
* @param port : on local mode, port used for the TCP listen socket as int
|
||||||
|
*/
|
||||||
public static void setSelf(String id, String pseudo, String host, int port) throws UnknownHostException {
|
public static void setSelf(String id, String pseudo, String host, int port) throws UnknownHostException {
|
||||||
if(Utilisateur.self == null) {
|
if(Utilisateur.self == null) {
|
||||||
Utilisateur.self = new Utilisateur(id, pseudo, InetAddress.getByName(host), port);
|
Utilisateur.self = new Utilisateur(id, pseudo, InetAddress.getByName(host), port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Utilisateur getSelf() {
|
/**
|
||||||
return Utilisateur.self;
|
* Sets the self static attribute with null
|
||||||
}
|
*/
|
||||||
|
|
||||||
public static void resetSelf() {
|
public static void resetSelf() {
|
||||||
Utilisateur.self = null;
|
Utilisateur.self = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString(){
|
|
||||||
return id + "###" + pseudo;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,48 +6,101 @@ import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
public abstract class Message implements Serializable {
|
public abstract class Message implements Serializable {
|
||||||
|
|
||||||
public enum TypeMessage {JE_SUIS_CONNECTE, JE_SUIS_DECONNECTE, INFO_PSEUDO, TEXTE, IMAGE, FICHIER, MESSAGE_NUL, FICHIER_INIT, FICHIER_ANSWER}
|
public enum TypeMessage {JE_SUIS_CONNECTE, JE_SUIS_DECONNECTE, INFO_PSEUDO, TEXTE, IMAGE, FICHIER, FICHIER_INIT, FICHIER_ANSWER}
|
||||||
protected TypeMessage type;
|
protected TypeMessage type;
|
||||||
private String dateMessage;
|
private String dateMessage;
|
||||||
private String sender;
|
private String sender;
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ------- GETTERS ------ //
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current date and time as a string using DateTimeFormatter and LocalDateTime
|
||||||
|
*
|
||||||
|
* @return date and time as a String
|
||||||
|
*/
|
||||||
public static String getDateAndTime() {
|
public static String getDateAndTime() {
|
||||||
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
|
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
|
||||||
LocalDateTime now = LocalDateTime.now();
|
LocalDateTime now = LocalDateTime.now();
|
||||||
return dtf.format(now);
|
return dtf.format(now);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the type of the message
|
||||||
|
*
|
||||||
|
* @return message type as TypeMessage
|
||||||
|
*/
|
||||||
public TypeMessage getTypeMessage() {
|
public TypeMessage getTypeMessage() {
|
||||||
return this.type;
|
return this.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDateMessage(String dateMessage) {
|
/**
|
||||||
this.dateMessage = dateMessage;
|
* Returns the date and time to which the message was timestamped
|
||||||
}
|
*
|
||||||
|
* @return date and time of timestamp as String
|
||||||
|
*/
|
||||||
public String getDateMessage() {
|
public String getDateMessage() {
|
||||||
return this.dateMessage;
|
return this.dateMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the sender of the message (used in the database)
|
||||||
|
*
|
||||||
|
* @return sender of message as String
|
||||||
|
*/
|
||||||
public String getSender() {
|
public String getSender() {
|
||||||
return this.sender ;
|
return this.sender ;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSender(String sender) {
|
|
||||||
this.sender = sender;
|
|
||||||
|
// ------ SETTERS ------ //
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the date of the message to a specific timestamp
|
||||||
|
*
|
||||||
|
* @param timestamp as (formatted) String
|
||||||
|
*/
|
||||||
|
public void setDateMessage(String dateMessage) {
|
||||||
|
this.dateMessage = dateMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the sender of the message to a specified string
|
||||||
|
*
|
||||||
|
* @param sender pseudo as String
|
||||||
|
*/
|
||||||
|
public void setSender(String sender) {
|
||||||
|
this.sender = sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----- MESSAGE-STRING CONVERSION METHODS -------//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a string representing the formatted list of attributes
|
||||||
|
*
|
||||||
|
*@return attributes as a String
|
||||||
|
*/
|
||||||
protected abstract String attributsToString();
|
protected abstract String attributsToString();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the message as a formatted string
|
||||||
|
*
|
||||||
|
*@return message as a String
|
||||||
|
*/
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.type+"###"+this.attributsToString();
|
return this.type+"###"+this.attributsToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Static method. Returns a message obtainer by parsing a given string
|
||||||
|
*
|
||||||
|
*@param String representing a message
|
||||||
|
*@return Message
|
||||||
|
*/
|
||||||
public static Message stringToMessage(String messageString) {
|
public static Message stringToMessage(String messageString) {
|
||||||
try {
|
try {
|
||||||
String[] parts = messageString.split("###");
|
String[] parts = messageString.split("###");
|
||||||
|
@ -74,21 +127,4 @@ public abstract class Message implements Serializable {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//tests ici
|
|
||||||
public static void main(String[] args) throws MauvaisTypeMessageException {
|
|
||||||
Message m1 = new MessageSysteme(TypeMessage.JE_SUIS_CONNECTE);
|
|
||||||
Message m2 = new MessageSysteme(TypeMessage.JE_SUIS_DECONNECTE,"aker", "man", 5000);
|
|
||||||
Message m3 = new MessageSysteme(TypeMessage.INFO_PSEUDO, "pseudo156434518", "id236", 1500);
|
|
||||||
Message m4 = new MessageTexte(TypeMessage.TEXTE, "blablabla");
|
|
||||||
Message m5 = new MessageFichier(TypeMessage.FICHIER, "truc", ".pdf");
|
|
||||||
|
|
||||||
|
|
||||||
System.out.println(Message.stringToMessage(m1.toString()));
|
|
||||||
System.out.println(Message.stringToMessage(m2.toString()));
|
|
||||||
System.out.println(Message.stringToMessage(m3.toString()));
|
|
||||||
System.out.println(Message.stringToMessage(m4.toString()));
|
|
||||||
System.out.println(Message.stringToMessage(m5.toString()));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,22 @@ public class MessageFichier extends Message {
|
||||||
private String contenu;
|
private String contenu;
|
||||||
private String extension;
|
private String extension;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a file message. These message are used for all interactions regarding file transfer.
|
||||||
|
*
|
||||||
|
* The "FICHIER_INIT" messages are used to inform the recipient application that you wish to transfer them files.
|
||||||
|
* The "FICHIER_ANSWER" messages are answers to "FICHIER_INIT" messages. They indicate that you are ready to receive the file.
|
||||||
|
* The "contenu" argument then contains the port on which you wish to receive the files.
|
||||||
|
*
|
||||||
|
* The "FICHIER" messages contains the files themselves.
|
||||||
|
* The "IMAGE" messages contains images files, which means the application will display a thumbnail for the image to the recipient.
|
||||||
|
*
|
||||||
|
* @param TypeMessage type (must be FICHIER_INIT, FICHIER_ANSWER, FICHIER or IMAGE, else an error is raised)
|
||||||
|
* @param contenu : message content as String
|
||||||
|
* @param extension : file extension as string
|
||||||
|
*
|
||||||
|
* @throws MauvaisTypeMessageException
|
||||||
|
*/
|
||||||
public MessageFichier(TypeMessage type, String contenu, String extension) throws MauvaisTypeMessageException{
|
public MessageFichier(TypeMessage type, String contenu, String extension) throws MauvaisTypeMessageException{
|
||||||
if ((type==TypeMessage.IMAGE)||(type==TypeMessage.FICHIER) ||(type==TypeMessage.FICHIER_INIT) || (type==TypeMessage.FICHIER_ANSWER) ) {
|
if ((type==TypeMessage.IMAGE)||(type==TypeMessage.FICHIER) ||(type==TypeMessage.FICHIER_INIT) || (type==TypeMessage.FICHIER_ANSWER) ) {
|
||||||
this.type=type;
|
this.type=type;
|
||||||
|
@ -17,14 +33,34 @@ public class MessageFichier extends Message {
|
||||||
else throw new MauvaisTypeMessageException();
|
else throw new MauvaisTypeMessageException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----- GETTERS ----- //
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns content of the message
|
||||||
|
*
|
||||||
|
* @return content as String
|
||||||
|
*/
|
||||||
public String getContenu() {
|
public String getContenu() {
|
||||||
return this.contenu;
|
return this.contenu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns extension of the file contained in the message (if the message contains a file)
|
||||||
|
*
|
||||||
|
* @return extension as String
|
||||||
|
*/
|
||||||
public String getExtension() {
|
public String getExtension() {
|
||||||
return this.extension;
|
return this.extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----- MESSAGE-STRING CONVERSION METHODS -------//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements attributsToString method of Message
|
||||||
|
*
|
||||||
|
* @return attributes as a String
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected String attributsToString() {
|
protected String attributsToString() {
|
||||||
return this.contenu+"###"+this.extension;
|
return this.contenu+"###"+this.extension;
|
||||||
|
|
|
@ -7,8 +7,18 @@ public class MessageSysteme extends Message {
|
||||||
private String id;
|
private String id;
|
||||||
private int port;
|
private int port;
|
||||||
|
|
||||||
|
// ------ CONSTRUCTORS ------ //
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a system message. These message are used for all system interactions by the UDP channel.
|
||||||
|
* The "JE_SUIS_CONNECTE" messages are used to inform the network that you just joined.
|
||||||
|
* They are sent directly after an user log in, and await multiple "INFO_PSEUDO" messages as answers, to build the table of users logged in.
|
||||||
|
*
|
||||||
|
* @param TypeMessage type (must be JE_SUIS_CONNECTE, else an error is raised)
|
||||||
|
* @throws MauvaisTypeMessageException
|
||||||
|
*/
|
||||||
public MessageSysteme(TypeMessage type) throws MauvaisTypeMessageException{
|
public MessageSysteme(TypeMessage type) throws MauvaisTypeMessageException{
|
||||||
if ((type==TypeMessage.JE_SUIS_CONNECTE)||(type==TypeMessage.MESSAGE_NUL)) {
|
if (type==TypeMessage.JE_SUIS_CONNECTE) {
|
||||||
this.type=type;
|
this.type=type;
|
||||||
this.pseudo="";
|
this.pseudo="";
|
||||||
this.id="";
|
this.id="";
|
||||||
|
@ -17,6 +27,20 @@ public class MessageSysteme extends Message {
|
||||||
else throw new MauvaisTypeMessageException();
|
else throw new MauvaisTypeMessageException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a system message. These message are used for all system interactions by the UDP channel.
|
||||||
|
* The "JE_SUIS_DECONNECTE" messages are used to inform the network that you just quit it.
|
||||||
|
*
|
||||||
|
* The "INFO_PSEUDO" are used to give informations about you to another user, much like an business card.
|
||||||
|
* They are used either as an answer to a "JE_SUIS_CONNECTE" message or to inform the network of a change of pseudo.
|
||||||
|
*
|
||||||
|
* @param TypeMessage type (must be JE_SUIS_DECONNECTE or INFO_PSEUDO, else an error is raised)
|
||||||
|
* @param pseudo : user pseudo as String
|
||||||
|
* @param id : user id as String
|
||||||
|
* @param port : "server" UDP port used by the application (used when the application id in local mode)
|
||||||
|
*
|
||||||
|
* @throws MauvaisTypeMessageException
|
||||||
|
*/
|
||||||
public MessageSysteme(TypeMessage type, String pseudo, String id, int port) throws MauvaisTypeMessageException {
|
public MessageSysteme(TypeMessage type, String pseudo, String id, int port) throws MauvaisTypeMessageException {
|
||||||
if (type==TypeMessage.INFO_PSEUDO ||(type==TypeMessage.JE_SUIS_DECONNECTE)) {
|
if (type==TypeMessage.INFO_PSEUDO ||(type==TypeMessage.JE_SUIS_DECONNECTE)) {
|
||||||
this.type=type;
|
this.type=type;
|
||||||
|
@ -28,18 +52,43 @@ public class MessageSysteme extends Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----- GETTERS ----- //
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns pseudo of the sender of the message (when type == INFO_PSEUDO)
|
||||||
|
*
|
||||||
|
* @return user pseudo as String
|
||||||
|
*/
|
||||||
public String getPseudo() {
|
public String getPseudo() {
|
||||||
return this.pseudo;
|
return this.pseudo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns id of the sender of the message (when type == INFO_PSEUDO)
|
||||||
|
*
|
||||||
|
* @return user id as String
|
||||||
|
*/
|
||||||
public String getId() {
|
public String getId() {
|
||||||
return this.id;
|
return this.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the "server" UDP port used by the sender of the message
|
||||||
|
*
|
||||||
|
* @return port as integer
|
||||||
|
*/
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
return this.port;
|
return this.port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----- MESSAGE-STRING CONVERSION METHODS -------//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements attributsToString method of Message
|
||||||
|
*
|
||||||
|
* @return attributes as a String
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected String attributsToString() {
|
protected String attributsToString() {
|
||||||
return this.pseudo+"###"+this.id+"###"+this.port;
|
return this.pseudo+"###"+this.id+"###"+this.port;
|
||||||
|
|
|
@ -7,6 +7,14 @@ public class MessageTexte extends Message {
|
||||||
private String contenu;
|
private String contenu;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a text message. These message are used for basic text conversation via TCP.
|
||||||
|
*
|
||||||
|
* @param TypeMessage type (must be TEXT, else an error is raised)
|
||||||
|
* @param contenu : message content as String
|
||||||
|
*
|
||||||
|
* @throws MauvaisTypeMessageException
|
||||||
|
*/
|
||||||
public MessageTexte(TypeMessage type, String contenu) throws MauvaisTypeMessageException{
|
public MessageTexte(TypeMessage type, String contenu) throws MauvaisTypeMessageException{
|
||||||
if (type==TypeMessage.TEXTE) {
|
if (type==TypeMessage.TEXTE) {
|
||||||
this.type=type;
|
this.type=type;
|
||||||
|
@ -16,11 +24,25 @@ public class MessageTexte extends Message {
|
||||||
else throw new MauvaisTypeMessageException();
|
else throw new MauvaisTypeMessageException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----- GETTERS ----- //
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns content of the message
|
||||||
|
*
|
||||||
|
* @return content as String
|
||||||
|
*/
|
||||||
public String getContenu() {
|
public String getContenu() {
|
||||||
return this.contenu;
|
return this.contenu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----- MESSAGE-STRING CONVERSION METHODS -------//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements attributsToString method of Message
|
||||||
|
*
|
||||||
|
* @return attributes as a String
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected String attributsToString() {
|
protected String attributsToString() {
|
||||||
return this.contenu;
|
return this.contenu;
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 77 KiB |
Loading…
Reference in a new issue