📄 dhtnatpuncherimpl.java
字号:
if ( new_rendezvous_target[0] != null ){
rendezvous_target = new_rendezvous_target[0];
rendezvous_local_contact = local_contact;
log( "Rendezvous found: " + rendezvous_local_contact.getString() + " -> " + rendezvous_target.getString());
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()
{
SimpleTimer.addPeriodicEvent(
"DHTNAT:cp",
RENDEZVOUS_CLIENT_PING_PERIOD,
new TimerEventPerformer() {
public void perform( TimerEvent ev ) {
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
diversified()
{
}
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 ),
"NAT Traversal: rendezvous 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
diversified()
{
}
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
diversified()
{
}
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 ){
trace( "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 ){
rendezvousFailed( current_target, false );
}
}
}
}catch( Throwable e ){
log(e);
}
}
}
);
}
protected void
rendezvousFailed(
DHTTransportContact current_target,
boolean tidy )
{
log( "Rendezvous " + (tidy?"closed":"failed") + ": " + current_target.getString());
try{
pub_mon.enter();
failed_rendezvous.put( current_target.getAddress(), "" );
}finally{
pub_mon.exit();
}
publish( true );
}
protected byte[]
sendRequest(
DHTTransportContact target,
byte[] data,
int timeout )
{
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,
timeout ));
}catch( DHTTransportException e ){
// log(e); timeout most likely
return( null );
}
}
protected byte[]
receiveRequest(
DHTTransportUDPContact 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,
int timeout )
{
try{
byte[] res = sendRequest( target, formatters.bEncode( data ), timeout );
if ( res == null ){
return( null );
}
return( formatters.bDecode( res ));
}catch( Throwable e ){
log(e);
return( null );
}
}
protected Map
receiveRequest(
DHTTransportUDPContact 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_CLOSE_REQUEST:
{
response.put( "type", new Long( RT_CLOSE_REPLY ));
receiveClose( originator, data, response );
break;
}
case RT_QUERY_REQUEST:
{
response.put( "type", new Long( RT_QUERY_REPLY ));
receiveQuery( 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;
}
case RT_TUNNEL_INBOUND:
{
receiveTunnelInbound( originator, data );
response = null;
break;
}
case RT_TUNNEL_OUTBOUND:
{
receiveTunnelOutbound( originator, data );
response = null;
break;
}
default:
{
response = null;
break;
}
}
return( response );
}
protected boolean
sendTunnelMessage(
DHTTransportContact target,
Map data )
{
try{
return( sendTunnelMessage( target, formatters.bEncode( data )));
}catch( Throwable e ){
log(e);
return( false );
}
}
protected boolean
sendTunnelMessage(
DHTTransportContact target,
byte[] data )
{
try{
dht.getTransport().writeTransfer(
new DHTTransportProgressListener()
{
public void
reportSize(
long size )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -