예제 #1
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...");
                }
            }
        }
예제 #2
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);
 }
예제 #3
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"
                });
            }
        }