📄 trtrackerservertorrentimpl.java
字号:
}
rep_peer.put( "port", new Long( peer.getPort()));
rep_peers.add( rep_peer );
}
}
}
}
}
Map root = new TreeMap(); // user TreeMap to pre-sort so encoding quicker
if ( preprocess_map.size() > 0 ){
root.putAll( preprocess_map );
}
int num_peers_returned = rep_peers.size();
if ( compact ){
byte[] compact_peers = new byte[rep_peers.size()*6];
for ( int i=0;i<num_peers_returned;i++){
Map rep_peer = (Map)rep_peers.get(i);
byte[] ip = (byte[])rep_peer.get( "ip" );
int port = ((Long)rep_peer.get( "port" )).intValue();
int pos = i*6;
System.arraycopy( ip, 0, compact_peers, pos, 4 );
pos += 4;
compact_peers[pos++] = (byte)(port>>8);
compact_peers[pos++] = (byte)(port&0xff);
}
root.put( "peers", compact_peers );
}else{
root.put( "peers", rep_peers );
}
root.put( "interval", new Long( interval ));
root.put( "min interval", new Long( min_interval ));
if ( nat_warning ){
requesting_peer.setNATStatus( TRTrackerServerPeerImpl.NAT_CHECK_FAILED_AND_REPORTED );
root.put(
"warning message",
("Unable to connect to your incoming data port (" + requesting_peer.getIP() + ":" + requesting_peer.getPort() +"). " +
"This will result in slow downloads. Please check your firewall/router settings").getBytes());
}
// also include scrape details
root.put( "complete", new Long( getSeedCount() ));
root.put( "incomplete", new Long( getLeecherCount() ));
root.put( "downloaded", new Long(stats.getCompletedCount()));
if ( add_to_cache ){
announce_cache.put( new Integer((num_peers_returned+9)/10), new announceCacheEntry( root, send_peer_ids, compact ));
}
return( root );
}finally{
this_mon.exit();
}
}
public Map
exportScrapeToMap(
boolean allow_cache )
{
try{
this_mon.enter();
stats.addScrape();
long now = SystemTime.getCurrentTime();
long diff = now - last_scrape_calc_time;
if( allow_cache && last_scrape != null && diff < TRTrackerServerImpl.getScrapeCachePeriod() && !(diff < 0) ){
return( last_scrape );
}
last_scrape = new TreeMap();
last_scrape_calc_time = now;
last_scrape.put( "complete", new Long( getSeedCount()));
last_scrape.put( "incomplete", new Long( getLeecherCount()));
last_scrape.put( "downloaded", new Long(stats.getCompletedCount()));
return( last_scrape );
}finally{
this_mon.exit();
}
}
protected void
checkTimeouts()
{
try{
this_mon.enter();
long now = SystemTime.getCurrentTime();
int new_bad_NAT_count = 0;
try{
peer_list_compaction_suspended = true;
for (int i=0;i<peer_list.size();i++){
TRTrackerServerPeerImpl peer = (TRTrackerServerPeerImpl)peer_list.get(i);
if ( peer == null ){
continue;
}
if ( now > peer.getTimeout()){
removePeer( peer, i );
}else{
if ( peer.isNATStatusBad()){
new_bad_NAT_count++;
}
}
}
}finally{
peer_list_compaction_suspended = false;
}
bad_NAT_count = new_bad_NAT_count;
if ( removed_count > 1000 ){
removed_count = 0;
checkForPeerListCompaction( true );
// rehash
HashMap new_peer_map = new HashMap(peer_map);
HashMap new_peer_reuse_map = new HashMap(peer_reuse_map);
peer_map = new_peer_map;
peer_reuse_map = new_peer_reuse_map;
}else{
checkForPeerListCompaction( false );
}
Iterator it = lightweight_seed_map.values().iterator();
while( it.hasNext()){
lightweightSeed lws = (lightweightSeed)it.next();
if ( now > lws.getTimeout()){
it.remove();
}
}
}finally{
this_mon.exit();
}
}
protected void
checkForPeerListCompaction(
boolean force )
{
if ( peer_list_hole_count > 0 && !peer_list_compaction_suspended ){
if ( force || peer_list_hole_count > peer_map.size()/10 ){
ArrayList new_peer_list = new ArrayList( peer_list.size() - (peer_list_hole_count/2));
int holes_found = 0;
for (int i=0;i<peer_list.size();i++){
Object obj = peer_list.get(i);
if ( obj == null ){
holes_found++;
}else{
new_peer_list.add( obj );
}
}
if( holes_found != peer_list_hole_count ){
Debug.out( "TRTrackerTorrent:compactHoles: count mismatch" );
}
peer_list = new_peer_list;
peer_list_hole_count = 0;
}
}
}
protected void
updateXferStats(
int bytes_in,
int bytes_out )
{
stats.addXferStats( bytes_in, bytes_out );
}
public TRTrackerServerTorrentStats
getStats()
{
return( stats );
}
public int
getPeerCount()
{
return( peer_map.size() + lightweight_seed_map.size());
}
public int
getSeedCount()
{
if ( seed_count < 0 ){
Debug.out( "seed count negative" );
}
return( seed_count + lightweight_seed_map.size());
}
public int
getLeecherCount()
{
// this isn't synchronised so could possible end up negative
int res = peer_map.size() - seed_count;
return( res<0?0:res );
}
public TRTrackerServerPeer[]
getPeers()
{
try{
this_mon.enter();
TRTrackerServerPeer[] res = new TRTrackerServerPeer[peer_map.size()];
peer_map.values().toArray( res );
return( res );
}finally{
this_mon.exit();
}
}
public HashWrapper
getHash()
{
return( hash );
}
public void
addListener(
TRTrackerServerTorrentListener l )
{
listeners.add(l);
if ( deleted ){
l.deleted(this);
}
}
public void
removeListener(
TRTrackerServerTorrentListener l )
{
listeners.remove(l);
}
public void
disableCaching()
{
caching_enabled = false;
}
public boolean
isCachingEnabled()
{
return( caching_enabled );
}
public int
getBadNATPeerCount()
{
return( bad_NAT_count );
}
protected void
delete()
{
deleted = true;
for (int i=0;i<listeners.size();i++){
((TRTrackerServerTorrentListener)listeners.get(i)).deleted(this);
}
}
static class
announceCacheEntry
{
protected Map data;
protected boolean send_peer_ids;
protected boolean compact;
protected long time;
protected
announceCacheEntry(
Map _data,
boolean _send_peer_ids,
boolean _compact )
{
data = _data;
send_peer_ids = _send_peer_ids;
compact = _compact;
time = SystemTime.getCurrentTime();
}
protected boolean
getSendPeerIds()
{
return( send_peer_ids );
}
protected boolean
getCompact()
{
return( compact );
}
protected long
getTime()
{
return( time );
}
protected Map
getData()
{
return( data );
}
}
protected static class
lightweightSeed
{
long timeout;
long last_contact_time;
long uploaded;
byte nat_status;
protected
lightweightSeed(
long _now,
long _timeout,
long _uploaded,
byte _nat_status )
{
last_contact_time = _now;
timeout = _timeout;
uploaded = _uploaded;
nat_status = _nat_status;
}
protected long
getTimeout()
{
return( timeout );
}
protected long
getLastContactTime()
{
return( last_contact_time );
}
protected long
getUploaded()
{
return( uploaded );
}
protected byte
getNATStatus()
{
return( nat_status );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -