public async Task HandleClient(Socket socket)
        {
            DataTransferSuper transfer = new StringDataTransfer();
            Response          response = new StringResponse();
            User   user;
            string fileName = "";

            while (true)
            {
                DataTransferResult result = transfer.RecieveData(socket);

                int    command    = result.Header.GetCommand();
                int    dataLength = result.Header.GetDataLength();
                string direction  = result.Header.GetDirection();
                string word       = "";

                if (command != 31)
                {
                    word = (string)result.objectResult;
                }
                ;
                Log log = new Log()
                {
                    Command = command,
                    Message = word,
                    Date    = DateTime.Now,
                };

                dynamic responseData;
                switch (command)
                {
                case CommandConstants.Login:
                    try
                    {
                        user         = Login(word);
                        response     = new StringResponse();
                        responseData = "true";
                        response.SendResponse(command, responseData, socket, responseData.Length);
                        log.Level    = Log.SUCCESS_LEVEL;
                        log.Username = user.Username;
                        SendLog(log);
                        break;
                    }
                    catch (Exception)
                    {
                        response     = new StringResponse();
                        responseData = "false";
                        response.SendResponse(command, responseData, socket, responseData.Length);
                        log.Level    = Log.WARNING_LEVEL;
                        log.Username = "******";
                        SendLog(log);
                        break;
                    }

                case CommandConstants.SignUp:
                    try
                    {
                        SignUp(word);
                        //response = new StringResponse();
                        //responseData = "true";
                        //response.SendResponse(command, responseData, socket, responseData.Length);
                        break;
                    }
                    catch (UserAlreadyExistException)
                    {
                        break;
                    }

                case CommandConstants.ListUsers:
                    List <string> usersList = GetUsers();
                    response     = new ListStringResponse();
                    responseData = usersList;
                    int responseDataLength = ListStringDataTransfer.ListLength(usersList);
                    response.SendResponse(command, responseData, socket, responseDataLength);
                    break;

                case CommandConstants.ListFiles:
                    User userPhoto = new User();
                    userPhoto.Username = word;
                    List <string> fileList = GetUserPhotos(userPhoto);
                    response           = new ListStringResponse();
                    responseData       = fileList;
                    responseDataLength = ListStringDataTransfer.ListLength(fileList);
                    response.SendResponse(command, responseData, socket, responseDataLength);
                    break;

                case CommandConstants.UploadFile:
                    long fileSize;
                    ReciveFileData(word, out fileName, out fileSize);
                    transfer = new ByteDataTransfer();
                    break;

                case CommandConstants.UploadFileSignal:
                    byte[]             fileBytes     = (byte[])result.objectResult;
                    IFileSenderHandler senderHandler = new FileSenderHandler();
                    senderHandler.Write(fileName, fileBytes);
                    Console.WriteLine(Directory.GetCurrentDirectory());
                    break;

                default:
                    Console.WriteLine("Invalid command");
                    break;
                }
            }
        }
Esempio 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;
                }
            }
        }