public BitTorrentManager(BitTorrentCache bittorrentCache, string selfNameSpace, DictionaryServiceProxy dhtProxy, DictionaryServiceTracker dhtTracker, ClientEngine clientEngine, TorrentSettings torrentDefaults, TorrentHelper torrentHelper, bool startSeedingAtStartup) { _bittorrentCache = bittorrentCache; SelfNameSpace = selfNameSpace; _dictProxy = dhtProxy; _dictTracker = dhtTracker; _torrentDefaults = torrentDefaults; _startSeedingAtStartup = startSeedingAtStartup; RegisterClientEngineEventHandlers(clientEngine); _clientEngine = clientEngine; _torrentHelper = torrentHelper; try { _fastResumeData = BEncodedValue.Decode <BEncodedDictionary>( File.ReadAllBytes(_bittorrentCache.FastResumeFilePath)); } catch { _fastResumeData = new BEncodedDictionary(); } // CacheRegistry is created here because the default cache registry file path is // defined here. CacheRegistry = new CacheRegistry(_bittorrentCache.CacheRegistryFilePath, selfNameSpace); CacheRegistry.LoadCacheDir(_bittorrentCache.DownloadsDirPath); }
public BitTorrentManager(BitTorrentCache bittorrentCache, string selfNameSpace, DictionaryServiceProxy dhtProxy, DictionaryServiceTracker dhtTracker, ClientEngine clientEngine, TorrentSettings torrentDefaults, TorrentHelper torrentHelper, bool startSeedingAtStartup) { _bittorrentCache = bittorrentCache; SelfNameSpace = selfNameSpace; _dictProxy = dhtProxy; _dictTracker = dhtTracker; _torrentDefaults = torrentDefaults; _startSeedingAtStartup = startSeedingAtStartup; RegisterClientEngineEventHandlers(clientEngine); _clientEngine = clientEngine; _torrentHelper = torrentHelper; try { _fastResumeData = BEncodedValue.Decode<BEncodedDictionary>( File.ReadAllBytes(_bittorrentCache.FastResumeFilePath)); } catch { _fastResumeData = new BEncodedDictionary(); } // CacheRegistry is created here because the default cache registry file path is // defined here. CacheRegistry = new CacheRegistry(_bittorrentCache.CacheRegistryFilePath, selfNameSpace); CacheRegistry.LoadCacheDir(_bittorrentCache.DownloadsDirPath); }
public PieceLevelTorrentManager(BitTorrentManager manager, BitTorrentCache bittorrentCache, DictionaryServiceProxy dhtProxy, TorrentHelper torrentHelper, int pieceInfoServerPort) { _manager = manager; _dhtProxy = dhtProxy; _bittorrentCache = bittorrentCache; _torrentHelper = torrentHelper; _pieceInfoServerPort = pieceInfoServerPort; _pieceInfoServer = new HttpPieceInfoServer(_pieceInfoServerPort, this); }
public PieceLevelTorrentManager(BitTorrentManager manager, BitTorrentCache bittorrentCache, DictionaryServiceProxy dhtProxy, TorrentHelper torrentHelper, int pieceInfoServerPort) { _manager = manager; _dhtProxy = dhtProxy; _bittorrentCache = bittorrentCache; _torrentHelper = torrentHelper; _pieceInfoServerPort = pieceInfoServerPort; _pieceInfoServer = new HttpPieceInfoServer(_pieceInfoServerPort, this); }
public bool TryReadOrDownloadTorrent(string nameSpace, string name, DictionaryServiceProxy proxy, out byte[] torrent) { try { torrent = ReadOrDownloadTorrent(nameSpace, name, proxy); return(true); } catch (DictionaryServiceException) { torrent = null; return(false); } }
/// <summary> /// Reads torrent from file system if exists or download it from DHT and writes it /// to file system. /// </summary> /// <param name="nameSpace">The name space.</param> /// <param name="name">The name.</param> /// <param name="proxy">The proxy.</param> /// <returns>Torrent bytes.</returns> /// <exception cref="DictionaryServiceException">When such a torrent isn't available. /// </exception> public byte[] ReadOrDownloadTorrent(string nameSpace, string name, DictionaryServiceProxy proxy) { var torrentPath = _bittorrentCache.GetTorrentFilePath(nameSpace, name); if (File.Exists(torrentPath)) { Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format("Torrent file already exists. Reading it.")); return(File.ReadAllBytes(torrentPath)); } else { Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format("Torrent file doesn't exist. Downloading it.")); byte[] torrentKey = ServiceUtil.GetDictKeyBytes(nameSpace, name); IList <byte[]> urls; urls = proxy.GetUrlsToDownloadTorrent(torrentKey).ToList <byte[]>(); // Count should > 0 otherwise it's an exception. int numServers = urls.Count; var rnd = new Random(); var webClient = new WebClient(); byte[] torrentBytes = null; do { int index = rnd.Next(numServers); byte[] urlBytes = urls[index]; urls.RemoveAt(index); string urlToTry = Encoding.UTF8.GetString(urlBytes); try { Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format( "Trying to download torrent from the #{0} peer in the {1}-item list.", index, numServers)); torrentBytes = webClient.DownloadData(urlToTry); break; } catch (WebException ex) { Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format( "Failed to download torrent from this peer: {0}. Exception: {1}", urlToTry, ex)); } } while (--numServers > 0); if (torrentBytes == null) { throw new DictionaryServiceException(string.Format( "No such torrent file: {0} available.", name)); } IOUtil.PrepareParentDirForPath(torrentPath); File.WriteAllBytes(torrentPath, torrentBytes); return(torrentBytes); } }
/// <summary> /// Constructs DhtTracker using the given DhtSerivceProxy instance. /// </summary> /// <param name="dhtProxy"></param> public DictionaryServiceTracker(DictionaryServiceProxy dhtProxy, string listeningPrefix) { _dictListener = new DictionaryServiceTrackerListener(dhtProxy); ListeningPrefix = listeningPrefix; _httpListener = new HttpListener(ListeningPrefix); Logger.WriteLineIf(LogLevel.Info, _log_props, string.Format( "DictionaryServiceTracker starting at: {0}", ListeningPrefix)); // Subscribe the HttpListener events to do our nifty stuff. _httpListener.AnnounceReceived += this.OnAnnounceReceived; _httpListener.ScrapeReceived += this.OnScrapeReceived; _tracker = new Tracker(); // This also subscribes the same above 2 events but does this AFTER them, // so HttpListener invokes DHT operations first. _tracker.RegisterListener(_httpListener); // And... the events from DHT _tracker.RegisterListener(_dictListener); }
/// <summary> /// Constructs DhtTracker using the given DhtSerivceProxy instance. /// </summary> /// <param name="dhtProxy"></param> public DictionaryServiceTracker(DictionaryServiceProxy dhtProxy, string listeningPrefix) { _dictListener = new DictionaryServiceTrackerListener(dhtProxy); ListeningPrefix = listeningPrefix; _httpListener = new HttpListener(ListeningPrefix); Logger.WriteLineIf(LogLevel.Info, _log_props, string.Format( "DictionaryServiceTracker starting at: {0}", ListeningPrefix)); // Subscribe the HttpListener events to do our nifty stuff. _httpListener.AnnounceReceived += this.OnAnnounceReceived; _httpListener.ScrapeReceived += this.OnScrapeReceived; _tracker = new Tracker(); // This also subscribes the same above 2 events but does this AFTER them, // so HttpListener invokes DHT operations first. _tracker.RegisterListener(_httpListener); // And... the events from DHT _tracker.RegisterListener(_dictListener); }
public bool TryReadOrDownloadTorrent(string nameSpace, string name, DictionaryServiceProxy proxy, out byte[] torrent) { try { torrent = ReadOrDownloadTorrent(nameSpace, name, proxy); return true; } catch (DictionaryServiceException) { torrent = null; return false; } }
/// <summary> /// Reads torrent from file system if exists or download it from DHT and writes it /// to file system. /// </summary> /// <param name="nameSpace">The name space.</param> /// <param name="name">The name.</param> /// <param name="proxy">The proxy.</param> /// <returns>Torrent bytes.</returns> /// <exception cref="DictionaryServiceException">When such a torrent isn't available. /// </exception> public byte[] ReadOrDownloadTorrent(string nameSpace, string name, DictionaryServiceProxy proxy) { var torrentPath = _bittorrentCache.GetTorrentFilePath(nameSpace, name); if (File.Exists(torrentPath)) { Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format("Torrent file already exists. Reading it.")); return File.ReadAllBytes(torrentPath); } else { Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format("Torrent file doesn't exist. Downloading it.")); byte[] torrentKey = ServiceUtil.GetDictKeyBytes(nameSpace, name); IList<byte[]> urls; urls = proxy.GetUrlsToDownloadTorrent(torrentKey).ToList<byte[]>(); // Count should > 0 otherwise it's an exception. int numServers = urls.Count; var rnd = new Random(); var webClient = new WebClient(); byte[] torrentBytes = null; do { int index = rnd.Next(numServers); byte[] urlBytes = urls[index]; urls.RemoveAt(index); string urlToTry = Encoding.UTF8.GetString(urlBytes); try { Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format( "Trying to download torrent from the #{0} peer in the {1}-item list.", index, numServers)); torrentBytes = webClient.DownloadData(urlToTry); break; } catch (WebException ex) { Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format( "Failed to download torrent from this peer: {0}. Exception: {1}", urlToTry, ex)); } } while (--numServers > 0); if (torrentBytes == null) { throw new DictionaryServiceException(string.Format( "No such torrent file: {0} available.", name)); } IOUtil.PrepareParentDirForPath(torrentPath); File.WriteAllBytes(torrentPath, torrentBytes); return torrentBytes; } }
public DictionaryServiceTrackerListener(DictionaryServiceProxy proxy) { _proxy = proxy; }
public DictionaryServiceTrackerListener(DictionaryServiceProxy proxy) { _proxy = proxy; }