Exemplo n.º 1
0
        public void PullTest()
        {
            ReadResponseMessage readResponse = new ReadResponseMessage();

            readResponse.IsPasswordCorrect    = true;
            readResponse.CurrentServerTimeUtc = new DateTime(2012, 1, 23);
            readResponse.Data = new byte[] { 0xaa, 0xbb };

            MockFolderUnpacker folderUnpacker = new MockFolderUnpacker();

            using (Stream responseStream = ServerResponseToStream(readResponse))
            {
                FilePuller filePuller = new FilePuller(folderUnpacker, "the_r1ght_passw0rd");

                const string destinationFolderExpected = @"c:\pulledfiles\";

                filePuller.Pull(responseStream, destinationFolderExpected);

                Assert.AreEqual(destinationFolderExpected, folderUnpacker.LastDestinationFolder);

                Assert.AreEqual(readResponse.CurrentServerTimeUtc, filePuller.LastPullTime);

                Assert.IsTrue(Util.ArraysEqual <byte>(new byte[] { 0xaa, 0xbb }, folderUnpacker.LastPackedData));
            }
        }
Exemplo n.º 2
0
        public void Enque_some_files_and_download()
        {
            var queue = new PersistentQueue(Path.Combine(_fileStorePath, "_FileQueueTest"));

            IWorker fileServer = new FileServer(_fileServer_Port, _fileStorePath, queue);

            fileServer.Start();

            using (var session = queue.OpenSession())
            {
                //clean queue
                while (session.Dequeue() != null)
                {
                    ;
                }

                //20 images and 1 mp4
                foreach (var file in Directory.GetFiles(@"D:\Files\Source\", "*", SearchOption.AllDirectories))
                {
                    session.Enqueue(Encoding.UTF8.GetBytes(file.Replace(@"D:\Files\Source\", @"http://localhost:9001/file/Source/")));
                }

                session.Flush();
            }

            IWorker filePuller = new FilePuller(_fileStorePath,
                                                _filePuller_Interval, _filePuller_DownloadTimeout, _filePuller_RetryTimes, queue);

            filePuller.Start();


            Thread.Sleep(100000);
        }
Exemplo n.º 3
0
        public void Enque_remote_files_and_download()
        {
            var queue = new PersistentQueue(Path.Combine(_fileStorePath, "_FileQueueTest"));

            using (var session = queue.OpenSession())
            {
                //clean queue
                while (session.Dequeue() != null)
                {
                    ;
                }
                for (int i = 0; i < 2; i++)
                {
                    session.Enqueue(Encoding.UTF8.GetBytes(@"http://jx.taedu.gov.cn:83//Resource.Portal.Web/SubjectResource/DownloadById/030183bb-409f-43be-b3dd-4dd683e7dffc"));

                    session.Enqueue(Encoding.UTF8.GetBytes(@"http://jx.taedu.gov.cn:83/Storages/温秀云/2016/8/1/image/师说2_104707/jpg/师说2.jpg"));

                    session.Enqueue(Encoding.UTF8.GetBytes(@"http://jx.taedu.gov.cn:83/Storages/张华/2016/9/12/image/劝学01_110337/jpg/劝学01.jpg"));

                    session.Enqueue(Encoding.UTF8.GetBytes(@"http://jx.taedu.gov.cn:83/Storages/晁阳/2016/9/8/flash/正多边形内角和_181554/swf/正多边形内角和.swf"));

                    session.Enqueue(Encoding.UTF8.GetBytes(@"http://jx.taedu.gov.cn:83/Storages/周小广/2016/9/13/pdf/一年级英语上册-Unit1-School(2)课件-人教新起点_155027/pdf/一年级英语上册-Unit1-School(2)课件-人教新起点.pdf"));
                }

                session.Flush();
            }

            IWorker filePuller = new FilePuller(_fileStorePath,
                                                _filePuller_Interval, _filePuller_DownloadTimeout, _filePuller_RetryTimes, queue);

            filePuller.Start();

            Thread.Sleep(100000);
        }
Exemplo n.º 4
0
        public void TestFilePuller()
        {
            // misc test
            //            Assert.True(typeof (object).IsInstanceOfType(new string('a', 1)));

            // puller
            var puller = new FilePuller("https://www.baidu.com/favicon.ico", "puller_download/favicon.ico",
                                        ".puller_metas");

            puller.Start();
            while (!puller.IsFinished)
            {
                Thread.Sleep(1);
            }
            Assert.AreEqual(null, puller.Error);
            Console.WriteLine("Has Downloaded? : {0}", puller.HasDownloaded);
            Assert.True(File.Exists("puller_download/favicon.ico"));
            Assert.True(Directory.Exists(".puller_metas"));
            Assert.True(Directory.GetFiles(".puller_metas").Length > 0);
        }
Exemplo n.º 5
0
        public void InvalidPasswordPullTest()
        {
            ReadResponseMessage serverResponseMessage = new ReadResponseMessage();

            serverResponseMessage.IsPasswordCorrect = false;

            using (Stream responseStream = ServerResponseToStream(serverResponseMessage))
            {
                MockFolderUnpacker folderUnpacker = new MockFolderUnpacker();

                FilePuller filePuller = new FilePuller(folderUnpacker, "the_wr0ng_passw0rd");

                filePuller.Pull(responseStream, @"c:\fakedestination");

                //
                // Verify that client gracefully handles rejected passwords and does not try to download more data
                //

                Assert.IsNull(folderUnpacker.LastDestinationFolder);
                Assert.IsNull(folderUnpacker.LastPackedData);
            }
        }
Exemplo n.º 6
0
        public void Start_file_server_for_10_minute()
        {
            var queue = new PersistentQueue(Path.Combine(_fileStorePath, "_FileQueueTest"));

            using (var session = queue.OpenSession())
            {
                while (session.Dequeue() != null)
                {
                    ;
                }
                session.Flush();
            }

            IWorker fileServer = new FileServer(_fileServer_Port, _fileStorePath, queue);

            fileServer.Start();

            IWorker filePuller = new FilePuller(_fileStorePath,
                                                _filePuller_Interval, _filePuller_DownloadTimeout, _filePuller_RetryTimes, queue);

            filePuller.Start();

            Thread.Sleep(600000);
        }
Exemplo n.º 7
0
        private static void Main(string[] args)
        {
            List <IWorker>  wokers          = new List <IWorker>();
            PersistentQueue queue           = null;
            var             exitForUpdating = false;

            try
            {
                Info($"CDN Starting... Version : {ApplicationHelper.Version}");
                ApplicationHelper.CheckSingleRunning(mutex);
                ApplicationHelper.InitDeployQueryString("SyncApiParam");

                #region Get Configs From url or app.config

                string _fileStorePath = GetConfigFromDeployThenAppConfig <string>("FileStorePath");

                Boolean _fileServer_Enabled = GetConfigFromDeployThenAppConfig <Boolean>("FileServer_Enabled");

                int _fileServer_Port = GetConfigFromDeployThenAppConfig <int>("FileServer_Port");

                Boolean _fileEnqueuer_Enabled = GetConfigFromDeployThenAppConfig <Boolean>("FileEnqueuer_Enabled");

                Int32 _fileEnqueuer_Interval = GetConfigFromDeployThenAppConfig <Int32>("FileEnqueuer_Interval");

                string _fileEnqueuer_SyncApi = GetConfigFromDeployThenAppConfig <string>("FileEnqueuer_SyncApi");

                Boolean _filePuller_Enabled = GetConfigFromDeployThenAppConfig <Boolean>("FilePuller_Enabled");

                Int32 _filePuller_DownloadTimeout = GetConfigFromDeployThenAppConfig <Int32>("FilePuller_DownloadTimeout");

                Int32 _filePuller_Interval = GetConfigFromDeployThenAppConfig <Int32>("FilePuller_Interval");

                Int32 _filePuller_RetryTimes = GetConfigFromDeployThenAppConfig <Int32>("FilePuller_RetryTimes");

                Int32 _filePuller_DownloadThreadCount =
                    GetConfigFromDeployThenAppConfig <Int32>("FilePuller_DownloadThreadCount");

                Int32 _updateInterval = GetConfigFromDeployThenAppConfig <Int32>("UpdateInterval");

                #endregion Get Configs From url or app.config

                queue = new PersistentQueue(Path.Combine(_fileStorePath, "_FileQueue"));

                if (_fileEnqueuer_Enabled)
                {
                    IWorker fileEnqueuer = new FileEnqueuer(_fileStorePath,
                                                            _fileEnqueuer_SyncApi, _fileEnqueuer_Interval, queue);
                    fileEnqueuer.Start();
                    wokers.Add(fileEnqueuer);
                    Info("FileEnqueuer Started");
                }

                if (_filePuller_Enabled)
                {
                    for (int i = 0; i < _filePuller_DownloadThreadCount; i++)
                    {
                        IWorker filePuller = new FilePuller(_fileStorePath,
                                                            _filePuller_Interval, _filePuller_DownloadTimeout,
                                                            _filePuller_RetryTimes, queue);
                        filePuller.Start();
                        wokers.Add(filePuller);
                        Info($"FilePuller {i} Started");
                    }
                }

                if (_fileServer_Enabled)
                {
                    IWorker fileServer = new FileServer(_fileServer_Port, _fileStorePath, queue);
                    fileServer.Start();
                    wokers.Add(fileServer);

                    Info($"FileServer Started at port {_fileServer_Port}");
                }

                IWorker updateWorker = new UpdateCheckingWorker(_updateInterval);
                updateWorker.Start();
                updateWorker.Wait();
                Info("Restart for updating...");
                exitForUpdating = true;
            }
            catch (Exception ex)
            {
                Info(ex.Message);
                Console.ReadLine();
            }
            finally
            {
                //Clean
                Parallel.ForEach(wokers, w => w.Stop());
                queue?.Dispose();
                mutex.Close();

                if (exitForUpdating)
                {
                    Application.Restart();
                }
            }
        }