Beispiel #1
0
        public ConnectedStatuses Connect(UserDTO user)
        {
            if (!_users.ContainsValue(CurrentCallback) &&
                !SearchClientsByName(user.Name)) //exists user in chatroom
            {
                lock (_syncObj)
                {
                    if (ContainsUser(user)) //exists user in db
                    {
                        if(!ValidateUserPassword(user))
                        {
                            return ConnectedStatuses.PasswordNotValid;
                        }
                    }
                    else
                    {
                        AddUserToDataBase(user);
                    }

                    RegisterUser(user); //register user in chatroom

                    foreach (var key in _users.Keys)
                    {
                        var callback = _users[key];
                        try
                        {
                            UpdateClients(callback);
                        }
                        catch
                        {
                            _users.Remove(key);
                            return ConnectedStatuses.Error;
                        }
                    }
                }
                return ConnectedStatuses.Connected;
            }
            return ConnectedStatuses.AlreadyConnected;
        }
Beispiel #2
0
        private bool ValidateUserPassword(UserDTO user)
        {
            var dbUser = _userRepository.GetUserByName(user.Name);

            dbUser.Password = user.Password;

            return dbUser.IsPasswordValid();
        }
Beispiel #3
0
 private void RegisterUser(UserDTO user)
 {
     var newUser = GetUserByName(user.Name);
     _users.Add(newUser, CurrentCallback);
     CurrentCallback.ReturnUser(newUser);
 }
Beispiel #4
0
 private bool ContainsUser(UserDTO user)
 {
     return _userRepository.Contains(user.Name);
 }
Beispiel #5
0
 private void AddUserToDataBase(UserDTO user)
 {
     _userRepository.Add(Mapper.Map<UserDTO, User>(user));
 }
Beispiel #6
0
 public List<MessageDTO> GetLogByUser(UserDTO user)
 {
     var byUser = Mapper.Map<UserDTO, User>(user);
     var messages = _messageRepository.GetMessagesByUser(byUser).ToList();
     return
         Mapper.Map<List<Message>, List<MessageDTO>>(messages);
 }
Beispiel #7
0
        public void Disconnect(UserDTO user)
        {
            foreach (var c in _users.Keys)
            {
                if (user.Name != c.Name) continue;

                lock (_syncObj)
                {
                    _users.Remove(c);
                    NotifyClientsForUserDisconnect();
                }
                return;
            }
        }