public void logout()
 {
     Console.WriteLine("Ctrl logout");
     server.logout(currentUser, this);
     currentUser = null;
 }
Example #2
0
        private ChatResponse handleRequest(ChatRequest request)
        {
            ChatResponse response = null;

            ChatRequest.Types.Type reqType = request.Type;
            switch (reqType)
            {
            case ChatRequest.Types.Type.Login:
            {
                Console.WriteLine("Login request ...");
                //LoginRequest logReq =(LoginRequest)request;
                //UserDTO udto =logReq.User;
                chat.model.User user = ProtoUtils.getUser(request);
                try
                {
                    lock (server)
                    {
                        server.login(user, this);
                    }
                    return(ProtoUtils.createOkResponse());
                }
                catch (ChatException e)
                {
                    connected = false;
                    return(ProtoUtils.createErrorResponse(e.Message));
                }
            }

            case ChatRequest.Types.Type.Logout:
            {
                Console.WriteLine("Logout request");
                //LogoutRequest logReq =(LogoutRequest)request;
                //UserDTO udto =logReq.User;
                chat.model.User user = ProtoUtils.getUser(request);
                try
                {
                    lock (server)
                    {
                        server.logout(user, this);
                    }
                    connected = false;
                    return(ProtoUtils.createOkResponse());
                }
                catch (ChatException e)
                {
                    return(ProtoUtils.createErrorResponse(e.Message));
                }
            }

            case ChatRequest.Types.Type.SendMessage:
            {
                Console.WriteLine("SendMessageRequest ...");
                //SendMessageRequest senReq =(SendMessageRequest)request;
                //MessageDTO mdto =senReq.Message;
                chat.model.Message message = ProtoUtils.getMessage(request);
                try
                {
                    lock (server)
                    {
                        server.sendMessage(message);
                    }
                    return(ProtoUtils.createOkResponse());
                }
                catch (ChatException e)
                {
                    return(ProtoUtils.createErrorResponse(e.Message));
                }
            }

            case ChatRequest.Types.Type.GetLoggedFriends:
            {
                Console.WriteLine("GetLoggedFriends Request ...");
                //GetLoggedFriendsRequest getReq =(GetLoggedFriendsRequest)request;
                //UserDTO udto =getReq.User;
                chat.model.User user = ProtoUtils.getUser(request);  //DTOUtils.getFromDTO(udto);
                try
                {
                    chat.model.User[] friends;
                    lock (server)
                    {
                        friends = server.getLoggedFriends(user);
                    }
                    //UserDTO[] frDTO =DTOUtils.getDTO(friends);
                    return(ProtoUtils.createLoggedFriendsResponse(friends));
                }
                catch (ChatException e)
                {
                    return(ProtoUtils.createErrorResponse(e.Message));
                }
            }
            }
            return(response);
        }
        private Response handleRequest(Request request)
        {
            Response response = null;

            if (request is LoginRequest)
            {
                Console.WriteLine("Login request ...");
                LoginRequest logReq = (LoginRequest)request;
                UserDTO      udto   = logReq.User;
                User         user   = DTOUtils.getFromDTO(udto);
                try
                {
                    lock (server)
                    {
                        server.login(user, this);
                    }
                    return(new OkResponse());
                }
                catch (ChatException e)
                {
                    connected = false;
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is LogoutRequest)
            {
                Console.WriteLine("Logout request");
                LogoutRequest logReq = (LogoutRequest)request;
                UserDTO       udto   = logReq.User;
                User          user   = DTOUtils.getFromDTO(udto);
                try
                {
                    lock (server)
                    {
                        server.logout(user, this);
                    }
                    connected = false;
                    return(new OkResponse());
                }
                catch (ChatException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is SendMessageRequest)
            {
                Console.WriteLine("SendMessageRequest ...");
                SendMessageRequest senReq  = (SendMessageRequest)request;
                MessageDTO         mdto    = senReq.Message;
                Message            message = DTOUtils.getFromDTO(mdto);
                try
                {
                    lock (server)
                    {
                        server.sendMessage(message);
                    }
                    return(new OkResponse());
                }
                catch (ChatException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetLoggedFriendsRequest)
            {
                Console.WriteLine("GetLoggedFriends Request ...");
                GetLoggedFriendsRequest getReq = (GetLoggedFriendsRequest)request;
                UserDTO udto = getReq.User;
                User    user = DTOUtils.getFromDTO(udto);
                try
                {
                    User[] friends;
                    lock (server)
                    {
                        friends = server.getLoggedFriends(user);
                    }
                    UserDTO[] frDTO = DTOUtils.getDTO(friends);
                    return(new GetLoggedFriendsResponse(frDTO));
                }
                catch (ChatException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            return(response);
        }