Example #1
0
        static void Main(string[] args)
        {
            if (args == null || args.Length < 1)
            {
                Console.WriteLine("Specify the file to hash");
                return;
            }

            var hasher = new ThexThreaded <Tiger>();

            var sw   = Stopwatch.StartNew();
            var hash = Base32Encoding.ToString(hasher.GetTTHRoot(args[0]));

            sw.Stop();

            FileInfo fi = new FileInfo(args[0]);

            Console.WriteLine("{0} {2}ms {1}/s ", hash, Utils.FormatBytes(fi.Length / sw.Elapsed.TotalSeconds), sw.ElapsedMilliseconds);
            if (Debugger.IsAttached)
            {
                Console.ReadLine();
            }
        }
        private void DownloadCacheFiles()
        {
            lock (_downloadLock)
            {
                while (_downloadQueue.Count > 0)
                {
                    Tuple <string, CachedItem> tuple;
                    lock (_downloadQueue)
                    {
                        tuple = _downloadQueue.Dequeue();
                    }

                    var httpUri = tuple.Item1;
                    var item    = tuple.Item2;

                    try
                    {
                        var point = _points.First(p => item.CachePath.StartsWith(p.SystemPath));

                        var driveInfo = new DriveInfo(Path.GetPathRoot(point.SystemPath));

                        Logger.Info("Downloading file to the cache {0} Estimated free space {1} Real free space {2} {3}", httpUri, point.FreeSpace, driveInfo.AvailableFreeSpace, point.SystemPath);

                        if (File.Exists(item.CachePath))
                        {
                            File.Delete(item.CachePath);
                        }

                        var timer = PerfTimer.StartNew();

                        if (httpUri.StartsWith("http"))
                        {
                            HttpHelper.DownloadFile(httpUri, item.CachePath, true);
                        }
                        else if (httpUri.StartsWith("hyp"))
                        {
                            HyperUploadItem.Manager.DownloadFile(httpUri, item.CachePath, true).Wait();
                        }

                        timer.Stop();
                        Logger.Info("Download finished at {0}/s", Utils.FormatBytes(item.Magnet.Size / timer.Elapsed.TotalSeconds));

                        if (CacheVerification)
                        {
                            Logger.Info("Verifying the data");
                            var hasher = new ThexThreaded <TigerNative>();
                            hasher.LowPriority            = true;
                            hasher.FileStreamBufferLength = 1024 * 1024;
                            var tth = Base32Encoding.ToString(hasher.GetTTHRoot(item.CachePath));

                            if (tth == item.Magnet.TTH)
                            {
                                Logger.Info("File cache match {0}", tth);
                                item.Complete = true;
                                item.CachedSegments.SetAll(true);
                                WriteBitfieldFile(item);
                            }
                            else
                            {
                                Logger.Info("Error! File cache mismatch {0}, expected {1} repeating the download", tth,
                                            item.Magnet.TTH);
                                File.Delete(item.CachePath);
                                lock (_downloadQueue)
                                {
                                    _downloadQueue.Enqueue(tuple);
                                }
                                Thread.Sleep(TimeSpan.FromSeconds(1));
                            }
                        }
                        else
                        {
                            Logger.Info("Adding file without verification");
                            item.Complete = true;
                            item.CachedSegments.SetAll(true);
                            WriteBitfieldFile(item);
                        }
                    }
                    catch (Exception ex)
                    {
                        while (ex != null)
                        {
                            Logger.Error("Error occured when processing download of cache item {0} {1}", tuple.Item1,
                                         ex.Message);
                            ex = ex.InnerException;
                        }

                        RemoveItemFromCache(item);

                        Thread.Sleep(TimeSpan.FromSeconds(1));
                    }
                }

                lock (_downloadQueue)
                {
                    _downloadThreadAlive = false;
                }
            }
        }