Example #1
0
        public void RequestUserPhotos(string message)
        {
            DataTransformSuper dataTransferSender = new StringDataTransform();

            Header header = new Header(HeaderConstants.Request, CommandConstants.ListFiles, message.Length);

            var codedMesagge = dataTransferSender.GenMenssage(message, header);

            DataTransference.SendData(codedMesagge, socket);
        }
Example #2
0
        private List <string> ListFiles(string message, Socket socket)
        {
            DataTransformSuper dataTransferReciver = new StringDataTransform();
            DataTransferResult result = new DataTransferResult();

            result = DataTransference.RecieveData(socket);

            result.objectResult = dataTransferReciver.DecodeMessage((byte[])result.objectResult);

            List <string> resultData = result.objectResult as List <string>;

            return(resultData);
        }
Example #3
0
        private void SendFile(string path)
        {
            DataTransformSuper dataTransferSender = new StringDataTransform();

            IFileSenderHandler senderHandler = new FileSenderHandler();
            IFileHandler       fileHandler   = new FileHandler();
            long   fileSize = fileHandler.GetFileSize(path);
            string fileName = fileHandler.GetFileName(path);
            string message  = fileName + Common.DataTransfer.SEPARATOR + fileSize;
            Header header   = new Header(HeaderConstants.Request, CommandConstants.UploadFile, message.Length);

            var codedMessage = dataTransferSender.GenMenssage(message, header);

            DataTransference.SendData(codedMessage, socket);

            long segments = (fileSize / FileSenderHandler.FileSegmentSize);

            segments = segments * FileSenderHandler.FileSegmentSize == fileSize ? segments : segments + 1;

            long offset          = 0;
            long currentSegments = 1;

            while (fileSize > offset)
            {
                byte[] fileData;
                int    size = 0;
                if (currentSegments == segments)
                {
                    size     = (int)(fileSize - offset);
                    fileData = senderHandler.Read(path, offset, size);
                    offset  += size;
                    currentSegments++;
                }
                else
                {
                    size     = (int)FileSenderHandler.FileSegmentSize;
                    fileData = senderHandler.Read(path, offset, size);
                    offset  += size;
                    currentSegments++;
                }

                dataTransferSender = new ByteDataTransform();
                Header header1 = new Header(HeaderConstants.Request, CommandConstants.UploadFileSignal, size);

                var data = dataTransferSender.GenMenssage(fileData, header1);
                Console.WriteLine("Mande paquete " + currentSegments);
                DataTransference.SendData(data, socket);
            }
        }
Example #4
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");
            }
        }
Example #5
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;
                }
            }
        }