using System; using System.Threading ; using System.Web; using System.Collections; using System.Collections.Generic ; using System.Text; using System.Configuration; namespace ASPNETChat { /// /// The business logic of the chat application /// public static class ChatEngine { #region Members private static Dictionary Rooms = new Dictionary(Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["MaxChatRooms"])); private static int userChatRoomSessionTimeout = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["UserChatRoomSessionTimeout"]); #endregion #region Methods /// /// Cleans all the chat roomsDeletes the empty chat rooms /// /// public static void CleanChatRooms(object state) { lock (Rooms) { foreach (object key in Rooms.Keys) { ChatRoom room = Rooms[key.ToString()]; room.ExpireUsers(userChatRoomSessionTimeout); if (room.IsEmpty()) { room.Dispose(); Rooms.Remove(key.ToString()); } } } } /// /// Returns the chat room for this two users or create a new one if nothing exists /// /// /// /// public static ChatRoom GetRoom(string roomID) { ChatRoom room=null; lock (Rooms) { if (Rooms.ContainsKey (roomID)) room = Rooms[roomID]; else { room = new ChatRoom(roomID); Rooms.Add(roomID, room); } } return room; } /// /// Deletes the specified room /// /// public static void DeleteRoom(string roomID) { if (!Rooms.ContainsKey(roomID)) return; lock (Rooms) { ChatRoom room = Rooms[roomID]; room.Dispose(); Rooms.Remove(roomID); } } #endregion } }