Esempio n. 1
0
        private void SetupTorrentWatcher()
        {
            watcher = new TorrentFolderWatcher(Path.GetFullPath(TORRENT_DIR), "*.torrent");
            watcher.TorrentFound += delegate(object sender, TorrentWatcherEventArgs e) {
                try {
                    // This is a hack to work around the issue where a file triggers the event
                    // before it has finished copying. As the filesystem still has an exclusive lock
                    // on the file, monotorrent can't access the file and throws an exception.
                    // The best way to handle this depends on the actual application.
                    // Generally the solution is: Wait a few hundred milliseconds
                    // then try load the file.
                    System.Threading.Thread.Sleep(500);

                    Torrent t = Torrent.Load(e.TorrentPath);

                    // There is also a predefined 'InfoHashTrackable' MonoTorrent.Tracker which
                    // just stores the infohash and name of the torrent. This is all that the tracker
                    // needs to run. So if you want an ITrackable that "just works", then use InfoHashTrackable.

                    // ITrackable trackable = new InfoHashTrackable(t);
                    ITrackable trackable = new CustomITrackable(t);

                    // The lock is here because the TorrentFound event is asyncronous and I have
                    // to ensure that only 1 thread access the tracker at the same time.
                    lock (tracker)
                        tracker.Add(trackable);
                } catch (Exception ex) {
                    Debug.WriteLine("Error loading torrent from disk: {0}", ex.Message);
                    Debug.WriteLine("Stacktrace: {0}", ex.ToString());
                }
            };

            watcher.Start();
            watcher.ForceScan();
        }
        public static void SetupTorrentWatcher(BitTorrentManager manager, 
            string torrentsDir)
        {
            TorrentFolderWatcher watcher =
            new TorrentFolderWatcher(torrentsDir, "*.torrent");
              watcher.TorrentFound += delegate(object sender, TorrentWatcherEventArgs e) {
            try {
              // This is a hack to work around the issue where a file triggers the event
              // before it has finished copying. As the filesystem still has an exclusive lock
              // on the file, monotorrent can't access the file and throws an exception.
              // The best way to handle this depends on the actual application.
              // Generally the solution is: Wait a few hundred milliseconds
              // then try load the file.
              System.Threading.Thread.Sleep(500);

              Torrent t = Torrent.Load(e.TorrentPath);
              Logger.WriteLineIf(LogLevel.Verbose, _log_props,
            string.Format("Torrent file {1} at {0} loaded", e.TorrentPath, t.Name));

              manager.AddTorrentToTracker(t);
            } catch (Exception ex) {
              Logger.WriteLineIf(LogLevel.Error, _log_props,
            string.Format("Error loading torrent from disk: {0}", ex.ToString()));
            }
              };

              watcher.Start();
              // We have two levels in the torrents directory.
              // The FileSystemWatcher is instantiated in Start().
              watcher.FileSystemWatcher.IncludeSubdirectories = true;
              watcher.ForceScan();
        }
Esempio n. 3
0
        private void StartSeedingAllTorrents()
        {
            var config         = Catalog.Factory.Resolve <IConfig>();
            var torrentFolder  = config[BitTorrentSettings.TrackerTorrentFolder];
            var torrentDirInfo = new DirectoryInfo(torrentFolder);

            if (!torrentDirInfo.Exists)
            {
                torrentDirInfo = Directory.CreateDirectory(torrentFolder);
            }

            foreach (var torrentFileInfo in torrentDirInfo.GetFiles("*.torrent"))
            {
                StartSeedingTorrentFile(torrentFileInfo);
            }

            watcher = new TorrentFolderWatcher(Path.GetFullPath(torrentFolder), "*.torrent");
            watcher.TorrentFound += (sender, e) =>
            {
                if (e == null)
                {
                    throw new ArgumentNullException("e");
                }
                try
                {
                    // hack
                    Thread.Sleep(500);
                    Debug.WriteLine("Torrent found at " + torrentFolder + " " + e.TorrentPath);
                    this.StartSeedingTorrentFile(new FileInfo(e.TorrentPath));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException != null)
                    {
                        _log.Error("Error loading torrent from disk: " + ex.ToString() + " \n InnerException: " + ex.InnerException.ToString());
                    }
                    else
                    {
                        _log.Error("Error loading torrent from disk: " + ex.ToString());
                    }
                    Debug.WriteLine("Error loading torrent from disk: {0}", ex.Message);
                    Debug.WriteLine("Stacktrace: {0}", ex.ToString());
                }
            };

            watcher.Start();
            watcher.ForceScan();
        }
Esempio n. 4
0
        public SimpleTracker(string announcementEndpoint, string torrentsDirectoryPath)
        {
            // Make the listner.
            var listener = new HttpListener(announcementEndpoint);

            // Make the tracker.
            Tracker = new Tracker();
            Tracker.AnnounceInterval          = new TimeSpan(0, 1, 0);
            Tracker.AllowUnregisteredTorrents = true;
            Tracker.RegisterListener(listener);

            // Make mappings.
            Mappings = new ConcurrentDictionary <string, InfoHashTrackable> ();

            // Make watcher.
            Watcher = new TorrentFolderWatcher(torrentsDirectoryPath, "*.torrent");
            Watcher.TorrentFound += (sender, e) =>
            {
                try
                {
                    // Wait for file to finish copying.
                    System.Threading.Thread.Sleep(500);

                    // Make InfoHashTrackable from torrent.
                    var torrent   = Torrent.Load(e.TorrentPath);
                    var trackable = new InfoHashTrackable(torrent);

                    // Add to tracker.
                    lock (Tracker)
                        Tracker.Add(trackable);

                    // Save to mappings.
                    Mappings[e.TorrentPath] = trackable;

                    // Log.
                    Console.WriteLine("Added {0}", e.TorrentPath);
                }
                catch (Exception exception)
                {
                    Debug.WriteLine("Error loading torrent from disk: {0}", exception.Message);
                    Debug.WriteLine("Stacktrace: {0}", exception.ToString());
                }
            };
            Watcher.TorrentLost += (sender, e) =>
            {
                try
                {
                    // Get from mappings.
                    var trackable = Mappings[e.TorrentPath];

                    // Remove from tracker.
                    lock (Tracker)
                        Tracker.Remove(trackable);

                    // Remove from mappings.
                    Mappings.TryRemove(e.TorrentPath, out trackable);

                    // Log.
                    Console.WriteLine("Removed {0}", e.TorrentPath);
                }
                catch (Exception exception)
                {
                    Debug.WriteLine("Error uploading torrent from disk: {0}", exception.Message);
                    Debug.WriteLine("Stacktrace: {0}", exception.ToString());
                }
            };

            // Register close events.
            AppDomain.CurrentDomain.ProcessExit += (sender, e) => Tracker.Dispose();

            // Run.
            listener.Start();
            Watcher.Start();
            Watcher.ForceScan();
        }