diff --git a/POO/src/communication/udp/UDPServer.java b/POO/src/communication/udp/UDPServer.java index ee117dd..bb46858 100644 --- a/POO/src/communication/udp/UDPServer.java +++ b/POO/src/communication/udp/UDPServer.java @@ -34,7 +34,6 @@ class UDPServer extends Thread { @Override public void run() { while (this.running) { - try { //When a datagram is received, converts its data in a Message @@ -42,7 +41,7 @@ class UDPServer extends Thread { this.sockUDP.receive(inPacket); String msgString = new String(inPacket.getData(), 0, inPacket.getLength()); Message msg = Message.stringToMessage(msgString); - + //Depending on the type of the message switch (msg.getTypeMessage()) { case JE_SUIS_CONNECTE: diff --git a/POO/src/connexion/ControleurConnexion.java b/POO/src/connexion/ControleurConnexion.java index 5de1cd2..53772db 100644 --- a/POO/src/connexion/ControleurConnexion.java +++ b/POO/src/connexion/ControleurConnexion.java @@ -39,7 +39,7 @@ public class ControleurConnexion implements ActionListener{ this.sqlManager = new SQLiteManager(0); this.vueStd = null; - int[] portServer = {2209, 2309, 2409, 2509}; + int[] portServer = {2209, 2309, 2409, 2509, 3334}; try { switch(numtest) { case 0 : diff --git a/POO/src/standard/ControleurStandard.java b/POO/src/standard/ControleurStandard.java index 607068b..74feec4 100644 --- a/POO/src/standard/ControleurStandard.java +++ b/POO/src/standard/ControleurStandard.java @@ -116,7 +116,7 @@ public class ControleurStandard implements ActionListener, ListSelectionListener } } catch (IOException e1) { - e1.printStackTrace(); + vue.displayJOptionResponse("refusee"); } } diff --git a/POO_Server/src/communication/CommunicationUDP.java b/POO_Server/src/communication/CommunicationUDP.java index e418865..973b3d3 100644 --- a/POO_Server/src/communication/CommunicationUDP.java +++ b/POO_Server/src/communication/CommunicationUDP.java @@ -108,7 +108,6 @@ public class CommunicationUDP extends Thread { Message message = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, idClient, pseudoClient, port); observer.update(this, message); } 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); observer.update(this, message); } 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); observer.update(this, message); } catch (MauvaisTypeMessageException e) { - e.printStackTrace(); } } @@ -165,7 +162,6 @@ public class CommunicationUDP extends Thread { this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost()); } } catch (Exception e) { - e.printStackTrace(); } } @@ -188,8 +184,6 @@ public class CommunicationUDP extends Thread { this.client.sendMessageUDP_local(m, port, InetAddress.getLocalHost()); } } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } } @@ -198,8 +192,6 @@ public class CommunicationUDP extends Thread { try { this.client.sendMessageUDP_local(m, port, InetAddress.getLocalHost()); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } } diff --git a/POO_Server/src/main/ServletPresence.java b/POO_Server/src/main/ServletPresence.java index b5e3fb6..7a373ff 100644 --- a/POO_Server/src/main/ServletPresence.java +++ b/POO_Server/src/main/ServletPresence.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.io.PrintWriter; import java.net.InetAddress; import java.net.UnknownHostException; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; @@ -33,20 +32,20 @@ public class ServletPresence extends HttpServlet implements Observer { public ServletPresence() { 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) { - // TODO Auto-generated catch block - e.printStackTrace(); } comUDP.setObserver(this); remoteUsers = new ArrayList(); try { Utilisateur.setSelf("serv_p", "Serveur de presence", "localhost", 3334); } catch (UnknownHostException e) { - e.printStackTrace(); } } + + // ----- REMOTE USER LIST MANAGEMENT ----- // + private int getIndexByID(String id) { for(int i=0; i < remoteUsers.size() ; i++) { if(remoteUsers.get(i).getId().equals(id) ) { @@ -65,7 +64,8 @@ public class ServletPresence extends HttpServlet implements Observer { return false; } - //Fonctions d'affichage + + // ----- HTML PRINT METHODS ----- // //Affiche la liste des utilisateurs actifs private void printActiveUsers(PrintWriter out) { @@ -103,6 +103,7 @@ public class ServletPresence extends HttpServlet implements Observer { out.println( "

Se connecter : ?type=POST&id=[votre id]&pseudo=[pseudo voulu]&port=[port utilisé]

" ); out.println( "

Se déconnecter : ?type=DELETE&id=[votre id]

" ); out.println( "

Changer de pseudo : ?type=PUT&id=[votre id]&pseudo=[pseudo voulu]

" ); + out.println( "

Rafraîchir la liste des utilisateurs : ?type=GET" ); out.println( "" ); out.println( "" ); } @@ -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) { if (remoteUsers.contains(user)) { //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 //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 { @@ -202,8 +209,6 @@ public class ServletPresence extends HttpServlet implements Observer { printActiveUsersOnly(out); } } catch (UnknownHostException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } out.close(); } @@ -212,13 +217,11 @@ public class ServletPresence extends HttpServlet implements Observer { String id = request.getParameter("id"); int index = getIndexByID(id); Utilisateur user = remoteUsers.get(index); - remoteUsers.remove(index); 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) { - // TODO Auto-generated catch block - e.printStackTrace(); } + remoteUsers.remove(index); response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); printHomePage(out); @@ -245,14 +248,13 @@ public class ServletPresence extends HttpServlet implements Observer { try { snotify(new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudo, id, 3334), user); } catch (MauvaisTypeMessageException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } printActiveUsersOnly(out); } 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{ try { @@ -271,6 +273,12 @@ public class ServletPresence extends HttpServlet implements Observer { doPut(request, response); 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 default : response.setContentType( "text/html" ); @@ -289,6 +297,7 @@ public class ServletPresence extends HttpServlet implements Observer { } } + // ----- OBSERVER METHOD ----- // @Override //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 { comUDP.sendMessage(new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, u.getPseudo(), u.getId(), u.getPort()), port); } catch (MauvaisTypeMessageException e) { - e.printStackTrace(); } } } diff --git a/POO_Server/src/main/Utilisateur.java b/POO_Server/src/main/Utilisateur.java index 70c6348..1c69b88 100644 --- a/POO_Server/src/main/Utilisateur.java +++ b/POO_Server/src/main/Utilisateur.java @@ -4,9 +4,7 @@ import java.net.*; public class Utilisateur implements Serializable{ - /** - * - */ + private static final long serialVersionUID = 1L; private String id; @@ -14,53 +12,104 @@ public class Utilisateur implements Serializable{ private InetAddress ip; private int port; + //Represents the user that is currently using the application 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 { this.id = id; this.pseudo = pseudo; this.ip = ip; - this.port = port; } - + // ----- GETTERS ----- // + + /** + * Returns user id as String + * + * @return user id as String + */ public String getId() { return id; } + /** + * Returns user pseudo as String + * + * @return user pseudo as String + */ public String getPseudo() { 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() { 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() { 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 { if(Utilisateur.self == null) { 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() { Utilisateur.self = null; } - - public String toString(){ - return id + "###" + pseudo; - } } diff --git a/POO_Server/src/messages/Message.java b/POO_Server/src/messages/Message.java index fc5c65b..71a2c7f 100644 --- a/POO_Server/src/messages/Message.java +++ b/POO_Server/src/messages/Message.java @@ -6,48 +6,101 @@ import java.time.format.DateTimeFormatter; 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; private String dateMessage; private String sender; 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() { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); LocalDateTime now = LocalDateTime.now(); return dtf.format(now); } - + /** + * Returns the type of the message + * + * @return message type as TypeMessage + */ public TypeMessage getTypeMessage() { 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() { return this.dateMessage; } + /** + * Returns the sender of the message (used in the database) + * + * @return sender of message as String + */ public String getSender() { 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(); + /** + * Returns the message as a formatted string + * + *@return message as a String + */ public String toString() { 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) { try { String[] parts = messageString.split("###"); @@ -74,21 +127,4 @@ public abstract class Message implements Serializable { 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())); - - } - } diff --git a/POO_Server/src/messages/MessageFichier.java b/POO_Server/src/messages/MessageFichier.java index 15b2ded..0492ae8 100644 --- a/POO_Server/src/messages/MessageFichier.java +++ b/POO_Server/src/messages/MessageFichier.java @@ -7,6 +7,22 @@ public class MessageFichier extends Message { private String contenu; 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{ if ((type==TypeMessage.IMAGE)||(type==TypeMessage.FICHIER) ||(type==TypeMessage.FICHIER_INIT) || (type==TypeMessage.FICHIER_ANSWER) ) { this.type=type; @@ -17,14 +33,34 @@ public class MessageFichier extends Message { else throw new MauvaisTypeMessageException(); } + // ----- GETTERS ----- // + + + /** + * Returns content of the message + * + * @return content as String + */ public String getContenu() { 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() { return this.extension; } + // ----- MESSAGE-STRING CONVERSION METHODS -------// + + /** + * Implements attributsToString method of Message + * + * @return attributes as a String + */ @Override protected String attributsToString() { return this.contenu+"###"+this.extension; diff --git a/POO_Server/src/messages/MessageSysteme.java b/POO_Server/src/messages/MessageSysteme.java index 9c76271..58ab678 100644 --- a/POO_Server/src/messages/MessageSysteme.java +++ b/POO_Server/src/messages/MessageSysteme.java @@ -7,8 +7,18 @@ public class MessageSysteme extends Message { private String id; 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{ - if ((type==TypeMessage.JE_SUIS_CONNECTE)||(type==TypeMessage.MESSAGE_NUL)) { + if (type==TypeMessage.JE_SUIS_CONNECTE) { this.type=type; this.pseudo=""; this.id=""; @@ -17,6 +27,20 @@ public class MessageSysteme extends Message { 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 { if (type==TypeMessage.INFO_PSEUDO ||(type==TypeMessage.JE_SUIS_DECONNECTE)) { 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() { return this.pseudo; } + /** + * Returns id of the sender of the message (when type == INFO_PSEUDO) + * + * @return user id as String + */ public String getId() { return this.id; } + /** + * Returns the "server" UDP port used by the sender of the message + * + * @return port as integer + */ public int getPort() { return this.port; } + + // ----- MESSAGE-STRING CONVERSION METHODS -------// + + /** + * Implements attributsToString method of Message + * + * @return attributes as a String + */ @Override protected String attributsToString() { return this.pseudo+"###"+this.id+"###"+this.port; diff --git a/POO_Server/src/messages/MessageTexte.java b/POO_Server/src/messages/MessageTexte.java index 434598b..d68b215 100644 --- a/POO_Server/src/messages/MessageTexte.java +++ b/POO_Server/src/messages/MessageTexte.java @@ -7,6 +7,14 @@ public class MessageTexte extends Message { 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{ if (type==TypeMessage.TEXTE) { this.type=type; @@ -16,11 +24,25 @@ public class MessageTexte extends Message { else throw new MauvaisTypeMessageException(); } + // ----- GETTERS ----- // + + /** + * Returns content of the message + * + * @return content as String + */ public String getContenu() { return this.contenu; } + // ----- MESSAGE-STRING CONVERSION METHODS -------// + + /** + * Implements attributsToString method of Message + * + * @return attributes as a String + */ @Override protected String attributsToString() { return this.contenu; diff --git a/modelisation/Diagramme_de_classe_projet_coo_poo.png b/modelisation/Diagramme_de_classe_projet_coo_poo.png deleted file mode 100644 index 55031ed..0000000 Binary files a/modelisation/Diagramme_de_classe_projet_coo_poo.png and /dev/null differ