Permalink
Browse files

merged Thomas's changes

  • Loading branch information...
2 parents 9d5a688 + 8d99f40 commit faa1288c463cb2cb68e8258f0e7efe6dba595ac2 @theicfire committed May 16, 2012
View
@@ -10,9 +10,12 @@
public class Utils {
+ /**
+ * Helper function to return the current time as a timestamp.
+ * Used in the creation of messages then.
+ */
public static Timestamp getCurrentTimestamp() {
Date date= new Date();
return new Timestamp(date.getTime());
}
-
}
@@ -5,14 +5,27 @@
import java.util.regex.Pattern;
public class ConnectionMessage extends Message {
+ /**
+ * A ConnectionMessage is a message that is sent from the server to all users
+ * whenever a user connects or disconnects. If the user connects, the type of
+ * the message is CONNECT. Similarly, if the user disconnects, the type of the
+ * message is DISCONNECT.
+ *
+ * Extends the Message class, only carrying the additional information of what
+ * type of ConnectionMessage it is (CONNECT/DISCONNECT).
+ */
public static String patternStr = "(CONNECT|DISCONNECT)\\|([^\\|]*)\\|([^\\|]*)";
public static enum types {CONNECT, DISCONNECT};
public ConnectionMessage.types type;
+
+ /**
+ * Standard constructors.
+ * Uses the Message constructors, but also keeps track of the type.
+ */
public ConnectionMessage(String fromUsername, Timestamp timestamp, ConnectionMessage.types type) {
super(fromUsername, timestamp);
this.type = type;
}
-
public ConnectionMessage(String fromUsername, ConnectionMessage.types type) {
super(fromUsername);
this.type = type;
@@ -22,6 +35,12 @@ public String getStringMessage() {
return (type == types.CONNECT? "CONNECT" : "DISCONNECT") + "|" + fromUsername + "|" + timestamp.toString();
}
+ /**
+ * Parses a string input into a ConnectionMessage using regex.
+ * @param input - the input (a string)
+ * @return ConnectionMessage - the parsed ConnectionMessage
+ * @throws Exception if input isn't in the proper format.
+ */
public static ConnectionMessage parseStringMessage(String input) throws Exception {
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(input);
@@ -34,7 +53,12 @@ public static ConnectionMessage parseStringMessage(String input) throws Exceptio
}
throw new Exception("Invalid ConnectionMessage string: " + input);
}
-
+
+ /**
+ * Checks if the input is a valid ConnectionMessage.
+ * @param input - the input (a string)
+ * @return boolean - true if the input is valid, false otherwise.
+ */
public static boolean isConnectionMessage(String input){
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(input);
@@ -50,9 +74,6 @@ public String toString() {
+ super.toString() + "]";
}
public static void main(String[] args) throws Exception{
- ConnectionMessage connectionMessage = new ConnectionMessage("from",types.CONNECT);
- System.out.println(connectionMessage.toString());
- System.out.println(connectionMessage.getStringMessage());
- System.out.println(parseStringMessage(connectionMessage.getStringMessage()).toString());
+
}
}
View
@@ -5,19 +5,36 @@
import Utils.Utils;
public abstract class Message {
+ /**
+ * This is the base class for all messages sent between servers and clients.
+ * Note that there are never any plain Messages sent, always specific types
+ * of messages such as ConnectionMessages, TextMessages, and TypingMessages.
+ *
+ * Look more in the respective classes for more information about each. The
+ * important things to note are that each type of message will have a
+ * parseStringMethod and a
+ * getStringMethod method to aid in the translation between message and string
+ * so that we can easily send Messages between the server and client.
+ */
public static String patternStr = ""; //TODO
protected String fromUsername;
protected Timestamp timestamp;
+ /**
+ * Standard constructors.
+ */
public Message(String fromUsername, Timestamp timestamp) {
this.fromUsername = fromUsername.toLowerCase();
this.timestamp = timestamp;
}
-
public Message(String fromUsername) {
this.fromUsername = fromUsername;
this.timestamp = Utils.getCurrentTimestamp();
}
+
+ /**
+ * Standard getters and setters
+ */
public String getFromUsername() {
return fromUsername;
}
@@ -30,5 +47,11 @@ public Timestamp getTimestamp() {
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
+
+ /**
+ * Returns the message in the form of a string that can easily be
+ * passed between the server and clients. Each type of message will
+ * have different string representations, so this method is abstract.
+ */
public abstract String getStringMessage();
}
@@ -5,10 +5,21 @@
import java.util.regex.Pattern;
public class TextMessage extends ToMessage {
- // MSG|FROM|ROOM_ID|TIMESTAMP|TEXT
+ /**
+ * A ConnectionMessage is a message that is sent from the user to the server,
+ * and then propagated to other users in the room specified by roomID.
+ *
+ * Extends the ToMessage class, only carrying the additional information of
+ * what text the message contains.
+ */
+
public static String patternStr = "MSG\\|([0-9a-zA-Z]+)\\|([0-9]+)\\|([^\\|]*)\\|(.+)";
private String text;
-
+
+ /**
+ * Standard constructors.
+ * Uses the Message constructors, but also keeps track of the text.
+ */
public TextMessage(String fromUsername, int roomID,
Timestamp timestamp, String text) {
super(fromUsername, "", roomID, timestamp);
@@ -20,18 +31,26 @@ public TextMessage(String fromUsername, int roomID, String text) {
this.text = text;
}
+ /**
+ * Standard getter/setter
+ */
public String getText() {
return text;
}
-
public void setText(String text) {
this.text = text;
}
public String getStringMessage() {
return "MSG|" + fromUsername + "|" + roomID + "|" + timestamp.toString() + "|" + text;
}
-
+
+ /**
+ * Parses a string input into a TextMessage using regex.
+ * @param input - the input (a string)
+ * @return TextMessage - the parsed ConnectionMessage
+ * @throws Exception if input isn't in the proper format.
+ */
public static TextMessage parseStringMessage(String input) throws Exception {
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(input);
@@ -45,6 +64,17 @@ public static TextMessage parseStringMessage(String input) throws Exception {
throw new Exception("Invalid TextMessage string: " + input);
}
+ /**
+ * Checks if the input is a valid TextMessage.
+ * @param input - the input (a string)
+ * @return boolean - true if the input is valid, false otherwise.
+ */
+ public static boolean isTextMessage(String input){
+ Pattern pattern = Pattern.compile(patternStr);
+ Matcher matcher = pattern.matcher(input);
+ return matcher.matches();
+ }
+
@Override
public String toString() {
return "TextMessage [text=" + text + ", getText()=" + getText()
@@ -55,15 +85,7 @@ public String toString() {
+ super.toString() + "]";
}
- public static boolean isTextMessage(String input){
- Pattern pattern = Pattern.compile(patternStr);
- Matcher matcher = pattern.matcher(input);
- return matcher.matches();
- }
-
public static void main(String[] args) throws Exception{
- TextMessage textMessage = new TextMessage("from",0,"text");
- System.out.println(textMessage.toString());
- System.out.println(parseStringMessage(textMessage.getStringMessage()).toString());
+
}
}
@@ -7,6 +7,10 @@
protected String toUsername;
protected int roomID;
+ /**
+ * Standard Constructors.
+ * Users the Message constructors, but also keeps track of toUsername and roomID.
+ */
public ToMessage(String fromUsername, String toUsername, int roomID,
Timestamp timestamp) {
super(fromUsername, timestamp);
@@ -19,22 +23,28 @@ public ToMessage(String fromUsername, String toUsername, int roomID) {
this.toUsername = toUsername;
}
-
+ @Deprecated
+ /**
+ * Simply a helper function for getStringMessage().
+ * Was used in RequestMessage, but no longer used for anything.
+ * Kept just in case it might be helpful in the future.
+ */
public String getStringMessageSuffix(){
return "|" + fromUsername + "|" + toUsername + "|" + roomID + "|" + timestamp.toString();
}
+
+ /**
+ * Standard getters/setters
+ */
public String getToUsername() {
return toUsername;
}
-
public void setToUsername(String toUsername) {
this.toUsername = toUsername;
}
-
public int getRoomID() {
return roomID;
}
-
public void setRoomID(int roomID) {
this.roomID = roomID;
}
@@ -5,15 +5,27 @@
import java.util.regex.Pattern;
public class TypingMessage extends ToMessage {
+ /**
+ * A TypingMessage is a message that is sent whenever the typing status of a
+ * client in a chatroom changes. A TypingMessage can specify that the user is
+ * currently not doing anything, that the user is currently typing, or that the
+ * user has entered text.
+ *
+ * Extends the ToMessage class, only carrying the additional information of what
+ * type of TypingMessage it is (NOTHING/TYPING/ENETERED).
+ */
public static String patternStr = "TYPING\\|(NOTHING|TYPING|ENTERED)\\|([^\\|]*)\\|([^\\|]*)\\|([^\\|]*)";
public static enum types {NOTHING,TYPING,ENTERED};
public TypingMessage.types type;
+ /**
+ * Standard constructors.
+ * Uses the Message constructors, but also keeps track of the type.
+ */
public TypingMessage(String fromUsername, int roomID, Timestamp timestamp, TypingMessage.types type) {
super(fromUsername, "", roomID, timestamp);
this.type = type;
}
-
public TypingMessage(String fromUsername, int roomID, TypingMessage.types type) {
super(fromUsername, "", roomID);
this.type = type;
@@ -23,6 +35,9 @@ public String getStringMessage() {
return ("TYPING|" + typeAsString() + "|" + fromUsername + "|" + roomID + "|" + timestamp.toString());
}
+ /**
+ * Returns the type of the message as a string.
+ */
public String typeAsString(){
if(type == types.NOTHING)
return "NOTHING";
@@ -33,6 +48,9 @@ public String typeAsString(){
return null;
}
+ /**
+ * Returns the type as a string to help with getStringMessage().
+ */
public static TypingMessage.types stringAsType(String str){
if(str.equals("NOTHING"))
return types.NOTHING;
@@ -43,6 +61,27 @@ public String typeAsString(){
return null;
}
+ /**
+ * Returns the type as a suffix to describe if the conversation parter is
+ * not doing anything, typing, or has entered text. This suffix is then
+ * appended to a base title, and set to the title in another method.
+ */
+ public String toTitle() {
+ if(type == types.NOTHING)
+ return "";
+ if(type == types.TYPING)
+ return " - " + fromUsername + " is typing...";
+ if(type == types.ENTERED)
+ return " - " + fromUsername + " has entered text.";
+ return null;
+ }
+
+ /**
+ * Parses a string input into a TypingMessage using regex.
+ * @param input - the input (a string)
+ * @return TypingMessage - the parsed ConnectionMessage
+ * @throws Exception if input isn't in the proper format.
+ */
public static TypingMessage parseStringMessage(String input) throws Exception {
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(input);
@@ -55,7 +94,12 @@ public static TypingMessage parseStringMessage(String input) throws Exception {
}
throw new Exception("Invalid TypingMessage string: " + input);
}
-
+
+ /**
+ * Checks if the input is a valid TypingMessage.
+ * @param input - the input (a string)
+ * @return boolean - true if the input is valid, false otherwise.
+ */
public static boolean isTypingMessage(String input){
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(input);
@@ -70,20 +114,8 @@ public String toString() {
+ getClass() + ", hashCode()=" + hashCode() + ", toString()="
+ super.toString() + "]";
}
+
public static void main(String[] args) throws Exception{
- TypingMessage TypingMessage = new TypingMessage("from",0,types.TYPING);
- System.out.println(TypingMessage.toString());
- System.out.println(TypingMessage.getStringMessage());
- System.out.println(parseStringMessage(TypingMessage.getStringMessage()).toString());
- }
-
- public String toTitle() {
- if(type == types.NOTHING)
- return "";
- if(type == types.TYPING)
- return " - " + fromUsername + " is typing...";
- if(type == types.ENTERED)
- return " - " + fromUsername + " has entered text.";
- return null;
+
}
}

0 comments on commit faa1288

Please sign in to comment.