예제 #1
0
 /// <summary>
 /// Adds a ChatroomLogic object to the list managed by this
 /// class.
 /// </summary>
 /// <param name="crl">ChatroomLogic to add</param>
 public void addChat(ChatroomLogic crl)
 {
     if (!chatrooms.Contains(crl))
     {
         chatrooms.Add(crl);
     }
 }
예제 #2
0
파일: Program.cs 프로젝트: A-Fitz/Chat-App
        /// <summary>
        /// Main entrypoint of the program.
        /// Starts the server, then listens for user commands.
        /// </summary>
        /// <param name="args">Command line arguments</param>
        static void Main(string[] args)
        {
            while (restart)
            {
                exit    = false;
                restart = true;
                Console.Out.WriteLine("Server autostarting at port " + PORT);
                //Start server

                ChatroomList chatroomList = new ChatroomList();

                Console.WriteLine("Loading...");
                if (LoadSaveData(chatroomList))
                {
                    Console.WriteLine("Loading successful");
                }
                else
                {
                    Console.WriteLine("No data found. Starting with defaults.");
                    ChatroomLogic mainRoom  = new ChatroomLogic();
                    ChatroomLogic mainRoom2 = new ChatroomLogic();
                    mainRoom.name  = "Global 1";
                    mainRoom2.name = "Global 2";
                    chatroomList.addChat(mainRoom);
                    chatroomList.addChat(mainRoom2);
                    ChatroomList.chatroomServices.CreateChatroom(
                        mainRoom.name, mainRoom.chatroomID, TEST_CLIENT, "pass", ChatType.MULTIPLE_USERS);
                    ChatroomList.chatroomServices.CreateChatroom(
                        mainRoom2.name, mainRoom2.chatroomID, TEST_CLIENT, "pass", ChatType.MULTIPLE_USERS);
                }

                TcpListener serverSocket
                    = new TcpListener(System.Net.IPAddress.Loopback, PORT);
                serverSocket.Start();
                Thread connectorThread = new Thread(
                    () => handleIncomingConnections(serverSocket, chatroomList));
                connectorThread.Name     = "Connector Thread";
                connectorThread.Priority = ThreadPriority.Lowest;
                connectorThread.Start();
                while (!exit)
                {
                    RespondToUserInput();
                }
                Console.WriteLine("Saving...");
                serverSocket.Stop();
                chatroomList.SendGlobalMessage(new Message {
                    chatID  = -1,
                    command = "CLOSING",
                    message = "0"
                });
                chatroomList.Stop();
                if (restart == true)
                {
                    Console.WriteLine("Restarting...");
                }
            }
        }
예제 #3
0
        /// <summary>
        /// All messages with the command "SEND" will come through this function.
        /// This function acts as a main pipe for all messages and sends
        /// an update to the proper ChatroomLogic. Does nothing
        /// if the ChatroomLogic object is not found.
        /// </summary>
        /// <param name="msg"></param>
        public void update(Message msg, int userId)
        {
            ChatroomLogic chatroom = idToChatroom(msg.chatID);

            if (chatroom != null)
            {
                chatroomServices.AddMessage(msg.chatID, userId, msg.message);
                chatroom.update(msg);
            }
        }
예제 #4
0
        /// <summary>
        /// Handles when a user attempts to join a chat.
        /// </summary>
        /// <param name="incomingMsg">The message used to
        /// login to a chat with.</param>
        private void JoinChatCommand(Message incomingMsg)
        {
            string hashword2 = incomingMsg.message.Substring(0, HASHED_PW_SIZE);
            int    id        = -1;

            if (int.TryParse(incomingMsg.message.Substring(HASHED_PW_SIZE,
                                                           incomingMsg.message.Length - HASHED_PW_SIZE), out id))
            {
                ChatroomLogic tempChatroom2 =
                    chatroomList.idToChatroom(id);
                if (tempChatroom2 != null)
                {
                    if (ChatroomList.chatroomServices.AddUser(
                            tempChatroom2.chatroomID,
                            this.userID, hashword2))
                    {
                        tempChatroom2.Subscribe(this);
                        tempChatroom2.RegisteredUsers.Add(this.userID);
                        messageService.SendMessage(
                            new Message
                        {
                            chatID  = -1,
                            command = "ACK",
                            message = "Chatroom login succeded"
                        });
                        SendChatroomList();
                        SendChatHistory(tempChatroom2.chatroomID);
                    }
                    else
                    {
                        messageService.SendMessage(
                            new Message
                        {
                            chatID  = -1,
                            command = "EXCEPTION",
                            message = "Chatroom login failed"
                        });
                        return;
                    }
                }
            }
            else
            {
                messageService.SendMessage(
                    new Message
                {
                    chatID  = -1,
                    command = "EXCEPTION",
                    message = "Bad chatroom id"
                });
            }
        }
