1 4 package chipchat; 5 6 import java.io.IOException ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 10 13 public final class Room extends Thread { 14 16 private Channel channel; 17 18 private Long roomid; 19 20 private String name; 21 22 private int maxMan; 23 24 private String passwd; 25 26 private int host; 27 28 private HashMap users = new HashMap (); 29 30 private HashMap admins = new HashMap (); 31 32 private Msg msg; 33 34 private boolean isFinish = false; 35 36 37 private Object msgInputLock = new Object (); 38 39 private Object msgOutputLock = new Object (); 40 41 49 public Room( 50 final Channel channel, 51 final Long roomid, 52 final String name, 53 final int maxMan, 54 final String passwd, 55 final int host) { 56 57 this.channel = channel; 58 this.roomid = roomid; 59 this.name = name; 60 this.maxMan = maxMan; 61 if (passwd != null && passwd.equals("")) { 62 this.passwd = null; 63 } else { 64 this.passwd = passwd; 65 } 66 this.host = host; 67 this.setDaemon(true); 68 this.start(); 69 } 70 71 74 79 int setMaster(final int master) { 80 User user; 81 synchronized (users) { 82 user = (User) users.get(new Integer (master)); 83 } 84 if (user != null) { 85 this.host = master; 86 inputAdminInfo(); 87 inputAdminChange(user.getUsername()); 88 return this.host; 89 } else { 90 return setMaster(); 91 } 92 93 } 94 98 int setMaster() { 99 User user; 100 101 synchronized (users) { 102 if (!users.isEmpty()) { 103 user = (User) users.get(users.keySet().iterator().next()); 104 } else { 105 return host; 106 } 107 } 108 109 host = user.getUserid().intValue(); 110 inputAdminInfo(); 111 inputAdminChange(user.getUsername()); 112 113 return host; 114 } 115 116 120 boolean isFull() { 121 return (maxMan == users.size()); 122 } 123 124 128 boolean isEmpty() { 129 return (0 == users.size()); 130 } 131 132 138 int enterUser(final User user, final String passwd) { 139 synchronized (users) { 140 if (users.size() >= maxMan) { 142 return -1; 143 } 144 if (this.passwd != null 146 && (!this.passwd.equalsIgnoreCase(passwd))) { 147 return -2; 148 } 149 users.put(user.getUserid(), user); 150 } 151 channel.setListChanged(); 152 inputInfo("GetIn", user.getUserid() + ">" + user.getUsername()); 153 inputUserInfo(); 154 return 0; 155 } 156 157 163 int enterAdmin(final User user, final String passwd) { 164 synchronized (admins) { 165 admins.put(user.getUserid(), user); 166 } 167 return 0; 168 } 169 170 174 void exitUser(final User user) { 175 boolean needSetMaster = false; 176 synchronized (users) { 177 if (users.remove(user.getUserid()) == null) { 178 if (admins.remove(user.getUserid()) == null) { 179 System.out.println("Error : Userid not Exist in room."); 180 } 181 return; 182 } 183 channel.setListChanged(); 184 if (users.size() == 0) { 185 this.maxMan = 0; 186 this.isFinish = true; 187 channel.removeRoom(roomid); 188 } 189 if (user.getUserid().intValue() == host) { 190 needSetMaster = true; 191 } 192 } 193 if (needSetMaster) { 194 setMaster(); 195 } 196 inputInfo("GetOut", user.getUsername()); 197 inputUserInfo(); 198 } 199 200 203 207 void inputMsg(final Msg msg) { 208 synchronized (msgInputLock) { 209 while (this.msg != null && !isFinish) { 210 try { 211 msgInputLock.wait(100); 212 } catch (InterruptedException e) { 213 e.printStackTrace(); 214 } 215 } 216 this.msg = msg; 217 } 218 synchronized (msgOutputLock) { 219 msgOutputLock.notify(); 220 } 221 } 222 223 229 void inputMsg(final int to, final String msg, final String writer) { 230 inputMsg(new Msg(Msg.TYPE_MSG, to, msg, writer)); 231 } 232 233 239 void inputCustomMsg(final int to, final String msg, final String writer) { 240 inputMsg(new Msg(Msg.TYPE_CUSTOMMSG, to, msg, writer)); 241 } 242 243 250 void inputMsg( 251 final int msgType, 252 final int to, 253 final String msg, 254 final String writer) { 255 inputMsg(new Msg(msgType, to, msg, writer)); 256 } 257 258 265 void inputWhisper( 266 final int from, 267 final int to, 268 final String msg, 269 final String writer) { 270 if (from == to) { 271 inputError(from, "WSP:SendToMyself"); 272 return; 273 } 274 User user = (User) users.get(new Integer (to)); 275 if (user == null) { 276 inputError(from, "WSP:NotExistAnother"); 277 } else { 278 inputMsg(new Msg(Msg.TYPE_WSPSND, from, msg, user.getUsername())); 279 inputMsg(new Msg(Msg.TYPE_WSPRCV, to, msg, writer)); 280 } 281 } 282 283 288 void inputError(final int to, final String msg) { 289 inputMsg(new Msg(Msg.TYPE_ERROR, to, msg, null)); 290 } 291 292 295 void inputRoomInfo() { 296 int isPasswd = (passwd != null && (!"".equals(passwd))) ? 1 : 0; 297 inputInfo("RoomInfo", maxMan + ">" + isPasswd + ">" + name); 298 } 299 300 303 void inputUserInfo() { 304 StringBuffer sb = new StringBuffer (); 305 synchronized (users) { 306 for (Iterator i = users.keySet().iterator(); i.hasNext();) { 307 Object key = i.next(); 308 User user = (User) users.get(key); 309 sb.append(user.getUserid()).append("<").append( 310 user.getUsername()).append( 311 ">"); 312 } 313 } 314 inputMsg(Msg.TYPE_USERS, -1, sb.toString(), null); 315 } 316 317 320 void inputAdminInfo() { 321 inputMsg(new Msg(Msg.TYPE_ADMIN, -1, "" + host, null)); 322 } 323 324 328 void inputAdminChange(final String to) { 329 inputMsg(new Msg(Msg.TYPE_ADMINCHANGE, -1, to, null)); 330 } 331 332 337 void inputKeepQuiet(final int from, final int to) { 338 if (from == host) { 339 User user = (User) users.get(new Integer (to)); 340 if (user == null) { 341 inputError(host, "KEEPQUIET:NotExistUser"); 342 } else { 343 inputMsg( 344 new Msg( 345 Msg.TYPE_KEEPQUIET, 346 -1, 347 "" + to, 348 user.getUsername())); 349 } 350 } else { 351 inputError(from, "YouNotMaster"); 352 } 353 } 354 355 360 void inputKickOut(final int from, final int to) { 361 if (from == host) { 362 User user = (User) users.get(new Integer (to)); 363 if (user == null) { 364 inputError(host, "KICKOUT:NotExistUser"); 365 } else { 366 inputMsg( 367 new Msg(Msg.TYPE_KICKOUT, -1, "" + to, user.getUsername())); 368 } 369 } else { 370 inputError(from, "YouNotMaster"); 371 } 372 } 373 374 379 void inputEntrust(final int from, final int to) { 380 if (from == host) { 381 User user = (User) users.get(new Integer (to)); 382 if (user == null) { 383 inputError(host, "ENTRUST:NotExistUser"); 384 } else { 385 host = to; 386 inputAdminInfo(); 387 inputAdminChange(user.getUsername()); 388 } 389 } else { 390 inputError(from, "YouNotMaster"); 391 } 392 } 393 394 399 void inputInfo(final String infoName, final String appendMsg) { 400 inputMsg(new Msg(Msg.TYPE_INFO, -1, appendMsg, infoName)); 401 } 402 403 408 void changePasswd(final int from, final String newPasswd) { 409 if (from == host) { 410 this.passwd = newPasswd; 411 inputRoomInfo(); 412 inputInfo("ChangePasswd", ""); 413 } else { 414 inputError(from, "YouNotMaster"); 415 } 416 } 417 418 423 void changeRoomName(final int from, final String name) { 424 if (from == host) { 425 this.name = name; 426 inputRoomInfo(); 427 inputInfo("ChangeRoomName", name); 428 } else { 429 inputError(from, "YouNotMaster"); 430 } 431 } 432 433 438 void changeMaxMan(final int from, final int num) { 439 if (from == host) { 440 this.maxMan = num; 441 inputRoomInfo(); 442 inputInfo("ChangeMaxMan", "" + num); 443 } else { 444 inputError(from, "YouNotMaster"); 445 } 446 } 447 448 451 public void run() { 452 synchronized (msgOutputLock) { 454 try { 455 msgOutputLock.wait(60000); 456 } catch (InterruptedException e) { 457 e.printStackTrace(); 458 } 459 } 460 461 if (msg == null) { 463 System.out.println( 464 "This room is timeout. Member does not enter. [" 465 + this.roomid 466 + "]:" 467 + this.name); 468 maxMan = 0; 469 isFinish = true; 470 channel.removeRoom(roomid); 471 return; 472 } 473 474 while (!isFinish) { 475 synchronized (msgOutputLock) { 476 if (msg != null) { 477 Msg lmsg = msg; 478 msg = null; 479 synchronized (msgInputLock) { 480 msgInputLock.notify(); 481 } 482 synchronized (users) { 483 for (Iterator i = users.keySet().iterator(); 484 i.hasNext(); 485 ) { 486 Object key = i.next(); 487 User user = (User) users.get(key); 488 String smsg = 489 lmsg.getString(user.getUserid().intValue()); 490 if (smsg != null) { 491 try { 492 user.getOutputStream().write( 493 smsg.getBytes()); 494 user.getOutputStream().flush(); 495 } catch (IOException e) { 496 e.printStackTrace(); 497 } 498 } 499 } 500 } 501 synchronized (admins) { 502 if (admins.size() > 0) { 503 Iterator i = admins.keySet().iterator(); 504 while (i.hasNext()) { 505 Object key = i.next(); 506 User user = (User) admins.get(key); 507 String smsg = lmsg.getString(-2); 508 if (smsg != null) { 509 try { 510 user.getOutputStream().write( 511 smsg.getBytes()); 512 user.getOutputStream().flush(); 513 } catch (IOException e) { 514 e.printStackTrace(); 515 } 516 } 517 } 518 } 519 } 520 } else { 521 try { 522 msgOutputLock.wait(1000); 523 } catch (InterruptedException e) { 524 e.printStackTrace(); 525 } 526 } 527 } 528 } 529 } 530 531 535 public int getMaxMan() { 536 return maxMan; 537 } 538 539 543 public String getRoomName() { 544 return name; 545 } 546 547 551 public String getPasswd() { 552 return passwd; 553 } 554 555 559 public HashMap getUsers() { 560 return users; 561 } 562 563 567 public Long getRoomid() { 568 return roomid; 569 } 570 571 575 public int getMaster() { 576 return host; 577 } 578 579 } 580 | Popular Tags |