public void AddUser_SuccessTest(string nickname) { TcpClient tcpClient = new TcpClient(); string ip = ServerChatServiceConstants.MANUAL_IP; tcpClient.Connect(ip, ServerChatServiceConstants.SERVER_PORT); var user = serverChatService.AddUser(tcpClient, nickname); Assert.True(user.Success); }
/// <summary> /// Accept the Client inserting him in the Server Hash Tables and starts /// to listening user messages. /// </summary> private void AcceptClient() { try { Reader = new System.IO.StreamReader(TcpClient.GetStream()); Writer = new System.IO.StreamWriter(TcpClient.GetStream()); // Read the user information UserInformation = Reader.ReadLine(); // If the information exists if (UserInformation != "") { // Check if the user already exists in the current connection if (ServerChatService.Users.Contains(UserInformation) == true) { // 0: not connected Writer.WriteLine("0|This is nickname already exists in the chat."); Writer.Flush(); CloseConnection(); return; } else if (UserInformation == "Administrator") { // 0: not connected Writer.WriteLine("0|Wow! You can't be the Administrator! hehe"); Writer.Flush(); CloseConnection(); return; } else { // 1: Connected successfully Writer.WriteLine("1"); Writer.Flush(); // Add the user in the HashTable and starts the message listener var user = _serverChatService.AddUser(TcpClient, UserInformation); if (!user.Success) { throw new Exception("Error when trying to add the new user."); } } } else { CloseConnection(); return; } try { // Still waiting for new user messages while ((Response = Reader.ReadLine()) != "") { // If invalid, remove the user if (Response == null) { _serverChatService.RemoveUser(TcpClient); } else { // If OK, send the message to the Server treatment var message = _serverChatService.SendMessage(UserInformation, Response); if (!message.Success) { throw new Exception("Error when trying to send the message. "); } } } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("*** Exception Warning:"); Console.WriteLine(ex.Message); Console.ResetColor(); // If any problem with the user, user is disconnected _serverChatService.RemoveUser(TcpClient); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("*** Exception Warning:"); Console.WriteLine(ex.Message); Console.ResetColor(); } }