// // Summary: // Removes a connection id from a given chat room by updating the chat room's active connection pool. // // Returns: // ChatRoomConnectionPool - an updated chat room connection pool without the user connection. // // Raises: // ObjectDoesNotExistException - the chat room or the user was not found. public async Task <Tuple <ChatRoomConnectionPool, string> > RemoveConnectionFromChatRoomConnectionPool(string chatRoomName, string connectionId) { var chatRoomConnectionPool = await GetChatRoomConnectionPool(chatRoomName); var chatRoomId = chatRoomConnectionPool.ChatRoomId; var serverInstanceId = GlobalState.ServerInstanceId; var updatedChatRoomConnectionPool = ChatRoomLogic.DecrementChatRoomConnectionPool(serverInstanceId, connectionId, chatRoomConnectionPool); await _redis.SetKey(chatRoomId, updatedChatRoomConnectionPool, null); return(new Tuple <ChatRoomConnectionPool, string>(updatedChatRoomConnectionPool, connectionId)); }
// // Summary: // Processes messages sent by users. // // Raises: // ObjectDoesNotExistException - if the chat room or user is not found public async Task <string> ProcessUserMessage(string userName, string chatRoomName, string rawMessage) { var chatRoomConnectionPool = await GetChatRoomConnectionPool(chatRoomName); var user = await _userService.GetUser(userName); if (!ChatRoomLogic.IsUserOnChatRoomConnectionPool(user.Id, chatRoomConnectionPool)) { throw new ObjectDoesNotExistException("User is not in the room!"); } // TODO: check if it's a command and process it return($"{userName}: {rawMessage}"); }
// // Summary: // Connects a user to a given chat room by updating the chat room's active connection pool with the user's // new connection. If the the user or chat room are not found or if the room is full, it raises an exception. // // Returns: // ChatRoomConnectionPool - an updated chat room connection pool with the users new connection. // // Raises: // ChatRoomIsFullException - the chat is full (risen by the chat room domain logic) // ObjectDoesNotExistException - the chat room or the user were not found // UserIsAlreadyConnectedException - the user is attempting to connect to a chat room in which he's already connected public async Task <ChatRoomConnectionPool> AddUserToChatRoomConnectionPool(string chatRoomName, string userName, string connectionId) { var chatRoomConnectionPool = await GetChatRoomConnectionPool(chatRoomName); var user = await _userService.GetUser(userName); var chatRoomId = chatRoomConnectionPool.ChatRoomId; var serverInstanceId = GlobalState.ServerInstanceId; // domain logic to increment the chat room distributed connections var updatedChatRoomConnectionPool = ChatRoomLogic.IncrementChatRoomConnectionPool(user.Id, serverInstanceId, connectionId, chatRoomConnectionPool); await _redis.SetKey(chatRoomId, updatedChatRoomConnectionPool, null); return(updatedChatRoomConnectionPool); }
// // Summary: // Removes a username id from a given chat room by updating the chat room's active connection pool. // // Returns: // ChatRoomConnectionPool - an updated chat room connection pool without the user connection. // // Raises: // ObjectDoesNotExistException - the chat room or the user was not found. public async Task <Tuple <ChatRoomConnectionPool, string> > RemoveUserFromChatRoomConnectionPool(string chatRoomName, string userName) { var chatRoomConnectionPool = await GetChatRoomConnectionPool(chatRoomName); var user = await _userService.GetUser(userName); var chatRoomId = chatRoomConnectionPool.ChatRoomId; var serverInstanceId = GlobalState.ServerInstanceId; var removedConnectionId = ChatRoomLogic.GetConnectionIdFromChatRoomConnectionPool(user.Id, chatRoomConnectionPool); var updatedChatRoomConnectionPool = ChatRoomLogic.DecrementChatRoomConnectionPool(serverInstanceId, user.Id, chatRoomConnectionPool); await _redis.SetKey(chatRoomId, updatedChatRoomConnectionPool, null); return(new Tuple <ChatRoomConnectionPool, string>(updatedChatRoomConnectionPool, removedConnectionId)); }
public void TestChatRoomLogicShouldAddNewUserToAChatRoomWithEmptyConnectionPool() { // arrange var connectionId = "fakeId"; var chatRoomId = Guid.NewGuid(); var instanceId = Guid.NewGuid(); var firstUserId = Guid.NewGuid(); var emptyConnectionPool = TestingScenarioBuilder.BuildChatRoomConnectionPoolEmpty(chatRoomId); // act: FIRST user wants to connect to the room var updatedConnectionPool = ChatRoomLogic.IncrementChatRoomConnectionPool(firstUserId, instanceId, connectionId, emptyConnectionPool); // 0 connections // assert Assert.Equal(1, updatedConnectionPool.TotalActiveConnections); // 1 connection after Assert.Equal(firstUserId, updatedConnectionPool.ServerInstances.ElementAt(0).ConnectedUsers.First().UserId); // user has been appended Assert.Equal(connectionId, updatedConnectionPool.ServerInstances.ElementAt(0).ConnectedUsers.First().ConnectionId); // user has been appended }
public void TestChatRoomLogicShouldRemoveUserFromAnExistingChatRoomConnectionPool() { // arrange var chatRoomId = Guid.NewGuid(); var instanceId1 = Guid.NewGuid(); var instanceId2 = Guid.NewGuid(); var initialConnectionPool = TestingScenarioBuilder .BuildChatRoomConnectionPoolTwoInstances(chatRoomId, 6, instanceId1, instanceId2); // act: remove user with connection 1 from room -- starts with 4 users var updatedConnectionPool = ChatRoomLogic.DecrementChatRoomConnectionPool(instanceId1, "connection 1", initialConnectionPool); // assert var remainingConnectedUser = updatedConnectionPool.ServerInstances.First().ConnectedUsers.First(); Assert.Equal(3, updatedConnectionPool.TotalActiveConnections); // 3 users after one is removed Assert.Single(updatedConnectionPool.ServerInstances.First().ConnectedUsers); // only 1 connection on instanceId1 Assert.Equal("connection 2", remainingConnectedUser.ConnectionId); }
public void TestChatRoomLogicShouldAddNewUserToExistingChatRoomConnectionPool() { // arrange var connectionId = "fakeId"; var chatRoomId = Guid.NewGuid(); var instanceId1 = Guid.NewGuid(); var instanceId2 = Guid.NewGuid(); var newUserId = Guid.NewGuid(); var initialConnectionPool = TestingScenarioBuilder .BuildChatRoomConnectionPoolTwoInstances(chatRoomId, 6, instanceId1, instanceId2); // act: new user that wants to connect to the room being on instance 2 var updatedConnectionPool = ChatRoomLogic.IncrementChatRoomConnectionPool(newUserId, instanceId2, connectionId, initialConnectionPool); // 4 connections // assert Assert.Equal(5, updatedConnectionPool.TotalActiveConnections); // 5 connections after Assert.Equal(newUserId, updatedConnectionPool.ServerInstances.ElementAt(1).ConnectedUsers.ElementAt(2).UserId); // user has been appended Assert.Equal(connectionId, updatedConnectionPool.ServerInstances.ElementAt(1).ConnectedUsers.ElementAt(2).ConnectionId); // user has been appended }