/// <summary>
        /// Initializes a new instance of the <see cref="RemoteFileSubscriptionStreamReader"/> class.
        /// </summary>
        /// <param name="dataCacheProvider">The <see cref="IDataCacheProvider"/> used to retrieve a stream of data</param>
        /// <param name="source">The remote url to be downloaded via web client</param>
        /// <param name="downloadDirectory">The local directory and destination of the download</param>
        /// <param name="headers">Defines header values to add to the request</param>
        public RemoteFileSubscriptionStreamReader(IDataCacheProvider dataCacheProvider, string source, string downloadDirectory, IEnumerable <KeyValuePair <string, string> > headers)
        {
            // don't use cache if data is ephemeral
            // will be false for live history requests and live subscriptions
            var useCache = !dataCacheProvider.IsDataEphemeral;

            // create a hash for a new filename
            var filename    = (useCache ? source.ToMD5() : Guid.NewGuid().ToString()) + source.GetExtension();
            var destination = Path.Combine(downloadDirectory, filename);

            string contents = null;

            if (useCache)
            {
                lock (_fileSystemLock)
                {
                    if (!File.Exists(destination))
                    {
                        contents = _downloader.Download(source, headers, null, null);
                        File.WriteAllText(destination, contents);
                    }
                }
            }
            else
            {
                contents = _downloader.Download(source, headers, null, null);
                File.WriteAllText(destination, contents);
            }

            if (contents != null)
            {
                // Send the file to the dataCacheProvider so it is available when the streamReader asks for it
                dataCacheProvider.Store(destination, System.Text.Encoding.UTF8.GetBytes(contents));
            }

            // now we can just use the local file reader
            _streamReader = new LocalFileSubscriptionStreamReader(dataCacheProvider, destination);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RemoteFileSubscriptionStreamReader"/> class.
        /// </summary>
        /// <param name="dataCacheProvider">The <see cref="IDataCacheProvider"/> used to retrieve a stream of data</param>
        /// <param name="source">The remote url to be downloaded via web client</param>
        /// <param name="downloadDirectory">The local directory and destination of the download</param>
        /// <param name="headers">Defines header values to add to the request</param>
        public RemoteFileSubscriptionStreamReader(IDataCacheProvider dataCacheProvider, string source, string downloadDirectory, IEnumerable <KeyValuePair <string, string> > headers)
        {
            // create a hash for a new filename
            var filename    = Guid.NewGuid() + source.GetExtension();
            var destination = Path.Combine(downloadDirectory, filename);
            var contents    = _downloader.Download(source, headers, null, null);

            File.WriteAllText(destination, contents);

            // Send the file to the dataCacheProvider so it is available when the streamReader asks for it
            dataCacheProvider.Store(destination, System.Text.Encoding.UTF8.GetBytes(contents));

            // now we can just use the local file reader
            _streamReader = new LocalFileSubscriptionStreamReader(dataCacheProvider, destination);
        }