javadoc package messages ok
This commit is contained in:
parent
dce7df5494
commit
df7fb8c542
4 changed files with 172 additions and 26 deletions
|
@ -1,53 +1,107 @@
|
|||
package messages;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
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 +128,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()));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package messages;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MessageSysteme extends Message {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
@ -7,8 +9,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 +29,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 +54,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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue