📄 chatserver.java
字号:
/**
* Returns dynamic id of a User currently online or -1 if he is not online.
*
* @param userNameParam the User's name
*/
public int getOnlineUserId(String userNameParam) {
Enumeration userEnum;
User user;
userEnum = onlineTable.elements();
while(userEnum.hasMoreElements()) {
if (((user = (User)userEnum.nextElement())).getName().equals(userNameParam))
return user.getId();
}
return -1;
}
/**
* Returns the true if a User's name exists already.
*
* @param userNameParam the User's name
*/
public boolean exists(String userNameParam) {
return new File(storageFolder + userNameParam).exists();
}
/**
* Stores a User to the ChatServer's local file system. The User's name will be
* used as the filename.
*
* @param userParam the User to be stored
*/
public void saveUser(User userParam) {
save(userParam, storageFolder + userParam.getName());
}
/**
* Loads a User from the ChatServer's local file system.
*
* @param userNameParam the name of the User to be loaded
*/
public User loadUser(String userNameParam) {
Object loadedObject;
loadedObject = load(storageFolder + userNameParam);
if (loadedObject instanceof User)
return (User)loadedObject;
else
return null;
}
/**
* Deletes a User from the ChatServer's local file system.
*
* @param userNameParam the name of the User to be loaded
*/
private synchronized void deleteUser(String userNameParam) {
new File(storageFolder + userNameParam).delete();
}
/**
* Returns the next available User id. The id will be created dynamically.
*/
public synchronized int getNextAvailableUserId() {
int id;
do {
id = rand.nextInt();
} while (onlineTable.containsKey(id));
return id;
}
/**
* Returns the next available Room id. The id will be created dynamically.
*/
public synchronized int getNextAvailableRoomId() {
return roomTable.getMaxKey() + 1;
}
/**
* Returns whether a password is correct or not.
*/
public boolean correctPassword(String userNameParam, String userPasswordParam) {
Object loadedObject;
loadedObject = loadUser(userNameParam);
if (loadedObject instanceof User)
return ((User)loadedObject).getPassword().equals(userPasswordParam);
else
return false;
}
/**
* Stores an object under a given filename to the ChatServer's local file system.
*
* @param objectParam the object to be stored
* @param fileName the filename under which to store the object
*/
public synchronized void save(Object objectParam, String fileName) {
ObjectOutputStream output;
try {
output = new ObjectOutputStream(new FileOutputStream(fileName));
output.writeObject(objectParam);
output.close();
}
catch (IOException excpt) {
log("IOException while writing file " + fileName);
}
}
/**
* Loads an object from the ChatServer's local file system.
*
* @param fileName the filename under which the object is stored
*/
public synchronized Object load(String fileName) {
ObjectInputStream input;
Object object;
object = null;
try {
input = new ObjectInputStream(new FileInputStream(fileName));
object = input.readObject();
input.close();
}
catch (ClassNotFoundException excpt) {
log("ClassNotFoundException while reading file " + fileName);
}
catch (IOException excpt) {
log("IOException while reading file " + fileName);
}
return object;
}
/**
* Adds a User to the list of currently logged in users.
*
* @param userParam the User to be added
*/
public void addOnlineUser(User userParam) {
onlineTable.put(userParam.getId(), userParam);
}
/**
* Removes a User from the list of currently logged in users.
*
* @param idParam the id of the User to be removed
*/
public void removeOnlineUser(int idParam) {
onlineTable.remove(idParam);
}
/**
* Adds a Room.
*
* @param roomParam the Room to be added
*/
public void addRoom(Room roomParam) {
roomTable.put(roomParam.getId(), roomParam);
}
/**
* Removes a Room.
*
* @param idParam the id of the Room to be removed
*/
public void removeRoom(int idParam) {
roomTable.remove(idParam);
}
/**
* Returns a User from the list of currently logged in users.
*
* @param idParam the id of the User to be returned
*/
public User getOnlineUser(int idParam) {
Object object;
object = onlineTable.get(idParam);
if (object != null)
return (User)object;
else
return null;
}
/**
* Returns a Room.
*
* @param idParam the id of the Room to be returned
*/
public Room getRoom(int idParam) {
Object object;
object = roomTable.get(idParam);
if (object != null)
return (Room)object;
else
return null;
}
/**
* Returns a free position in a certain Room to place the User after logging in.
*
* @param roomid the number of the Room to place the User
*/
public synchronized Point getAvailablePosition(int roomId) {
Point position, userPosition;
Enumeration userEnum;
do {
position = new Point((int)(rand.nextDouble() * (double)ChatRepository.ROOM_DIMENSION.width - 2.0) + 1, (int)(rand.nextDouble() * (double)ChatRepository.ROOM_DIMENSION.height - 2.0) + 1);
}
while (collides(roomId, position, -1));
return position;
}
/**
* Checks whether a Point in a Room collides with a User being placed there.
*
* @param roomid the number of the Room
* @param position the position to be checked
* @param excludeUserId exclude the position of this User
*/
public boolean collides(int roomId, Point position, int excludeUserId) {
Enumeration userEnum;
User user;
userEnum = onlineTable.elements();
while (userEnum.hasMoreElements()) {
if ((user = (User)userEnum.nextElement()).getRoom() == roomId && user.getId() != excludeUserId) {
if (new Rectangle(user.getPosition().x - ChatRepository.USER_SIZE / 2 - ChatRepository.MINIMUM_DISTANCE, user.getPosition().y - ChatRepository.USER_SIZE / 2 - ChatRepository.MINIMUM_DISTANCE, ChatRepository.USER_SIZE + ChatRepository.MINIMUM_DISTANCE * 2, ChatRepository.USER_SIZE + ChatRepository.MINIMUM_DISTANCE * 2).contains(position))
return true;
}
}
return false;
}
/**
* Writes a timestamp and logging information to the standard output.
*
* @param logText the text to be logged
*/
public synchronized void log(String logText) {
log(logText, STANDARD_LOGLEVEL);
}
/**
* Writes a timestamp and logging information to the standard output.
*
* @param logText the text to be logged
* @param level the logLevel
*/
public synchronized void log(String logText, int level) {
if (level <= logLevel) {
System.out.println(DateFormat.getDateInstance(DateFormat.SHORT).format(new Date()) + " " + DateFormat.getTimeInstance(DateFormat.MEDIUM).format(new Date()) + ": " + logText);
}
}
/**
* Returns the folder where images should be stored.
*/
public String getImageFolder() {
return imageFolder;
}
/**
* Returns a Vector which contains the names of all Users that are stored.
*/
public Vector getUserListVector() {
String[] userFileList;
Vector userListVector;
userListVector = new Vector();
userFileList = new File(storageFolder).list();
for (int i = 0; i < userFileList.length; i++)
userListVector.addElement(userFileList[i]);
return userListVector;
}
/**
* Creates the standard Rooms. Is being called after starting the ChatServer for
* the first time.
*/
private void createStandardRooms() {
Room room;
roomTable = new IntegerHashtable();
for (int i = 0; i < ChatRepository.STANDARD_ROOM_NAME.length; i++) {
room = new Room(i, ChatRepository.STANDARD_ROOM_NAME[i], ChatRepository.ROOM_DIMENSION);
room.setPrivate(ChatRepository.STANDARD_ROOM_PRIVATE[i]);
room.setAdministrator(ChatRepository.ADMIN);
addRoom(room);
}
log("Created standard rooms...");
saveRooms();
}
/**
* Persistently stores all the Rooms to the server's file system.
*/
private void saveRooms() {
save(roomTable, roomFolder + roomFilename);
log("Saved rooms...");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -