FileDocCategorySizeDatePackage
PeerExchangerItem.javaAPI DocAzureus 3.0.3.46598Mon Oct 16 10:09:14 BST 2006com.aelitis.azureus.core.peermanager.peerdb

PeerExchangerItem

public class PeerExchangerItem extends Object

Fields Summary
public static final int
MAX_PEERS_PER_VOLLEY
private static final int
MAX_KNOWN_PER_PEER
private final PeerDatabase
parent_db
private final PeerItem
base_peer
private final LinkedList
connections_added
private final LinkedList
connections_dropped
private final HashMap
connected_peers
private final org.gudy.azureus2.core3.util.AEMonitor
peers_mon
private boolean
maintain_peers_state
private final Helper
helper
Constructors Summary
protected PeerExchangerItem(PeerDatabase parent_db, PeerItem peer, Helper helper)

  

           
    this.parent_db = parent_db;
    this.base_peer = peer;
    this.helper = helper;
  
Methods Summary
public voidaddConnectedPeer(PeerItem peer)
Add peer info obtained via peer exchange.

param
peer to add

    try{  peers_mon.enter();
      if( !maintain_peers_state )  return;

      int max_cache_size = PeerUtils.MAX_CONNECTIONS_PER_TORRENT;
      if( max_cache_size < 1 || max_cache_size > MAX_KNOWN_PER_PEER )  max_cache_size = MAX_KNOWN_PER_PEER;

      if( connected_peers.size() < max_cache_size ) {
        connected_peers.put( peer, null );
      }
    }
    finally{  peers_mon.exit();  }
  
public voiddestroy()

    parent_db.deregisterPeerConnection( base_peer );
    
    try{  peers_mon.enter();
      connections_added.clear();
      connections_dropped.clear();
      connected_peers.clear();
    }
    finally{  peers_mon.exit();  }
  
public voiddisableStateMaintenance()
Clears all current peer state records and stops any future state maintenance.

    try{  peers_mon.enter();
      maintain_peers_state = false;
      connections_added.clear();
      connections_dropped.clear();
      connected_peers.clear();
    }
    finally{  peers_mon.exit();  }
  
public voiddropConnectedPeer(PeerItem peer)
Remove peer info obtained via peer exchange.

param
peer to remove

    try{  peers_mon.enter();

      connected_peers.remove( peer );
    }
    finally{  peers_mon.exit();  }
  
protected PeerItemgetBasePeer()

 return base_peer; 
protected PeerItem[]getConnectedPeers()

    try{  peers_mon.enter();
    
      PeerItem[] peers = new PeerItem[ connected_peers.size() ];
      connected_peers.keySet().toArray( peers );
      return peers;
    }
    finally{  peers_mon.exit();  }
  
protected com.aelitis.azureus.core.peermanager.peerdb.PeerExchangerItem$HelpergetHelper()

  return helper;  
public PeerItem[]getNewlyAddedPeerConnections()
Get the list of peer connections added since this method was last called.

return
new peer connections

    try{  peers_mon.enter();
      if( connections_added.isEmpty() )  return null;
      
      int num_to_send = connections_added.size() > MAX_PEERS_PER_VOLLEY ? MAX_PEERS_PER_VOLLEY : connections_added.size();

      PeerItem[] peers = new PeerItem[ num_to_send ];
      
      for( int i=0; i < num_to_send; i++ ) {
        peers[i] = (PeerItem)connections_added.removeFirst();       
      }

      return peers;
    }
    finally{  peers_mon.exit();  }
  
public PeerItem[]getNewlyDroppedPeerConnections()
Get the list of peer connections dropped since this method was last called.

return
dropped peer connections

    try{  peers_mon.enter();
      if( connections_dropped.isEmpty() )  return null;
      
      int num_to_send = connections_dropped.size() > MAX_PEERS_PER_VOLLEY ? MAX_PEERS_PER_VOLLEY : connections_dropped.size();

      PeerItem[] peers = new PeerItem[ num_to_send ];
      
      for( int i=0; i < num_to_send; i++ ) {
        peers[i] = (PeerItem)connections_dropped.removeFirst();       
      }
      return peers;
    }
    finally{  peers_mon.exit();  }
  
protected booleanisConnectedToPeer(PeerItem peer)

    try{  peers_mon.enter();
    
      return connected_peers.containsKey( peer );
    }
    finally{  peers_mon.exit();  }
  
protected voidnotifyAdded(PeerItem peer_connection)

    try{  peers_mon.enter();
      if( !maintain_peers_state )  return;
    
      if( !connections_dropped.contains( peer_connection ) ) {
        if( !connections_added.contains( peer_connection ) ) {
          connections_added.addLast( peer_connection );  //register new add
        }
      }
      else {  //was dropped and then re-added
        connections_dropped.remove( peer_connection );  //pull drop and ignore add
      }
    }
    finally{  peers_mon.exit();  }
  
protected voidnotifyDropped(PeerItem peer_connection)

    try{  peers_mon.enter();
      if( !maintain_peers_state )  return;
      
      if( !connections_added.contains( peer_connection ) ) {
        if( !connections_dropped.contains( peer_connection ) ) {
          connections_dropped.addLast( peer_connection );  //register new drop
        }
      }
      else {  //was added and then re-dropped
        connections_added.remove( peer_connection );  //pull add and ignore drop
      }
    }
    finally{  peers_mon.exit();  }
  
public voidseedStatusChanged()

	  parent_db.seedStatusChanged( this );