1 4 package chipchat; 5 6 import java.io.BufferedReader ; 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 import java.io.InputStreamReader ; 10 import java.io.OutputStream ; 11 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.http.HttpServletResponse ; 14 import javax.servlet.http.HttpSession ; 15 16 21 public final class Communicator { 22 25 26 27 private static final String SERVERNAME = 28 Env.getInstance().getProperty("Communicator.serverName"); 29 30 31 private static final String ADMINPW = 32 Env.getInstance().getProperty("Communicator.adminpasswd"); 33 34 37 public Communicator() { 38 } 39 40 45 public void service( 46 final HttpServletRequest req, 47 final HttpServletResponse resp) { 48 try { 49 InputStream inputStream = req.getInputStream(); 51 OutputStream outputStream = resp.getOutputStream(); 52 53 HttpSession 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 name = (String ) session.getAttribute("chipchat_username"); 65 String channel = (String ) session.getAttribute("chipchat_channel"); 66 String roompw = (String ) session.getAttribute("chipchat_roompw"); 67 int userid; 68 Long roomid; 69 try { 70 userid = 71 Integer.parseInt( 72 (String ) session.getAttribute("chipchat_userid")); 73 roomid = (Long ) session.getAttribute("chipchat_roomid"); 74 session.setAttribute("chipchat_roomid", null); 75 } catch (NumberFormatException 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 e) { 86 System.err.println("Exception : 93"); 87 e.printStackTrace(); 88 } 89 } 90 91 97 public void service( 98 String conName, 99 final InputStream input, 100 final OutputStream 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 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 e) { 119 System.err.println("Exception : 93"); 120 e.printStackTrace(); 121 } 122 } 123 124 132 public void service( 133 final User user, 134 final String channel, 135 final Long roomid, 136 final String passwd, 137 final InputStream inputStream) { 138 try { 139 BufferedReader reader = 141 new BufferedReader (new InputStreamReader (inputStream)); 142 OutputStream 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 r = reader.readLine(); 191 if (r == null) { 192 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 cmd = r.substring(0, index); 203 if (cmd.equalsIgnoreCase("MSG")) { room.inputMsg( 205 -1, 206 r.substring(index + 1), 207 user.getUsername()); 208 } else if (cmd.equalsIgnoreCase("WSP")) { 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 e) { 220 System.out.println("Wrong User ID:" + r); 221 } 222 } else if (cmd.equalsIgnoreCase("ACK")) { 223 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 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 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 e) { 257 System.out.println("Wrong User ID:" + r); 258 } 259 continue; 260 } else if (cmd.equalsIgnoreCase("GETOUT")) { 261 break; 263 } else if (cmd.equalsIgnoreCase("CHGPASSWORD")) { 264 String str = r.substring(index + 1); 265 room.changePasswd(user.getUserid().intValue(), str); 266 } else if (cmd.equalsIgnoreCase("CHGROOMNAME")) { 267 String 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 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 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 e) { 300 e.printStackTrace(); 301 } 302 } 303 } catch (IOException e) { 304 e.printStackTrace(); 305 } catch (Exception 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 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 e) { 323 System.err.println("IOException : 155"); 324 e.printStackTrace(); 325 } 326 } 327 } 328 | Popular Tags |