aboutsummaryrefslogtreecommitdiff
path: root/mobileapp/src/smoots/udesign/packet/.svn/text-base/ClientToServerPacket.java.svn-base
blob: b9fce4a5f95d0287a22baec2b0c054bd87bf0896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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;
	}
}