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. Thread.Sleep(500); var 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 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(); }
public MainWindow() : base(Gtk.WindowType.Toplevel) { this.portController = ServiceManager.Get <ListenPortController> (); this.torrentController = ServiceManager.Get <TorrentController> (); interfaceSettings = new InterfaceSettings (); Ticker.Tick (); LoadAllSettings (); Ticker.Tock ("Loaded all settings: {0}"); Ticker.Tick (); Ticker.Tick (); Build (); AddInManagerAction.Activated += AddInManagerActionActivated; InitNatStatus (); Ticker.Tock ("Build"); Ticker.Tick(); BuildStatusBar(); Ticker.Tock ("Status bar"); Ticker.Tick (); BuildTray(); Ticker.Tock ("Tray"); Ticker.Tick (); BuildPiecesTreeView(); Ticker.Tock ("PiecesTreeview"); Ticker.Tick (); BuildTorrentTreeView(); Ticker.Tock ("TorrentTreeview"); Ticker.Tick (); BuildPeerTreeView(); Ticker.Tock ("PeerTreeview"); Ticker.Tick (); BuildFileTreeView(); Ticker.Tock ("FileTreeview"); Ticker.Tick (); BuildLabelTreeView(); Ticker.Tock ("Label treeview"); Ticker.Tick (); BuildOptionsPage(); Ticker.Tock ("Options page"); Ticker.Tick (); BuildSpeedsPopup(); Ticker.Tock ("Speeds popup"); Ticker.Tock ("Built all stuff"); GLib.Timeout.Add (1000, new GLib.TimeoutHandler (updateView)); Ticker.Tick (); RestoreInterfaceSettings (); Ticker.Tock ("Restored Interface"); if (SettingsManager.Preferences.UpnpEnabled) portController.Start(); Ticker.Tick (); try{ torrentController.LoadStoredTorrents (); }catch (Exception ex) { Console.WriteLine (ex); Environment.Exit(414); } Ticker.Tock ("Loaded torrents"); Ticker.Tick (); logger.Info ("Restoring labels"); LabelController.Restore (); // Restore previously labeled torrents foreach (Download download in torrentController.Torrents){ foreach (TorrentLabel l in LabelController.Labels) { if (l.TruePaths == null) continue; if (Array.IndexOf <string> (l.TruePaths, download.Manager.Torrent.TorrentPath) < 0) continue; l.AddTorrent (download); } } Ticker.Tock ("Restored labels"); torrentController.Initialise (); folderWatcher = new TorrentFolderWatcher (new DirectoryInfo (SettingsManager.Preferences.ImportLocation)); folderWatcher.TorrentFound += delegate(object o, TorrentWatcherEventArgs e) { GLib.Idle.Add(Event.Wrap ((GLib.IdleHandler) delegate { TorrentFound (o, e); return false; })); }; torrentController.SelectionChanged += delegate { updateToolBar (); }; torrentController.ShouldRemove += HandleShouldRemove; if (SettingsManager.Preferences.ImportEnabled) { logger.Info ("Starting import folder watcher"); folderWatcher.Start (); } logger.Info ("Starting RSS manager"); rssManagerController = new RssManagerController(SettingsManager.EngineSettings); rssManagerController.TorrentFound += delegate(object sender, TorrentRssWatcherEventArgs e) { string savePath = e.Filter == null ? SettingsManager.EngineSettings.SavePath : e.Filter.SavePath; try { LoadTorrent(e.Item.Link, true, false, false, null, savePath, true); } catch { logger.Error("RSS Manager: Unable to add - " + e.Item.Title); } }; logger.Info ("Started RSS manager"); rssManagerController.StartWatchers(); AddGConfListeners (); updateToolBar (); ShowAll(); }
internal void AddWatcher(string line) { TorrentFolderWatcher watcher = new TorrentFolderWatcher(line, "*.torrent"); watcher.TorrentFound += TorrentFound; watcher.ForceScan(); watcher.Start(); watchers.Add(line, watcher); }