📄 nameserviceclient.java
字号:
synchronized( response ) {
do {
try {
synchronized( LOCK ) {
request.nameTrnId = getNextNameTrnId();
nid = new Integer( request.nameTrnId );
out.setAddress( request.addr );
out.setLength( request.writeWireFormat( snd_buf, 0 ));
response.received = false;
responseTable.put( nid, response );
ensureOpen( timeout + 1000 );
socket.send( out );
if( log.level > 3 ) {
log.println( request );
Hexdump.hexdump( log, snd_buf, 0, out.getLength() );
}
}
response.wait( timeout );
} catch( InterruptedException ie ) {
} finally {
responseTable.remove( nid );
}
if( !response.received &&
NbtAddress.NBNS.length > 1 &&
NbtAddress.isWINS( request.addr )) {
/* Message was sent to WINS but
* failed to receive response.
* Try a different WINS server.
*/
request.addr = NbtAddress.switchWINS();
if( count == 0 ) {
count = NbtAddress.NBNS.length - 1;
}
}
} while( count-- > 0 );
}
}
NbtAddress[] getAllByName( Name name, InetAddress addr )
throws UnknownHostException {
int n;
NameQueryRequest request = new NameQueryRequest( name );
NameQueryResponse response = new NameQueryResponse();
request.addr = addr != null ? addr : NbtAddress.getWINSAddress();
request.isBroadcast = request.addr == null;
if( request.isBroadcast ) {
request.addr = baddr;
n = RETRY_COUNT;
} else {
request.isBroadcast = false;
n = 1;
}
do {
try {
send( request, response, RETRY_TIMEOUT );
} catch( IOException ioe ) {
if( log.level > 1 )
ioe.printStackTrace( log );
throw new UnknownHostException( name.name );
}
if( response.received && response.resultCode == 0 ) {
return response.addrEntry;
}
} while( --n > 0 && request.isBroadcast );
throw new UnknownHostException( name.name );
}
NbtAddress getByName( Name name, InetAddress addr )
throws UnknownHostException {
int n;
NameQueryRequest request = new NameQueryRequest( name );
NameQueryResponse response = new NameQueryResponse();
if( addr != null ) { /* UniAddress calls always use this
* because it specifies addr
*/
request.addr = addr; /* if addr ends with 255 flag it bcast */
request.isBroadcast = (addr.getAddress()[3] == (byte)0xFF);
n = RETRY_COUNT;
do {
try {
send( request, response, RETRY_TIMEOUT );
} catch( IOException ioe ) {
if( log.level > 1 )
ioe.printStackTrace( log );
throw new UnknownHostException( name.name );
}
if( response.received && response.resultCode == 0 ) {
int last = response.addrEntry.length - 1;
response.addrEntry[last].hostName.srcHashCode = addr.hashCode();
return response.addrEntry[last];
}
} while( --n > 0 && request.isBroadcast );
throw new UnknownHostException( name.name );
}
/* If a target address to query was not specified explicitly
* with the addr parameter we fall into this resolveOrder routine.
*/
for( int i = 0; i < resolveOrder.length; i++ ) {
try {
switch( resolveOrder[i] ) {
case RESOLVER_LMHOSTS:
NbtAddress ans = Lmhosts.getByName( name );
if( ans != null ) {
ans.hostName.srcHashCode = 0; // just has to be different
// from other methods
return ans;
}
break;
case RESOLVER_WINS:
case RESOLVER_BCAST:
if( resolveOrder[i] == RESOLVER_WINS &&
name.name != NbtAddress.MASTER_BROWSER_NAME &&
name.hexCode != 0x1d ) {
request.addr = NbtAddress.getWINSAddress();
request.isBroadcast = false;
} else {
request.addr = baddr;
request.isBroadcast = true;
}
n = RETRY_COUNT;
while( n-- > 0 ) {
try {
send( request, response, RETRY_TIMEOUT );
} catch( IOException ioe ) {
if( log.level > 1 )
ioe.printStackTrace( log );
throw new UnknownHostException( name.name );
}
if( response.received && response.resultCode == 0 ) {
/* Before we return, in anticipation of this address being cached we must
* augment the addresses name's hashCode to distinguish those resolved by
* Lmhosts, WINS, or BCAST. Otherwise a failed query from say WINS would
* get pulled out of the cache for a BCAST on the same name.
*/
response.addrEntry[0].hostName.srcHashCode =
request.addr.hashCode();
return response.addrEntry[0];
} else if( resolveOrder[i] == RESOLVER_WINS ) {
/* If WINS reports negative, no point in retry
*/
break;
}
}
break;
}
} catch( IOException ioe ) {
}
}
throw new UnknownHostException( name.name );
}
NbtAddress[] getNodeStatus( NbtAddress addr ) throws UnknownHostException {
int n, srcHashCode;
NodeStatusRequest request;
NodeStatusResponse response;
response = new NodeStatusResponse( addr );
request = new NodeStatusRequest(
new Name( NbtAddress.ANY_HOSTS_NAME, 0x00, null));
request.addr = addr.getInetAddress();
n = RETRY_COUNT;
while( n-- > 0 ) {
try {
send( request, response, RETRY_TIMEOUT );
} catch( IOException ioe ) {
if( log.level > 1 )
ioe.printStackTrace( log );
throw new UnknownHostException( addr.toString() );
}
if( response.received && response.resultCode == 0 ) {
/* For name queries resolved by different sources (e.g. WINS,
* BCAST, Node Status) we need to augment the hashcode generated
* for the addresses hostname or failed lookups for one type will
* be cached and cause other types to fail even though they may
* not be the authority for the name. For example, if a WINS lookup
* for FOO fails and caches unknownAddress for FOO, a subsequent
* lookup for FOO using BCAST should not fail because of that
* name cached from WINS.
*
* So, here we apply the source addresses hashCode to each name to
* make them specific to who resolved the name.
*/
srcHashCode = request.addr.hashCode();
for( int i = 0; i < response.addressArray.length; i++ ) {
response.addressArray[i].hostName.srcHashCode = srcHashCode;
}
return response.addressArray;
}
}
throw new UnknownHostException( addr.hostName.name );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -