Example #1
0
 /// <summary>
 /// Creates a StreamProvider for the given <see cref="Torrent"/> so that files
 /// contained within the torrent can be accessed as they are downloading.
 /// </summary>
 /// <param name="engine">The engine used to host the download.</param>
 /// <param name="saveDirectory">The directory where the torrents data will be saved</param>
 /// <param name="torrent">The torrent to download</param>
 public StreamProvider(ClientEngine engine, string saveDirectory, Torrent torrent)
 {
     Engine  = engine;
     Manager = new TorrentManager(torrent, saveDirectory);
     Manager.ChangePicker(Picker = new StreamingPiecePicker(new StandardPicker()));
     Files = Manager.Files;
 }
Example #2
0
        void LoadTorrent()
        {
            // Load a .torrent file into memory
            Torrent torrent = Torrent.Load("myfile.torrent");

            // Set all the files to not download
            foreach (TorrentFile file in torrent.Files)
            {
                file.Priority = Priority.DoNotDownload;
            }

            // Set the first file as high priority and the second one as normal
            torrent.Files[0].Priority = Priority.Highest;
            torrent.Files[1].Priority = Priority.Normal;

            TorrentManager manager = new TorrentManager(torrent, "DownloadFolder", new TorrentSettings());

            managers.Add(manager);
            engine.Register(manager);

            // Disable rarest first and randomised picking - only allow priority based picking (i.e. selective downloading)
            PiecePicker picker = new StandardPicker();

            picker = new PriorityPicker(picker);
            manager.ChangePicker(picker);
        }
Example #3
0
        private void TorrentManager_TorrentStateChanged(object sender, TorrentStateChangedEventArgs e)
        {
            if (e.NewState == TorrentState.Error)
            {
                CurrentState = State.Error;
                Error?.Invoke(this, EventArgs.Empty);
                return;
            }

            if (e.OldState != TorrentState.Metadata)
            {
                return;
            }
            CurrentState = State.Preparing;
            OnStreamProgress(0, 0, 0, 0);

            SetSelectedFile(-1);

            SlidingPicker = new SlidingWindowPicker(new PriorityPicker(new StandardPicker()))
            {
                HighPrioritySetStart = _firstPieceIndex,
                HighPrioritySetSize  = _pieceToPrepare
            };
            TorrentManager.ChangePicker(SlidingPicker);
            TorrentManager.PieceManager.BlockReceived -= PieceManagerOnBlockReceived;
            TorrentManager.PieceManager.BlockReceived += PieceManagerOnBlockReceived;

            var blockCount = _pieceToPrepare * TorrentManager.Torrent.PieceLength /
                             (double)Piece.BlockSize;

            _progressStep = 100 / blockCount;
        }
Example #4
0
        public void RegisterTorrent(TorrentManager torrent)
        {
            if (torrent.HasMetadata)
            {
                torrent.ChangePicker(CreateSlidingPicker(torrent));
            }
            else
            {
                torrent.MetadataReceived += TorrentOnMetadataReceived;
            }

            _engine.Register(torrent);
            _torrents.Add(torrent);
        }
Example #5
0
        /// <summary>
        /// Creates a StreamProvider for the given <see cref="MagnetLink"/> so that files
        /// contained within the torrent can be accessed as they are downloading.
        /// </summary>
        /// <param name="engine">The engine used to host the download.</param>
        /// <param name="saveDirectory">The directory where the torrents data will be saved</param>
        /// <param name="magnetLink">The MagnetLink to download</param>
        /// <param name="metadataSaveDirectory">The directory where the metadata will be saved. The
        /// filename will be constucted by appending '.torrent' to the value returned by <see cref="InfoHash.ToHex ()"/>
        /// </param>
        public StreamProvider(ClientEngine engine, string saveDirectory, MagnetLink magnetLink, string metadataSaveDirectory)
        {
            Engine = engine;
            var path = Path.Combine(metadataSaveDirectory, $"{magnetLink.InfoHash.ToHex ()}.torrent");

            Manager = new TorrentManager(magnetLink, saveDirectory, new TorrentSettings(), path);
            Manager.ChangePicker(Picker = new StreamingPiecePicker(new StandardPicker()));

            // If the metadata for this MagnetLink has been downloaded/cached already, we will synchronously
            // load it here and will have access to the list of Files. Otherwise we need to wait.
            if (Manager.HasMetadata)
            {
                Files = Manager.Files;
            }
        }
Example #6
0
        void LoadTorrent()
        {
            // Load a .torrent file into memory
            Torrent torrent = Torrent.Load("myfile.torrent");

            // Set all the files to not download
            foreach (TorrentFile file in torrent.Files)
                file.Priority = Priority.DoNotDownload;

            // Set the first file as high priority and the second one as normal
            torrent.Files[0].Priority = Priority.Highest;
            torrent.Files[1].Priority = Priority.Normal;

            TorrentManager manager = new TorrentManager(torrent, "DownloadFolder", new TorrentSettings(), (FastResume) null);
            managers.Add(manager);
            engine.Register(manager);

            // Disable rarest first and randomised picking - only allow priority based picking (i.e. selective downloading)
            PiecePicker picker = new StandardPicker();
            picker = new PriorityPicker(picker);
            manager.ChangePicker(picker);
        }
Example #7
0
        void RestoreTorrents()
        {
            SaveClass save = null;

            if (File.Exists(DatFile))
            {
                try {
                    save = Utils.DeSerializeObject <SaveClass>(DatFile);
                } catch (System.Xml.XmlException e) {
                    Console.WriteLine(e.StackTrace);
                    File.Move(DatFile, Path.Combine(ConfigFolder, "dat1.itor"));
                    var controller = UIAlertController.Create("Config file loading error", "There was a problem loading the configuration file, a copy will be created under the \"dat1\" name, and a new one will be created", UIAlertControllerStyle.Alert);

                    var topWindow = new UIWindow(UIScreen.MainScreen.Bounds);
                    topWindow.RootViewController = new UIViewController();
                    topWindow.WindowLevel        = UIWindowLevel.Alert + 1;

                    var ok = UIAlertAction.Create("OK", UIAlertActionStyle.Cancel, delegate {
                        topWindow.Hidden = true;
                        topWindow        = null;
                    });
                    controller.AddAction(ok);

                    topWindow.MakeKeyAndVisible();
                    topWindow.RootViewController.PresentViewController(controller, true, null);
                }
            }
            if (File.Exists(Path.Combine(ConfigFolder, "_temp.torrent")))
            {
                File.Delete(Path.Combine(ConfigFolder, "_temp.torrent"));
            }

            if (Directory.Exists(ConfigFolder))
            {
                foreach (var file in Directory.GetFiles(ConfigFolder))
                {
                    new Thread(() => {
                        if (file.EndsWith(".torrent", StringComparison.Ordinal))
                        {
                            Torrent torrent        = Torrent.Load(file);
                            TorrentManager manager = new TorrentManager(torrent, RootFolder, new TorrentSettings());

                            engine.Register(manager);
                            manager.TorrentStateChanged += (sender, e) => {
                                Manager.OnFinishLoading(manager, e);
                            };

                            if (save != null && save.data.ContainsKey(torrent.InfoHash.ToHex()))
                            {
                                if (save.data[torrent.InfoHash.ToHex()].resume != null)
                                {
                                    manager.LoadFastResume(new FastResume(BEncodedValue.Decode(save.data[torrent.InfoHash.ToHex()].resume) as BEncodedDictionary));
                                    manager.dateOfAdded  = save.data[torrent.InfoHash.ToHex()].date;
                                    manager.allowSeeding = save.data[torrent.InfoHash.ToHex()].allowSeeding;
                                    switch (save.data[torrent.InfoHash.ToHex()].state)
                                    {
                                    case TorrentState.Downloading:
                                        manager.Start();
                                        break;

                                    default:
                                        manager.Stop();
                                        break;
                                    }
                                }
                                foreach (var _file in torrent.Files)
                                {
                                    if (save.data[torrent.InfoHash.ToHex()].downloading.ContainsKey(_file.Path))
                                    {
                                        _file.Priority = save.data[torrent.InfoHash.ToHex()].downloading[_file.Path] ? Priority.Highest : Priority.DoNotDownload;
                                    }
                                }
                            }
                            else
                            {
                                foreach (var _file in torrent.Files)
                                {
                                    _file.Priority = Priority.DoNotDownload;
                                }
                                manager.HashCheck(true);
                            }

                            PiecePicker picker = new StandardPicker();
                            picker             = new PriorityPicker(picker);
                            manager.ChangePicker(picker);

                            foreach (TrackerTier tier in manager.TrackerManager)
                            {
                                foreach (Tracker t in tier.Trackers)
                                {
                                    t.AnnounceComplete += delegate(object sender, AnnounceResponseEventArgs e) {
                                        Console.WriteLine(string.Format("{0}!: {1}", e.Successful, e.Tracker));
                                    };
                                }
                            }

                            managers.Add(manager);

                            UIApplication.SharedApplication.InvokeOnMainThread(() => {
                                restoreAction?.Invoke();
                            });
                        }
                    }).Start();
                }
            }
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            files = torrent.Files.Clone() as TorrentFile[];
            Array.Sort(files, delegate(TorrentFile f1, TorrentFile f2) {
                return(string.Compare(f1.Path, f2.Path, StringComparison.Ordinal));
            });
            foreach (var file in files)
            {
                Console.WriteLine(file.Path);
            }

            tableView.DataSource = this;
            tableView.Delegate   = this;

            tableView.RowHeight = 78;

            Cancel.Clicked += delegate {
                if (torrent.TorrentPath.EndsWith("/_temp.torrent"))
                {
                    File.Delete(torrent.TorrentPath);
                }

                DismissViewController(true, null);
            };

            DeselectAllAction.Clicked += delegate {
                foreach (var file in files)
                {
                    file.Priority = Priority.DoNotDownload;
                }
                foreach (var cell in tableView.VisibleCells)
                {
                    ((FileCell)cell).Update();
                }
            };

            SelectAllAction.Clicked += delegate {
                foreach (var file in files)
                {
                    file.Priority = Priority.Highest;
                }
                foreach (var cell in tableView.VisibleCells)
                {
                    ((FileCell)cell).Update();
                }
            };

            Download.Clicked += delegate {
                TorrentManager manager = new TorrentManager(torrent, Manager.RootFolder, new TorrentSettings());
                Manager.Singletone.managers.Add(manager);
                Manager.Singletone.RegisterManager(manager);
                if (MainController.Instance != null)
                {
                    MainController.Instance.TableView.ReloadData();
                }

                // Disable rarest first and randomised picking - only allow priority based picking (i.e. selective downloading)
                PiecePicker picker = new StandardPicker();
                picker = new PriorityPicker(picker);
                manager.ChangePicker(picker);
                manager.TorrentStateChanged += delegate {
                    Manager.OnFinishLoading(manager);
                };

                foreach (var file in files)
                {
                    if (file.Priority != Priority.DoNotDownload)
                    {
                        manager.Start();
                        break;
                    }
                }

                if (!Directory.Exists(Manager.ConfigFolder))
                {
                    Directory.CreateDirectory(Manager.ConfigFolder);
                }
                if (File.Exists(Path.Combine(Manager.ConfigFolder, torrent.Name + ".torrent")))
                {
                    File.Delete(Path.Combine(Manager.ConfigFolder, torrent.Name + ".torrent"));
                }
                File.Copy(torrent.TorrentPath, Path.Combine(Manager.ConfigFolder, torrent.Name + ".torrent"));
                if (torrent.TorrentPath.EndsWith("/_temp.torrent"))
                {
                    File.Delete(torrent.TorrentPath);
                }

                foreach (var file in files)
                {
                    Console.WriteLine(file.Path + " " + file.Priority);
                }
                //SaveClass save = new SaveClass(manager);
                //Utils.SerializeObject<SaveClass>(save, AppDelegate.documents + "/Config/" + manager.Torrent.Name + ".sav");

                DismissViewController(true, null);
            };
        }
