Exemple #1
0
        public static int DecideNextPiece(DownloadStrategyManager manager, Peer peer)
        {
            int pieceId        = -1;
            int lowestNumPeers = -1;

            // find the rarest piece in the swarm which is owned by the peer
            for (int i = 0; i < manager.PieceCounter.PieceCount; ++i)
            {
                // make sure the peer owns the piece, the piece hasn't already been downloaded,
                // has at least one peer and isn't in the process of being downloaded
                // by another peer
                if (!manager.Torrent.DownloadFile.Bitfield.Get(i) && manager.PieceCounter[i] > 0 && !manager.IsPieceDownloading(i))
                {
                    if (peer != null && !peer.BitField.Get(i))
                    {
                        continue;
                    }

                    // see if the piece has the lowest number of peers so far
                    if (manager.PieceCounter[i] < lowestNumPeers || lowestNumPeers == -1)
                    {
                        lowestNumPeers = manager.PieceCounter[i];
                        pieceId        = i;
                    }
                }
            }

            return(pieceId);
        }
Exemple #2
0
 public static bool DoesPeerHavePieceWeDont(DownloadStrategyManager manager, Peer peer)
 {
     for (int i = 0; i < manager.Torrent.Metainfo.PieceCount; ++i)
     {
         if (peer.BitField.Get(i) && !manager.Torrent.DownloadFile.Bitfield.Get(i))
         {
             return(true);
         }
     }
     return(false);
 }
Exemple #3
0
        /// <summary>Contructs a torrent using the metainfo filename</summary>
        /// <param name="metafilename">Filename of the metainfo file</param>
        internal Torrent(Session session, string metafilename)
        {
            this.mSession          = session;
            this.infofile          = new MetainfoFile(metafilename);
            this.downloadFile      = new DownloadFile(infofile);
            this.peerManager       = new PeerManager();
            this.mDownloadStrategy = new DownloadStrategyManager(this);
            this.uploadManager     = new UploadManager(infofile, downloadFile);
            this.tp = new TrackerProtocol(this, infofile, downloadFile);

            //		this.downloadManager.PieceFinished += new PieceFinishedCallback(downloadManager_PieceFinished);
            this.uploadManager.PieceSectionFinished += new PieceSectionFinishedCallback(uploadManager_PieceSectionFinished);

            this.tp.TrackerUpdate += new TrackerUpdateCallback(tp_TrackerUpdate);
        }
Exemple #4
0
 public static void SetInterestedOnPeerIfNecessary(DownloadStrategyManager manager, Peer peer, int pieceId)
 {
     if (pieceId >= 0)
     {
         // new piece came in
         if (!manager.Torrent.DownloadFile.Bitfield.Get(pieceId))
         {
             peer.AmIInterested = true;
         }
     }
     else
     {
         // whole bitfield has changed
         if (DoesPeerHavePieceWeDont(manager, peer))
         {
             peer.AmIInterested = true;
         }
     }
 }
Exemple #5
0
		/// <summary>Contructs a torrent using the metainfo filename</summary>
		/// <param name="metafilename">Filename of the metainfo file</param>
		internal Torrent( Session session, string metafilename )
		{
			this.mSession = session;
			this.infofile = new MetainfoFile(metafilename);
			this.downloadFile = new DownloadFile(infofile);
			this.peerManager = new PeerManager();
			this.mDownloadStrategy = new DownloadStrategyManager( this );
			this.uploadManager = new UploadManager(infofile, downloadFile);
			this.tp = new TrackerProtocol(this, infofile, downloadFile);

	//		this.downloadManager.PieceFinished += new PieceFinishedCallback(downloadManager_PieceFinished);
			this.uploadManager.PieceSectionFinished += new PieceSectionFinishedCallback(uploadManager_PieceSectionFinished);

			this.tp.TrackerUpdate += new TrackerUpdateCallback(tp_TrackerUpdate);
		}
Exemple #6
0
 public static void SetInterestedOnPeerIfNecessary(DownloadStrategyManager manager, Peer peer)
 {
     SetInterestedOnPeerIfNecessary(manager, peer, -1);
 }
Exemple #7
0
        public static int CalculatePieceSectionLength(DownloadStrategyManager manager, int pieceId, int begin)
        {
            int pieceDataLeft = manager.Torrent.Metainfo.GetPieceLength(pieceId) - begin;

            return(pieceDataLeft < Config.MaxRequestSize ? pieceDataLeft : Config.MaxRequestSize);
        }
Exemple #8
0
 public DefaultDownloadStrategy(DownloadStrategyManager manager)
 {
     this.mManager         = manager;
     this.mEndGameStrategy = new EndGameDownloadStrategy(manager);
     this.mManager.AddNewStrategy(this.mEndGameStrategy);
 }
Exemple #9
0
 public EndGameDownloadStrategy(DownloadStrategyManager manager)
 {
     mManager = manager;
 }