public void SellTickets(Ticket ticket)
        {
            TicketDto dto = DtoUtils.GetDto(ticket);

            sendRequest(new BuyTicketRequest(dto));
            Response response = readResponse();

            if (response is ErrorResponse)
            {
                ErrorResponse err = (ErrorResponse)response;
                throw new Error(err.Message);
            }
        }
        public virtual void Logout(User user, IObserver client)
        {
            UserDto udto = DtoUtils.GetDto(user);

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

            closeConnection();
            if (response is ErrorResponse)
            {
                ErrorResponse err = (ErrorResponse)response;
                throw new Error(err.Message);
            }
        }
        public void TicketSold(Ticket ticket)
        {
            TicketDto dto = DtoUtils.GetDto(ticket);

            Console.WriteLine("Ticket sold  " + ticket);
            try
            {
                sendResponse(new BuyTicketResponse(dto));
            }
            catch (Exception e)
            {
                throw new Error("Sending error: " + e);
            }
        }
        public virtual void Login(User user, IObserver 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 Error(err.Message);
            }
        }
        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 (Error 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 (Error e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetShowsRequest)
            {
                Console.WriteLine("Get shows request");
                GetShowsRequest getShowsRequest = (GetShowsRequest)request;
                try
                {
                    Show[]    shows    = server.GetAll();
                    ShowDto[] showDtos = DtoUtils.getDto(shows);
                    return(new GetShowsResponse(showDtos));
                }
                catch (Error e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetArtistsByDateRequest)
            {
                Console.WriteLine("Get artists by date request");
                GetArtistsByDateRequest getArtistsByDateRequest = (GetArtistsByDateRequest)request;
                String date = getArtistsByDateRequest.Date;
                try
                {
                    Artist[]    artists    = server.GetArtists(date);
                    ArtistDto[] artistDtos = DtoUtils.GetDto(artists);
                    return(new GetArtistsByDateResponse(artistDtos));
                }
                catch (Error e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is BuyTicketRequest)
            {
                Console.WriteLine("Buy ticket request");
                BuyTicketRequest buyTicketRequest = (BuyTicketRequest)request;
                TicketDto        ticketDto        = buyTicketRequest.TicketDto;
                Ticket           ticket           = DtoUtils.GetFromDto(ticketDto);
                try
                {
                    lock (server)
                    {
                        server.SellTickets(ticket);
                    }
                    return(new OkResponse());
                }
                catch (InvalidPurchaseException e)
                {
                    return(new ErrorResponse(e.Message));
                }
                catch (Error e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            return(response);
        }