public DownloadWorker(ref PWGlacierAPI api, string jobId, long start, long end, ref BufferBucket bucket, ref TransferMetric metric)
 {
     this.api    = api;
     this.jobId  = jobId;
     this.start  = start;
     this.end    = end;
     this.bucket = bucket;
     this.metric = metric;
 }
Beispiel #2
0
 public DownloadWorker(ref PWGlacierAPI api, string jobId, long start, long end, ref BufferBucket bucket, ref TransferMetric metric)
 {
     this.api = api;
     this.jobId = jobId;
     this.start = start;
     this.end = end;
     this.bucket = bucket;
     this.metric = metric;
 }
        public bool Download()
        {
            string jobId = this.jobId != null ? this.jobId : glacierAPI.InitiateDownloadRequest(archiveId);

            while (!glacierAPI.JobCompleted(jobId))
            {
                Console.WriteLine("Waiting for AWS Glacier Archive retrieval...");
                if (this.jobId != null)
                {
                    Console.WriteLine("Write JobId to file & quit?");
                    ConsoleKeyInfo key = Console.ReadKey();
                    if (key.KeyChar == 'y' || key.KeyChar == 'Y')
                    {
                        System.IO.File.WriteAllText("jobId.out", jobId);
                        return(false);
                    }
                }
                Thread.Sleep(60 * 1000);
            }

            GlacierArchiveInfo info = glacierAPI.ArchiveInfo(jobId);

            long currentStart = 0;
            long currentEnd   = 0;

            // Start the writer thread
            Thread writer = new Thread(new ThreadStart(WriteToStream));

            writer.Start();

            // Start the metrics reporting thread
            Timer timer = new Timer(DisplayMetrics, null, 1000, 1000);

            while (currentEnd < info.SizeInBytes)
            {
                currentEnd = currentStart + partSize - 1;

                WaitForNextAvailableThread();

                BufferBucket bucket = new BufferBucket();
                lock (outputQueue)
                {
                    outputQueue.Enqueue(bucket);
                }
                DownloadWorker worker = new DownloadWorker(ref glacierAPI, jobId, currentStart, currentEnd, ref bucket, ref transferMetrics);
                Thread         t      = new Thread(new ThreadStart(worker.Download));
                t.Start();
                threads.Add(t);

                currentStart = currentEnd + 1;
            }

            // Wait for all download thread workers to finish
            while (threads.Count > 0)
            {
                for (int i = 0; i < threads.Count; ++i)
                {
                    if (!threads[i].IsAlive)
                    {
                        threads.RemoveAt(i);
                    }
                }
                Thread.Sleep(1000);
            }

            // Wait for the output writer thread worker to finish, then kill it.
            while (outputQueue.Count > 0)
            {
                Thread.Sleep(500);
            }

            writer.Abort();

            outputStream.Close();

            string checksum = Amazon.Glacier.TreeHashGenerator.CalculateTreeHash(treeHashes);

            if (checksum == info.Checksum)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public bool Download()
        {
            string jobId = this.jobId != null ? this.jobId : glacierAPI.InitiateDownloadRequest(archiveId);

            while (!glacierAPI.JobCompleted(jobId))
            {
                Console.WriteLine("Waiting for AWS Glacier Archive retrieval...");
                if (this.jobId != null)
                {
                    Console.WriteLine("Write JobId to file & quit?");
                    ConsoleKeyInfo key = Console.ReadKey();
                    if (key.KeyChar == 'y' || key.KeyChar== 'Y')
                    {
                        System.IO.File.WriteAllText("jobId.out", jobId);
                        return false;
                    }
                }
                Thread.Sleep(60 * 1000);
            }

            GlacierArchiveInfo info = glacierAPI.ArchiveInfo(jobId);

            long currentStart = 0 ;
            long currentEnd = 0;

            // Start the writer thread
            Thread writer = new Thread(new ThreadStart(WriteToStream));
            writer.Start();

            // Start the metrics reporting thread
            Timer timer = new Timer(DisplayMetrics, null, 1000, 1000);

            while (currentEnd < info.SizeInBytes)
            {
                currentEnd = currentStart + partSize - 1;

                WaitForNextAvailableThread();

                BufferBucket bucket = new BufferBucket();
                lock (outputQueue)
                {
                    outputQueue.Enqueue(bucket);
                }
                DownloadWorker worker = new DownloadWorker(ref glacierAPI, jobId, currentStart, currentEnd, ref bucket, ref transferMetrics);
                Thread t = new Thread(new ThreadStart(worker.Download));
                t.Start();
                threads.Add(t);

                currentStart = currentEnd + 1;
            }

            // Wait for all download thread workers to finish
            while (threads.Count > 0)
            {
                for (int i = 0; i < threads.Count; ++i)
                {
                    if (!threads[i].IsAlive)
                        threads.RemoveAt(i);
                }
                Thread.Sleep(1000);
            }

            // Wait for the output writer thread worker to finish, then kill it.
            while (outputQueue.Count > 0)
                Thread.Sleep(500);

            writer.Abort();

            outputStream.Close();

            string checksum = Amazon.Glacier.TreeHashGenerator.CalculateTreeHash(treeHashes);

            if (checksum == info.Checksum)
                return true;
            else
                return false;
        }