Example #9
0
        void RestoreTorrents()
        {
            SaveClass save = null;

            if (File.Exists(DatFile))
            {
                save = Utils.DeSerializeObject <SaveClass>(DatFile);
            }

            if (Directory.Exists(ConfigFolder))
            {
                foreach (var file in Directory.GetFiles(ConfigFolder))
                {
                    new Thread(() => {
                        if (file.EndsWith(".torrent", StringComparison.Ordinal))
                        {
                            Torrent torrent        = Torrent.Load(file);
                            TorrentManager manager = new TorrentManager(torrent, RootFolder, new TorrentSettings());

                            engine.Register(manager);

                            if (save != null && save.data.ContainsKey(torrent.InfoHash.ToHex()))
                            {
                                if (save.data[torrent.InfoHash.ToHex()].resume != null)
                                {
                                    manager.LoadFastResume(new FastResume(BEncodedValue.Decode(save.data[torrent.InfoHash.ToHex()].resume) as BEncodedDictionary));
                                    switch (save.data[torrent.InfoHash.ToHex()].state)
                                    {
                                    case TorrentState.Downloading:
                                        manager.Start();
                                        break;

                                    case TorrentState.Paused:
                                        manager.Pause();
                                        break;

                                    case TorrentState.Stopped:
                                        manager.Stop();
                                        break;
                                    }
                                }
                                foreach (var _file in torrent.Files)
                                {
                                    if (save.data[torrent.InfoHash.ToHex()].downloading.ContainsKey(_file.Path))
                                    {
                                        _file.Priority = save.data[torrent.InfoHash.ToHex()].downloading[_file.Path] ? Priority.Highest : Priority.DoNotDownload;
                                    }
                                }
                            }

                            PiecePicker picker = new StandardPicker();
                            picker             = new PriorityPicker(picker);
                            manager.ChangePicker(picker);
                            manager.TorrentStateChanged += delegate {
                                Manager.OnFinishLoading(manager);
                            };

                            foreach (TrackerTier tier in manager.TrackerManager)
                            {
                                foreach (Tracker t in tier.Trackers)
                                {
                                    t.AnnounceComplete += delegate(object sender, AnnounceResponseEventArgs e) {
                                        Console.WriteLine(string.Format("{0}!: {1}", e.Successful, e.Tracker));
                                    };
                                }
                            }

                            managers.Add(manager);

                            UIApplication.SharedApplication.InvokeOnMainThread(() => {
                                restoreAction?.Invoke();
                            });
                        }
                    }).Start();
                }
            }
        }
