Beispiel #1
0
        /// <summary>
        /// Tests creating torrent.
        /// </summary>
        /// <remarks>MonoTorrent TorrentCreator needs file to be available (even
        /// though it could be empty) even if dedup writer is used.</remarks>
        public static void TestCreateTorrent(string db, string dataFile, string savePath, string savePath1)
        {
            var dedupWriter = new DedupDiskWriter(new DeduplicationService(new ChunkDbService(db, false)));
            var creator     = new DedupTorrentCreator(dedupWriter);
            var ip          = NetUtil.GetLocalIPByInterface("Local Area Connection");
            var tier        = new RawTrackerTier {
                string.Format("http://{0}:25456/announce", ip.ToString()),
                "udp://tracker.publicbt.com:80/announce",
                "udp://tracker.openbittorrent.com:80/announce"
            };
            var filename = Path.GetFileName(dataFile);

            creator.GetrightHttpSeeds.Add(string.Format(
                                              "http://{0}:49645/FileServer/FileRange/{1}", ip.ToString(),
                                              filename));
            creator.Announces.Add(tier);
            var    binaryTorrent = creator.Create(new TorrentFileSource(dataFile));
            var    torrent       = Torrent.Load(binaryTorrent);
            string infoHash      = torrent.InfoHash.ToHex();

            File.WriteAllBytes(savePath, binaryTorrent.Encode());

            // Now read from the real file.
            var creator1 = new TorrentCreator();

            creator1.Announces.Add(tier);
            creator1.GetrightHttpSeeds.Add(string.Format(
                                               "http://{0}:49645/FileServer/FileRange/{1}", ip.ToString(),
                                               filename));
            var    binary1   = creator1.Create(new TorrentFileSource(dataFile));
            string infoHash1 = Torrent.Load(binary1).InfoHash.ToHex();

            File.WriteAllBytes(savePath1, binary1.Encode());

            Assert.AreEqual(infoHash, infoHash1);
            logger.DebugFormat("InfoHash: {0}", infoHash);
        }
        public static void TestReadFile(string db, string torrentPath, string baseDir, string filePath, string origianlFile)
        {
            //System.Diagnostics.Debugger.Launch();
            var kernel = new StandardKernel();

            kernel.Load(new ServiceNinjectModule());

            kernel.Bind <FileInfoTable <TorrentManager> >().ToSelf().InSingletonScope();

            var dbs = new ChunkDbService(db, false);
            var ds  = new DeduplicationService(dbs);

            kernel.Bind <DeduplicationService>().ToConstant(ds).InSingletonScope();

            var writer = new DedupDiskWriter(ds);

            var engineSettings = new EngineSettings();

            engineSettings.PreferEncryption  = false;
            engineSettings.AllowedEncryption = EncryptionTypes.All;
            int port = 33123;
            var ip   = NetUtil.GetLocalIPByInterface("Local Area Connection");

            engineSettings.ReportedAddress = new IPEndPoint(ip, port);
            var engine = new ClientEngine(engineSettings, new DedupDiskWriter(ds));

            kernel.Bind <DiskManager>().ToConstant(engine.DiskManager).InSingletonScope();
            kernel.Bind <ClientEngine>().ToConstant(engine).InSingletonScope();

            kernel.Bind <DistributedDiskManager>().ToSelf();
            kernel.Bind <FileService>().ToSelf().WithConstructorArgument("baseDir", baseDir);

            kernel.Bind <VirtualDiskDownloadService>().ToSelf();
            var vd = kernel.Get <VirtualDiskDownloadService>();

            var torrent = Torrent.Load(torrentPath);

            logger.DebugFormat("Loaded torrent file: {0}, piece length: {1}.",
                               torrent.Name, torrent.PieceLength);
            vd.StartDownloadingFile(torrent, baseDir, -1);

            KernelContainer.Kernel = kernel;

            var m = new HurricaneServiceManager();

            m.Start();

            var url = string.Format("http://{0}:18081/FileService/", ip.ToString());

            logger.DebugFormat("Contacting service at {0}", url);
            var    client = new ManualFileServiceClient(url);
            string tstmsg = "tstmsg";
            var    resp   = client.Echo(tstmsg);

            Assert.AreEqual(resp, tstmsg);

            byte[] resultData = null;

            try {
                var pathStatus = client.GetPathStatus(filePath);
                logger.DebugFormat("File size: {0}", pathStatus.FileSize);
            } catch (Exception ex) {
                logger.Error(ex);
            }

            try {
                // can only read 49352.
                resultData = client.Read(filePath, 0, 49253);
            } catch (Exception ex) {
                logger.Error(ex);
            }

            var actualData = IOUtil.Read(origianlFile, 0, 49252);

            try {
                Assert.IsTrue(actualData.SequenceEqual(resultData),
                              "File part should match.");
                logger.Debug("Read succeeded.");
            } catch (Exception ex) {
                logger.Error(ex);
            }

            Console.Read();
        }
        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
        }