Package org.gudy.azureus2.core3.peer

Examples of org.gudy.azureus2.core3.peer.PEPeer


    long[] bests = new long[ max_to_unchoke ]//ensure we never pick more slots than allowed to unchoke
   
   
    //get all the currently unchoked peers
    for( int i=0; i < all_peers.size(); i++ ) {
      PEPeer peer = all_peers.get( i );
     
      if( !peer.isChokedByMe() ) {
        if( UnchokerUtil.isUnchokable( peer, true ) ) {
          unchokes.add( peer );
          if( peer.isOptimisticUnchoke() ) {
            optimistic_unchokes.add( peer );
          }
        }
        else //should be immediately choked
          chokes.add( peer );
        }
      }
    }
      
   
    if( !force_refresh ) {  //ensure current optimistic unchokes remain unchoked
      for( int i=0; i < optimistic_unchokes.size(); i++ ) {
      PEPeer peer = optimistic_unchokes.get( i );
       
        if( i < max_optimistic ) {
          best_peers.add( peer )//add them to the front of the "best" list
        }
        else { //too many optimistics
          peer.setOptimisticUnchoke( false );
        }
      }
    }
   
   
    //fill slots with peers who we are currently downloading the fastest from
    int start_pos = best_peers.size();
    for( int i=0; i < all_peers.size(); i++ ) {
      PEPeer peer = all_peers.get( i );

      if( peer.isInteresting() && UnchokerUtil.isUnchokable( peer, false ) && !best_peers.contains( peer ) ) {  //viable peer found
        long rate = peer.getStats().getSmoothDataReceiveRate();
        if( rate > 256 ) {  //filter out really slow peers
          UnchokerUtil.updateLargestValueFirstSort( rate, bests, peer, best_peers, start_pos );
        }
      }
    }
   

    //if we havent yet picked enough slots
    if( best_peers.size() < max_to_unchoke ) { 
      start_pos = best_peers.size();
     
      //fill the remaining slots with peers that we have downloaded from in the past
      for( int i=0; i < all_peers.size(); i++ ) {
      PEPeer peer = all_peers.get( i );

        if( peer.isInteresting() && UnchokerUtil.isUnchokable( peer, false ) && !best_peers.contains( peer ) ) {  //viable peer found
          long uploaded_ratio = peer.getStats().getTotalDataBytesSent() / (peer.getStats().getTotalDataBytesReceived() + (DiskManager.BLOCK_SIZE-1));
          //make sure we haven't already uploaded several times as much data as they've sent us
          if( uploaded_ratio <3) {
            UnchokerUtil.updateLargestValueFirstSort( peer.getStats().getTotalDataBytesReceived(), bests, peer, best_peers, start_pos );
         
        }
      }
    }
   
   
    if( force_refresh ) {
      //make space for new optimistic unchokes
      while( best_peers.size() > max_to_unchoke - max_optimistic ) {
        best_peers.remove( best_peers.size() - 1 );
      }
    }
   
   
    //if we still have remaining slots
    while( best_peers.size() < max_to_unchoke ) {
      PEPeer peer = UnchokerUtil.getNextOptimisticPeer( all_peers, true, true )//just pick one optimistically
      if( peer == null break//no more new unchokes avail
     
      if( !best_peers.contains( peer ) ) {
        best_peers.add( peer );
        peer.setOptimisticUnchoke( true );
      }
      else {
        //we're here because the given optimistic peer is already "best", but is choked still,
        //which means it will continually get picked by the getNextOptimisticPeer() method,
        //and we'll loop forever if there are no other peers to choose from
        peer.sendUnChoke()//send unchoke immediately, so it won't get picked optimistically anymore
        //all_peers.remove( peer );  //remove from all_peers list, so it won't get picked optimistically anymore    //TODO
      }
    }
     

    //update chokes
    for( Iterator<PEPeer> it = unchokes.iterator(); it.hasNext(); ) {
      PEPeer peer = it.next();

      if( !best_peers.contains( peer ) ) {  //should be choked
        if( best_peers.size() < max_to_unchoke ) {  //but there are still slots needed (no optimistics avail), so don't bother choking them
          best_peers.add( peer );
        }
        else {
          chokes.add( peer );
          it.remove();
        }
      }
    }
   
    //update unchokes
    for( int i=0; i < best_peers.size(); i++ ) {
      PEPeer peer = best_peers.get( i );
     
      if( !unchokes.contains( peer ) ) {
        unchokes.add( peer );
      }
    }
View Full Code Here


 

  public static void main(String[] args) {
    TreeMap counts = new TreeMap( new Comparator(){
      public int compare( Object  o1, Object  o2 ) {
        PEPeer peer1 = (PEPeer)o1;
        PEPeer peer2 = (PEPeer)o2;
       
        long score1 = peer1.getStats().getTotalDataBytesSent() - peer1.getStats().getTotalDataBytesReceived();
        long score2 = peer2.getStats().getTotalDataBytesSent() - peer2.getStats().getTotalDataBytesReceived();
       
        return (int)(score1 - score2);
      }
    });
   
    ArrayList test_peers = generateTestPeers();
    
    for( int i=0; i < TEST_ROUNDS; i++ ) {
      if( i % 100000 == 0 System.out.println( "round=" +i );
     
      PEPeer opt_peer = UnchokerUtil.getNextOptimisticPeer( test_peers, true, false );
      Integer count = (Integer)counts.get( opt_peer );
      if( count == null count = new Integer( 0 );
      counts.put( opt_peer, new Integer( count.intValue() + 1 ) );
    }
   
   
    int max_picked = 0;
   
    for( Iterator it = counts.values().iterator(); it.hasNext(); ) {
      int count = ((Integer)it.next()).intValue();
      if( count > max_picked max_picked = count;
    }
   
    int pos = 0;
   
    for( Iterator it = counts.entrySet().iterator(); it.hasNext(); ) {
      Map.Entry entry = (Map.Entry)it.next();
      PEPeer peer = (PEPeer)entry.getKey();
      int count = ((Integer)entry.getValue()).intValue();
     
      long score = peer.getStats().getTotalDataBytesSent() - peer.getStats().getTotalDataBytesReceived();
     
      float ratio = (float)peer.getStats().getTotalDataBytesSent() / (peer.getStats().getTotalDataBytesReceived() + 1);

      int percentile = (count *100) / max_picked;     
     
      System.out.println( "[" +pos+ "] score=" +score+ ", ratio=" +ratio+ ", picked=" +count+ "x, percentile=" +percentile+ "%" );
      pos++;
View Full Code Here

    for( int i=0; i < NUM_PEERS_TO_TEST; i++ ) {
      final int bytes_received = RandomUtils.nextInt( BYTE_RANGE );
      final int bytes_sent = RandomUtils.nextInt( BYTE_RANGE );
     
      final PEPeerStats[] f_stats = { null };
      final PEPeer peer = new PEPeer() {
      public InetAddress getAlternativeIPv6() { return null; }
        public void addListener( PEPeerListener listener ){}
        public void removeListener( PEPeerListener listener ){}
        public int getPeerState(){  return PEPeer.TRANSFERING;  }
        public PEPeerManager getManager(){ return null; }
View Full Code Here

    }
 
    //count all the currently unchoked peers
    int num_unchoked = 0;
    for( int i=0; i < all_peers.size(); i++ ) {
      PEPeer peer = all_peers.get( i );
      if( !peer.isChokedByMe() )  num_unchoked++;
    }
   
    //if not enough unchokes
    int needed = max_to_unchoke - num_unchoked;
   
View Full Code Here

   
  int max_optimistic = ((max_to_unchoke - 1) / 5) + 1//one optimistic unchoke for every 5 upload slots
   
    //get all the currently unchoked peers
    for( int i=0; i < all_peers.size(); i++ ) {
      PEPeer peer = all_peers.get( i );
     
      if( !peer.isChokedByMe() ) {
        if( UnchokerUtil.isUnchokable( peer, false ) ) {
          unchokes.add( peer );
        }
        else //should be immediately choked
          chokes.add( peer );
        }
      }
    }
   
   
    //if too many unchokes
    while( unchokes.size() > max_to_unchoke ) {
      chokes.add( unchokes.remove( unchokes.size() - 1 ) );
    }
   
   
    //we only recalculate the uploads when we're forced to refresh the optimistic unchokes
    if( force_refresh ) {     

      //we need to make room for new opt unchokes by finding the "worst" peers
      ArrayList<PEPeer> peers_ordered_by_rate     = new ArrayList<PEPeer>();
      ArrayList<PEPeer> peers_ordered_by_uploaded   = new ArrayList<PEPeer>();
     
      long[] rates = new long[ unchokes.size() ]//0-initialized
      long[] uploaded = new long[ rates.length ]//0-initialized
       
      //calculate factor rankings
      for( int i=0; i < unchokes.size(); i++ ) {
      PEPeer peer = unchokes.get( i );

        long rate = peer.getStats().getDataSendRate();
        if( rate > 256 ) {  //filter out really slow peers
          //calculate reverse order by our upload rate to them
          UnchokerUtil.updateLargestValueFirstSort( rate, rates, peer, peers_ordered_by_rate, 0 );
         
          //calculate order by the total number of bytes we've uploaded to them
          UnchokerUtil.updateLargestValueFirstSort( peer.getStats().getTotalDataBytesSent(), uploaded, peer, peers_ordered_by_uploaded, 0 );
        }
      }

      Collections.reverse( peers_ordered_by_rate )//we want higher rates at the end

      ArrayList<PEPeer> peers_ordered_by_rank = new ArrayList<PEPeer>();
      long[] ranks = new long[ peers_ordered_by_rate.size() ];
      Arrays.fill( ranks, Long.MIN_VALUE );
     
      //combine factor rankings to get best
      for( int i=0; i < unchokes.size(); i++ ) {
      PEPeer peer = unchokes.get( i );
       
        //"better" peers have high indexes (toward the end of each list)
        long rate_factor = peers_ordered_by_rate.indexOf( peer );
        long uploaded_factor = peers_ordered_by_uploaded.indexOf( peer );
       
        if( rate_factor == -1 continue//wasn't downloading fast enough, skip add so it will be choked automatically
       
        long rank_factor = rate_factor + uploaded_factor;
       
        UnchokerUtil.updateLargestValueFirstSort( rank_factor, ranks, peer, peers_ordered_by_rank, 0 );
      }

      //make space for new optimistic unchokes
      while( peers_ordered_by_rank.size() > max_to_unchoke - max_optimistic ) {
        peers_ordered_by_rank.remove( peers_ordered_by_rank.size() - 1 );
      }

      //update choke list with drops and unchoke list with optimistic unchokes
      ArrayList<PEPeer> to_unchoke = new ArrayList<PEPeer>();
      for( Iterator<PEPeer> it = unchokes.iterator(); it.hasNext(); ) {
      PEPeer peer = it.next();
       
        peer.setOptimisticUnchoke( false );
       
        if( !peers_ordered_by_rank.contains( peer ) ) {  //should be choked
          //we assume that any/all chokes are to be replace by optimistics
          PEPeer optimistic_peer = UnchokerUtil.getNextOptimisticPeer( all_peers, false, false );
         
          if( optimistic_peer != null ) {  //only choke if we've got a peer to replace it with
            chokes.add( peer );
            it.remove();
           
            to_unchoke.add( optimistic_peer );
            optimistic_peer.setOptimisticUnchoke( true );
          }
        }
      }
     
      for( int i=0; i < to_unchoke.size(); i++ ) {
View Full Code Here

   
    ArrayList<PEPeer> buddies = new ArrayList<PEPeer>();
   
    //find all buddies
    for( int i=0; i < all_peers.size(); i++ ) {
      PEPeer peer = all_peers.get( i );
       
      if( peer.isPriorityConnection() && UnchokerUtil.isUnchokable( peer, true ) ) {
        buddies.add( peer );         
      }
    }
   
    //we want to give all connected friends an equal chance if there are more than max_friends allowed
    Collections.shuffle( buddies );
   
    int num_unchoked = 0;
   
    int max = max_buddies > unchokes.size() ? unchokes.size() : max_buddies;
   
    while( num_unchoked < max && !buddies.isEmpty() ) {    //go through unchoke list and replace non-buddy peers with buddy ones
     
      PEPeer peer = unchokes.remove( 0 );     //pop peer from front of unchoke list
     
      if( buddies.remove( peer ) ) {   //peer is already in the buddy list
        unchokes.add( peer )//so insert confirmed buddy peer back into list at the end     
      }
      else //not a buddy, so replace
        PEPeer buddy = buddies.remove( buddies.size() - 1 )//get next buddy
       
        chokes.remove( buddy );    //just in case
       
        unchokes.add( buddy );   //add buddy to back of list     
      }
View Full Code Here

        } catch (DownloadException e) { /* Ignore */
        }
      }
    }
    if (coreDataSource instanceof PEPeer) {
      PEPeer peer = (PEPeer) coreDataSource;
      if (peer != null) {
        pluginDataSource = PeerManagerImpl.getPeerForPEPeer(peer);
      }
    }

View Full Code Here

    // find upload pieces
    ArrayList currentULPieces = new ArrayList();
    ArrayList futureULPieces = new ArrayList();
    PEPeer[] peers = (PEPeer[]) pm.getPeers().toArray(new PEPeer[0]);
    for (int i = 0; i < peers.length; i++) {
      PEPeer peer = peers[i];
      int[] peerRequestedPieces = peer.getIncomingRequestedPieceNumbers();
      if (peerRequestedPieces != null && peerRequestedPieces.length > 0) {
        currentULPieces.add(new Long(peerRequestedPieces[0]));
        for (int j = 1; j < peerRequestedPieces.length; j++) {
          futureULPieces.add(new Long(peerRequestedPieces[j]));
        }
View Full Code Here

  {
    super("publish", null, "Publishing");
  }

  protected boolean performCommand(ConsoleInput ci, DownloadManager dm, List args) {
    TOTorrent torrent = dm.getTorrent();
        if (torrent != null) {
          try {
          TRHost  host = ci.azureus_core.getTrackerHost();
         
          TRHostTorrent  existing = host.getHostTorrent( torrent );
View Full Code Here

  protected boolean performCommand(ConsoleInput ci, DownloadManager dm, List args) {
    TOTorrent torrent = dm.getTorrent();
        if (torrent != null) {
          try {
          TRHost  host = ci.azureus_core.getTrackerHost();
         
          TRHostTorrent  existing = host.getHostTorrent( torrent );
         
          if ( existing == null ){
           
            host.publishTorrent(torrent);
          }else{
            try{
              existing.remove();
             
            }catch( Throwable e ){
View Full Code Here

TOP

Related Classes of org.gudy.azureus2.core3.peer.PEPeer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.