Beispiel #1
0
        /* Funciones auxiliares */

        /*  función estática CheckArguments(string) => bool
         *      Comprueba si el primer argumento con el directorio de salido es correcto,
         *      y si el segundo argumento (modo de operación del Provider) es correcto.
         *      Comprueba en el modo --specific si se provee el tercer argumento necesario.
         *      Actualiza los campos _providerCode, _providerFilePath (solo --specific) y _packagerDirPath
         *      @return OK si es correcto, o el código de error en otro caso
         */
        private static EStatusCodes CheckArguments(IEnumerator <string> args)
        {
            args.MoveNext();

            if (!Directory.Exists(args.Current))
            {
                Log(null, "Please provide a valid directory to store processed documents", ELogLevel.WARN);
                return(EStatusCodes.INVALID_DIR);
            }

            _packagerDirPath = args.Current;

            if (!args.MoveNext())
            {
                DisplayHelp();
                return(EStatusCodes.DISPLAY_HELP);
            }

            if (args.Current.Equals("--all"))
            {
                _providerCode = EProviderOperationCode.PROVIDE_ALL;
            }
            else if (args.Current.Equals("--daily"))
            {
                _providerCode = EProviderOperationCode.PROVIDE_DAILY;
            }
            else if (args.Current.Equals("--latest"))
            {
                _providerCode = EProviderOperationCode.PROVIDE_LATEST;
            }
            else if (args.Current.Equals("--specific"))
            {
                // En caso de querer mapear un documento específico, comprueba si se ha pasado como argumento
                if (!args.MoveNext())
                {
                    Log(null, "Please provide a file to be mapped", ELogLevel.WARN);
                    return(EStatusCodes.PATH_UNPROVIDED);
                }

                _providerCode     = EProviderOperationCode.PROVIDE_SPECIFIC;
                _providerFilePath = args.Current;
            }
            else
            {
                // En caso de no proveer un argumento correcto, muestra el mensaje de ayuda
                DisplayHelp();
                return(EStatusCodes.WRONG_CODE);
            }

            return(EStatusCodes.OK);
        }
Beispiel #2
0
        /* Constructor */

        // @param Log : Puntero a la función de logging
        // @param code : descriptor del modo de operación
        // @param filePath : ruta al documento para proveer, local o remoto
        // @throws FileNotFoundException : si el parámetro filePath no se corresponde ni a un archivo local ni a una URI correcta
        // @throws InvalidOperationCodeException : si el parámetro code no es vaĺido
        public Provider(Action <object, string, ELogLevel> Log, EProviderOperationCode code, string filePath)
        {
            _Log = Log;

            // Inicializa la estructura de ficheros y crea el directirio temporal si no existe
            Files = new AsyncCollection <Document>();
            if (!Directory.Exists("./tmp"))
            {
                Directory.CreateDirectory("./tmp");
            }

            if (code == EProviderOperationCode.PROVIDE_SPECIFIC)
            {
                // Si el fichero provisto existe en local, lo añade a la estructura y termina
                if (File.Exists(filePath))
                {
                    _fileName = filePath;
                    Files.Add(new Document(filePath));

                    _Log(this, $"Retrieving local file {filePath}", ELogLevel.INFO);

                    Files.CompleteAdding();
                }
                // Si el fichero provisto no existe en local, se asevera si está formado como una URI correcta
                else if (Uri.TryCreate(filePath, UriKind.RelativeOrAbsolute, out Uri uri) && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))
                {
                    _fileName = filePath;

                    // Crea el cliente Web e inicia la descarga
                    _webClient = new WebClient();

                    _webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);
                    _webClient.DownloadFileAsync(uri, _OUTPUT_PATH);

                    _Log(this, $"Downloading {_fileName} file", ELogLevel.INFO);
                }
                else
                {
                    _Log(this, $"Path {filePath} doesn't match with any local file or correct URI", ELogLevel.ERROR);
                    throw new FileNotFoundException();
                }
            }
            else
            {
                // Inicializa el WebClient
                _webClient = new WebClient();
                _webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);

                if (code == EProviderOperationCode.PROVIDE_ALL || code == EProviderOperationCode.PROVIDE_DAILY)
                {
                    // Inicializa el mecanismo de sincronización
                    _mre = new ManualResetEvent(false);

                    // Lanza en background la tarea que irá proveyendo documentos
                    Thread downloader = new Thread(BackgroundDownloader);
                    downloader.IsBackground = true;
                    downloader.Start();

                    _Log(this, "Provider background downloader thread launched", ELogLevel.INFO);
                }
                else if (code == EProviderOperationCode.PROVIDE_LATEST)
                {
                    // Descarga el último fichero, siempre descrito por esta ruta
                    _fileName = _LATEST_PATH;
                    _webClient.DownloadFileAsync(new Uri(_fileName), _OUTPUT_PATH);

                    _Log(this, $"Downloading {_fileName} file", ELogLevel.INFO);
                }
                else
                {
                    _Log(this, "Please provide a valid operational code", ELogLevel.ERROR);
                    throw new InvalidOperationCodeException();
                }
            }
        }