Exemplo n.º 1
0
        public void AddTorrent(string pathToTorrentFile)
        {
            Messenger.Default.Send(new AddTorrentShowDialog((dialogResult) =>
            {
                if (dialogResult == true)
                {
                    ViewModelLocator locator = new ViewModelLocator();
                    var addTorrentViewModel  = locator.AddTorrent;

                    TorrentManager manager = new TorrentManager(addTorrentViewModel.Torrent, addTorrentViewModel.PathToFolder, tSettings);
                    if (!engine.Contains(manager.InfoHash))
                    {
                        saveLoadManager.Add(manager);

                        engine.Register(manager);

                        manager.Start();

                        TorrentManagerWrapper wrapper = new TorrentManagerWrapper(manager);

                        Torrents.Add(wrapper);
                    }
                }
            }, pathToTorrentFile));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the data from the given dht name.
        /// </summary>
        /// <param name="nameSpace">The name space.</param>
        /// <param name="name">The name.</param>
        /// <param name="waitHandle">The wait handle.</param>
        /// <param name="downloadPath">The download path.</param>
        /// <returns>Torrent bytes</returns>
        /// <exception cref="DictionaryKeyNotFoundException">Torrent at the given key is
        /// invalid.</exception>
        /// <remarks>MonoTorrent library provides easy conversion from bytes to
        /// Torrent object but not vise versa so we return bytes.</remarks>
        byte[] GetData(string nameSpace, string name, out string downloadPath, bool peek)
        {
            ManualResetEvent waitHandle = new ManualResetEvent(false);

            byte[] torrentDhtKey = ServiceUtil.GetDictKeyBytes(nameSpace, name);
            downloadPath =
                _bittorrentCache.GetPathOfItemInDownloads(nameSpace, name);
            // @TODO Check integrity of the data -- How do we know the download is complete?
            // A possbile solution. Use the name <name.part> and change it to <name> after
            // download completes.
            byte[] torrentBytes;
            if (!CacheRegistry.IsInCacheRegistry(nameSpace, name))
            {
                try {
                    var torrentSavePath = _bittorrentCache.GetTorrentFilePath(nameSpace, name);
                    torrentBytes = _torrentHelper.ReadOrDownloadTorrent(nameSpace,
                                                                        name, _dictProxy);
                    var torrent = Torrent.Load(torrentBytes);
                    if (!peek)
                    {
                        GetDataInternal(torrentDhtKey, torrent, downloadPath,
                                        waitHandle);

                        // Wait until downloading finishes
                        waitHandle.WaitOne();
                        // Download completed.
                        CacheRegistry.AddPathToRegistry(downloadPath, true);
                    }
                    else
                    {
                        // If we are only peeking, we don't add it to the registry.
                    }
                } catch (TorrentException ex) {
                    throw new DictionaryKeyNotFoundException(string.Format(
                                                                 "Torrent at key {0} (UrlBase64) is invalid.",
                                                                 UrlBase64.Encode(torrentDhtKey)), ex);
                }
            }
            else
            {
                torrentBytes = _torrentHelper.ReadOrDownloadTorrent(nameSpace,
                                                                    name, _dictProxy);
                var torrent = Torrent.Load(torrentBytes);
                if (!_clientEngine.Contains(torrent))
                {
                    // This is the case where the manager start seeding data when it boots up.
                    GetDataInternal(torrentDhtKey, torrent, downloadPath, null);
                }
                else
                {
                    // If the data is already there and the client engine is busily downloading or
                    // seeding, we don't need to do it again.
                    return(torrentBytes);
                }
            }
            return(torrentBytes);
        }
Exemplo n.º 3
0
        public Download addTorrent(Torrent torrent, bool startTorrent, bool removeOriginal, TorrentSettings savedSettings, string savePath, bool isUrl)
        {
            string   originalPath = torrent.TorrentPath;
            Download manager;

            if (!Directory.Exists(savePath))
            {
                throw new TorrentException("Torrent save path does not exist, " + savePath);
            }

            // Check to see if torrent already exists
            if (engine.Contains(torrent))
            {
                logger.Error("Failed to add torrent, " + torrent.Name + " already exists.");
                throw new TorrentException("Failed to add torrent, " + torrent.Name + " already exists.");
            }

            // Move the .torrent to the local storage folder if it's not there already
            MoveToStorage(ref torrent);

            TorrentSettings settings = savedSettings ?? defaultTorrentSettings.Clone();
            FastResume      resume   = this.fastResume.Find(delegate(FastResume f) { return(f.Infohash == torrent.InfoHash); });

            manager = new Download(savePath, new TorrentManager(torrent, savePath, settings));
            if (resume != null)
            {
                manager.Manager.LoadFastResume(resume);
            }

            engine.Register(manager.Manager);

            if (removeOriginal)
            {
                logger.Info("Removing {0}", originalPath);
                File.Delete(originalPath);
            }

            Event.Raise <DownloadAddedEventArgs> (Added, this, new DownloadAddedEventArgs(manager));
            allTorrents.Add(manager);

            if (startTorrent)
            {
                logger.Info("Auto starting torrent " + manager.Torrent.Name);
                manager.Start();
            }

            logger.Info("Added torrent " + manager.Torrent.Name);

            return(manager);
        }
Exemplo n.º 4
0
 public void RecreateManager()
 {
     if (_manager != null)
     {
         _manager.Dispose();
         if (_engine.Contains(_manager))
         {
             _engine.Unregister(_manager);
         }
     }
     _torrentDict = CreateTorrent(_piecelength, _files, _tier);
     _torrent     = Torrent.Load(_torrentDict);
     _manager     = MetadataMode
                    ? new TorrentManager(_torrent.infoHash, _savePath, new TorrentSettings(), MetadataPath, new RawTrackerTiers())
                    : new TorrentManager(_torrent, _savePath, new TorrentSettings());
     _engine.Register(_manager);
 }
Exemplo n.º 5
0
        public TorrentManager addTorrent(string torrentPath, bool startTorrent, bool moveToStorage, bool removeOriginal, TorrentSettings savedSettings, string savePath, bool isUrl)
        {
            Torrent        torrent;
            Torrent        torrentCheck;
            TorrentManager manager;
            string         newPath;

            if (!Directory.Exists(savePath))
            {
                throw new TorrentException("Torrent save path does not exist, " + savePath);
            }

            if (!isUrl && !File.Exists(torrentPath))
            {
                throw new TorrentException("Torrent path does not exist, " + torrentPath);
            }

            // Check to see if Torrent is valid
            if (!isUrl && !Torrent.TryLoad(torrentPath, out torrentCheck))
            {
                logger.Error("Failed to add torrent, " + Path.GetFileName(torrentPath) + " is not valid.");
                throw new TorrentException("Failed to add torrent, " + Path.GetFileName(torrentPath) + " is not valid.");
            }
            else if (isUrl && !Torrent.TryLoad(new System.Uri(torrentPath), Path.Combine(prefSettings.TorrentStorageLocation, Path.GetFileName(torrentPath)), out torrentCheck))
            {
                logger.Error("Failed to add URL, " + torrentPath + " is not valid.");
                throw new TorrentException("Failed to add URL, " + torrentPath + " is not valid.");
            }

            // Check to see if torrent already exists
            if (engine.Contains(torrentCheck))
            {
                logger.Error("Failed to add torrent, " + torrentCheck.Name + " already exists.");
                throw new TorrentException("Failed to add torrent, " + torrentCheck.Name + " already exists.");
            }

            // Move torrent to storage folder
            if (!isUrl && moveToStorage && (prefSettings.TorrentStorageLocation != Directory.GetParent(torrentPath).ToString()))
            {
                newPath = Path.Combine(prefSettings.TorrentStorageLocation, Path.GetFileName(torrentPath));
                logger.Debug("Copying torrent to " + newPath);
                File.Copy(torrentPath, newPath, true);

                if (removeOriginal)
                {
                    logger.Info("Deleting original torrent " + torrentPath);
                    try{
                        File.Delete(torrentPath);
                    } catch {
                        logger.Error("Unable to delete " + Path.GetFileName(torrentPath) + ".");
                        throw new Exception("Unable to delete " + Path.GetFileName(torrentPath) + ".");
                    }
                }
            }
            else
            {
                newPath = torrentPath;
            }



            // Load and register torrent
            if (!isUrl && !Torrent.TryLoad(newPath, out torrent))
            {
                logger.Error("Failed to register " + Path.GetFileName(newPath));
                throw new TorrentException("Failed to register " + Path.GetFileName(newPath));
            }
            else if (isUrl && !Torrent.TryLoad(new System.Uri(newPath), Path.Combine(prefSettings.TorrentStorageLocation, Path.GetFileName(newPath)), out torrent))
            {
                logger.Error("Failed to register " + newPath);
                throw new TorrentException("Failed to register " + newPath);
            }

            if (savedSettings == null)
            {
                manager = new TorrentManager(torrent, savePath, (TorrentSettings)torrentSettings.Clone());
            }
            else
            {
                manager = new TorrentManager(torrent, savePath, savedSettings);
            }

            engine.Register(manager);

            torrents.Add(manager, torrentListStore.AppendValues(manager));
            allTorrents.Add(manager);

            if (startTorrent)
            {
                logger.Info("Auto starting torrent " + manager.Torrent.Name);
                manager.Start();
                // Add to label
                if (manager.State == TorrentState.Downloading)
                {
                    mainWindow.DownloadingLabel.AddTorrent(manager);
                }
                else if (manager.State == TorrentState.Seeding)
                {
                    mainWindow.SeedingLabel.AddTorrent(manager);
                }
            }

            logger.Info("Added torrent " + manager.Torrent.Name);

            manager.TorrentStateChanged         += OnTorrentStateChanged;
            manager.PieceManager.BlockRequested += OnBlockRequested;
            manager.PieceHashed += OnPieceHashed;

            // add to "All" label
            mainWindow.AllLabel.AddTorrent(manager);

            return(manager);
        }