Skip to content

Commit 42c601c

Browse files
committed
Added cancel to SubnetDevices and fixed blocking UI bug #PLAY_BETA
1 parent b7a64cd commit 42c601c

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed

app/src/main/java/com/stealthcotper/networktools/MainActivity.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private void doPortScan() throws Exception {
206206
final long startTimeMillis = System.currentTimeMillis();
207207

208208
// Perform an asynchronous port scan
209-
PortScan.onAddress(ipAddress).setPortsAll().setMethodTCP().doScan(new PortScan.PortListener() {
209+
PortScan portScan = PortScan.onAddress(ipAddress).setPortsAll().setMethodTCP().doScan(new PortScan.PortListener() {
210210
@Override
211211
public void onResult(int portNo, boolean open) {
212212
if (open) appendResultsText("Open: " + portNo);
@@ -219,14 +219,16 @@ public void onFinished(ArrayList<Integer> openPorts) {
219219
}
220220
});
221221

222+
// Below is example of how to cancel a running scan
223+
// portScan.cancel();
222224
}
223225

224226

225227
private void findSubnetDevices() {
226228

227229
final long startTimeMillis = System.currentTimeMillis();
228230

229-
SubnetDevices.fromLocalAddress().findDevices(new SubnetDevices.OnSubnetDeviceFound() {
231+
SubnetDevices subnetDevices = SubnetDevices.fromLocalAddress().findDevices(new SubnetDevices.OnSubnetDeviceFound() {
230232
@Override
231233
public void onDeviceFound(Device device) {
232234
appendResultsText("Device: " + device.ip+" "+ device.hostname);
@@ -239,6 +241,10 @@ public void onFinished(ArrayList<Device> devicesFound) {
239241
appendResultsText("Finished "+timeTaken+" s");
240242
}
241243
});
244+
245+
// Below is example of how to cancel a running scan
246+
// subnetDevices.cancel();
247+
242248
}
243249

244250
@Override

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ subprojects {
3030
ext.targetSdkVer = 27
3131
ext.supportLibVer = "27.1.1"
3232

33-
ext.appVersionName = "0.4.1"
34-
ext.appVersionCode = 15
33+
ext.appVersionName = "0.4.2"
34+
ext.appVersionCode = 16
3535
}

library/src/main/java/com/stealthcopter/networktools/PortScan.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public void cancel() {
288288
}
289289

290290
/**
291-
* Perform a synchronous port scan and return a list of open ports
291+
* Perform a synchronous (blocking) port scan and return a list of open ports
292292
*
293293
* @return - ping result
294294
*/
@@ -320,7 +320,7 @@ public ArrayList<Integer> doScan() {
320320
}
321321

322322
/**
323-
* Perform an asynchronous port scan
323+
* Perform an asynchronous (non-blocking) port scan
324324
*
325325
* @param portListener - the listener to fire portscan results to.
326326
* @return - this object so we can cancel the scan if needed

library/src/main/java/com/stealthcopter/networktools/SubnetDevices.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class SubnetDevices {
2121
private ArrayList<Device> devicesFound;
2222
private OnSubnetDeviceFound listener;
2323
private int timeOutMillis = 2500;
24+
private boolean cancelled = false;
2425

2526
// This class is not to be instantiated
2627
private SubnetDevices() {
@@ -134,31 +135,53 @@ public SubnetDevices setTimeOutMillis(int timeOutMillis) throws IllegalArgumentE
134135
return this;
135136
}
136137

138+
/**
139+
* Cancel a running scan
140+
*/
141+
public void cancel() {
142+
this.cancelled = true;
143+
}
137144

138-
public void findDevices(final OnSubnetDeviceFound listener) {
145+
/**
146+
* Starts the scan to find other devices on the subnet
147+
*
148+
* @param listener - to pass on the results
149+
* @return this object so we can call cancel on it if needed
150+
*/
151+
public SubnetDevices findDevices(final OnSubnetDeviceFound listener) {
139152

140153
this.listener = listener;
141154

155+
cancelled = false;
142156
devicesFound = new ArrayList<>();
143157

144-
ExecutorService executor = Executors.newFixedThreadPool(this.noThreads);
158+
new Thread(new Runnable() {
159+
@Override
160+
public void run() {
145161

146-
for (final String add : addresses) {
147-
Runnable worker = new SubnetDeviceFinderRunnable(add);
148-
executor.execute(worker);
149-
}
162+
ExecutorService executor = Executors.newFixedThreadPool(noThreads);
150163

151-
// This will make the executor accept no new threads
152-
// and finish all existing threads in the queue
153-
executor.shutdown();
154-
// Wait until all threads are finish
155-
try {
156-
executor.awaitTermination(1, TimeUnit.HOURS);
157-
} catch (InterruptedException e) {
158-
e.printStackTrace();
159-
}
164+
for (final String add : addresses) {
165+
Runnable worker = new SubnetDeviceFinderRunnable(add);
166+
executor.execute(worker);
167+
}
160168

161-
this.listener.onFinished(devicesFound);
169+
// This will make the executor accept no new threads
170+
// and finish all existing threads in the queue
171+
executor.shutdown();
172+
// Wait until all threads are finish
173+
try {
174+
executor.awaitTermination(1, TimeUnit.HOURS);
175+
} catch (InterruptedException e) {
176+
e.printStackTrace();
177+
}
178+
179+
listener.onFinished(devicesFound);
180+
181+
}
182+
}).start();
183+
184+
return this;
162185
}
163186

164187
private synchronized void subnetDeviceFound(Device device) {
@@ -175,6 +198,9 @@ public class SubnetDeviceFinderRunnable implements Runnable {
175198

176199
@Override
177200
public void run() {
201+
202+
if (cancelled) return;
203+
178204
try {
179205
InetAddress ia = InetAddress.getByName(address);
180206
PingResult pingResult = Ping.onAddress(ia).setTimeOutMillis(timeOutMillis).doPing();

0 commit comments

Comments
 (0)