Esempio n. 1
0
 /// <summary>
 /// Adds torrent to tracker thread-safely
 /// </summary>
 public void AddTorrentToTracker(Torrent torrent)
 {
     ITrackable trackable = new InfoHashTrackable(torrent);
       lock (_dictTracker.Tracker) {
     _dictTracker.Tracker.Add(trackable);
       }
 }
Esempio n. 2
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();
        }
Esempio n. 3
0
        void TorrentFound(object o, TorrentWatcherEventArgs e)
        {
            Torrent torrent = null;
            try
            {
                torrent = Torrent.Load(e.TorrentPath);
            }
            catch(Exception ex)
            {
                AddHistory("Could not load: {0}. Reason: {1}", e.TorrentPath, ex.Message);
            }

            InfoHashTrackable trackable = new InfoHashTrackable(torrent);
            tracker.Add(trackable);
        }