예제 #5
0
        /// <summary>
        /// Checks to see if the client is sending a message
        /// to a valid chat room, then sends it to all
        /// subscribes members of that chat room.
        /// </summary>
        /// <param name="incomingMsg">Details about the message and the
        /// chat room it will be sent to.</param>
        private void SendCommand(Message incomingMsg)
        {
            incomingMsg.message = DateTime.Now.ToString()
                                  + " : " + username + " : " + incomingMsg.message;
            ChatroomLogic sendChatroom
                = chatroomList.idToChatroom(incomingMsg.chatID);

            if (sendChatroom != null &&
                sendChatroom.RegisteredUsers.Contains(userID))
            {
                chatroomList.update(incomingMsg, userID);
            }
            else
            {
                Console.WriteLine("Bad SEND message chatID");
            }
        }
예제 #6
0
파일: Program.cs 프로젝트: A-Fitz/Chat-App
 /// <summary>
 /// Helper function for loading in SQL database data.
 /// </summary>
 /// <param name="chatroomList"></param>
 /// <returns>Returns true if load was successful,
 /// false otherwise.</returns>
 private static bool LoadSaveData(ChatroomList chatroomList)
 {
     try
     {
         int       hightestChatroomID = 0;
         DataTable dataTable
             = ChatroomList.chatroomServices.GetAllChatrooms();
         if (dataTable.Rows.Count != 0)
         {
             foreach (DataRow row in dataTable.Rows)
             {
                 string        name = row["chatroomname"].ToString();
                 int           id   = int.Parse(row["chatroomid"].ToString());
                 ChatroomLogic temp = new ChatroomLogic();
                 temp.name          = name;
                 temp.chatroomID    = id;
                 hightestChatroomID =
                     hightestChatroomID > id ? hightestChatroomID : id;
                 chatroomList.addChat(temp);
                 DataTable userTable
                     = ChatroomList.chatroomServices.GetChatUsers(
                           temp.chatroomID);
                 foreach (DataRow userRow in userTable.Rows)
                 {
                     int userId = -1;
                     if (int.TryParse(userRow["userid"].ToString(),
                                      out userId))
                     {
                         temp.RegisteredUsers.Add(userId);
                     }
                 }
             }
             ChatroomLogic.numChatRoomsCreated = hightestChatroomID + 1;
             return(true);
         }
     }
     catch (Exception e) { }
     return(false);
 }
예제 #7
0
        /// <summary>
        /// Attempts to create a new chat for the client.
        /// </summary>
        /// <param name="incomingMsg">Details about the new chat.</param>
        private void NewChatCommand(Message incomingMsg)
        {
            //message = password for chatroom
            //chatID = wether it is a private room. 1 = direct message
            //room, 0 = normal password protected room
            ChatroomLogic tempChatroom = new ChatroomLogic();
            string        chatname     = incomingMsg.message.Substring(HASHED_PW_SIZE,
                                                                       incomingMsg.message.Length - HASHED_PW_SIZE);
            string hashword = incomingMsg.message.Substring(0, HASHED_PW_SIZE);

            if (ChatroomList.chatroomServices.CreateChatroom(chatname,
                                                             tempChatroom.chatroomID,
                                                             this.userID, hashword, incomingMsg.chatID))
            {
                chatroomList.addChat(tempChatroom);
                tempChatroom.name = chatname;
                tempChatroom.Subscribe(this);
                tempChatroom.RegisteredUsers.Add(this.userID);
                messageService.SendMessage(new Message
                {
                    chatID  = -1,
                    command = "ACK",
                    message = "Chatroom has been created"
                });
                SendChatroomList();
                SendChatHistory(tempChatroom.chatroomID);
            }
            else
            {
                messageService.SendMessage(new Message
                {
                    chatID  = -1,
                    command = "EXCEPTION",
                    message = "Chatroom name already taken"
                });
            }
        }
예제 #8
0
 /// <summary>
 /// Subscribes this client to a chat. Any new information to the ChatroomLogic that
 /// this object is subsribed to will come through the
 /// OnNext() function.
 /// </summary>
 /// <param name="crl"></param>
 public void subsribeToChat(ChatroomLogic crl)
 {
     crl.Subscribe(this);
     crl.RegisteredUsers.Add(this.userID);
     chatrooms.Add(crl);
 }