Exemplo n.º 1
0
        public void logout(User user, IChatObserver client)
        {
            IChatObserver localClient = loggedClients[user.Id];

            if (localClient == null)
            {
                throw new ChatException("User " + user.Id + " is not logged in.");
            }
            loggedClients.Remove(user.Id);
            notifyFriendsLoggedOut(user);
        }
Exemplo n.º 2
0
 private void notifyFriendsLoggedOut(User user)
 {
     User[] friends = userRepository.getFriends(user);
     foreach (User us in friends)
     {
         if (loggedClients.ContainsKey(us.Id))
         {
             IChatObserver chatClient = loggedClients[us.Id];
             chatClient.friendLoggedOut(user);
         }
     }
 }
Exemplo n.º 3
0
        private void notifyFriendsLoggedOut(User user)
        {
            IEnumerable <User> friends = userRepository.getFriendsOf(user);

            foreach (User us in friends)
            {
                if (loggedClients.ContainsKey(us.Id))
                {
                    IChatObserver chatClient = loggedClients[us.Id];
                    Task.Run(() => chatClient.friendLoggedOut(user));
                }
            }
        }
Exemplo n.º 4
0
 private void notifyFriendsLoggedIn(User user)
 {
     User[] friends = userRepository.getFriends(user);
     Console.WriteLine("notify logged friends " + friends.Count());
     foreach (User us in friends)
     {
         if (loggedClients.ContainsKey(us.Id))
         {
             IChatObserver chatClient = loggedClients[us.Id];
             chatClient.friendLoggedIn(user);
         }
     }
 }
Exemplo n.º 5
0
        private void notifyFriendsLoggedIn(User user)
        {
            IEnumerable <User> friends = userRepository.getFriendsOf(user);

            Console.WriteLine("notify logged friends " + friends.Count());
            foreach (User us in friends)
            {
                if (loggedClients.ContainsKey(us.Id))
                {
                    IChatObserver chatClient = loggedClients[us.Id];
                    Task.Run(() => chatClient.friendLoggedIn(user));
                }
            }
        }
Exemplo n.º 6
0
        public void sendMessage(Message message)
        {
            String id_receiver = message.Receiver.Id;

            if (loggedClients.ContainsKey(id_receiver))    //the receiver is logged in
            {
                IChatObserver receiverClient = loggedClients[id_receiver];
                receiverClient.messageReceived(message);
            }
            else
            {
                throw new ChatException("User " + id_receiver + " not logged in.");
            }
        }
Exemplo n.º 7
0
        public virtual void logout(User user, IChatObserver client)
        {
            UserDTO udto = DTOUtils.getDTO(user);

            sendRequest(new LogoutRequest(udto));
            Response response = readResponse();

            closeConnection();
            if (response is ErrorResponse)
            {
                ErrorResponse err = (ErrorResponse)response;
                throw new ChatException(err.Message);
            }
        }
Exemplo n.º 8
0
 private void closeConnection()
 {
     finished = true;
     try
     {
         stream.Close();
         //output.close();
         connection.Close();
         _waitHandle.Close();
         client = null;
     }
     catch (Exception e)
     {
         Console.WriteLine(e.StackTrace);
     }
 }
Exemplo n.º 9
0
        public void login(User user, IChatObserver client)
        {
            User userOk = userRepository.findBy(user.Id, user.Password);

            if (userOk != null)
            {
                if (loggedClients.ContainsKey(user.Id))
                {
                    throw new ChatException("User already logged in.");
                }
                loggedClients[user.Id] = client;
                notifyFriendsLoggedIn(user);
            }
            else
            {
                throw new ChatException("Authentication failed.");
            }
        }
Exemplo n.º 10
0
        public void login(User user, IChatObserver client)
        {
            bool loginOk = userRepository.verifyUser(user);

            if (loginOk)
            {
                if (loggedClients.ContainsKey(user.Id))
                {
                    throw new ChatException("User already logged in.");
                }
                loggedClients[user.Id] = client;
                notifyFriendsLoggedIn(user);
            }
            else
            {
                throw new ChatException("Authentication failed.");
            }
        }
Exemplo n.º 11
0
        public virtual void login(User user, IChatObserver client)
        {
            initializeConnection();
            UserDTO udto = DTOUtils.getDTO(user);

            sendRequest(new LoginRequest(udto));
            Response response = readResponse();

            if (response is OkResponse)
            {
                this.client = client;
                return;
            }
            if (response is ErrorResponse)
            {
                ErrorResponse err = (ErrorResponse)response;
                closeConnection();
                throw new ChatException(err.Message);
            }
        }
Exemplo n.º 12
0
 public void logout(User user, IChatObserver client)
 {
 }
Exemplo n.º 13
0
 public Task UnsubscribeFromMessages(IChatObserver subscriber)
 {
     _subscribers.Unsubscribe(subscriber);
     return TaskDone.Done;
 }
Exemplo n.º 14
0
 public Task SubscribeForMessages(IChatObserver subscriber)
 {
     _subscribers.Subscribe(subscriber);
     return TaskDone.Done;
 }