Esempio n. 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;
        }
Esempio n. 2
0
        private bool ValidateUserPassword(UserDTO user)
        {
            var dbUser = _userRepository.GetUserByName(user.Name);

            dbUser.Password = user.Password;

            return dbUser.IsPasswordValid();
        }
Esempio n. 3
0
 private void RegisterUser(UserDTO user)
 {
     var newUser = GetUserByName(user.Name);
     _users.Add(newUser, CurrentCallback);
     CurrentCallback.ReturnUser(newUser);
 }
Esempio n. 4
0
 private bool ContainsUser(UserDTO user)
 {
     return _userRepository.Contains(user.Name);
 }
Esempio n. 5
0
 private void AddUserToDataBase(UserDTO user)
 {
     _userRepository.Add(Mapper.Map<UserDTO, User>(user));
 }
Esempio n. 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);
 }
Esempio n. 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;
            }
        }