Esempio n. 1
0
        public BitTorrentManager(BitTorrentCache bittorrentCache, string selfNameSpace,
            DictionaryServiceProxy dhtProxy, DictionaryServiceTracker dhtTracker, ClientEngine clientEngine,
            TorrentSettings torrentDefaults, TorrentHelper torrentHelper,
            bool startSeedingAtStartup)
        {
            _bittorrentCache = bittorrentCache;
              SelfNameSpace = selfNameSpace;
              _dictProxy = dhtProxy;
              _dictTracker = dhtTracker;
              _torrentDefaults = torrentDefaults;
              _startSeedingAtStartup = startSeedingAtStartup;

              RegisterClientEngineEventHandlers(clientEngine);
              _clientEngine = clientEngine;

              _torrentHelper = torrentHelper;

              try {
            _fastResumeData = BEncodedValue.Decode<BEncodedDictionary>(
              File.ReadAllBytes(_bittorrentCache.FastResumeFilePath));
              } catch {
            _fastResumeData = new BEncodedDictionary();
              }

              // CacheRegistry is created here because the default cache registry file path is
              // defined here.
              CacheRegistry = new CacheRegistry(_bittorrentCache.CacheRegistryFilePath, selfNameSpace);
              CacheRegistry.LoadCacheDir(_bittorrentCache.DownloadsDirPath);
        }
Esempio n. 2
0
        public BitTorrentManager(BitTorrentCache bittorrentCache, string selfNameSpace,
                                 DictionaryServiceProxy dhtProxy, DictionaryServiceTracker dhtTracker, ClientEngine clientEngine,
                                 TorrentSettings torrentDefaults, TorrentHelper torrentHelper,
                                 bool startSeedingAtStartup)
        {
            _bittorrentCache       = bittorrentCache;
            SelfNameSpace          = selfNameSpace;
            _dictProxy             = dhtProxy;
            _dictTracker           = dhtTracker;
            _torrentDefaults       = torrentDefaults;
            _startSeedingAtStartup = startSeedingAtStartup;

            RegisterClientEngineEventHandlers(clientEngine);
            _clientEngine = clientEngine;

            _torrentHelper = torrentHelper;

            try {
                _fastResumeData = BEncodedValue.Decode <BEncodedDictionary>(
                    File.ReadAllBytes(_bittorrentCache.FastResumeFilePath));
            } catch {
                _fastResumeData = new BEncodedDictionary();
            }

            // CacheRegistry is created here because the default cache registry file path is
            // defined here.
            CacheRegistry = new CacheRegistry(_bittorrentCache.CacheRegistryFilePath, selfNameSpace);
            CacheRegistry.LoadCacheDir(_bittorrentCache.DownloadsDirPath);
        }
 public PieceLevelTorrentManager(BitTorrentManager manager, BitTorrentCache bittorrentCache,
     DictionaryServiceProxy dhtProxy, TorrentHelper torrentHelper, int pieceInfoServerPort)
 {
     _manager = manager;
       _dhtProxy = dhtProxy;
       _bittorrentCache = bittorrentCache;
       _torrentHelper = torrentHelper;
       _pieceInfoServerPort = pieceInfoServerPort;
       _pieceInfoServer = new HttpPieceInfoServer(_pieceInfoServerPort, this);
 }
 public PieceLevelTorrentManager(BitTorrentManager manager, BitTorrentCache bittorrentCache,
                                 DictionaryServiceProxy dhtProxy, TorrentHelper torrentHelper, int pieceInfoServerPort)
 {
     _manager             = manager;
     _dhtProxy            = dhtProxy;
     _bittorrentCache     = bittorrentCache;
     _torrentHelper       = torrentHelper;
     _pieceInfoServerPort = pieceInfoServerPort;
     _pieceInfoServer     = new HttpPieceInfoServer(_pieceInfoServerPort, this);
 }
Esempio n. 5
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);
        }
