📄 dhtnatpuncherimpl.java
字号:
runRendezvous();
break;
}
}finally{
pub_mon.exit();
}
}
if ( new_rendezvous_target[0] == null ){
log( "No rendezvous found: candidates=" + reachables.length +",tried="+ reachables_tried+",skipped=" +reachables_skipped );
try{
pub_mon.enter();
rendezvous_local_contact = null;
rendezvous_target = null;
}finally{
pub_mon.exit();
}
}
}
}else{
try{
pub_mon.enter();
rendezvous_local_contact = null;
rendezvous_target = null;
}finally{
pub_mon.exit();
}
}
}
protected void
runRendezvous()
{
try{
pub_mon.enter();
if ( !rendezvous_thread_running ){
rendezvous_thread_running = true;
plugin_interface.getUtilities().createThread(
"DHTNatPuncher:rendevzous",
new Runnable()
{
public void
run()
{
runRendezvousSupport();
}
});
}
}finally{
pub_mon.exit();
}
}
protected void
runRendezvousSupport()
{
DHTTransportContact current_local = null;
DHTTransportContact current_target = null;
int rendevzous_fail_count = 0;
while( true ){
try{
DHTTransportContact latest_local;
DHTTransportContact latest_target;
try{
pub_mon.enter();
latest_local = rendezvous_local_contact;
latest_target = rendezvous_target;
}finally{
pub_mon.exit();
}
if ( current_local != null || latest_local != null ){
// one's not null, worthwhile further investigation
if ( current_local != latest_local ){
// local has changed, remove existing publish
if ( current_local != null ){
log( "Removing publish for " + current_local.getString() + " -> " + current_target.getString());
dht.remove(
getPublishKey( current_local ),
"DHTNatPuncher: removal of publish",
new DHTOperationListener()
{
public void
searching(
DHTTransportContact contact,
int level,
int active_searches )
{}
public void
found(
DHTTransportContact contact )
{}
public void
read(
DHTTransportContact contact,
DHTTransportValue value )
{}
public void
wrote(
DHTTransportContact contact,
DHTTransportValue value )
{}
public void
complete(
boolean timeout )
{}
});
}
if ( latest_local != null ){
log( "Adding publish for " + latest_local.getString() + " -> " + latest_target.getString());
rendevzous_fail_count = RENDEZVOUS_PING_FAIL_LIMIT - 2; // only 2 attempts to start with
dht.put(
getPublishKey( latest_local ),
"DHTNatPuncher: publish",
encodePublishValue( latest_target ),
DHT.FLAG_SINGLE_VALUE,
new DHTOperationListener()
{
public void
searching(
DHTTransportContact contact,
int level,
int active_searches )
{}
public void
found(
DHTTransportContact contact )
{}
public void
read(
DHTTransportContact contact,
DHTTransportValue value )
{}
public void
wrote(
DHTTransportContact contact,
DHTTransportValue value )
{}
public void
complete(
boolean timeout )
{}
});
}
}else if ( current_target != latest_target ){
// here current_local == latest_local and neither is null!
// target changed, update publish
log( "Updating publish for " + latest_local.getString() + " -> " + latest_target.getString());
rendevzous_fail_count = RENDEZVOUS_PING_FAIL_LIMIT - 2; // only 2 attempts to start with
dht.put(
getPublishKey( latest_local ),
"DHTNatPuncher: update publish",
encodePublishValue( latest_target ),
DHT.FLAG_SINGLE_VALUE,
new DHTOperationListener()
{
public void
searching(
DHTTransportContact contact,
int level,
int active_searches )
{}
public void
found(
DHTTransportContact contact )
{}
public void
read(
DHTTransportContact contact,
DHTTransportValue value )
{}
public void
wrote(
DHTTransportContact contact,
DHTTransportValue value )
{}
public void
complete(
boolean timeout )
{}
});
}
}
current_local = latest_local;
current_target = latest_target;
if ( current_target != null ){
int bind_result = sendBind( current_target );
if ( bind_result == RESP_OK ){
if ( TRACE ){
System.out.println( "Rendezvous:" + current_target.getString() + " OK" );
}
rendevzous_fail_count = 0;
if ( last_ok_rendezvous != current_target ){
last_ok_rendezvous = current_target;
log( "Rendezvous " + latest_target.getString() + " operational" );
}
}else{
if ( bind_result == RESP_NOT_OK ){
// denied access
rendevzous_fail_count = RENDEZVOUS_PING_FAIL_LIMIT;
}else{
rendevzous_fail_count++;
}
if ( rendevzous_fail_count == RENDEZVOUS_PING_FAIL_LIMIT ){
log( "Rendezvous failed: " + current_target.getString());
if ( TRACE ){
log( "Rendezvous:" + current_target.getString() + " Failed" );
}
try{
pub_mon.enter();
failed_rendezvous.put( current_target.getAddress(),"");
}finally{
pub_mon.exit();
}
publish( true );
}
}
}
}catch( Throwable e ){
log(e);
}finally{
try{
Thread.sleep( RENDEZVOUS_CLIENT_PING_PERIOD );
}catch( Throwable e ){
log(e);
break;
}
}
}
}
protected byte[]
sendRequest(
DHTTransportContact target,
byte[] data )
{
try{
return(
dht.getTransport().writeReadTransfer(
new DHTTransportProgressListener()
{
public void
reportSize(
long size )
{
}
public void
reportActivity(
String str )
{
}
public void
reportCompleteness(
int percent )
{
}
},
target,
transfer_handler_key,
data,
TRANSFER_TIMEOUT ));
}catch( DHTTransportException e ){
// log(e); timeout most likely
return( null );
}
}
protected byte[]
receiveRequest(
DHTTransportContact originator,
byte[] data )
{
try{
Map res = receiveRequest( originator, formatters.bDecode( data ));
if ( res == null ){
return( null );
}
return( formatters.bEncode( res ));
}catch( Throwable e ){
log(e);
return( null );
}
}
protected Map
sendRequest(
DHTTransportContact target,
Map data )
{
try{
byte[] res = sendRequest( target, formatters.bEncode( data ));
if ( res == null ){
return( null );
}
return( formatters.bDecode( res ));
}catch( Throwable e ){
log(e);
return( null );
}
}
protected Map
receiveRequest(
DHTTransportContact originator,
Map data )
{
int type = ((Long)data.get("type")).intValue();
Map response = new HashMap();
switch( type ){
case RT_BIND_REQUEST:
{
response.put( "type", new Long( RT_BIND_REPLY ));
receiveBind( originator, data, response );
break;
}
case RT_PUNCH_REQUEST:
{
response.put( "type", new Long( RT_PUNCH_REPLY ));
receivePunch( originator, data, response );
break;
}
case RT_CONNECT_REQUEST:
{
response.put( "type", new Long( RT_CONNECT_REPLY ));
receiveConnect( originator, data, response );
break;
}
default:
{
response = null;
break;
}
}
return( response );
}
protected int
sendBind(
DHTTransportContact target )
{
try{
Map request = new HashMap();
request.put("type", new Long( RT_BIND_REQUEST ));
Map response = sendRequest( target, request );
if ( response == null ){
return( RESP_FAILED );
}
if (((Long)response.get( "type" )).intValue() == RT_BIND_REPLY ){
int result = ((Long)response.get("ok")).intValue();
if ( TRACE ){
System.out.println( "received bind reply: " + (result==0?"failed":"ok" ));
}
if ( result == 1 ){
return( RESP_OK );
}
}
return( RESP_NOT_OK );
}catch( Throwable e ){
log(e);
return( RESP_FAILED );
}
}
protected void
receiveBind(
DHTTransportContact originator,
Map request,
Map response )
{
if ( TRACE ){
System.out.println( "received bind request" );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -