Ejemplo n.º 1
0
        public void ConectToServer()
        {
            try
            {
                socket.Bind(clientEndPoint);

                socket.Connect(serverEndPoint);

                Console.WriteLine("Conectado al servidor\n");
                Console.WriteLine("Bienvenido.....\n");
                int                option       = -1;
                bool               exit         = false;
                string             credentials  = "";
                string             username     = "";
                string             userPassword = "";
                Header             header;
                DataTransformSuper dataTransferReciver = new StringDataTransform();
                DataTransformSuper dataTransferSender  = new StringDataTransform();
                while (!exit)
                {
                    DisplayStarMenu();

                    option = GetOption(option);
                    Console.WriteLine("Ingrese usuario:\n");
                    username = Console.ReadLine();
                    Console.WriteLine("Ingrese Contrasenia\n");
                    userPassword = Console.ReadLine();
                    credentials  = username + Common.DataTransfer.SEPARATOR + userPassword;

                    switch (option)
                    {
                    case 1:
                        option = CommandConstants.SignUp;
                        break;

                    case 2:
                        option = CommandConstants.Login;
                        exit   = true;
                        break;

                    default:
                        Console.WriteLine("Opcion Invalida");
                        break;
                    }
                    header = new Header(HeaderConstants.Request, option, credentials.Length);
                    var codedMessage = dataTransferSender.GenMenssage(credentials, header);
                    DataTransference.SendData(codedMessage, socket);
                }
                DataTransferResult result = DataTransference.RecieveData(socket);
                result.objectResult = dataTransferReciver.DecodeMessage((byte[])result.objectResult);

                dynamic resultData = result.objectResult;
                bool    isLogged   = Boolean.Parse(resultData);


                while (isLogged)
                {
                    if (isLogged)
                    {
                        DisplayMenu();
                    }
                    bool needToSend = false;
                    option = -1;
                    int    command      = -1;
                    string data         = "";
                    byte[] codedRequest = new byte[1];
                    option = GetOption(option);
                    switch (option)
                    {
                    case CommandConstants.UploadFile:
                        command = CommandConstants.UploadFile;
                        Console.WriteLine("Ingrese el path del archivo");
                        var path = Console.ReadLine();
                        SendFile(path);
                        break;

                    case CommandConstants.ListUsers:
                        command             = CommandConstants.ListUsers;
                        dataTransferReciver = new ListStringDataTransform();
                        NeedToSend(dataTransferSender, dataTransferReciver, Common.CommandConstants.ListUsers, data, socket);

                        break;

                    case CommandConstants.ListFiles:
                        command = CommandConstants.ListFiles;
                        Console.WriteLine("Ingrese el usuario");
                        data = Console.ReadLine();
                        dataTransferReciver = new ListStringDataTransform();
                        NeedToSend(dataTransferSender, dataTransferReciver, Common.CommandConstants.ListFiles, data, socket);
                        break;

                    case 6:
                        command = CommandConstants.ViewComents;
                        NeedToSend(dataTransferSender, dataTransferReciver, Common.CommandConstants.ListFiles, username, socket);
                        Console.WriteLine("Seleccione foto");
                        string photo = Console.ReadLine();
                        data = username + Common.DataTransfer.SEPARATOR + photo;
                        dataTransferReciver = new ListStringDataTransform();
                        NeedToSend(dataTransferSender, dataTransferReciver, Common.CommandConstants.ViewComents, data, socket);
                        break;

                    case 7:
                        command             = CommandConstants.AddComent;
                        dataTransferReciver = new ListStringDataTransform();
                        NeedToSend(dataTransferSender, dataTransferReciver, Common.CommandConstants.ListUsers, "", socket);
                        Console.WriteLine("Seleccione  usuario");
                        string userComment = Console.ReadLine();
                        NeedToSend(dataTransferSender, dataTransferReciver, Common.CommandConstants.ListFiles, userComment, socket);
                        Console.WriteLine("Seleccione foto");
                        photo = Console.ReadLine();
                        Console.WriteLine("Ingrese comentario");
                        string comment = Console.ReadLine();
                        data = userComment + Common.DataTransfer.SEPARATOR + photo + Common.DataTransfer.SEPARATOR + comment;

                        header       = new Header(HeaderConstants.Request, command, data.Length);
                        codedRequest = dataTransferSender.GenMenssage(data, header);

                        DataTransfer.SendData(codedRequest, socket);
                        break;

                    default:
                        Console.WriteLine("Invalid command");
                        break;
                    }
                    if (needToSend)
                    {
                        header       = new Header(HeaderConstants.Request, command, data.Length);
                        codedRequest = dataTransferSender.GenMenssage(data, header);

                        result = DataTransference.RecieveData(socket);
                        if (result.Header.GetCommand() == CommandConstants.ErrorListing)
                        {
                            Console.WriteLine("No hay elementos para listar");
                        }
                        else
                        {
                            result.objectResult = dataTransferReciver.DecodeMessage((byte[])result.objectResult);


                            resultData = result.objectResult as List <string>;
                            Display(resultData);
                        }
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Ocurrio algun error con el servidor");
            }
        }
Ejemplo n.º 2
0
        public void HandleClient(Socket socket)
        {
            DataTransformSuper transfer = new StringDataTransform();
            Response           response = new StringResponse();
            User   user     = new User();
            bool   endConn  = false;
            long   fileSize = 0;
            string fileName = "";

            while (isServerUp && !endConn)
            {
                DataTransferResult result = DataTransference.RecieveData(socket);
                if (result != null)
                {
                    int    command    = result.Header.GetCommand();
                    int    dataLength = result.Header.GetDataLength();
                    string direction  = result.Header.GetDirection();
                    string word       = "";
                    Log    log        = new Log()
                    {
                        Command = command,
                        Date    = DateTime.Now,
                    };
                    if (command != 31)
                    {
                        transfer            = new StringDataTransform();
                        result.objectResult = transfer.DecodeMessage((byte[])result.objectResult);
                        word        = (string)result.objectResult;
                        log.Message = word;
                    }
                    ;
                    dynamic responseData;


                    switch (command)
                    {
                    case CommandConstants.Login:
                        try
                        {
                            user         = Login(word);
                            response     = new StringResponse();
                            responseData = "true";
                            response.SendResponse(command, responseData, socket, responseData.Length);
                            SendSuccessfullLog(user, log);
                            break;
                        }
                        catch (Exception)
                        {
                            response     = new StringResponse();
                            responseData = "false";
                            response.SendResponse(command, responseData, socket, responseData.Length);
                            SendWarningLog(log);
                            break;
                        }

                    case CommandConstants.SignUp:
                        try
                        {
                            SignUp(word);
                            SendSuccessfullLog(user, log);
                            break;
                        }
                        catch (UserAlreadyExistException)
                        {
                            SendWarningLog(log);
                            break;
                        }

                    case CommandConstants.ListUsers:
                        try
                        {
                            List <string> usersList = GetUsers();
                            response     = new ListStringResponse();
                            responseData = usersList;
                            int responseDataLength = ListStringDataTransform.ListLength(usersList);
                            response.SendResponse(command, responseData, socket, responseDataLength);
                            SendSuccessfullLog(user, log);
                            break;
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Ocurrio un error al listar los usuarios");
                            SendWarningLog(log);
                            break;
                        }

                    case CommandConstants.ListFiles:
                        try
                        {
                            User userPhoto = new User();
                            userPhoto.Username = word;
                            List <string> fileList = GetUserPhotos(userPhoto);
                            response     = new ListStringResponse();
                            responseData = fileList;
                            int responseDataLength = ListStringDataTransform.ListLength(fileList);
                            if (responseDataLength == 0)
                            {
                                throw new Exception();
                            }
                            response.SendResponse(command, responseData, socket, responseDataLength);
                            SendSuccessfullLog(user, log);
                            break;
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Ocurrio un problema al listar los archivos");
                            response     = new StringResponse();
                            responseData = "Error al listar fotos";
                            response.SendResponse(Common.CommandConstants.ErrorListing, responseData, socket, responseData.Length);
                            SendWarningLog(log);
                            break;
                        }

                    case CommandConstants.UploadFile:
                        ReciveFileData(word, out fileName, out fileSize);
                        transfer = new ByteDataTransform();
                        Photo photo1 = new Photo();
                        photo1.Name     = fileName;
                        photo1.Comments = new List <string>();
                        user.AddPhoto(photo1);

                        SendSuccessfullLog(user, log);
                        break;

                    case CommandConstants.UploadFileSignal:
                        byte[]             fileBytes     = (byte[])result.objectResult;
                        IFileSenderHandler senderHandler = new FileSenderHandler();
                        senderHandler.Write(fileName, fileBytes);
                        log.Username = user.Username;
                        log.Message  = "File part";
                        SendLog(log);
                        break;

                    case CommandConstants.AddComent:
                        try
                        {
                            AddComment(word);
                            SendSuccessfullLog(user, log);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Ocurrio un problema al agregar el comentario");
                            SendWarningLog(log);
                        }
                        break;

                    case CommandConstants.ViewComents:
                        try
                        {
                            List <string> comments;
                            string        userName, photo;
                            GetCredentials(word, out userName, out photo);
                            Photo photoForComments = GetPhoto(userName, photo);
                            comments = photoForComments.Comments;

                            response     = new ListStringResponse();
                            responseData = comments;
                            int responseDataLength = ListStringDataTransform.ListLength(comments);
                            response.SendResponse(command, responseData, socket, responseDataLength);
                            SendSuccessfullLog(user, log);
                            break;
                        }catch (Exception)
                        {
                            Console.WriteLine("Error al mostrar comentarios de la foto");
                            response     = new StringResponse();
                            responseData = "Error al listar fotos";
                            response.SendResponse(Common.CommandConstants.ErrorListing, responseData, socket, responseData.Length);
                            SendWarningLog(log);
                            break;
                        }

                    default:
                        Console.WriteLine("Invalid command");
                        break;
                    }
                }
                else
                {
                    socket.Close();
                    _conectedClients.Remove(socket);
                    endConn = true;
                }
            }
        }