aboutsummaryrefslogtreecommitdiff
path: root/mobileapp/src/smoots/udesign/packet/.svn/text-base/ClientToServerPacket.java.svn-base
diff options
context:
space:
mode:
Diffstat (limited to 'mobileapp/src/smoots/udesign/packet/.svn/text-base/ClientToServerPacket.java.svn-base')
-rwxr-xr-xmobileapp/src/smoots/udesign/packet/.svn/text-base/ClientToServerPacket.java.svn-base236
1 files changed, 236 insertions, 0 deletions
diff --git a/mobileapp/src/smoots/udesign/packet/.svn/text-base/ClientToServerPacket.java.svn-base b/mobileapp/src/smoots/udesign/packet/.svn/text-base/ClientToServerPacket.java.svn-base
new file mode 100755
index 0000000..b9fce4a
--- /dev/null
+++ b/mobileapp/src/smoots/udesign/packet/.svn/text-base/ClientToServerPacket.java.svn-base
@@ -0,0 +1,236 @@
+package packet;
+
+/**
+ * Client to Server Packet data type including parser and toText converters.
+ * @author seojin
+ *
+ */
+public class ClientToServerPacket implements Packet{
+
+ /* PROTOCOL
+ Packet ::= AdministrativePacket | ConversationalPacket
+ AdministrativePacket ::= (defaultID | clientID) \t unaryAction \t screenName?
+ ConversationalPacket ::= clientID \t actionType \t (ConversationID | clientID) \n (Message | inviteID)?
+ actionType ::= OpenConversation | ExitConversation | sendMessage | InviteUser | acceptInvitation | rejectInvitation
+ unaryAction ::= logIn | logOut | changeScreenName | requestWholeUserList
+ **optional clientID(with choice of conversationID after actionType) only follows openConversation.
+ **inviteID is for the invitation action. For invitation action, we have conversationID first and clientID to invite in next line.
+ **defaultID is only followed by logIn and screenName.
+ */
+
+ /**
+ * Class for Admin type packet in Client to Server Packet
+ */
+ public static class Admin extends ClientToServerPacket{
+ public final boolean isDefaultID; //if this is false, unaryAction should be logIn and have screenName after.
+ public final int senderID;
+ public final UnaryAction unaryAction;
+ public final String screenName;
+ public String host;
+ /**
+ * Constructor for Admin packet.
+ * @param senderID sender's client ID.
+ * @param unaryAction unary action type for this packet.
+ * @param screenName screenName of sender.
+ */
+ public Admin(int senderID, UnaryAction unaryAction, String screenName){
+ this.isDefaultID = false;
+ this.unaryAction = unaryAction;
+ this.screenName = screenName;
+ this.senderID = senderID;
+ }
+ /**
+ * Constructor for Admin packet.
+ * @param isDefaultID set this as true if the sender doesn't know its ID yet.
+ * @param unaryAction unary action type for this packet.
+ * @param screenName screenName of sender.
+ */
+ public Admin(boolean isDefaultID, UnaryAction unaryAction, String screenName){
+ this.isDefaultID = isDefaultID;
+ this.unaryAction = unaryAction;
+ this.screenName = screenName;
+ this.senderID = 0;
+ }
+
+ /**
+ * Constructor for Admin packet.
+ * @param isDefaultID set this as true if the sender doesn't know its ID yet.
+ * @param unaryAction unary action type for this packet.
+ * @param host for user to designate the host. String host is handled at the processor when creating the socket. this is not sent through socket.
+ * @param screenName screenName of sender.
+ */
+ public Admin(boolean isDefaultID, UnaryAction unaryAction, String host, String screenName){
+ this.isDefaultID = isDefaultID;
+ this.unaryAction = unaryAction;
+ this.screenName = screenName;
+ this.host = host;
+ this.senderID = 0;
+ }
+ /**
+ * Constructor for Admin packet.
+ * @param senderID sender's client ID.
+ * @param unaryAction unary action type for this packet.
+ */
+ public Admin(int senderID, UnaryAction unaryAction){
+ this.isDefaultID = false;
+ this.unaryAction = unaryAction;
+ this.screenName = null;
+ this.senderID = senderID;
+ }
+ }
+
+ /**
+ * Class for Conversation type packet in Client to Server Packet
+ * @author seojin
+ */
+ public static class Conv extends ClientToServerPacket{
+ public final int senderID;
+ public final CSActionType action;
+ public final int receiverID; //either conversation or a client.
+ public final String message;
+ public final Integer inviteID;
+ /**
+ * Constructor for Conv packet.
+ * @param senderID sender's client ID.
+ * @param action action type of this packet.
+ * @param receiverID it should be conversationID if it is for already existing conversation, or another clientID if it is to initiate conversation.
+ * @param message message
+ */
+ public Conv(int senderID, CSActionType action, int receiverID, String message){
+ this.senderID = senderID;
+ this.action = action;
+ this.receiverID = receiverID;
+ this.message = message;
+ this.inviteID = null;
+ }
+ /**
+ * Constructor for Conv packet.
+ * @param senderID sender's client ID.
+ * @param action action type of this packet. (Here, invitation is suggested)
+ * @param conversationID conversationID of conversation of this packet about.
+ * @param inviteID another client ID to invite user to this conversation.
+ */
+ public Conv(int senderID, CSActionType action, int conversationID, int inviteID){
+ this.senderID = senderID;
+ this.action = action;
+ this.receiverID = conversationID;
+ this.message = null;
+ this.inviteID = inviteID;
+ }
+ /**
+ * Constructor for Conv packet.
+ * @param senderID sender's client ID.
+ * @param action action type of this packet.
+ * @param receiverID it should be conversationID if it is for already existing conversation, or another clientID if it is to initiate conversation.
+ */
+ public Conv(int senderID, CSActionType action, int receiverID){
+ this.senderID = senderID;
+ this.action = action;
+ this.receiverID = receiverID;
+ this.message = null;
+ this.inviteID = null;
+ }
+ }
+
+
+
+ /**
+ * Will return either ClientToServerPacket.Admin or ClientToServerPacket.Conv.
+ * You may use instanceof to figure out which one it is.
+ * @param textFormat text format packet to be parsed into Packet.
+ * @return parsed packet either ClientToServerPacket.Admin or ClientToServerPacket.Conv
+ */
+ public static ClientToServerPacket parsePacket(String textFormat) {
+ System.out.println(textFormat);
+ String[] lines = textFormat.split("\n");
+ int nLine = Integer.parseInt(lines[0]);
+ String[] token = lines[1].split("\t");
+ if(token[0].equals("Admin")){
+ Admin res;
+ int senderID = Integer.parseInt(token[1]);
+ UnaryAction ua = UnaryAction.valueOf(token[2]);
+ if(ua == UnaryAction.changeScreenName || ua == UnaryAction.logIn){
+ String screenName = token[3];
+ if(senderID == 0){
+ res = new Admin(true, ua, screenName);
+ }else{
+ res = new Admin(senderID, ua, screenName);
+ }
+ }else{
+ res = new Admin(senderID, ua);
+ }
+ return res;
+ }
+ else if(token[0].equals("Conv")){
+ Conv res;
+ int senderID = Integer.parseInt(token[1]);
+ CSActionType action = CSActionType.valueOf(token[2]);
+ int receiverID = Integer.parseInt(token[3]);
+ if(action == CSActionType.sendMessage){
+ String message = lines[2];
+ for(int i = 3; i < nLine; i++){
+ message += "\n" + lines[i];
+ }
+ res = new Conv(senderID, action, receiverID, message);
+ }else if(action == CSActionType.InviteUser){
+ int inviteID = Integer.parseInt(lines[2]);
+ res = new Conv(senderID, action, receiverID, inviteID);
+ }else{
+ res = new Conv(senderID, action, receiverID);
+ }
+ return res;
+ }else{
+ System.out.println("Error in format.");
+ }
+ return null;
+ }
+
+ /**
+ * Count newline characters.
+ * @param s string to count newline character.
+ * @return number of newline characters in string s
+ */
+ private static int countNewline(String s){
+ int count = 0;
+ for(int i = 0; i < s.length(); i++){
+ if(s.charAt(i) == '\n')
+ count++;
+ }
+ return count;
+ }
+
+ @Override
+ public String packetToText(){
+ String res = "";
+ int nLine = 1;
+ if (this instanceof ClientToServerPacket.Admin){
+ Admin p = (Admin) this;
+ res += "Admin\t";
+ if(p.isDefaultID == true){
+ res += "0\t";
+ }else{
+ res += p.senderID + "\t";
+ }
+ res += p.unaryAction + "\t";
+ if(p.screenName != null){
+ res += p.screenName;
+ }
+ res += "\n";
+ nLine += 1;
+ }
+ else if(this instanceof Conv){
+ Conv p = (Conv) this;
+ res += "Conv\t";
+ res += p.senderID + "\t";
+ res += p.action + "\t";
+ res += p.receiverID + "\n";
+ if(p.action == CSActionType.sendMessage)
+ res += p.message + "\n";
+ else if(p.action == CSActionType.InviteUser)
+ res += p.inviteID + "\n";
+ nLine = 1 + countNewline(res);
+ }
+ res = nLine + "\n" + res;
+ return res;
+ }
+}