📄 trtrackerserverimpl.java
字号:
public TRTrackerServerStats
getStats()
{
return( stats );
}
public void
updateStats(
TRTrackerServerTorrentImpl torrent,
int bytes_in,
int bytes_out )
{
try{
class_mon.enter();
stats.update( bytes_in, bytes_out );
if ( torrent != null ){
torrent.updateXferStats( bytes_in, bytes_out );
}else{
int num = torrent_map.size();
if ( num > 0 ){
// full scrape or error - spread the reported bytes across the torrents
int ave_in = bytes_in/num;
int ave_out = bytes_out/num;
int rem_in = bytes_in-(ave_in*num);
int rem_out = bytes_out-(ave_out*num);
Iterator it = torrent_map.values().iterator();
while(it.hasNext()){
TRTrackerServerTorrentImpl this_torrent = (TRTrackerServerTorrentImpl)it.next();
if ( it.hasNext()){
this_torrent.updateXferStats( ave_in, ave_out );
}else{
this_torrent.updateXferStats( ave_in+rem_in, ave_out+rem_out );
}
}
}
}
}finally{
class_mon.exit();
}
}
protected void
timerLoop()
{
long time_to_go = TIMEOUT_CHECK;
while( !destroyed ){
try{
Thread.sleep( RETRY_MINIMUM_MILLIS );
time_to_go -= RETRY_MINIMUM_MILLIS;
// recalc tracker interval every minute
current_min_poll_interval = COConfigurationManager.getIntParameter("Tracker Poll Interval Min", DEFAULT_MIN_RETRY_DELAY );
if ( current_min_poll_interval < RETRY_MINIMUM_SECS ){
current_min_poll_interval = RETRY_MINIMUM_SECS;
}
int min = current_min_poll_interval;
int max = COConfigurationManager.getIntParameter("Tracker Poll Interval Max", DEFAULT_MAX_RETRY_DELAY );
int inc_by = COConfigurationManager.getIntParameter("Tracker Poll Inc By", DEFAULT_INC_BY );
int inc_per = COConfigurationManager.getIntParameter("Tracker Poll Inc Per", DEFAULT_INC_PER );
int scrape_percentage = COConfigurationManager.getIntParameter("Tracker Scrape Retry Percentage", DEFAULT_SCRAPE_RETRY_PERCENTAGE );
int retry = min;
int clients = 0;
try{
class_mon.enter();
Iterator it = torrent_map.values().iterator();
while(it.hasNext()){
TRTrackerServerTorrentImpl t = (TRTrackerServerTorrentImpl)it.next();
clients += t.getPeerCount();
}
}finally{
class_mon.exit();
}
if ( inc_by > 0 && inc_per > 0 ){
retry += inc_by * (clients/inc_per);
}
if ( max > 0 && retry > max ){
retry = max;
}
if ( retry < RETRY_MINIMUM_SECS ){
retry = RETRY_MINIMUM_SECS;
}
current_announce_retry_interval = retry;
current_scrape_retry_interval = (current_announce_retry_interval*scrape_percentage)/100;
current_total_clients = clients;
// timeout dead clients
if ( time_to_go <= 0 ){
time_to_go = TIMEOUT_CHECK;
try{
class_mon.enter();
Iterator it = torrent_map.values().iterator();
while(it.hasNext()){
TRTrackerServerTorrentImpl t = (TRTrackerServerTorrentImpl)it.next();
t.checkTimeouts();
}
}finally{
class_mon.exit();
}
}
}catch( InterruptedException e ){
Debug.printStackTrace( e );
}
}
}
public TRTrackerServerTorrent
permit(
byte[] _hash,
boolean _explicit )
throws TRTrackerServerException
{
return( permit( _hash, _explicit, true ));
}
public TRTrackerServerTorrent
permit(
byte[] _hash,
boolean _explicit,
boolean _enabled )
throws TRTrackerServerException
{
// System.out.println( "TRTrackerServerImpl::permit( " + _explicit + ")");
HashWrapper hash = new HashWrapper( _hash );
// don't invoke listeners when synched, deadlock possible
TRTrackerServerTorrentImpl entry;
try{
class_mon.enter();
entry = (TRTrackerServerTorrentImpl)torrent_map.get( hash );
}finally{
class_mon.exit();
}
if ( entry == null ){
for (int i=0;i<listeners.size();i++){
if ( !((TRTrackerServerListener)listeners.elementAt(i)).permitted( _hash, _explicit )){
throw( new TRTrackerServerException( "operation denied"));
}
}
try{
class_mon.enter();
// double check in-case added in parallel
entry = (TRTrackerServerTorrentImpl)torrent_map.get( hash );
if ( entry == null ){
entry = new TRTrackerServerTorrentImpl( this, hash, _enabled );
torrent_map.put( hash, entry );
}
}finally{
class_mon.exit();
}
}
return( entry );
}
public void
deny(
byte[] _hash,
boolean _explicit )
throws TRTrackerServerException
{
// System.out.println( "TRTrackerServerImpl::deny( " + _explicit + ")");
HashWrapper hash = new HashWrapper( _hash );
for (int i=0;i<listeners.size();i++){
if ( !((TRTrackerServerListener)listeners.elementAt(i)).denied( _hash, _explicit )){
throw( new TRTrackerServerException( "operation denied"));
}
}
try{
class_mon.enter();
TRTrackerServerTorrentImpl entry = (TRTrackerServerTorrentImpl)torrent_map.get( hash );
if ( entry != null ){
entry.delete();
}
torrent_map.remove( hash );
}finally{
class_mon.exit();
}
}
public TRTrackerServerTorrentImpl
getTorrent(
byte[] hash )
{
try{
class_mon.enter();
return((TRTrackerServerTorrentImpl)torrent_map.get(new HashWrapper(hash)));
}finally{
class_mon.exit();
}
}
public TRTrackerServerTorrentImpl[]
getTorrents()
{
try{
class_mon.enter();
TRTrackerServerTorrentImpl[] res = new TRTrackerServerTorrentImpl[torrent_map.size()];
torrent_map.values().toArray( res );
return( res );
}finally{
class_mon.exit();
}
}
public int
getTorrentCount()
{
return( torrent_map.size());
}
public TRTrackerServerTorrentStats
getStats(
byte[] hash )
{
TRTrackerServerTorrentImpl torrent = getTorrent( hash );
if ( torrent == null ){
return( null );
}
return( torrent.getStats());
}
public TRTrackerServerPeer[]
getPeers(
byte[] hash )
{
TRTrackerServerTorrentImpl torrent = getTorrent( hash );
if ( torrent == null ){
return( null );
}
return( torrent.getPeers());
}
public void
addListener(
TRTrackerServerListener l )
{
try{
this_mon.enter();
listeners.addElement( l );
}finally{
this_mon.exit();
}
}
public void
removeListener(
TRTrackerServerListener l )
{
try{
this_mon.enter();
listeners.removeElement(l);
}finally{
this_mon.exit();
}
}
public void
addAuthenticationListener(
TRTrackerServerAuthenticationListener l )
{
auth_listeners.add( l );
}
public void
removeAuthenticationListener(
TRTrackerServerAuthenticationListener l )
{
auth_listeners.remove(l);
}
public void
preProcess(
TRTrackerServerPeer peer,
TRTrackerServerTorrent torrent,
int type,
String request,
Map response )
throws TRTrackerServerException
{
if ( request_listeners.size() > 0 ){
// if this is a scrape then we need to patch up stuff as it may be multi-scrape
if ( type == TRTrackerServerRequest.RT_SCRAPE ){
try{
int request_pos = 10;
while( true ){
int p = request.indexOf( "info_hash=", request_pos );
String bit;
if ( p == -1 ){
if ( request_pos == 10 ){
break; // only one entry, nothing to do
}
bit = request.substring( request_pos );
}else{
bit = request.substring( request_pos, p );
}
int pos = bit.indexOf('&');
String hash_str = pos==-1?bit:bit.substring(0,pos);
hash_str = URLDecoder.decode( hash_str, Constants.BYTE_ENCODING );
byte[] hash = hash_str.getBytes(Constants.BYTE_ENCODING);
if ( Arrays.equals( hash, torrent.getHash().getBytes())){
request = "info_hash=" + bit;
if ( request.endsWith("&")){
request = request.substring(0,request.length()-1);
}
break;
}
if ( p == -1 ){
break;
}
request_pos = p + 10;
}
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
TRTrackerServerRequestImpl req = new TRTrackerServerRequestImpl( this, peer, torrent, type, request, response );
for (int i=0;i<request_listeners.size();i++){
try{
((TRTrackerServerRequestListener)request_listeners.elementAt(i)).preProcess( req );
}catch( TRTrackerServerException e ){
throw( e );
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}
}
}
public void
postProcess(
TRTrackerServerPeer peer,
TRTrackerServerTorrentImpl torrent,
int type,
String request,
Map response )
throws TRTrackerServerException
{
if ( request_listeners.size() > 0 ){
TRTrackerServerRequestImpl req = new TRTrackerServerRequestImpl( this, peer, torrent, type, request, response );
for (int i=0;i<request_listeners.size();i++){
try{
((TRTrackerServerRequestListener)request_listeners.elementAt(i)).postProcess( req );
}catch( TRTrackerServerException e ){
throw( e );
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}
}
}
public void
addRequestListener(
TRTrackerServerRequestListener l )
{
request_listeners.addElement( l );
}
public void
removeRequestListener(
TRTrackerServerRequestListener l )
{
request_listeners.removeElement(l);
}
protected void
destroy()
{
destroyed = true;
COConfigurationManager.removeListener( config_listener );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -