mirror of
https://github.com/angryip/ipscan.git
synced 2025-10-26 11:18:17 +00:00
* timeoutAdaptation is now done in ScanningSubject (+tests written), so that it can be reused in other Fetchers
* Config getters now has more pretty names :-) git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk@336 375186e5-ef17-0410-b0b6-91563547dcda
This commit is contained in:
parent
40b1f4a179
commit
8347c4fcd2
@ -81,10 +81,10 @@ public class ComponentRegistry {
|
||||
// non-GUI
|
||||
Config globalConfig = Config.getConfig();
|
||||
container.registerComponentInstance(globalConfig.getPreferences());
|
||||
container.registerComponentInstance(globalConfig.getScanner());
|
||||
container.registerComponentInstance(globalConfig.getGUI());
|
||||
container.registerComponentInstance(globalConfig.getOpeners());
|
||||
container.registerComponentInstance(globalConfig.getFavorites());
|
||||
container.registerComponentInstance(globalConfig.forScanner());
|
||||
container.registerComponentInstance(globalConfig.forGUI());
|
||||
container.registerComponentInstance(globalConfig.forOpeners());
|
||||
container.registerComponentInstance(globalConfig.forFavorites());
|
||||
container.registerComponentInstance(Labels.getInstance());
|
||||
container.registerComponentImplementation(CommentsConfig.class);
|
||||
container.registerComponentImplementation(ConfigDetector.class);
|
||||
|
||||
@ -57,30 +57,30 @@ public final class Config {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return GlobalConfig instance (quick access)
|
||||
* @return ScannerConfig instance (quick access)
|
||||
*/
|
||||
public ScannerConfig getScanner() {
|
||||
public ScannerConfig forScanner() {
|
||||
return scannerConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Favorites config (only local access)
|
||||
*/
|
||||
NamedListConfig getFavorites() {
|
||||
NamedListConfig forFavorites() {
|
||||
return favoritesConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Openers config (only local access);
|
||||
*/
|
||||
OpenersConfig getOpeners() {
|
||||
OpenersConfig forOpeners() {
|
||||
return openersConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Dimensions config (quick access);
|
||||
*/
|
||||
public GUIConfig getGUI() {
|
||||
public GUIConfig forGUI() {
|
||||
return guiConfig;
|
||||
}
|
||||
|
||||
|
||||
@ -5,12 +5,14 @@
|
||||
*/
|
||||
package net.azib.ipscan.core;
|
||||
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.azib.ipscan.config.Config;
|
||||
import net.azib.ipscan.config.ScannerConfig;
|
||||
import net.azib.ipscan.core.ScanningResult.ResultType;
|
||||
import net.azib.ipscan.core.net.PingResult;
|
||||
|
||||
/**
|
||||
* Scanning subject represents a single scanned
|
||||
@ -21,6 +23,10 @@ import net.azib.ipscan.core.ScanningResult.ResultType;
|
||||
* @author Anton Keks
|
||||
*/
|
||||
public class ScanningSubject {
|
||||
|
||||
public static final String PARAMETER_PING_RESULT = "pinger";
|
||||
|
||||
ScannerConfig config;
|
||||
|
||||
/** The address being scanned */
|
||||
private InetAddress address;
|
||||
@ -30,6 +36,8 @@ public class ScanningSubject {
|
||||
private ResultType resultType = ResultType.UNKNOWN;
|
||||
/** Whether we need to continue scanning or it can be aborted */
|
||||
private boolean isScanningAborted = false;
|
||||
/** Adapted after pinging port timeout - any fetcher can make use of it */
|
||||
int adaptedPortTimeout = -1;
|
||||
|
||||
/**
|
||||
* This constructor should only be used by the Scanner class or unit tests.
|
||||
@ -37,6 +45,7 @@ public class ScanningSubject {
|
||||
public ScanningSubject(InetAddress address) {
|
||||
this.address = address;
|
||||
this.parameters = new HashMap<String, Object>();
|
||||
this.config = Config.getConfig().forScanner();
|
||||
}
|
||||
|
||||
public InetAddress getAddress() {
|
||||
@ -94,5 +103,25 @@ public class ScanningSubject {
|
||||
public void abortAddressScanning() {
|
||||
this.isScanningAborted = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return adapted port timeout for this host if available
|
||||
*/
|
||||
public int getAdaptedPortTimeout() {
|
||||
// see if it is already computed
|
||||
if (adaptedPortTimeout > 0)
|
||||
return adaptedPortTimeout;
|
||||
|
||||
// try to adapt timeout if it is enabled and pinging results are available
|
||||
PingResult pingResult = (PingResult) getParameter(PARAMETER_PING_RESULT);
|
||||
if (pingResult != null) {
|
||||
if (config.adaptPortTimeout && pingResult.isTimeoutAdaptationAllowed()) {
|
||||
adaptedPortTimeout = Math.min(Math.max(pingResult.getLongestTime() * 3, config.minPortTimeout), config.portTimeout);
|
||||
return adaptedPortTimeout;
|
||||
}
|
||||
}
|
||||
// if no pinging results are available yet, return the full timeout
|
||||
return config.portTimeout;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -20,9 +20,10 @@ public class PingResult {
|
||||
private long totalTime;
|
||||
private long longestTime;
|
||||
private int replyCount;
|
||||
private boolean timeoutAdaptationAllowed;
|
||||
|
||||
public PingResult(InetAddress address) {
|
||||
this.address = address;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public void addReply(long time) {
|
||||
@ -30,6 +31,8 @@ public class PingResult {
|
||||
if (time > longestTime)
|
||||
longestTime = time;
|
||||
totalTime += time;
|
||||
// this is for ports fetcher, etc
|
||||
timeoutAdaptationAllowed = replyCount > 2;
|
||||
}
|
||||
|
||||
public int getTTL() {
|
||||
@ -59,4 +62,12 @@ public class PingResult {
|
||||
return replyCount > 0;
|
||||
}
|
||||
|
||||
public void enableTimeoutAdaptation() {
|
||||
if (isAlive())
|
||||
timeoutAdaptationAllowed = true;
|
||||
}
|
||||
|
||||
public boolean isTimeoutAdaptationAllowed() {
|
||||
return timeoutAdaptationAllowed;
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ public class NotAvailableValue implements Comparable<Object> {
|
||||
* Displays a user-friendly text string :-)
|
||||
*/
|
||||
public String toString() {
|
||||
return Config.getConfig().getScanner().notAvailableText;
|
||||
return Config.getConfig().forScanner().notAvailableText;
|
||||
}
|
||||
|
||||
public int compareTo(Object obj) {
|
||||
|
||||
@ -21,7 +21,7 @@ public class NotScannedValue implements Comparable<Object> {
|
||||
* Displays a user-friendly text string :-)
|
||||
*/
|
||||
public String toString() {
|
||||
return Config.getConfig().getScanner().notScannedText;
|
||||
return Config.getConfig().forScanner().notScannedText;
|
||||
}
|
||||
|
||||
public int compareTo(Object obj) {
|
||||
|
||||
@ -29,8 +29,6 @@ public class PingFetcher extends AbstractFetcher {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger();
|
||||
|
||||
public static final String PARAMETER_PINGER = "pinger";
|
||||
|
||||
private ScannerConfig config;
|
||||
|
||||
/** The shared pinger - this one must be static, because PingTTLFetcher will use it as well */
|
||||
@ -52,8 +50,8 @@ public class PingFetcher extends AbstractFetcher {
|
||||
|
||||
PingResult result = null;
|
||||
|
||||
if (subject.hasParameter(PARAMETER_PINGER)) {
|
||||
result = (PingResult) subject.getParameter(PARAMETER_PINGER);
|
||||
if (subject.hasParameter(ScanningSubject.PARAMETER_PING_RESULT)) {
|
||||
result = (PingResult) subject.getParameter(ScanningSubject.PARAMETER_PING_RESULT);
|
||||
}
|
||||
else {
|
||||
try {
|
||||
@ -66,7 +64,7 @@ public class PingFetcher extends AbstractFetcher {
|
||||
result = new PingResult(subject.getAddress());
|
||||
}
|
||||
// remember the result for other fetchers to use
|
||||
subject.setParameter(PARAMETER_PINGER, result);
|
||||
subject.setParameter(ScanningSubject.PARAMETER_PING_RESULT, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -17,7 +17,6 @@ import net.azib.ipscan.config.ScannerConfig;
|
||||
import net.azib.ipscan.core.PortIterator;
|
||||
import net.azib.ipscan.core.ScanningSubject;
|
||||
import net.azib.ipscan.core.ScanningResult.ResultType;
|
||||
import net.azib.ipscan.core.net.PingResult;
|
||||
import net.azib.ipscan.core.values.NotAvailableValue;
|
||||
import net.azib.ipscan.core.values.NotScannedValue;
|
||||
import net.azib.ipscan.core.values.NumericListValue;
|
||||
@ -71,13 +70,7 @@ public class PortsFetcher extends AbstractFetcher {
|
||||
subject.setParameter(PARAMETER_OPEN_PORTS, openPorts);
|
||||
subject.setParameter(PARAMETER_FILTERED_PORTS, filteredPorts);
|
||||
|
||||
int adaptedTimeout = config.portTimeout;
|
||||
|
||||
// now try to adapt timeout if it is enabled and pinging results are available
|
||||
PingResult pingResult = (PingResult) subject.getParameter(PingFetcher.PARAMETER_PINGER);
|
||||
if (config.adaptPortTimeout && pingResult.getReplyCount() > 2) {
|
||||
adaptedTimeout = Math.min(Math.max(pingResult.getLongestTime() * 3, config.minPortTimeout), config.portTimeout);
|
||||
}
|
||||
int portTimeout = subject.getAdaptedPortTimeout();
|
||||
|
||||
Socket socket = null;
|
||||
// clone port iterator for performance instead of creating for every thread
|
||||
@ -97,7 +90,7 @@ public class PortsFetcher extends AbstractFetcher {
|
||||
socket.setReuseAddress(true);
|
||||
socket.setReceiveBufferSize(32);
|
||||
// now connect
|
||||
socket.connect(new InetSocketAddress(subject.getAddress(), port), adaptedTimeout);
|
||||
socket.connect(new InetSocketAddress(subject.getAddress(), port), portTimeout);
|
||||
// some more options
|
||||
socket.setSoLinger(true, 0);
|
||||
socket.setSendBufferSize(16);
|
||||
|
||||
@ -65,7 +65,7 @@ public abstract class AbstractModalDialog {
|
||||
Rectangle clientArea = shell.getClientArea();
|
||||
|
||||
Point size = okButton.computeSize(85, SWT.DEFAULT);
|
||||
size.y = Math.max(size.y, Config.getConfig().getGUI().standardButtonHeight);
|
||||
size.y = Math.max(size.y, Config.getConfig().forGUI().standardButtonHeight);
|
||||
|
||||
okButton.setSize(size);
|
||||
|
||||
@ -106,7 +106,7 @@ public abstract class AbstractModalDialog {
|
||||
cancelButton = fooButton;
|
||||
}
|
||||
// both buttons
|
||||
int height = Math.max(okButton.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, Config.getConfig().getGUI().standardButtonHeight);
|
||||
int height = Math.max(okButton.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, Config.getConfig().forGUI().standardButtonHeight);
|
||||
int distance = height/3;
|
||||
cancelButton.pack();
|
||||
cancelButton.setLayoutData(LayoutHelper.formData(Math.max(85, cancelButton.getSize().x), height, null, new FormAttachment(control, 0, SWT.RIGHT), new FormAttachment(control, 8), null));
|
||||
|
||||
@ -16,9 +16,9 @@ public class ConfigTest {
|
||||
public void testGetters() {
|
||||
Config config = Config.getConfig();
|
||||
assertNotNull(config.getPreferences());
|
||||
assertNotNull(config.getScanner());
|
||||
assertNotNull(config.getGUI());
|
||||
assertNotNull(config.getFavorites());
|
||||
assertNotNull(config.getOpeners());
|
||||
assertNotNull(config.forScanner());
|
||||
assertNotNull(config.forGUI());
|
||||
assertNotNull(config.forFavorites());
|
||||
assertNotNull(config.forOpeners());
|
||||
}
|
||||
}
|
||||
|
||||
87
test/net/azib/ipscan/core/ScanningSubjectTest.java
Normal file
87
test/net/azib/ipscan/core/ScanningSubjectTest.java
Normal file
@ -0,0 +1,87 @@
|
||||
/**
|
||||
* This file is a part of Angry IP Scanner source code,
|
||||
* see http://www.azib.net/ for more information.
|
||||
* Licensed under GPLv2.
|
||||
*/
|
||||
package net.azib.ipscan.core;
|
||||
|
||||
import static org.easymock.classextension.EasyMock.*;
|
||||
import static org.junit.Assert.*;
|
||||
import net.azib.ipscan.config.ScannerConfig;
|
||||
import net.azib.ipscan.core.net.PingResult;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* ScanningSubjectTest
|
||||
*
|
||||
* @author Anton Keks
|
||||
*/
|
||||
public class ScanningSubjectTest {
|
||||
|
||||
private ScanningSubject subject;
|
||||
private ScannerConfig config;
|
||||
private PingResult pingResult;
|
||||
|
||||
@Before
|
||||
public void initTest() {
|
||||
config = createMock(ScannerConfig.class);
|
||||
subject = new ScanningSubject(null);
|
||||
subject.config = config;
|
||||
config.portTimeout = 1000;
|
||||
config.adaptPortTimeout = true;
|
||||
pingResult = new PingResult(null);
|
||||
subject.setParameter(ScanningSubject.PARAMETER_PING_RESULT, pingResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adaptedPortTimeoutUsingReplies() throws Exception {
|
||||
subject.setParameter(ScanningSubject.PARAMETER_PING_RESULT, null);
|
||||
// no ping replies yet
|
||||
assertEquals(config.portTimeout, subject.getAdaptedPortTimeout());
|
||||
|
||||
subject.setParameter(ScanningSubject.PARAMETER_PING_RESULT, pingResult);
|
||||
// no ping replies yet
|
||||
assertEquals(config.portTimeout, subject.getAdaptedPortTimeout());
|
||||
|
||||
pingResult.addReply(100);
|
||||
// too few replies yet
|
||||
assertEquals(config.portTimeout, subject.getAdaptedPortTimeout());
|
||||
|
||||
pingResult.addReply(200);
|
||||
// too few replies yet
|
||||
assertEquals(config.portTimeout, subject.getAdaptedPortTimeout());
|
||||
|
||||
pingResult.addReply(300);
|
||||
// now we can shorten the timeout
|
||||
assertEquals(300 * 3, subject.getAdaptedPortTimeout());
|
||||
|
||||
pingResult.addReply(500);
|
||||
// adapted timeout is not recalculated
|
||||
assertEquals(300 * 3, subject.getAdaptedPortTimeout());
|
||||
|
||||
subject.adaptedPortTimeout = -1;
|
||||
// adapted timeout is now recalculated and longer than configured one
|
||||
assertEquals(config.portTimeout, subject.getAdaptedPortTimeout());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forcedAdaptedTimeout() throws Exception {
|
||||
pingResult.enableTimeoutAdaptation();
|
||||
// cannot force yet - no replies
|
||||
assertEquals(config.portTimeout, subject.getAdaptedPortTimeout());
|
||||
|
||||
pingResult.addReply(100);
|
||||
pingResult.enableTimeoutAdaptation();
|
||||
assertEquals(100 * 3, subject.getAdaptedPortTimeout());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adaptedTimeoutTooShort() throws Exception {
|
||||
config.minPortTimeout = 100;
|
||||
pingResult.addReply(0);
|
||||
pingResult.enableTimeoutAdaptation();
|
||||
assertEquals(config.minPortTimeout, subject.getAdaptedPortTimeout());
|
||||
}
|
||||
}
|
||||
@ -55,7 +55,7 @@ public class PingerRegistryImplTest {
|
||||
}
|
||||
|
||||
public void testCreateDefaultPinger() throws Exception {
|
||||
ScannerConfig config = Config.getConfig().getScanner();
|
||||
ScannerConfig config = Config.getConfig().forScanner();
|
||||
PingerRegistry registry = new PingerRegistryImpl(config);
|
||||
config.selectedPinger = "pinger.udp";
|
||||
assertTrue(registry.createPinger() instanceof UDPPinger);
|
||||
@ -63,7 +63,7 @@ public class PingerRegistryImplTest {
|
||||
|
||||
@Test
|
||||
public void checkSelectedPinger() throws Exception {
|
||||
ScannerConfig config = Config.getConfig().getScanner();
|
||||
ScannerConfig config = Config.getConfig().forScanner();
|
||||
PingerRegistryImpl registry = new PingerRegistryImpl(config);
|
||||
|
||||
config.selectedPinger = "pinger.udp";
|
||||
|
||||
@ -23,7 +23,7 @@ public class NotAvailableValueTest {
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception {
|
||||
assertEquals(Config.getConfig().getScanner().notAvailableText, NotAvailableValue.INSTANCE.toString());
|
||||
assertEquals(Config.getConfig().forScanner().notAvailableText, NotAvailableValue.INSTANCE.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@ -23,7 +23,7 @@ public class NotScannedValueTest {
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception {
|
||||
assertEquals(Config.getConfig().getScanner().notScannedText, NotScannedValue.INSTANCE.toString());
|
||||
assertEquals(Config.getConfig().forScanner().notScannedText, NotScannedValue.INSTANCE.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@ -16,7 +16,7 @@ public class PingFetcherTest extends AbstractFetcherTestCase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fetcher = new PingFetcher(null, Config.getConfig().getScanner());
|
||||
fetcher = new PingFetcher(null, Config.getConfig().forScanner());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ public class PingTTLFetcherTest extends AbstractFetcherTestCase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fetcher = new PingTTLFetcher(null, Config.getConfig().getScanner());
|
||||
fetcher = new PingTTLFetcher(null, Config.getConfig().forScanner());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user