Esempio n. 6
0
        public static void ConfigureUnityContainer(IUnityContainer container)
        {
            #region Common
              var dhtTrackerListenerPort =
            Int32.Parse(ConfigurationManager.AppSettings["DhtTrackerListeningPort"]);
              var infoServerListeningPort = Int32.Parse(ConfigurationManager.AppSettings[
            "HttpPieceInfoServerListeningPort"]); // listeningPort
              #endregion

              #region TorrentSettings
              // Create the default settings which a torrent will have.
              // 4 Upload slots - a good ratio is one slot per 5kB of upload speed
              // 50 open connections - should never really need to be changed
              // Unlimited download speed - valid range from 0 -> int.Max
              // Unlimited upload speed - valid range from 0 -> int.Max
              TorrentSettings torrentDefaults = new TorrentSettings(4, 150, 0, 0);
              container.RegisterInstance<TorrentSettings>(torrentDefaults);
              #endregion

              #region ClientEngine
              // DefaultListenPort = 52138;
              var engineSettings = new EngineSettings();
              engineSettings.PreferEncryption = false;
              engineSettings.AllowedEncryption = EncryptionTypes.All;
              var clientEngine = new ClientEngine(engineSettings);
              container.RegisterInstance<ClientEngine>(clientEngine);
              #endregion

              #region DhtProxy
              var peerTtlSecs = 60 * 50;
              container.RegisterType<DictionaryServiceProxy>(
            new InjectionConstructor(typeof(DictionaryServiceBase),
              peerTtlSecs));
              #endregion

              #region DhtTracker
              // Singleton.
              container.RegisterType<DictionaryServiceTracker>(new ContainerControlledLifetimeManager(),
            new InjectionConstructor(
              typeof(DictionaryServiceProxy),
              string.Format("http://*:{0}/", dhtTrackerListenerPort))); // listeningPrefix
              #endregion

              #region TorrentHelper
              // Singleton.
              var cacheBaseDirPath =
            ConfigurationManager.AppSettings["BitTorrentManagerBaseDirPath"];
              IPAddress ip = NetUtil.GetLocalIPByInterface(
            ConfigurationManager.AppSettings["DhtTrackerIface"]);
              int gsserverPort = Int32.Parse(ConfigurationManager.AppSettings["GSServerPort"]);
              var bittorrentCache = new BitTorrentCache(cacheBaseDirPath);
              container.RegisterInstance<BitTorrentCache>(bittorrentCache);
              var torrentHelper = new TorrentHelper(
            bittorrentCache, ip, dhtTrackerListenerPort, gsserverPort);
              container.RegisterInstance<TorrentHelper>(torrentHelper);
              #endregion

              #region BitTorrentManager
              // Singleton.
              container.RegisterType<BitTorrentManager>(
            new ContainerControlledLifetimeManager(),
            new InjectionConstructor(
              typeof(BitTorrentCache),
              ConfigurationManager.AppSettings["BitTorrentManagerSelfNamespace"],
              typeof(DictionaryServiceProxy),
              typeof(DictionaryServiceTracker),
              typeof(ClientEngine),
              typeof(TorrentSettings),
              typeof(TorrentHelper),
              Boolean.Parse(ConfigurationManager.AppSettings[
            "BitTorrentManagerStartSeedingAtStartup"])
              ));
              #endregion

              #region IPieceInfoServer
              // Singleton.
              //container.RegisterType<IPieceInfoServer, HttpPieceInfoServer>(
              //    new ContainerControlledLifetimeManager(),
              //    new InjectionConstructor(
              //      infoServerListeningPort,
              //      typeof(PieceLevelTorrentManager)));
              #endregion

              #region PieceLevelTorrentManager
              container.RegisterType<PieceLevelTorrentManager>(
            new ContainerControlledLifetimeManager(),
            new InjectionConstructor(
              typeof(BitTorrentManager),
              typeof(BitTorrentCache),
              typeof(DictionaryServiceProxy),
              typeof(TorrentHelper),
              infoServerListeningPort));
              #endregion
        }