KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chipchat > Communicator


1 /*
2  * Created on 2003. 2. 20.
3  */

4 package chipchat;
5
6 import java.io.BufferedReader JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.InputStreamReader JavaDoc;
10 import java.io.OutputStream JavaDoc;
11
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14 import javax.servlet.http.HttpSession JavaDoc;
15
16 /**
17  * Communicate with client.
18  * @author Mr. Lee
19  *
20  */

21 public final class Communicator {
22     /*
23      * Env values.
24      */

25
26     /** Server name */
27     private static final String JavaDoc SERVERNAME =
28         Env.getInstance().getProperty("Communicator.serverName");
29
30     /** Admin Password */
31     private static final String JavaDoc ADMINPW =
32         Env.getInstance().getProperty("Communicator.adminpasswd");
33
34     /**
35      * Constructor.
36      */

37     public Communicator() {
38     }
39
40     /**
41      * Service chipchat function.
42      * @param req ServletRequest
43      * @param resp ServletResponse
44      */

45     public void service(
46         final HttpServletRequest JavaDoc req,
47         final HttpServletResponse JavaDoc resp) {
48         try {
49             // Input Stream and output Stream...
50
InputStream JavaDoc inputStream = req.getInputStream();
51             OutputStream JavaDoc outputStream = resp.getOutputStream();
52
53             // Get User Information from session.
54
HttpSession JavaDoc session = req.getSession(false);
55
56             if (session == null) {
57                 outputStream.write("ERROR:SessionNotExist".getBytes());
58                 outputStream.flush();
59                 return;
60             }
61
62             session.setMaxInactiveInterval(60 * 60 * 2);
63
64             String JavaDoc name = (String JavaDoc) session.getAttribute("chipchat_username");
65             String JavaDoc channel = (String JavaDoc) session.getAttribute("chipchat_channel");
66             String JavaDoc roompw = (String JavaDoc) session.getAttribute("chipchat_roompw");
67             int userid;
68             Long JavaDoc roomid;
69             try {
70                 userid =
71                     Integer.parseInt(
72                         (String JavaDoc) session.getAttribute("chipchat_userid"));
73                 roomid = (Long JavaDoc) session.getAttribute("chipchat_roomid");
74                 session.setAttribute("chipchat_roomid", null);
75             } catch (NumberFormatException JavaDoc e) {
76                 outputStream.write("ERROR:WrongSessionValue".getBytes());
77                 outputStream.flush();
78                 return;
79             }
80
81             User user = new User(name, userid, outputStream);
82
83             service(user, channel, roomid, roompw, inputStream);
84
85         } catch (Exception JavaDoc e) {
86             System.err.println("Exception : 93");
87             e.printStackTrace();
88         }
89     }
90
91     /**
92      *
93      * @param conName
94      * @param input
95      * @param output
96      */

97     public void service(
98         String JavaDoc conName,
99         final InputStream JavaDoc input,
100         final OutputStream JavaDoc output) {
101         try {
102             ConnectionInfo info = ConnectionWaiter.getInstance().get(conName);
103             if (info == null) {
104                 output.write("ERROR:SessionNotExist".getBytes());
105                 output.flush();
106                 return;
107             }
108
109             // session.setMaxInactiveInterval(60 * 60 * 2);
110

111             User user = new User(info.getName(), info.getUserid(), output);
112             service(
113                 user,
114                 info.getChannel(),
115                 info.getRoomid(),
116                 info.getRoompw(),
117                 input);
118         } catch (Exception JavaDoc e) {
119             System.err.println("Exception : 93");
120             e.printStackTrace();
121         }
122     }
123
124     /**
125      * Service chipchat function.
126      * @param user User
127      * @param channel Channel name
128      * @param roomid Room id
129      * @param passwd Password
130      * @param inputStream Input Stream
131      */

132     public void service(
133         final User user,
134         final String JavaDoc channel,
135         final Long JavaDoc roomid,
136         final String JavaDoc passwd,
137         final InputStream JavaDoc inputStream) {
138         try {
139             // Input Stream and output Stream...
140
BufferedReader JavaDoc reader =
141                 new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream));
142             OutputStream JavaDoc outputStream = user.getOutputStream();
143
144             outputStream.write(
145                 ("ConnectID:" + user.getUserid() + "\r\n").getBytes());
146             outputStream.write(
147                 ("ConnectName:" + user.getUsername() + "\r\n").getBytes());
148
149             final Channel channel2 = ChipChat.getInstance().getChannel(channel);
150
151             final Room room = channel2.getRoom(roomid);
152
153             if (room == null) {
154                 outputStream.write("ERROR:RoomIsNotExist\r\n".getBytes());
155                 outputStream.flush();
156                 return;
157             }
158
159             int enter;
160             if ("00admin".equals(user.getUsername())
161                 && ("\t" + ADMINPW).equals(passwd)) {
162                 enter = room.enterAdmin(user, passwd);
163             } else {
164                 enter = room.enterUser(user, passwd);
165             }
166
167             if (enter != 0) {
168                 if (enter == -1) {
169                     outputStream.write("ERROR:RoomIsFull\r\n".getBytes());
170                 } else if (enter == -2) {
171                     outputStream.write("ERROR:PasswordNotMatch\r\n".getBytes());
172                 } else {
173                     outputStream.write("ERROR:Unknown\r\n".getBytes());
174                 }
175                 outputStream.flush();
176                 return;
177             }
178
179             outputStream.write(("Connected:" + SERVERNAME + "\r\n").getBytes());
180             outputStream.write(
181                 ("ADMIN:" + room.getMaster() + "\r\n").getBytes());
182             outputStream.flush();
183
184             boolean connected = true;
185
186             room.inputRoomInfo();
187
188             try {
189                 while (connected) {
190                     String JavaDoc r = reader.readLine();
191                     if (r == null) {
192                         // Connection Closed...
193
break;
194                     }
195                     try {
196                         int index = r.indexOf(":");
197                         if (index < 0) {
198                             System.out.println(
199                                 "NO Command Found .... Skipping....:" + r);
200                             continue;
201                         }
202                         String JavaDoc cmd = r.substring(0, index);
203                         if (cmd.equalsIgnoreCase("MSG")) { // message
204
room.inputMsg(
205                                 -1,
206                                 r.substring(index + 1),
207                                 user.getUsername());
208                         } else if (cmd.equalsIgnoreCase("WSP")) { // whisper
209
try {
210                                 int index2 = r.indexOf(":", index + 1);
211                                 int to =
212                                     Integer.parseInt(
213                                         r.substring(index + 1, index2));
214                                 room.inputWhisper(
215                                     user.getUserid().intValue(),
216                                     to,
217                                     r.substring(index2 + 1),
218                                     user.getUsername());
219                             } catch (NumberFormatException JavaDoc e) {
220                                 System.out.println("Wrong User ID:" + r);
221                             }
222                         } else if (cmd.equalsIgnoreCase("ACK")) {
223                             // Acknowlege
224
outputStream.write("ACK:\r\n".getBytes());
225                             outputStream.flush();
226                             continue;
227                         } else if (cmd.equalsIgnoreCase("KEEPQUIET")) {
228                             try {
229                                 int to =
230                                     Integer.parseInt(r.substring(index + 1));
231                                 room.inputKeepQuiet(
232                                     user.getUserid().intValue(),
233                                     to);
234                             } catch (NumberFormatException JavaDoc e) {
235                                 System.out.println("Wrong User ID:" + r);
236                             }
237                             continue;
238                         } else if (cmd.equalsIgnoreCase("KICKOUT")) {
239                             try {
240                                 int to =
241                                     Integer.parseInt(r.substring(index + 1));
242                                 room.inputKickOut(
243                                     user.getUserid().intValue(),
244                                     to);
245                             } catch (NumberFormatException JavaDoc e) {
246                                 System.out.println("Wrong User ID:" + r);
247                             }
248                             continue;
249                         } else if (cmd.equalsIgnoreCase("ENTRUST")) {
250                             try {
251                                 int to =
252                                     Integer.parseInt(r.substring(index + 1));
253                                 room.inputEntrust(
254                                     user.getUserid().intValue(),
255                                     to);
256                             } catch (NumberFormatException JavaDoc e) {
257                                 System.out.println("Wrong User ID:" + r);
258                             }
259                             continue;
260                         } else if (cmd.equalsIgnoreCase("GETOUT")) {
261                             // Acknowlege
262
break;
263                         } else if (cmd.equalsIgnoreCase("CHGPASSWORD")) {
264                             String JavaDoc str = r.substring(index + 1);
265                             room.changePasswd(user.getUserid().intValue(), str);
266                         } else if (cmd.equalsIgnoreCase("CHGROOMNAME")) {
267                             String JavaDoc str = r.substring(index + 1);
268                             room.changeRoomName(
269                                 user.getUserid().intValue(),
270                                 str);
271                         } else if (cmd.equalsIgnoreCase("CHGMAX")) {
272                             try {
273                                 int num =
274                                     Integer.parseInt(r.substring(index + 1));
275                                 room.changeMaxMan(
276                                     user.getUserid().intValue(),
277                                     num);
278                             } catch (NumberFormatException JavaDoc e) {
279                                 System.out.println("Wrong Number Format:" + r);
280                             }
281                         } else if (cmd.equalsIgnoreCase("CUSTOM")) {
282                             try {
283                                 int index2 = r.indexOf(":", index + 1);
284                                 int to =
285                                     Integer.parseInt(
286                                         r.substring(index + 1, index2));
287                                 room.inputCustomMsg(
288                                     to,
289                                     r.substring(index2 + 1),
290                                     user.getUsername());
291                             } catch (NumberFormatException JavaDoc e) {
292                                 System.out.println("Wrong Number Format:" + r);
293                             }
294                         } else {
295                             System.out.println(
296                                 "Wrong Command.... Skipping....:" + r);
297                             continue;
298                         }
299                     } catch (Exception JavaDoc e) {
300                         e.printStackTrace();
301                     }
302                 }
303             } catch (IOException JavaDoc e) {
304                 e.printStackTrace();
305             } catch (Exception JavaDoc e) {
306                 e.printStackTrace();
307             } finally {
308                 room.exitUser(user);
309                 connected = false;
310                 try {
311                     if (outputStream != null) {
312                         outputStream.write("CLOSED:\r\n".getBytes());
313                         outputStream.flush();
314                     }
315                 } catch (IOException JavaDoc e) {
316                     System.err.println("IOException : 203");
317                     e.printStackTrace();
318                 }
319             }
320             System.out.println(
321                 "Listen thread ended....[" + user.getUserid() + "]");
322         } catch (IOException JavaDoc e) {
323             System.err.println("IOException : 155");
324             e.printStackTrace();
325         }
326     }
327 }
328
Popular Tags