-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerMessage.java
executable file
·180 lines (153 loc) · 5.37 KB
/
ServerMessage.java
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
package myServer2;
/*
* AUTHOR : Min Gao
* Project1-Multi-Server Chat System
*/
import java.util.ArrayList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@SuppressWarnings("unchecked")
public class ServerMessage {
public static JSONObject lockIdentityRequest(String serverid, String identity) {
JSONObject lockIdentity = new JSONObject();
lockIdentity.put("type", "lockidentity");
lockIdentity.put("serverid", serverid);
lockIdentity.put("identity", identity);
return lockIdentity;
}
public static JSONObject lockIdentityReply(String serverid, String identity, boolean decide) {
JSONObject reply = new JSONObject();
reply.put("type", "lockidentity");
reply.put("serverid", serverid);
reply.put("identity", identity);
reply.put("locked", decide ? "true" : "false");
return reply;
}
public static JSONObject newIdentityReplyToClient(boolean decide) {
JSONObject reply = new JSONObject();
reply.put("type", "newidentity");
reply.put("approved", decide ? "true" : "false");
return reply;
}
public static JSONObject releaseIdentityLock(String serverid, String identity) {
JSONObject release = new JSONObject();
release.put("type", "releaseidentity");
release.put("serverid", serverid);
release.put("identity", identity);
return release;
}
public static JSONObject roomChange(String identity, String former, String roomidNow) {
JSONObject roomchange = new JSONObject();
roomchange.put("type", "roomchange");
roomchange.put("identity", identity);
roomchange.put("former", former);
roomchange.put("roomid", roomidNow);
return roomchange;
}
public static JSONObject roomListReply(ArrayList<String> roomList) {
JSONObject reply = new JSONObject();
JSONArray list = new JSONArray();
for (String room : roomList) {
list.add(room);
}
reply.put("type", "roomlist");
reply.put("rooms", list);
return reply;
}
public static JSONObject whoReply(ArrayList<String> clients, String roomid, String owner) {
JSONObject who = new JSONObject();
JSONArray list = new JSONArray();
for (String client : clients) {
list.add(client);
}
who.put("type", "roomcontents");
who.put("roomid", roomid);
who.put("identities", list);
who.put("owner", owner);
return who;
}
public static JSONObject lockRoomidRequest(String serverid, String roomid) {
JSONObject lockRoomid = new JSONObject();
lockRoomid.put("type", "lockroomid");
lockRoomid.put("serverid", serverid);
lockRoomid.put("roomid", roomid);
return lockRoomid;
}
public static JSONObject lockRoomidReply(String serverid, String roomid, boolean decide) {
JSONObject reply = new JSONObject();
reply.put("type", "lockroomid");
reply.put("serverid", serverid);
reply.put("roomid", roomid);
reply.put("locked", decide ? "true" : "false");
return reply;
}
public static JSONObject releaseRoomidLock(String serverid, String roomid, boolean decide) {
JSONObject release = new JSONObject();
release.put("type", "releaseroomid");
release.put("serverid", serverid);
release.put("roomid", roomid);
release.put("approved", decide ? "true" : "false");
return release;
}
public static JSONObject createRoomReplyToClient(String roomid, boolean decide) {
JSONObject reply = new JSONObject();
reply.put("type", "createroom");
reply.put("roomid", roomid);
reply.put("approved", decide ? "true" : "false");
return reply;
}
public static JSONObject routeInfoToClient(String roomid, String hostAddress, int port) {
JSONObject reply = new JSONObject();
reply.put("type", "route");
reply.put("roomid", roomid);
reply.put("host", hostAddress);
reply.put("port", Integer.toString(port));
return reply;
}
public static JSONObject serverChangeToClient(String serverid, boolean decide) {
JSONObject reply = new JSONObject();
reply.put("type", "serverchange");
reply.put("approved", decide ? "true" : "false");
reply.put("serverid", serverid);
return reply;
}
public static JSONObject deleteRoomToClient(String roomid, boolean decide) {
JSONObject reply = new JSONObject();
reply.put("type", "deleteroom");
reply.put("roomid", roomid);
reply.put("approved", decide ? "true" : "false");
return reply;
}
public static JSONObject deleteRoomInform(String serverid, String roomid) {
JSONObject inform = new JSONObject();
inform.put("type", "deleteroom");
inform.put("serverid", serverid);
inform.put("roomid", roomid);
return inform;
}
public static JSONObject message(String clientid, String content) {
JSONObject message = new JSONObject();
message.put("type", "message");
message.put("identity", clientid);
message.put("content", content);
return message;
}
public static JSONObject serverChange(String serverid, boolean decide) {
JSONObject serverChange = new JSONObject();
serverChange.put("type", "serverchange");
serverChange.put("approved", decide ? "true" : "false");
serverChange.put("serverid", serverid);
return serverChange;
}
public static JSONObject joinRoom(String roomid) {
JSONObject join = new JSONObject();
join.put("type", "join");
join.put("roomid", roomid);
return join;
}
public static JSONObject quit() {
JSONObject quit = new JSONObject();
quit.put("type", "quit");
return quit;
}
}