Exemple #1
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);
        }
Exemple #2
0
        /// <summary>
        /// Serves the file over BitTorrent.
        /// </summary>
        /// <param name="dhtKey">The DHT name to be used to put the torrent info</param>
        /// <param name="nameSpace">The name space.</param>
        /// <param name="path">The file or directory path.</param>
        /// <param name="unique">if set to <c>true</c>, the manager uses Create
        /// instead of Put to insert torrent to Dht.</param>
        void PublishDataInternal(string nameSpace, string name, bool unique)
        {
            byte[] dhtKey          = ServiceUtil.GetDictKeyBytes(nameSpace, name);
            var    dataPath        = _bittorrentCache.GetPathOfItemInDownloads(nameSpace, name);
            var    torrentSavePath = _bittorrentCache.GetTorrentFilePath(nameSpace, name);

            // Create torrent
            BEncodedDictionary bdict   = _torrentHelper.CreateTorrent(dataPath);
            Torrent            torrent = Torrent.Load(bdict);

            // Dump torrent to the torrent folder so that tracker could load it.
            byte[] torrentBytes = bdict.Encode();
            TorrentHelper.WriteTorrent(torrentBytes, torrentSavePath);

            string torrentUrl = _torrentHelper.GetTorrentFileUrlToPublish(nameSpace, name);
            // Put the Url bytes to the dictionary.
            var torrentUrlBytes = Encoding.UTF8.GetBytes(torrentUrl);

            if (unique)
            {
                _dictProxy.CreateTorrent(dhtKey, torrentUrlBytes, TorrentTtl);
                CacheRegistry.AddPathToRegistry(dataPath, true);
            }
            else
            {
                _dictProxy.PutTorrent(dhtKey, torrentUrlBytes, TorrentTtl);
                CacheRegistry.UpdatePathInRegistry(dataPath, true);
            }

            Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                               string.Format("{0}: Succesfully registered torrent url to dictionary.",
                                             torrent.Name));

            var saveDir = IOUtil.GetParent(dataPath, true).FullName;

            // Download without blocking.
            StartDownload(torrent, saveDir, null);
        }