diff --git a/lib/src/services/host_scanner_service.dart b/lib/src/services/host_scanner_service.dart index a6f50b2..ff023ba 100644 --- a/lib/src/services/host_scanner_service.dart +++ b/lib/src/services/host_scanner_service.dart @@ -22,6 +22,7 @@ abstract class HostScannerService { String subnet, { int firstHostId = defaultFirstHostId, int lastHostId = defaultLastHostId, + List hostIds = const [], int timeoutInSeconds = 1, ProgressCallback? progressCallback, bool resultsInAddressAscendingOrder = true, @@ -31,6 +32,7 @@ abstract class HostScannerService { String subnet, { int firstHostId = defaultFirstHostId, int lastHostId = defaultLastHostId, + List hostIds = const [], int timeoutInSeconds = 1, ProgressCallback? progressCallback, bool resultsInAddressAscendingOrder = true, @@ -46,6 +48,7 @@ abstract class HostScannerService { String subnet, { int firstHostId = defaultFirstHostId, int lastHostId = defaultLastHostId, + List hostIds = const [], int timeoutInSeconds = 1, ProgressCallback? progressCallback, bool resultsInAddressAscendingOrder = true, diff --git a/lib/src/services/impls/host_scanner_service_impl.dart b/lib/src/services/impls/host_scanner_service_impl.dart index 26f98d0..12d04fa 100644 --- a/lib/src/services/impls/host_scanner_service_impl.dart +++ b/lib/src/services/impls/host_scanner_service_impl.dart @@ -15,6 +15,7 @@ class HostScannerServiceImpl extends HostScannerService { /// Set maxHost to higher value if you are not getting results. /// It won't firstHostId again unless previous scan is completed due to heavy /// resource consumption. + /// Use hostIds to limit subnet scan to hosts given. /// [resultsInAddressAscendingOrder] = false will return results faster but not in /// ascending order and without [progressCallback]. @override @@ -22,6 +23,7 @@ class HostScannerServiceImpl extends HostScannerService { String subnet, { int firstHostId = HostScannerService.defaultFirstHostId, int lastHostId = HostScannerService.defaultLastHostId, + List hostIds = const [], int timeoutInSeconds = 1, ProgressCallback? progressCallback, bool resultsInAddressAscendingOrder = true, @@ -30,6 +32,7 @@ class HostScannerServiceImpl extends HostScannerService { subnet, firstHostId: firstHostId, lastHostId: lastHostId, + hostIds: hostIds, timeoutInSeconds: timeoutInSeconds, progressCallback: progressCallback, resultsInAddressAscendingOrder: resultsInAddressAscendingOrder, @@ -51,6 +54,7 @@ class HostScannerServiceImpl extends HostScannerService { String subnet, { int firstHostId = HostScannerService.defaultFirstHostId, int lastHostId = HostScannerService.defaultLastHostId, + List hostIds = const [], int timeoutInSeconds = 1, ProgressCallback? progressCallback, bool resultsInAddressAscendingOrder = true, @@ -61,14 +65,18 @@ class HostScannerServiceImpl extends HostScannerService { final StreamController activeHostsController = StreamController(); + final List pinged = []; for (int i = firstHostId; i <= lastValidSubnet; i++) { - activeHostsFuture.add( - getHostFromPing( - activeHostsController: activeHostsController, - host: '$subnet.$i', - timeoutInSeconds: timeoutInSeconds, - ), - ); + if (hostIds.isEmpty || hostIds.contains(i)) { + pinged.add(i); + activeHostsFuture.add( + getHostFromPing( + activeHostsController: activeHostsController, + host: '$subnet.$i', + timeoutInSeconds: timeoutInSeconds, + ), + ); + } } if (!resultsInAddressAscendingOrder) { @@ -80,8 +88,9 @@ class HostScannerServiceImpl extends HostScannerService { i++; final SendableActiveHost? tempHost = await host; - progressCallback - ?.call((i - firstHostId) * 100 / (lastValidSubnet - firstHostId)); + progressCallback?.call( + (pinged[i] - firstHostId) * 100 / (lastValidSubnet - firstHostId), + ); if (tempHost == null) { continue; @@ -160,6 +169,7 @@ class HostScannerServiceImpl extends HostScannerService { String subnet, { int firstHostId = HostScannerService.defaultFirstHostId, int lastHostId = HostScannerService.defaultLastHostId, + List hostIds = const [], int timeoutInSeconds = 1, ProgressCallback? progressCallback, bool resultsInAddressAscendingOrder = true, @@ -188,15 +198,16 @@ class HostScannerServiceImpl extends HostScannerService { resultsInAddressAscendingOrder.toString(), dbDirectory, enableDebugging.toString(), + hostIds.join(','), ], ); } else if (message is SendableActiveHost) { - progressCallback - ?.call((i - firstHostId) * 100 / (lastValidSubnet - firstHostId)); - // print('Address ${message.address}'); final activeHostFound = ActiveHost.fromSendableActiveHost(sendableActiveHost: message); await activeHostFound.resolveInfo(); + final j = int.tryParse(activeHostFound.hostId) ?? i; + progressCallback + ?.call((j - firstHostId) * 100 / (lastValidSubnet - firstHostId)); yield activeHostFound; } else if (message is String && message == 'Done') { isolate.kill(); @@ -221,6 +232,11 @@ class HostScannerServiceImpl extends HostScannerService { final bool resultsInAddressAscendingOrder = message[4] == "true"; final String dbDirectory = message[5]; final bool enableDebugging = message[6] == "true"; + final List hostIds = message[7] + .split(',') + .where((e) => e.isNotEmpty) + .map(int.parse) + .toList(); // configure again await configureNetworkTools( dbDirectory, @@ -234,6 +250,7 @@ class HostScannerServiceImpl extends HostScannerService { subnetIsolate, firstHostId: firstSubnetIsolate, lastHostId: lastSubnetIsolate, + hostIds: hostIds, timeoutInSeconds: timeoutInSeconds, resultsInAddressAscendingOrder: resultsInAddressAscendingOrder, ); diff --git a/test/network_tools_test.dart b/test/network_tools_test.dart index fb2ce12..2bb0028 100644 --- a/test/network_tools_test.dart +++ b/test/network_tools_test.dart @@ -7,6 +7,7 @@ import 'package:universal_io/io.dart'; void main() { int port = 0; + int hostId = 0; int firstHostId = 0; int lastHostId = 0; String myOwnHost = "0.0.0.0"; @@ -27,7 +28,7 @@ void main() { final interface = await NetInterface.localInterface(); if (interface != null) { - final hostId = interface.hostId; + hostId = interface.hostId; interfaceIp = interface.networkId; myOwnHost = interface.ipAddress; // Better to restrict to scan from hostId - 1 to hostId + 1 to prevent GHA timeouts @@ -78,6 +79,28 @@ void main() { ), emits(isA()), ); + expectLater( + //There should be at least one device pingable in network when limiting to own hostId + HostScannerService.instance.getAllPingableDevices( + interfaceIp, + timeoutInSeconds: 3, + hostIds: [hostId], + firstHostId: firstHostId, + lastHostId: lastHostId, + ), + emits(isA()), + ); + expectLater( + //There should be at least one device pingable in network when limiting to hostId other than own + HostScannerService.instance.getAllPingableDevices( + interfaceIp, + timeoutInSeconds: 3, + hostIds: [0], + firstHostId: firstHostId, + lastHostId: lastHostId, + ), + neverEmits(isA()), + ); expectLater( //Should emit at least our own local machine when pinging all hosts. HostScannerService.instance.getAllPingableDevices( @@ -122,6 +145,28 @@ void main() { ), ), ); + expectLater( + //There should be at least one device pingable in network when limiting to own hostId + HostScannerService.instance.getAllPingableDevicesAsync( + interfaceIp, + timeoutInSeconds: 3, + hostIds: [hostId], + firstHostId: firstHostId, + lastHostId: lastHostId, + ), + emits(isA()), + ); + expectLater( + //There should be at least one device pingable in network when limiting to hostId other than own + HostScannerService.instance.getAllPingableDevicesAsync( + interfaceIp, + timeoutInSeconds: 3, + hostIds: [0], + firstHostId: firstHostId, + lastHostId: lastHostId, + ), + neverEmits(isA()), + ); }, );