Value incrementing logic moved from all Pingers to PingResult, no fields are directly accessible anymore, setters intriduced.

PortsFetcher now uses getLongestTime() for adaptive timeout calculation. 
Bugfix: port list is initialized in init() now, so is rebuild before each scan.

git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk/ipscan@111 375186e5-ef17-0410-b0b6-91563547dcda
This commit is contained in:
angryziber 2007-01-04 13:42:25 +00:00
parent 2ba27e9b7a
commit 7bbd261044
6 changed files with 36 additions and 21 deletions

View File

@ -84,9 +84,8 @@ public class ICMPPinger implements Pinger {
long start = OctetConverter.octetsToLong(data, dataOffset);
long time = end - start;
result.totalTime += time;
result.replyCount++;
result.ttl = packet.getTTL() & 0xFF;
result.addReply(time);
result.setTTL(packet.getTTL() & 0xFF);
}
}
catch (InterruptedIOException e) {

View File

@ -116,7 +116,7 @@ public class ICMPSharedPinger implements Pinger {
}
int totalTimeout = timeout * count;
while (totalTimeout > 0 && result.replyCount < count) {
while (totalTimeout > 0 && result.getReplyCount() < count) {
if (LOG.isLoggable(Level.FINEST)) {
LOG.finest("Waiting for response " + address + ": " + totalTimeout);
}
@ -198,10 +198,9 @@ public class ICMPSharedPinger implements Pinger {
LOG.finest("Received " + packet.getSequenceNumber() + packet.getSourceAsInetAddress() + ": " + time);
}
result.totalTime += time;
result.replyCount++;
result.addReply(time);
// TTL should be the same among all packets
result.ttl = packet.getTTL() & 0xFF;
result.setTTL(packet.getTTL() & 0xFF);
synchronized (result) {
// notify the sender that we have an answer :-)

View File

@ -14,23 +14,42 @@ public class PingResult {
InetAddress address;
int ttl;
long totalTime;
int replyCount;
private int ttl;
private long totalTime;
private long longestTime;
private int replyCount;
public PingResult(InetAddress address) {
this.address = address;
this.address = address;
}
public void addReply(long time) {
replyCount++;
if (time > longestTime)
longestTime = time;
totalTime += time;
}
public int getTTL() {
return ttl;
}
public void setTTL(int ttl) {
this.ttl = ttl;
}
public int getAverageTime() {
return (int)(totalTime / replyCount);
}
public int getLongestTime() {
return (int)longestTime;
}
public int getReplyCount() {
return replyCount;
}
/**
* @return true in case at least one reply was received
*/

View File

@ -40,12 +40,10 @@ public class TCPPinger implements Pinger {
long startTime = System.currentTimeMillis();
try {
socket.connect(new InetSocketAddress(address, PROBE_TCP_PORT), timeout);
result.replyCount++;
result.totalTime+=System.currentTimeMillis()-startTime;
result.addReply(System.currentTimeMillis()-startTime);
}
catch (ConnectException e) {
result.replyCount++;
result.totalTime+=System.currentTimeMillis()-startTime;
result.addReply(System.currentTimeMillis()-startTime);
}
catch (SocketTimeoutException e) {
}

View File

@ -44,8 +44,7 @@ public class UDPPinger implements Pinger {
socket.receive(packet);
}
catch (PortUnreachableException e) {
result.replyCount++;
result.totalTime+=System.currentTimeMillis()-startTime;
result.addReply(System.currentTimeMillis()-startTime);
}
catch (SocketTimeoutException e) {
}

View File

@ -36,7 +36,6 @@ public class PortsFetcher implements Fetcher {
public PortsFetcher(GlobalConfig globalConfig) {
this.config = globalConfig;
this.portIteratorPrototype = new PortIterator(config.portString);
}
public String getLabel() {
@ -62,9 +61,9 @@ public class PortsFetcher implements Fetcher {
// now try to adapt timeout if it is enabled and pinging results are availbale
PingResult pingResult = (PingResult) subject.getParameter(PingFetcher.PARAMETER_PINGER);
if (config.adaptPortTimeout && pingResult.isAlive()) {
if (config.adaptPortTimeout && pingResult.getReplyCount() > 2) {
// TODO: use longest time istead of the average one for adapting
adaptedTimeout = Math.min(Math.max(pingResult.getAverageTime() * 4, 30), config.portTimeout);
adaptedTimeout = Math.min(Math.max(pingResult.getLongestTime() * 4, 50), config.portTimeout);
}
Socket socket = null;
@ -129,6 +128,8 @@ public class PortsFetcher implements Fetcher {
}
public void init() {
// rebuild port iterator before each scan
this.portIteratorPrototype = new PortIterator(config.portString);
}
public void cleanup() {