Exemplo n.º 1
0
        public void ScrapeTest()
        {
            Tracker.Tracker t = TrackerFactory.Create(new Uri(prefix.Substring(0, prefix.Length - 1)));
            Assert.IsTrue(t.CanScrape, "#1");
            TrackerConnectionID id = new TrackerConnectionID(t, false, TorrentEvent.Started, new ManualResetEvent(false));

            AnnounceResponseEventArgs p = null;

            t.AnnounceComplete += delegate(object o, AnnounceResponseEventArgs e)
            {
                p = e;
                id.WaitHandle.Set();
            };
            MonoTorrent.Client.Tracker.AnnounceParameters pars = new AnnounceParameters();
            pars.PeerId   = "id";
            pars.InfoHash = new InfoHash(new byte[20]);

            t.Announce(pars, id);
            Wait(id.WaitHandle);
            Assert.IsNotNull(p, "#2");
            Assert.IsTrue(p.Successful, "#3");
            Assert.AreEqual(1, t.Complete, "#1");
            Assert.AreEqual(0, t.Incomplete, "#2");
            Assert.AreEqual(0, t.Downloaded, "#3");
        }
 /// <summary>
 /// Creates a new TrackerUpdateEventArgs
 /// </summary>
 /// <param name="state">The current state of the update</param>
 /// <param name="response">The response of the tracker (if any)</param>
 public TrackerStateChangedEventArgs(TorrentManager manager, Tracker tracker, TrackerState oldState, TrackerState newState)
     : base(manager)
 {
     this.tracker = tracker;
     this.oldState = oldState;
     this.newState = newState;
 }
 public TrackerConnectionID(Tracker tracker, bool trySubsequent, TorrentEvent torrentEvent, ManualResetEvent waitHandle)
 {
     this.tracker = tracker;
     this.trySubsequent = trySubsequent;
     this.torrentEvent = torrentEvent;
     this.waitHandle = waitHandle;
 }
Exemplo n.º 4
0
        void PostLogicTick(int counter)
        {
            PeerId   id;
            DateTime nowTime                      = DateTime.Now;
            DateTime thirtySecondsAgo             = nowTime.AddSeconds(-50);
            DateTime nintySecondsAgo              = nowTime.AddSeconds(-90);
            DateTime onhundredAndEightySecondsAgo = nowTime.AddSeconds(-180);

            for (int i = 0; i < manager.Peers.ConnectedPeers.Count; i++)
            {
                id = manager.Peers.ConnectedPeers[i];
                if (id.Connection == null)
                {
                    continue;
                }

                if (id.QueueLength > 0 && !id.ProcessingQueue)
                {
                    id.ProcessingQueue = true;
                    id.ConnectionManager.ProcessQueue(id);
                }

                if (nintySecondsAgo > id.LastMessageSent)
                {
                    id.LastMessageSent = DateTime.Now;
                    id.Enqueue(new KeepAliveMessage());
                }

                if (onhundredAndEightySecondsAgo > id.LastMessageReceived)
                {
                    manager.Engine.ConnectionManager.CleanupSocket(id, "Inactivity");
                    continue;
                }

                if (thirtySecondsAgo > id.LastMessageReceived && id.AmRequestingPiecesCount > 0)
                {
                    manager.Engine.ConnectionManager.CleanupSocket(id, "Didn't send pieces");
                    continue;
                }
            }

            Tracker.Tracker tracker = manager.TrackerManager.CurrentTracker;
            if (tracker != null && (manager.State == TorrentState.Seeding || manager.State == TorrentState.Downloading))
            {
                // If the last connection succeeded, then update at the regular interval
                if (manager.TrackerManager.UpdateSucceeded)
                {
                    if (DateTime.Now > (manager.TrackerManager.LastUpdated.Add(tracker.UpdateInterval)))
                    {
                        manager.TrackerManager.Announce(TorrentEvent.None);
                    }
                }
                // Otherwise update at the min interval
                else if (DateTime.Now > (manager.TrackerManager.LastUpdated.Add(tracker.MinUpdateInterval)))
                {
                    manager.TrackerManager.Announce(TorrentEvent.None);
                }
            }
        }
Exemplo n.º 5
0
 public void FixtureSetup()
 {
     listener = new Listeners.HttpListener(uri.OriginalString);
     listener.Start();
     server = new Tracker();
     server.RegisterListener(listener);
     listener.Start();
 }
        public TrackerPeersAdded(TorrentManager manager, int peersAdded, int total, Tracker.Tracker tracker)
            : base(manager, peersAdded, total)
        {
            if (tracker == null)
                throw new ArgumentNullException("tracker");

            this.tracker = tracker;
        }
Exemplo n.º 7
0
        void PostLogicTick(int counter)
        {
            PeerId id;
            var    thirtySeconds             = TimeSpan.FromSeconds(30);
            var    nintySeconds              = TimeSpan.FromSeconds(90);
            var    onhundredAndEightySeconds = TimeSpan.FromSeconds(180);

            for (int i = 0; i < Manager.Peers.ConnectedPeers.Count; i++)
            {
                id = Manager.Peers.ConnectedPeers[i];
                if (id.Connection == null)
                {
                    continue;
                }

                if (id.QueueLength > 0 && !id.ProcessingQueue)
                {
                    id.ProcessingQueue = true;
                    Manager.Engine.ConnectionManager.ProcessQueue(id);
                }

                if (id.LastMessageSent.Elapsed > nintySeconds)
                {
                    id.LastMessageSent.Restart();
                    id.Enqueue(new KeepAliveMessage());
                }

                if (id.LastMessageReceived.Elapsed > onhundredAndEightySeconds)
                {
                    Manager.Engine.ConnectionManager.CleanupSocket(id, "Inactivity");
                    continue;
                }

                if (id.LastMessageReceived.Elapsed > thirtySeconds && id.AmRequestingPiecesCount > 0)
                {
                    Manager.Engine.ConnectionManager.CleanupSocket(id, "Didn't send pieces");
                    continue;
                }
            }

            Tracker.Tracker tracker = Manager.TrackerManager.CurrentTracker;
            if (tracker != null && (Manager.State == TorrentState.Seeding || Manager.State == TorrentState.Downloading))
            {
                // If the last connection succeeded, then update at the regular interval
                if (Manager.TrackerManager.UpdateSucceeded)
                {
                    if (DateTime.Now > (Manager.TrackerManager.LastUpdated.Add(tracker.UpdateInterval)))
                    {
                        Manager.TrackerManager.Announce(TorrentEvent.None);
                    }
                }
                // Otherwise update at the min interval
                else if (DateTime.Now > (Manager.TrackerManager.LastUpdated.Add(tracker.MinUpdateInterval)))
                {
                    Manager.TrackerManager.Announce(TorrentEvent.None);
                }
            }
        }
 protected TrackerResponseEventArgs(Tracker tracker, object state, bool successful)
 {
     if (tracker == null)
         throw new ArgumentNullException("tracker");
     if (!(state is TrackerConnectionID))
         throw new ArgumentException("The state object must be the same object as in the call to Announce", "state");
     this.id = (TrackerConnectionID)state;
     this.successful = successful;
     this.tracker = tracker;
 }
Exemplo n.º 9
0
        public void KeyTest()
        {
            MonoTorrent.Client.Tracker.AnnounceParameters pars = new AnnounceParameters();
            pars.PeerId   = "id";
            pars.InfoHash = new InfoHash(new byte[20]);

            Tracker.Tracker     t  = TrackerFactory.Create(new Uri(prefix + "?key=value"));
            TrackerConnectionID id = new TrackerConnectionID(t, false, TorrentEvent.Started, new ManualResetEvent(false));

            t.AnnounceComplete += delegate { id.WaitHandle.Set(); };
            t.Announce(pars, id);
            Wait(id.WaitHandle);
            Assert.AreEqual("value", keys[0], "#1");
        }
Exemplo n.º 10
0
        public void CanAnnouceOrScrapeTest()
        {
            Tracker.Tracker t = TrackerFactory.Create(new Uri("http://mytracker.com/myurl"));
            Assert.IsFalse(t.CanScrape, "#1");
            Assert.IsTrue(t.CanAnnounce, "#1b");

            t = TrackerFactory.Create(new Uri("http://mytracker.com/announce/yeah"));
            Assert.IsFalse(t.CanScrape, "#2");
            Assert.IsTrue(t.CanAnnounce, "#2b");

            t = TrackerFactory.Create(new Uri("http://mytracker.com/announce"));
            Assert.IsTrue(t.CanScrape, "#3");
            Assert.IsTrue(t.CanAnnounce, "#4");

            HTTPTracker tracker = (HTTPTracker)TrackerFactory.Create(new Uri("http://mytracker.com/announce/yeah/announce"));

            Assert.IsTrue(tracker.CanScrape, "#4");
            Assert.IsTrue(tracker.CanAnnounce, "#4");
            Assert.AreEqual("http://mytracker.com/announce/yeah/scrape", tracker.ScrapeUri.ToString(), "#5");
        }
Exemplo n.º 11
0
        public static string GetStatsLog(TorrentManager manager)
        {
            StringBuilder sb = new StringBuilder();

            AppendSeperator(sb);
            AppendFormat(sb, "State:           {0}", manager.State);
            AppendFormat(sb, "Name:            {0}", manager.Torrent == null ? "MetaDataMode" : manager.Torrent.Name);
            AppendFormat(sb, "Progress:           {0:0.00}", manager.Progress);
            AppendFormat(sb, "Download Speed:     {0:0.00} kB/s", manager.Monitor.DownloadSpeed / 1024.0);
            AppendFormat(sb, "Upload Speed:       {0:0.00} kB/s", manager.Monitor.UploadSpeed / 1024.0);
            AppendFormat(sb, "Total Downloaded:   {0:0.00} MB", manager.Monitor.DataBytesDownloaded / (1024.0 * 1024.0));
            AppendFormat(sb, "Total Uploaded:     {0:0.00} MB", manager.Monitor.DataBytesUploaded / (1024.0 * 1024.0));
            MonoTorrent.Client.Tracker.Tracker tracker = manager.TrackerManager.CurrentTracker;
            AppendFormat(sb, "Tracker Status:     {0}", tracker == null ? "<no tracker>" : tracker.Status.ToString());
            AppendFormat(sb, "Warning Message:    {0}", tracker == null ? "<no tracker>" : tracker.WarningMessage);
            AppendFormat(sb, "Failure Message:    {0}", tracker == null ? "<no tracker>" : tracker.FailureMessage);
            if (manager.PieceManager != null)
            {
                AppendFormat(sb, "Current Requests:   {0}", manager.PieceManager.CurrentRequestCount());
            }

            foreach (PeerId p in manager.GetPeers())
            {
                AppendFormat(sb, "\t{2} - {1:0.00}/{3:0.00}kB/sec - {0}", p.Peer.ConnectionUri,
                             p.Monitor.DownloadSpeed / 1024.0,
                             p.AmRequestingPiecesCount,
                             p.Monitor.UploadSpeed / 1024.0);
            }

            AppendFormat(sb, "", null);
            if (manager.Torrent != null)
            {
                foreach (TorrentFile file in manager.Torrent.Files)
                {
                    AppendFormat(sb, "{1:0.00}% - {0}", file.Path, file.BitField.PercentComplete);
                }
            }

            AppendFormat(sb, "Piece downloading sequence: [{0}].",
                         string.Join(",", manager.TorrentStats.PieceDownloadSequence.ConvertAll <string>(x => x.ToString())));

            AppendFormat(sb, "Prediction missies: {0}.", manager.TorrentStats.OnDemandRequests.Count);

            var peerDict = new Dictionary <PeerId, int>();

            foreach (var piece in manager.TorrentStats.Pieces)
            {
                foreach (var block in piece.Blocks)
                {
                    if (!block.Deduplicated)
                    {
                        var peerId = block.RequestedOff;
                        if (!peerDict.ContainsKey(peerId))
                        {
                            peerDict[peerId] = 1;
                        }
                        else
                        {
                            peerDict[peerId] = peerDict[peerId] + 1;
                        }
                    }
                }
            }
            AppendFormat(sb, "Peers this torrent are downloaded from (peer, blocks): [{0}].",
                         string.Join(",", peerDict.ToList().ConvertAll <string>(x => string.Format("({0}, {1})", x.Key, x.Value))));

            return(sb.ToString());
        }
Exemplo n.º 12
0
        private void DownloaderWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (e.Argument == null)
                {
                    return;
                }

                basePath       = Environment.CurrentDirectory;
                dhtNodeFile    = System.IO.Path.Combine(basePath, "DhtNodes");
                downloadsPath  = System.IO.Path.Combine(basePath, "Downloads");
                torrentsPath   = System.IO.Path.Combine(basePath, "Torrents");
                fastResumeFile = System.IO.Path.Combine(torrentsPath, "fastresume.data");
                torrents       = new List <TorrentManager>(); // The list where all the torrentManagers will be stored that the engine gives us
                listener       = new Top10Listener(10);

                string torrentpath = e.Argument.ToString();

                int     port    = 6969;
                Torrent torrent = null;

                // Create the settings which the engine will use
                // downloadsPath - this is the path where we will save all the files to
                // port - this is the port we listen for connections on
                EngineSettings engineSettings = new EngineSettings(downloadsPath, port);
                engineSettings.PreferEncryption  = false;
                engineSettings.AllowedEncryption = EncryptionTypes.All;

                //engineSettings.GlobalMaxUploadSpeed = 30 * 1024;
                //engineSettings.GlobalMaxDownloadSpeed = 100 * 1024;
                //engineSettings.MaxReadRate = 1 * 1024 * 1024;


                // 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);

                // Create an instance of the engine.
                engine = new ClientEngine(engineSettings);
                engine.ChangeListenEndpoint(new IPEndPoint(IPAddress.Any, port));
                byte[] nodes = null;
                try
                {
                    nodes = File.ReadAllBytes(dhtNodeFile);
                }
                catch
                {
                    Console.WriteLine("No existing dht nodes could be loaded");
                }

                DhtListener dhtListner = new DhtListener(new IPEndPoint(IPAddress.Any, port));
                DhtEngine   dht        = new DhtEngine(dhtListner);
                engine.RegisterDht(dht);
                dhtListner.Start();
                engine.DhtEngine.Start(nodes);

                // If the SavePath does not exist, we want to create it.
                if (!Directory.Exists(engine.Settings.SavePath))
                {
                    Directory.CreateDirectory(engine.Settings.SavePath);
                }

                // If the torrentsPath does not exist, we want to create it
                if (!Directory.Exists(torrentsPath))
                {
                    Directory.CreateDirectory(torrentsPath);
                }

                BEncodedDictionary fastResume;
                try
                {
                    fastResume = BEncodedValue.Decode <BEncodedDictionary>(File.ReadAllBytes(fastResumeFile));
                }
                catch
                {
                    fastResume = new BEncodedDictionary();
                }


                // Load the .torrent from the file into a Torrent instance
                // You can use this to do preprocessing should you need to
                torrent = Torrent.Load(torrentpath);

                // When any preprocessing has been completed, you create a TorrentManager
                // which you then register with the engine.
                TorrentManager manager = new TorrentManager(torrent, downloadsPath, torrentDefaults);
                //if (fastResume.ContainsKey(torrent.InfoHash.ToHex()))
                //    manager.LoadFastResume(new FastResume((BEncodedDictionary)fastResume[torrent.infoHash.ToHex()]));
                engine.Register(manager);

                // Store the torrent manager in our list so we can access it later
                torrents.Add(manager);
                manager.PeersFound += new EventHandler <PeersAddedEventArgs>(manager_PeersFound);


                // Every time a piece is hashed, this is fired.
                manager.PieceHashed += delegate(object o, PieceHashedEventArgs ec)
                {
                    lock (listener)
                        listener.WriteLine(string.Format("Piece Hashed: {0} - {1}", ec.PieceIndex, ec.HashPassed ? "Pass" : "Fail"));
                };

                // Every time the state changes (Stopped -> Seeding -> Downloading -> Hashing) this is fired
                manager.TorrentStateChanged += delegate(object o, TorrentStateChangedEventArgs ev)
                {
                    lock (listener)
                        listener.WriteLine("OldState: " + ev.OldState.ToString() + " NewState: " + ev.NewState.ToString());
                };

                // Every time the tracker's state changes, this is fired
                //foreach (TrackerTier tier in manager.TrackerManager)
                //{
                //    //foreach (MonoTorrent.Client.Tracker.Tracker t in tier.Trackers)
                //    //{
                //    //    t.AnnounceComplete += delegate (object sender, AnnounceResponseEventArgs e)
                //    //    {
                //    //        listener.WriteLine(string.Format("{0}: {1}", e.Successful, e.Tracker.ToString()));
                //    //    };
                //    //}
                //}
                // Start the torrentmanager. The file will then hash (if required) and begin downloading/seeding
                manager.Start();


                // While the torrents are still running, print out some stats to the screen.
                // Details for all the loaded torrent managers are shown.
                bool          running = true;
                StringBuilder sb      = new StringBuilder(1024);
                while (running)
                {
                    //if ((i++) % 10 == 0)
                    //{
                    sb.Remove(0, sb.Length);
                    running = torrents.Exists(delegate(TorrentManager m) { return(m.State != TorrentState.Stopped); });

                    //AppendFormat(sb, "Total Download Rate: {0:0.00}kB/sec", engine.TotalDownloadSpeed / 1024.0);
                    downloadspeed = (engine.TotalDownloadSpeed / 1024.0).ToString();
                    //AppendFormat(sb, "Total Upload Rate:   {0:0.00}kB/sec", engine.TotalUploadSpeed / 1024.0);
                    //AppendFormat(sb, "Disk Read Rate:      {0:0.00} kB/s", engine.DiskManager.ReadRate / 1024.0);
                    //AppendFormat(sb, "Disk Write Rate:     {0:0.00} kB/s", engine.DiskManager.WriteRate / 1024.0);
                    //AppendFormat(sb, "Total Read:         {0:0.00} kB", engine.DiskManager.TotalRead / 1024.0);
                    //AppendFormat(sb, "Total Written:      {0:0.00} kB", engine.DiskManager.TotalWritten / 1024.0);
                    //AppendFormat(sb, "Open Connections:    {0}", engine.ConnectionManager.OpenConnections);


                    //AppendSeperator(sb);
                    //AppendFormat(sb, "State:           {0}", manager.State);
                    //AppendFormat(sb, "Name:            {0}", manager.Torrent == null ? "MetaDataMode" : manager.Torrent.Name);
                    //AppendFormat(sb, "Progress:           {0:0.00}", manager.Progress);
                    //progress = manager.Progress.ToString();
                    //AppendFormat(sb, "Download Speed:     {0:0.00} kB/s", manager.Monitor.DownloadSpeed / 1024.0);
                    //AppendFormat(sb, "Upload Speed:       {0:0.00} kB/s", manager.Monitor.UploadSpeed / 1024.0);
                    //AppendFormat(sb, "Total Downloaded:   {0:0.00} MB", manager.Monitor.DataBytesDownloaded / (1024.0 * 1024.0));
                    //AppendFormat(sb, "Total Uploaded:     {0:0.00} MB", manager.Monitor.DataBytesUploaded / (1024.0 * 1024.0));
                    MonoTorrent.Client.Tracker.Tracker tracker = manager.TrackerManager.CurrentTracker;
                    //AppendFormat(sb, "Tracker Status:     {0}", tracker == null ? "<no tracker>" : tracker.State.ToString());
                    //AppendFormat(sb, "Warning Message:    {0}", tracker == null ? "<no tracker>" : tracker.WarningMessage);
                    //AppendFormat(sb, "Failure Message:    {0}", tracker == null ? "<no tracker>" : tracker.FailureMessage);
                    //if (manager.PieceManager != null)
                    //    AppendFormat(sb, "Current Requests:   {0}", manager.PieceManager.CurrentRequestCount());

                    //foreach (PeerId p in manager.GetPeers())
                    //    AppendFormat(sb, "\t{2} - {1:0.00}/{3:0.00}kB/sec - {0}", p.Peer.ConnectionUri,
                    //                                                              p.Monitor.DownloadSpeed / 1024.0,
                    //                                                              p.AmRequestingPiecesCount,
                    //                                                              p.Monitor.UploadSpeed / 1024.0);

                    //AppendFormat(sb, "", null);
                    //if (manager.Torrent != null)
                    //    foreach (TorrentFile file in manager.Torrent.Files)
                    //        AppendFormat(sb, "{1:0.00}% - {0}", file.Path, file.BitField.PercentComplete);

                    //Console.Clear();
                    //Console.WriteLine(sb.ToString());
                    //listener.ExportTo(Console.Out);
                    //}
                    DownloaderWorker.ReportProgress(Convert.ToInt32(manager.Progress), manager.State.ToString());
                    System.Threading.Thread.Sleep(500);
                }
            }
            catch (Exception ex)
            {
            }
        }
Exemplo n.º 13
0
 static void doAnounce(MonoTorrent.Client.Tracker.Tracker t)
 {
     t.AnnounceComplete += delegate(object sender, AnnounceResponseEventArgs e) {
         listener.WriteLine(string.Format("{0}: {1}", e.Successful, e.Tracker.ToString()));
     };
 }
Exemplo n.º 14
0
 protected TrackerResponseEventArgs(Tracker tracker, TrackerConnectionID state, bool successful)
 {
     this.tracker    = tracker ?? throw new ArgumentNullException("tracker");
     this.id         = (TrackerConnectionID)state;
     this.successful = successful;
 }
Exemplo n.º 15
0
 public TrackerAdapter(TorrentManager manager, MonoTorrent.Client.Tracker.Tracker tracker, ObjectPath path)
 {
     this.manager = manager;
     this.tracker = tracker;
     this.path = path;
 }
Exemplo n.º 16
0
        public ScrapeResponseEventArgs(Tracker tracker, object state, bool successful)
            : base(tracker, state, successful)
        {

        }
 public AnnounceResponseEventArgs(Tracker tracker, object state, bool successful)
     : this(tracker, state, successful, new List <Peer>())
 {
 }
 public AnnounceResponseEventArgs(Tracker tracker, object state, bool successful, List <Peer> peers)
     : base(tracker, state, successful)
 {
     this.peers = peers;
 }
Exemplo n.º 19
0
        private static void StartEngine()
        {
            int     port    = 8589;
            Torrent torrent = null;



            // Create the settings which the engine will use
            // downloadsPath - this is the path where we will save all the files to
            // port - this is the port we listen for connections on
            EngineSettings engineSettings = new EngineSettings(downloadsPath, port);

            engineSettings.PreferEncryption  = false;
            engineSettings.AllowedEncryption = EncryptionTypes.All;

            //engineSettings.GlobalMaxUploadSpeed = 30 * 1024;
            //engineSettings.GlobalMaxDownloadSpeed = 100 * 1024;
            //engineSettings.MaxReadRate = 1 * 1024 * 1024;


            // 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

            int yj  = 0;
            int yjv = 0;

            if (File.Exists("dwnspeed.tx"))
            {
                string hfg = File.ReadAllText("dwnspeed.tx");

                yjv = int.Parse(hfg);
            }
            if (File.Exists("uplspeed.tx"))
            {
                string hfg = File.ReadAllText("uplspeed.tx");

                yj = int.Parse(hfg);
            }

            TorrentSettings torrentDefaults = new TorrentSettings(100, 150, yjv, yj);

            // Create an instance of the engine.
            engine = new ClientEngine(engineSettings);
            engine.ChangeListenEndpoint(new IPEndPoint(IPAddress.Any, port));
            byte[] nodes = null;
            try
            {
                nodes = File.ReadAllBytes(dhtNodeFile);
            }
            catch
            {
                Console.WriteLine("No existing dht nodes could be loaded");
            }

            DhtListener dhtListner = new DhtListener(new IPEndPoint(IPAddress.Any, port));
            DhtEngine   dht        = new DhtEngine(dhtListner);

            engine.RegisterDht(dht);
            dhtListner.Start();
            engine.DhtEngine.Start(nodes);

            // If the SavePath does not exist, we want to create it.
            if (!Directory.Exists(engine.Settings.SavePath))
            {
                Directory.CreateDirectory(engine.Settings.SavePath);
            }

            // If the torrentsPath does not exist, we want to create it
            if (!Directory.Exists(torrentsPath))
            {
                Directory.CreateDirectory(torrentsPath);
            }

            BEncodedDictionary fastResume;

            try
            {
                fastResume = BEncodedValue.Decode <BEncodedDictionary>(File.ReadAllBytes(fastResumeFile));
            }
            catch
            {
                fastResume = new BEncodedDictionary();
            }

            // For each file in the torrents path that is a .torrent file, load it into the engine.
            foreach (string file in Directory.GetFiles(torrentsPath))
            {
                if (file.EndsWith(".torrent"))
                {
                    try
                    {
                        // Load the .torrent from the file into a Torrent instance
                        // You can use this to do preprocessing should you need to
                        torrent = Torrent.Load(file);
                        Console.WriteLine(torrent.InfoHash.ToString());
                    }
                    catch (Exception e)
                    {
                        Console.Write("Couldn't decode {0}: ", file);
                        Console.WriteLine(e.Message);
                        continue;
                    }
                    // When any preprocessing has been completed, you create a TorrentManager
                    // which you then register with the engine.
                    TorrentManager manager = new TorrentManager(torrent, downloadsPath, torrentDefaults);
                    if (fastResume.ContainsKey(torrent.InfoHash.ToHex()))
                    {
                        manager.LoadFastResume(new FastResume((BEncodedDictionary)fastResume[torrent.infoHash.ToHex()]));
                    }
                    engine.Register(manager);

                    // Store the torrent manager in our list so we can access it later
                    torrents.Add(manager);
                    manager.PeersFound += new EventHandler <PeersAddedEventArgs>(manager_PeersFound);
                }
            }

            // If we loaded no torrents, just exist. The user can put files in the torrents directory and start
            // the client again
            if (torrents.Count == 0)
            {
                Console.WriteLine("No torrents found in the Torrents directory");
                Console.WriteLine("Exiting...");
                engine.Dispose();
                return;
            }

            // For each torrent manager we loaded and stored in our list, hook into the events
            // in the torrent manager and start the engine.
            foreach (TorrentManager manager in torrents)
            {
                // Every time a piece is hashed, this is fired.
                manager.PieceHashed += delegate(object o, PieceHashedEventArgs e) {
                    lock (listener)
                        listener.WriteLine(string.Format("Piece Hashed: {0} - {1}", e.PieceIndex, e.HashPassed ? "Pass" : "Fail"));
                };

                // Every time the state changes (Stopped -> Seeding -> Downloading -> Hashing) this is fired
                manager.TorrentStateChanged += delegate(object o, TorrentStateChangedEventArgs e) {
                    lock (listener)
                        listener.WriteLine("OldState: " + e.OldState.ToString() + " NewState: " + e.NewState.ToString());
                };

                // Every time the tracker's state changes, this is fired
                foreach (TrackerTier tier in manager.TrackerManager)
                {
                    foreach (MonoTorrent.Client.Tracker.Tracker t in tier.Trackers)
                    {
                        t.AnnounceComplete += delegate(object sender, AnnounceResponseEventArgs e) {
                            listener.WriteLine(string.Format("{0}: {1}", e.Successful, e.Tracker.ToString()));
                        };
                    }
                }
                // Start the torrentmanager. The file will then hash (if required) and begin downloading/seeding
                manager.Start();
            }

            // While the torrents are still running, print out some stats to the screen.
            // Details for all the loaded torrent managers are shown.
            int           i       = 0;
            bool          running = true;
            StringBuilder sb      = new StringBuilder(1024);

            while (running)
            {
                if ((i++) % 10 == 0)
                {
                    sb.Remove(0, sb.Length);
                    running = torrents.Exists(delegate(TorrentManager m) { return(m.State != TorrentState.Stopped); });
                    foreach (TorrentManager manager in torrents)
                    {
                        AppendSeperator(sb);
                        AppendFormat(sb, "State:           {0}", manager.State);
                        AppendFormat(sb, "Name:            {0}", manager.Torrent == null ? "MetaDataMode" : manager.Torrent.Name);
                        AppendFormat(sb, "Progress:           {0:0.00}", manager.Progress);
                        AppendFormat(sb, "Download Speed:     {0:0.00} kB/s", manager.Monitor.DownloadSpeed / 1024.0);
                        AppendFormat(sb, "Upload Speed:       {0:0.00} kB/s", manager.Monitor.UploadSpeed / 1024.0);
                        AppendFormat(sb, "Total Downloaded:   {0:0.00} MB", manager.Monitor.DataBytesDownloaded / (1024.0 * 1024.0));
                        AppendFormat(sb, "Total Uploaded:     {0:0.00} MB", manager.Monitor.DataBytesUploaded / (1024.0 * 1024.0));
                        MonoTorrent.Client.Tracker.Tracker tracker = manager.TrackerManager.CurrentTracker;
                        //AppendFormat(sb, "Tracker Status:     {0}", tracker == null ? "<no tracker>" : tracker.State.ToString());
                        AppendFormat(sb, "Warning Message:    {0}", tracker == null ? "<no tracker>" : tracker.WarningMessage);
                        AppendFormat(sb, "Failure Message:    {0}", tracker == null ? "<no tracker>" : tracker.FailureMessage);
                        if (manager.PieceManager != null)
                        {
                            AppendFormat(sb, "Current Requests:   {0}", manager.PieceManager.CurrentRequestCount());
                        }

                        foreach (PeerId p in manager.GetPeers())
                        {
                            AppendFormat(sb, "\t{2} - {1:0.00}/{3:0.00}kB/sec - {0}", p.Peer.ConnectionUri,
                                         p.Monitor.DownloadSpeed / 1024.0,
                                         p.AmRequestingPiecesCount,
                                         p.Monitor.UploadSpeed / 1024.0);
                        }

                        AppendFormat(sb, "", null);
                        if (manager.Torrent != null)
                        {
                            foreach (TorrentFile file in manager.Torrent.Files)
                            {
                                AppendFormat(sb, "{1:0.00}% - {0}", file.Path, file.BitField.PercentComplete);
                            }
                        }
                    }
                    AppendFormat(sb, "Total Download Rate: {0:0.00}kB/sec", engine.TotalDownloadSpeed / 1024.0);
                    AppendFormat(sb, "Total Upload Rate:   {0:0.00}kB/sec", engine.TotalUploadSpeed / 1024.0);
                    AppendFormat(sb, "Disk Read Rate:      {0:0.00} kB/s", engine.DiskManager.ReadRate / 1024.0);
                    AppendFormat(sb, "Disk Write Rate:     {0:0.00} kB/s", engine.DiskManager.WriteRate / 1024.0);
                    AppendFormat(sb, "Total Read:         {0:0.00} kB", engine.DiskManager.TotalRead / 1024.0);
                    AppendFormat(sb, "Total Written:      {0:0.00} kB", engine.DiskManager.TotalWritten / 1024.0);
                    AppendFormat(sb, "Open Connections:    {0}", engine.ConnectionManager.OpenConnections);


                    Console.Clear();
                    Console.WriteLine(sb.ToString());
                    listener.ExportTo(Console.Out);
                }

                System.Threading.Thread.Sleep(500);
            }
        }
Exemplo n.º 20
0
 public TrackerAdapter(TorrentManager manager, MonoTorrent.Client.Tracker.Tracker tracker, ObjectPath path)
 {
     this.manager = manager;
     this.tracker = tracker;
     this.path    = path;
 }
Exemplo n.º 21
0
 internal HdknTracker(Tracker tracker)
 {
     _tracker = tracker;
 }
 public AnnounceResponseEventArgs(Tracker tracker, object state, bool successful, List<Peer> peers)
     : base(tracker, state, successful)
 {
     Peers = peers;
 }
 public AnnounceResponseEventArgs(Tracker tracker, object state, bool successful)
     : this(tracker, state, successful, new List<Peer>())
 {
 }
Exemplo n.º 24
0
 public ScrapeResponseEventArgs(Tracker tracker, bool successful)
     : base(tracker, successful)
 {
 }