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(); }
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(); }
public Tracker(string hostIpAddress, int port, string torrentFolder) { torrentTrackables = new Dictionary <string, ITrackable>(); realTracker = new MonoTorrent.Tracker.Tracker { AllowUnregisteredTorrents = true, AnnounceInterval = TimeSpan.FromHours(1), MinAnnounceInterval = TimeSpan.FromMinutes(10) }; //var listenPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(hostIpAddress), port); //listener = new HttpListener(listenPoint); var endPoint = string.Format("http://{0}:{1}/announce/", hostIpAddress, port); listener = new HttpListener(endPoint); realTracker.RegisterListener(listener); this.watcher = new TorrentFolderWatcher(Path.GetFullPath(torrentFolder), "*.torrent"); this.watcher.TorrentFound += delegate(object sender, TorrentWatcherEventArgs e) { try { // hack System.Threading.Thread.Sleep(500); this.AddATorrent(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()); } }; }
public MainWindow (UserEngineSettings userEngineSettings, ListenPortController portController, bool isFirstRun): base (Gtk.WindowType.Toplevel) { prefSettings = new PreferencesSettings (); this.userEngineSettings = userEngineSettings; this.portController = portController; userTorrentSettings = new UserTorrentSettings (); interfaceSettings = new InterfaceSettings (); labels = new ArrayList (); torrents = new Dictionary<MonoTorrent.Client.TorrentManager,Gtk.TreeIter> (); selectLabelTreeView = new TreeView(); Build (); BuildTray(); BuildPiecesTreeView(); BuildTorrentTreeView(); BuildPeerTreeView(); BuildFileTreeView(); BuildLabelTreeView(); BuildOptionsPage(); torrentController.engine.StatsUpdate += OnUpdateStats; torrentController.engine.ConnectionManager.PeerConnected += OnPeerConnected; torrentController.engine.ConnectionManager.PeerDisconnected += OnPeerDisconnected; GLib.Timeout.Add (1000, new GLib.TimeoutHandler (updateView)); RestoreInterface (); //portController = new ListenPortController(userEngineSettings); if (prefSettings.UpnpEnabled) portController.Start(); torrentController.LoadStoredTorrents (); RestoreLabels (); folderWatcher = new TorrentFolderWatcher (new DirectoryInfo (prefSettings.ImportLocation)); folderWatcher.TorrentFound += torrentController.OnTorrentFound; if (prefSettings.ImportEnabled) { logger.Info ("Starting import folder watcher"); folderWatcher.StartWatching (); } rssManagerController = new RssManagerController(torrentController); rssManagerController.StartWatchers(); if(isFirstRun) OpenDruid(); }
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(); }