Example #10
0
    void LoadTorrent(string path)
    {
        // Load a .torrent file into memory
        Torrent torrent = Torrent.Load (path);
        // Set all the files to not download
        //foreach (TorrentFile file in torrent.Files)
        //	file.Priority = Priority.DoNotDownload;

        // Set the first file as high priority and the second one as normal
        //torrent.Files[0].Priority = Priority.Highest;
        TorrentSettings ts =  new TorrentSettings ();
        ts.EnablePeerExchange = true;
        //ts.InitialSeedingEnabled = true;
        ts.UseDht = true;
        TorrentManager manager = new TorrentManager (torrent, dp, ts);
        manager.PeersFound += HandleManagerPeersFound;

        PiecePicker picker = new StandardPicker ();
        picker = new PriorityPicker (picker);
        manager.ChangePicker (picker);

        managers.Add (manager);

        engine.Register(manager);

        // Disable rarest first and randomised picking - only allow priority based picking (i.e. selective downloading)

        nodeview2.NodeStore.AddNode (new TorrentInfoRow (manager.Torrent.Name, manager.State.ToString (), manager.Torrent.Name.Substring (0, 10), "0", "0", "0", manager, managers.Count -1));
        nodeview2.Columns[0].MinWidth=100;
        nodeview2.Columns[1].MinWidth = 80;
        nodeview2.Columns[2].MinWidth = 65;
        nodeview2.Columns[3].MinWidth = 65;
        nodeview2.Columns[4].MinWidth = 50;
        nodeview2.NodeSelection.Changed += new System.EventHandler (OnSelectionChanged);
    }