📄 room.java
字号:
/*
* Created on 2007.12.25.
*/
package conferenceroom;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
public final class Room extends Thread {
// Room Infomations..
/** 会议室ID */
private String roomid;
/** 会议室主题 */
private String name;
/** Users */
private HashMap<String,User> users = new HashMap<String,User>();
/** Administrator */
private User admin = null;
/** Message to send. */
private Msg msg;
/** 会议是否结束 */
private boolean isFinish = false;
/** Lock object */
private Object msgInputLock = new Object();
/** Lock object */
private Object msgOutputLock = new Object();
public Room(
final String roomid,
final String name) {
this.roomid = roomid;
this.name = name;
this.setDaemon(true);
this.start();
}
int enterUser(final User user) {
synchronized (users) {
}
return 0;
}
int enterAdmin(final User user) {
synchronized (admin) {
}
return 0;
}
void exitUser(final User user) {
synchronized (users) {
}
inputInfo("GetOut", user.getUsername());
inputUserInfo();
}
void inputMsg(final Msg msg) {
synchronized (msgInputLock) {
while (this.msg != null && !isFinish) {
try {
msgInputLock.wait(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.msg = msg;
}
synchronized (msgOutputLock) {
msgOutputLock.notify();
}
}
void inputMsg(final String to, final String msg, final String writer) {
inputMsg(new Msg(Msg.TYPE_MSG, to, msg, writer));
}
void inputCustomMsg(final String to, final String msg, final String writer) {
inputMsg(new Msg(Msg.TYPE_CUSTOMMSG, to, msg, writer));
}
void inputMsg(
final int msgType,
final String to,
final String msg,
final String writer) {
inputMsg(new Msg(msgType, to, msg, writer));
}
void inputWhisper(
final String from,
final String to,
final String msg,
final String writer) {
if (from == to) {
inputError(from, "WSP:SendToMyself");
return;
}
User user = users.get(new Integer(to));
if (user == null) {
inputError(from, "WSP:NotExistAnother");
} else {
inputMsg(new Msg(Msg.TYPE_WSPSND, from, msg, user.getUsername()));
inputMsg(new Msg(Msg.TYPE_WSPRCV, to, msg, writer));
}
}
void inputError(final String to, final String msg) {
inputMsg(new Msg(Msg.TYPE_ERROR, to, msg, null));
}
void inputUserInfo() {
StringBuffer sb = new StringBuffer();
User user = null;
synchronized (users) {
for (Iterator i = users.keySet().iterator(); i.hasNext();) {
Object key = i.next();
user = users.get(key);
sb.append(user.getUserid()).append("<").append(
user.getUsername()).append(
">");
}
}
inputMsg(Msg.TYPE_USERS, "-1", sb.toString(), null);
}
void inputInfo(final String infoName, final String appendMsg) {
inputMsg(new Msg(Msg.TYPE_INFO, "-1", appendMsg, infoName));
}
public void inputRoomInfo() {
}
public void run() {
synchronized (msgOutputLock) {
try {
msgOutputLock.wait(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (msg == null) {
System.out.println(
"This room is timeout. Member does not enter. ["
+ this.roomid
+ "]:"
+ this.name);
isFinish = true;
return;
}
while (!isFinish) {
synchronized (msgOutputLock) {
if (msg != null) {
Msg lmsg = msg;
msg = null;
synchronized (msgInputLock) {
msgInputLock.notify();
}
User user = null;
synchronized (users) {
for (Iterator i = users.keySet().iterator();
i.hasNext();
) {
Object key = i.next();
user = users.get(key);
String smsg =
lmsg.getString(user.getUserid());
if (smsg != null) {
try {
user.getOutputStream().write(
smsg.getBytes());
user.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} else {
try {
msgOutputLock.wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public String getRoomName() {
return name;
}
public HashMap getUsers() {
return users;
}
public String getRoomid() {
return roomid;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -