From: <fza...@us...> - 2005-11-30 22:04:41
|
Update of /cvsroot/struts/ajaxchat/WEB-INF/src/org/apache/struts/apps/ajaxchat/dao In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13448/WEB-INF/src/org/apache/struts/apps/ajaxchat/dao Modified Files: AjaxChatDAO.java Log Message: Index: AjaxChatDAO.java =================================================================== RCS file: /cvsroot/struts/ajaxchat/WEB-INF/src/org/apache/struts/apps/ajaxchat/dao/AjaxChatDAO.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** AjaxChatDAO.java 29 Nov 2005 23:08:29 -0000 1.1 --- AjaxChatDAO.java 30 Nov 2005 22:04:28 -0000 1.2 *************** *** 28,31 **** --- 28,32 ---- import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.apache.struts.apps.ajaxchat.AjaxChatConfig; import org.apache.struts.apps.ajaxchat.dto.MessageDTO; import org.apache.struts.apps.ajaxchat.dto.RoomDTO; *************** *** 37,41 **** * This Data Access Object (DAO) is really the heart and soul of the app. All * the real work is done here in terms of recording messages, dealing with ! * users and rooms and most everything else. * * @author <a href="mailto:fza...@om...">Frank W. Zammetti</a>. --- 38,46 ---- * This Data Access Object (DAO) is really the heart and soul of the app. All * the real work is done here in terms of recording messages, dealing with ! * users and rooms and most everything else. I think it's probably a bit more ! * than what a DAO is supposed to generally be, but in this case I don't think ! * it's a big deal. Besides, the idea is that if you want to make this a more ! * robust application, with real message persistance and such, then all you ! * should probably have to mess with is this class. That's the intent anyway. * * @author <a href="mailto:fza...@om...">Frank W. Zammetti</a>. *************** *** 72,76 **** */ private AjaxChatDAO() { ! } --- 77,81 ---- */ private AjaxChatDAO() { ! } // End constructor. *************** *** 83,92 **** public static AjaxChatDAO getInstance() { ! log.info("AjaxChatDAO getInstance()..."); if (instance == null) { instance = new AjaxChatDAO(); instance.init(null); } - log.info("AjaxChatDAO getInstance() Done"); return instance; --- 88,96 ---- public static AjaxChatDAO getInstance() { ! log.debug("getInstance()..."); if (instance == null) { instance = new AjaxChatDAO(); instance.init(null); } return instance; *************** *** 106,110 **** public synchronized void init(InputStream isConfigFile) { ! log.info("AjaxChatDAO init()..."); if (isConfigFile != null) { // Read in rooms config and create beans, hand off to DAO. --- 110,114 ---- public synchronized void init(InputStream isConfigFile) { ! log.debug("init()..."); if (isConfigFile != null) { // Read in rooms config and create beans, hand off to DAO. *************** *** 125,129 **** } } - log.info("AjaxChatDAO init() Done"); } // End init(). --- 129,132 ---- *************** *** 137,144 **** public synchronized void addRoom(RoomDTO inRoom) { ! log.info("AjaxChatDAO addRoom()..."); log.info("Adding room " + inRoom); rooms.put(inRoom.getName(), inRoom); - log.info("AjaxChatDAO addRoom() Done"); } // End addRoom(). --- 140,146 ---- public synchronized void addRoom(RoomDTO inRoom) { ! log.debug("addRoom()..."); log.info("Adding room " + inRoom); rooms.put(inRoom.getName(), inRoom); } // End addRoom(). *************** *** 152,163 **** public synchronized void removeRoom(String inRoomName) { ! log.info("AjaxChatDAO removeRoom()..."); ! log.info("Removing room " + inRoomName); RoomDTO room = (RoomDTO)rooms.get(inRoomName); if (room.getUserList().size() == 0) { rooms.remove(inRoomName); ! log.info("AjaxChatDAO removeRoom() Done"); } else { ! log.info("AjaxChatDAO removeRoom() Room not removed because " + "there are users in it"); } --- 154,164 ---- public synchronized void removeRoom(String inRoomName) { ! log.debug("removeRoom()..."); RoomDTO room = (RoomDTO)rooms.get(inRoomName); if (room.getUserList().size() == 0) { rooms.remove(inRoomName); ! log.info("removeRoom() removed room " + inRoomName); } else { ! log.info("removeRoom() Room not removed because " + "there are users in it"); } *************** *** 174,179 **** public synchronized void postMessage(String inRoom, MessageDTO inMessage) { ! log.info("AjaxChatDAO postMessage() : inRoom = " + inRoom + ! " - inMessage = " + inMessage); RoomDTO room = (RoomDTO)rooms.get(inRoom); room.postMessage(inMessage); --- 175,180 ---- public synchronized void postMessage(String inRoom, MessageDTO inMessage) { ! log.debug("postMessage(): inRoom = " + inRoom + ! " - inMessage = " + inMessage + "..."); RoomDTO room = (RoomDTO)rooms.get(inRoom); room.postMessage(inMessage); *************** *** 192,197 **** public synchronized Vector getMessages(String inRoom, Date inDateTime) { ! log.info("AjaxChatDAO getMessages() : inRoom = " + inRoom + ! " - inDateTime = " + inDateTime); RoomDTO room = (RoomDTO)rooms.get(inRoom); return room.getMessages(inDateTime); --- 193,198 ---- public synchronized Vector getMessages(String inRoom, Date inDateTime) { ! log.debug("getMessages(): inRoom = " + inRoom + ! " - inDateTime = " + inDateTime + "..."); RoomDTO room = (RoomDTO)rooms.get(inRoom); return room.getMessages(inDateTime); *************** *** 208,217 **** public synchronized Vector getRoomList() { ! log.info("AjaxChatDAO getRoomList()"); Vector roomList = new Vector(); for (Iterator it = rooms.keySet().iterator(); it.hasNext();) { roomList.add((String)it.next()); } ! log.info("AjaxChatDAO roomList = " + roomList); return roomList; --- 209,218 ---- public synchronized Vector getRoomList() { ! log.debug("getRoomList()..."); Vector roomList = new Vector(); for (Iterator it = rooms.keySet().iterator(); it.hasNext();) { roomList.add((String)it.next()); } ! log.info("roomList = " + roomList); return roomList; *************** *** 227,231 **** public synchronized LinkedHashMap getRoomUserCounts() { ! log.info("getRoomUserCounts getMessages()"); LinkedHashMap roomList = new LinkedHashMap(); for (Iterator it = rooms.keySet().iterator(); it.hasNext();) { --- 228,232 ---- public synchronized LinkedHashMap getRoomUserCounts() { ! log.debug("getRoomUserCounts()..."); LinkedHashMap roomList = new LinkedHashMap(); for (Iterator it = rooms.keySet().iterator(); it.hasNext();) { *************** *** 234,238 **** new Integer(((RoomDTO)rooms.get(roomName)).getUserList().size())); } ! log.info("getRoomUserCounts roomList = " + roomList); return roomList; --- 235,239 ---- new Integer(((RoomDTO)rooms.get(roomName)).getUserList().size())); } ! log.debug("roomList = " + roomList); return roomList; *************** *** 249,257 **** public synchronized Vector getUserList(String inRoom) { ! log.info("AjaxChatDAO getUserList() : inRoom = " + inRoom); Vector userList = null; RoomDTO room = (RoomDTO)rooms.get(inRoom); userList = room.getUserList(); ! log.info("AjaxChatDAO userList = " + userList); return userList; --- 250,258 ---- public synchronized Vector getUserList(String inRoom) { ! log.debug("getUserList(): inRoom = " + inRoom + "..."); Vector userList = null; RoomDTO room = (RoomDTO)rooms.get(inRoom); userList = room.getUserList(); ! log.info("userList = " + userList); return userList; *************** *** 267,274 **** public synchronized void addUserToRoom(String inRoom, UserDTO inUser) { ! log.info("AjaxChatDAO addUserToRoom()..."); RoomDTO room = (RoomDTO)rooms.get(inRoom); room.addUser(inUser); - log.info("AjaxChatDAO addUserToRoom() Done"); } // End addUserToRoom(). --- 268,274 ---- public synchronized void addUserToRoom(String inRoom, UserDTO inUser) { ! log.info("addUserToRoom()..."); RoomDTO room = (RoomDTO)rooms.get(inRoom); room.addUser(inUser); } // End addUserToRoom(). *************** *** 283,290 **** public synchronized void removeUserFromRoom(String inRoom, UserDTO inUser) { ! log.info("AjaxChatDAO removeUserFromRoom()..."); RoomDTO room = (RoomDTO)rooms.get(inRoom); room.removeUser(inUser); - log.info("AjaxChatDAO removeUserFromRoom() Done"); } // End removeUserFromRoom(). --- 283,289 ---- public synchronized void removeUserFromRoom(String inRoom, UserDTO inUser) { ! log.debug("removeUserFromRoom()..."); RoomDTO room = (RoomDTO)rooms.get(inRoom); room.removeUser(inUser); } // End removeUserFromRoom(). *************** *** 299,303 **** public synchronized void removeUserFromAllRooms(UserDTO inUser) { ! log.info("AjaxChatDAO removeUserFromAllRooms()..."); for (Iterator it = rooms.keySet().iterator(); it.hasNext();) { String roomName = (String)it.next(); --- 298,302 ---- public synchronized void removeUserFromAllRooms(UserDTO inUser) { ! log.debug("removeUserFromAllRooms()..."); for (Iterator it = rooms.keySet().iterator(); it.hasNext();) { String roomName = (String)it.next(); *************** *** 305,309 **** room.removeUser(inUser); } - log.info("AjaxChatDAO removeUserFromAllRooms() Done"); } // End removeUserFromAllRooms(). --- 304,307 ---- *************** *** 317,321 **** public synchronized void logUserIn(UserDTO inUser) { ! log.info("AjaxChatDAO logUserIn()..."); users.add(inUser); log.info(inUser.getUsername() + " logged in"); --- 315,319 ---- public synchronized void logUserIn(UserDTO inUser) { ! log.debug("logUserIn()..."); users.add(inUser); log.info(inUser.getUsername() + " logged in"); *************** *** 331,335 **** public synchronized void logUserOut(UserDTO inUser) { ! log.info("AjaxChatDAO logUserOut()..."); String usernameToLogOut = inUser.getUsername(); int i = 0; --- 329,333 ---- public synchronized void logUserOut(UserDTO inUser) { ! log.debug("logUserOut()..."); String usernameToLogOut = inUser.getUsername(); int i = 0; *************** *** 358,362 **** public synchronized boolean isUsernameInUse(String inUsername) { ! log.info("AjaxChatDAO isUsernameInUse()..."); boolean retVal = false; for (Iterator it = users.iterator(); it.hasNext();) { --- 356,360 ---- public synchronized boolean isUsernameInUse(String inUsername) { ! log.debug("isUsernameInUse()..."); boolean retVal = false; for (Iterator it = users.iterator(); it.hasNext();) { *************** *** 366,370 **** } } ! log.info("AjaxChatDAO isUsernameInUse() Done (" + retVal + ")"); return retVal; --- 364,368 ---- } } ! log.debug("Returning " + retVal); return retVal; *************** *** 372,374 **** --- 370,400 ---- + /** + * This method goes through the collection of users and determines which, if + * any, are inactive. Any that are inactive are removed. This is called + * from the UserClearerDaemon thread. + */ + public synchronized void removeInactiveUsers() { + + log.debug("removeInactiveUsers()..."); + Vector usersToRemove = new Vector(); + for (Iterator it = users.iterator(); it.hasNext();) { + UserDTO user = (UserDTO)it.next(); + long now = new Date().getTime(); + long lastAJAXRequest = user.getLastAJAXRequest().getTime(); + if ((now - lastAJAXRequest) >= + (AjaxChatConfig.getUserInactivitySeconds() * 1000)) { + log.info("User " + user.getUsername() + " will be removed"); + usersToRemove.add(user); + } + } + for (Iterator it = usersToRemove.iterator(); it.hasNext();) { + UserDTO user = (UserDTO)it.next(); + removeUserFromAllRooms(user); + logUserOut(user); + } + + } // End removeInactiveUsers(). + + } // End class. \ No newline at end of file |