⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 piecepickerimpl.java

📁 基于JXTA开发平台的下载软件开发源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                    if (!dmPiece.isDone())
                    {
                        for (int j =0; j <pePiece.getNbBlocks(); j++ )
                        {
                            endGameModeChunks.add(new EndGameModeChunk(pePiece, j));
                        }
                    }
                } else
                {
                    for (int j =0; j <written.length; j++ )
                    {
                        if (!written[j])
                            endGameModeChunks.add(new EndGameModeChunk(pePiece, j));
                    }
                }
            }
        } finally
        {
            endGameModeChunks_mon.exit();
        }
    }

    public boolean isInEndGameMode()
	{
		return endGameMode;
	}
	
    /** adds every block from the piece to the list of chuncks to be selected for egm requesting
     * 
     */ 
	public void addEndGameChunks(final PEPiece pePiece)
	{
		if (!endGameMode)
			return;
		try
		{
			endGameModeChunks_mon.enter();
			int nbChunks =pePiece.getNbBlocks();
			for (int i =0; i <nbChunks; i++ )
			{
				endGameModeChunks.add(new EndGameModeChunk(pePiece, i));
			}
		} finally
		{
			endGameModeChunks_mon.exit();
		}
	}

    /** adds blocks from the piece that are neither downloaded nor written to the list
     * of  chuncks to be selected for egm requesting
     */ 
	public void addEndGameBlocks(final PEPiece pePiece)
	{
		if (!endGameMode ||pePiece ==null)
			return;
		final DiskManagerPiece dmPiece =pePiece.getDMPiece();
		final int nbChunks =pePiece.getNbBlocks();
		try
		{
			endGameModeChunks_mon.enter();
			for (int i =0; i <nbChunks; i++ )
			{
				if (!pePiece.isDownloaded(i) &&!dmPiece.isWritten(i))
                    endGameModeChunks.add(new EndGameModeChunk(pePiece, i));
			}
		} finally
		{
			endGameModeChunks_mon.exit();
		}
	}

    protected int findPieceInEndGameMode(final PEPeerTransport pt, final int wants)
    {
        if (pt ==null ||wants <=0 ||pt.getPeerState() !=PEPeer.TRANSFERING)
            return 0;
        // Ok, we try one, if it doesn't work, we'll try another next time
        try
        {
            endGameModeChunks_mon.enter();

            final int nbChunks =endGameModeChunks.size();
            if (nbChunks >0)
            {
                final int random =RandomUtils.generateRandomIntUpto(nbChunks);
                final EndGameModeChunk chunk =(EndGameModeChunk) endGameModeChunks.get(random);
                final int pieceNumber =chunk.getPieceNumber();
                if (dmPieces[pieceNumber].isWritten(chunk.getBlockNumber()))
                {
                    endGameModeChunks.remove(chunk);
                    return 0;
                }
                if (pt.isPieceAvailable(pieceNumber))
                {
                    PEPiece pePiece =peerControl.getPiece(pieceNumber);
                    if (pePiece !=null)
                    {
                        if (pt.request(pieceNumber, chunk.getOffset(), chunk.getLength()))
                        {
                            pePiece.setRequested(pt, chunk.getBlockNumber());
                            pt.setLastPiece(pieceNumber);
                            return 1;
                        }
                    }
                    // System.out.println("End Game Mode :: Piece is null : chunk remove !!!NOT REQUESTED!!!" +
                    // chunk.getPieceNumber() + ":" + chunk.getOffset() + ":" + chunk.getLength());
                    return 0;
                }
            }
        } finally
        {
            endGameModeChunks_mon.exit();
        }
        return 0;
    }
    
	public void removeFromEndGameModeChunks(final int pieceNumber, final int offset)
	{
		try
		{
			endGameModeChunks_mon.enter();

			Iterator iter =endGameModeChunks.iterator();
			while (iter.hasNext())
			{
				EndGameModeChunk chunk =(EndGameModeChunk) iter.next();
				if (chunk.equals(pieceNumber, offset))
					iter.remove();
			}
		} finally
		{
			endGameModeChunks_mon.exit();
		}
	}
	
	public void clearEndGameChunks()
	{
		if (!endGameMode)
			return;
		try
		{
			endGameModeChunks_mon.enter();
			endGameModeChunks.clear();
			endGameMode =false;
		} finally
		{
			endGameModeChunks_mon.exit();
		}
	}
	
	
	/**
	 * An instance of this listener is registered with peerControl
	 * Through this, we learn of peers joining and leaving
	 * and attach/detach listeners to them
	 */
	private class PEPeerManagerListenerImpl
		implements PEPeerManagerListener
	{
		public void peerAdded(final PEPeerManager manager, PEPeer peer )
		{
			PEPeerListenerImpl peerListener;
			peerListener =(PEPeerListenerImpl)peerListeners.get(peer);
			if (peerListener ==null)
			{
				peerListener =new PEPeerListenerImpl();
				peerListeners.put(peer, peerListener);
			}
			peer.addListener(peerListener);
		}
		
		public void peerRemoved(final PEPeerManager manager, PEPeer peer)
		{
			// remove this listener from list of listeners and from the peer
			final PEPeerListenerImpl peerListener =(PEPeerListenerImpl)peerListeners.remove(peer);
			peer.removeListener(peerListener);
		}
	}
	
	/**
	 * An instance of this listener is registered with each peer
	 */
	private class PEPeerListenerImpl
		implements PEPeerListener
	{
		public void stateChanged(PEPeer peer, final int newState)
		{
            /*
			switch (newState)
			{
				case PEPeer.CONNECTING:
					return;
				
				case PEPeer.HANDSHAKING:
					return;
				
				case PEPeer.TRANSFERING:
					return;
				
				case PEPeer.CLOSING:
					return;
				
				case PEPeer.DISCONNECTED:
					return;
			}
            */
		}
		
		public void sentBadChunk(final PEPeer peer, final int piece_num, final int total_bad_chunks )
		{
			/* nothing to do here */
		}
		
		public void addAvailability(final PEPeer peer, final BitFlags peerHavePieces)
		{
			if (peerHavePieces ==null ||peerHavePieces.nbSet <=0)
				return;
			try
			{	availabilityMon.enter();
				if ( availabilityAsynch == null ){
					availabilityAsynch = (int[])availability.clone();
				}
				for (int i =peerHavePieces.start; i <=peerHavePieces.end; i++)
				{
					if ( peerHavePieces.flags[i] ){
					++availabilityAsynch[i];
				}
				}
				availabilityChange++;
			} finally {availabilityMon.exit();}
		}

        /**
         * Takes away the given pieces from global availability
         * @param PEPeer peer this is about
         * @param peerHasPieces BitFlags of the pieces
         */
		public void removeAvailability(final PEPeer peer, final BitFlags peerHavePieces)
		{
			if (peerHavePieces ==null ||peerHavePieces.nbSet <=0)
				return;
			try
			{	availabilityMon.enter();
				if (availabilityAsynch ==null)
                {
                    availabilityAsynch =(int[]) availability.clone();
                }
                for (int i =peerHavePieces.start; i <=peerHavePieces.end; i++)
                {
                    if (peerHavePieces.flags[i])
                    {
                        if (availabilityAsynch[i] >(dmPieces[i].isDone() ?1 :0))
                            --availabilityAsynch[i];
                        else
                            availabilityDrift++;
                    }
                }
                availabilityChange++;
			} finally {availabilityMon.exit();}
		}
	}
	
	/**
	 * An instance of this listener is registered with peerControl
	 * @author MjrTom
	 */
	private class DiskManagerListenerImpl
		implements DiskManagerListener
	{
		public void stateChanged(int oldState, int newState)
		{
			//starting torrent
		}

		public void filePriorityChanged(DiskManagerFileInfo file)
		{
			// record that user-based priorities changed
			filePriorityChange++;	// this is a user's priority change event
			
			// only need to re-calc Needed on file's pieces; priority is calculated seperatly
			boolean foundPieceToDownload =false;
			// if didn't have anything to do before, now only need to check if we need
			// to DL from this file, but if had something to do before,
			// must rescan all pieces to see if now nothing to do
			final int startI;
			final int endI;
			if (hasNeededUndonePiece)
			{
				startI =0;
				endI =nbPieces;
			} else
			{
				startI =file.getFirstPieceNumber();
				endI =file.getLastPieceNumber() +1;
			}
			for (int i =startI; i <endI; i++)
			{
				final DiskManagerPiece dmPiece =dmPieces[i];
				if (!dmPiece.isDone())
					foundPieceToDownload |=dmPiece.calcNeeded();
			}
			if (foundPieceToDownload)
			{
                if (!hasNeededUndonePiece)
                {
                    hasNeededUndonePiece =true;
                    neededUndonePieceChange++;
                }
			} else if (hasNeededUndonePiece)
            {
                hasNeededUndonePiece =false;
                neededUndonePieceChange++;
            }
		}
		
		
		public void pieceDoneChanged(DiskManagerPiece dmPiece)
		{
			int pieceNumber =dmPiece.getPieceNumber();
			if (dmPiece.isDone())
			{
				addHavePiece(pieceNumber);
				nbPiecesDone++;
                if (nbPiecesDone >=nbPieces)
                    checkDownloadablePiece();
			}else
			{
                try
                {   availabilityMon.enter();
                    if ( availabilityAsynch == null ){
                        availabilityAsynch = (int[])availability.clone();
                    }
                    if (availabilityAsynch[pieceNumber] >0)
                        --availabilityAsynch[pieceNumber];
                    else
                        availabilityDrift++;
                    availabilityChange++;
                } finally {availabilityMon.exit();}
				nbPiecesDone--;
				if (dmPiece.calcNeeded() &&!hasNeededUndonePiece)
				{
					hasNeededUndonePiece =true;
					neededUndonePieceChange++;
				}
			}
		}

		public void fileAccessModeChanged(DiskManagerFileInfo file, int old_mode, int new_mode)
		{
			//file done (write to read)
			//starting to upload from the file (read to write)
		}
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -