Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ping ios register Added #111

Merged
merged 5 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ name: Dart
on:
push:
branches: [ "main" ]
paths: ['network_tools/**']
pull_request:
branches: [ "main", "dev" ]
paths: ['network_tools/**']

concurrency:
group: ${{ github.head_ref }}
cancel-in-progress: true

jobs:
build:
Expand Down Expand Up @@ -45,6 +51,4 @@ jobs:
# want to change this to 'flutter test'.
- name: Run tests
run: dart test
working-directory: ./network_tools


working-directory: ./network_tools
71 changes: 71 additions & 0 deletions .github/workflows/flutter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Dart

on:
push:
branches: [ "main" ]
paths: ['network_tools_flutter/**']
pull_request:
branches: [ "main", "dev" ]
paths: ['network_tools_flutter/**']

concurrency:
group: ${{ github.head_ref }}
cancel-in-progress: true

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v3
- name: Setup Java JDK
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: '17'
- name: Flutter action
uses: subosito/flutter-action@v2.8.0
with:
channel: stable
- name: Flutter version
run: flutter --version
- name: Cache pubspec dependencies
uses: actions/cache@v3.0.7
with:
path: |
${{ env.FLUTTER_HOME }}/.pub-cache
**/.packages
**/.flutter-plugins
**/.flutter-plugin-dependencies
**/.dart_tool/package_config.json
key: build-pubspec-${{ hashFiles('**/pubspec.lock') }}
restore-keys: |
build-pubspec-
- name: Cache build runner
uses: actions/cache@v2
with:
path: |
**/.dart_tool
**/*.g.dart
**/*.mocks.dart
**/*.config.dart
key: build-runner-${{ hashFiles('**/asset_graph.json', '**/*.dart', '**/pubspec.lock', '**/outputs.json') }}
restore-keys: |
build-runner-
- name: Download pub dependencies
run: flutter pub get
working-directory: ./network_tools_flutter
- name: Run analyzer
run: flutter analyze
working-directory: ./network_tools_flutter
- name: Run tests
run: flutter test
working-directory: ./network_tools_flutter
4 changes: 4 additions & 0 deletions network_tools/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 3.2.2

1. Supporting changes for network_tools_flutter

## 3.2.1

1. Now GHA runs tests against 3 platforms windows, ubuntu, macos
Expand Down
9 changes: 5 additions & 4 deletions network_tools/lib/src/host_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HostScanner {
bool resultsInAddressAscendingOrder = true,
}) async* {
final int lastValidSubnet =
_validateAndGetLastValidSubnet(subnet, firstHostId, lastHostId);
validateAndGetLastValidSubnet(subnet, firstHostId, lastHostId);
final List<Future<ActiveHost?>> activeHostsFuture = [];
final StreamController<ActiveHost> activeHostsController =
StreamController<ActiveHost>();
Expand Down Expand Up @@ -88,7 +88,7 @@ class HostScanner {
return null;
}

static int _validateAndGetLastValidSubnet(
static int validateAndGetLastValidSubnet(
String subnet,
int firstHostId,
int lastHostId,
Expand Down Expand Up @@ -119,7 +119,7 @@ class HostScanner {

const int scanRangeForIsolate = 51;
final int lastValidSubnet =
_validateAndGetLastValidSubnet(subnet, firstHostId, lastHostId);
validateAndGetLastValidSubnet(subnet, firstHostId, lastHostId);
for (int i = firstHostId;
i <= lastValidSubnet;
i += scanRangeForIsolate + 1) {
Expand Down Expand Up @@ -149,6 +149,7 @@ class HostScanner {
}

/// Will search devices in the network inside new isolate
@pragma('vm:entry-point')
static Future<void> _startSearchingDevices(dynamic params) async {
final channel = IsolateManagerController(params);
channel.onIsolateMessage.listen((message) async {
Expand Down Expand Up @@ -202,7 +203,7 @@ class HostScanner {
bool resultsInAddressAscendingOrder = true,
}) async* {
final int lastValidSubnet =
_validateAndGetLastValidSubnet(subnet, firstHostId, lastHostId);
validateAndGetLastValidSubnet(subnet, firstHostId, lastHostId);
final List<Future<ActiveHost?>> activeHostOpenPortList = [];
final StreamController<ActiveHost> activeHostsController =
StreamController<ActiveHost>();
Expand Down
41 changes: 20 additions & 21 deletions network_tools/lib/src/models/active_host.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class ActiveHost extends Comparable<ActiveHost> {

deviceName = setDeviceName();
}

factory ActiveHost.buildWithAddress({
required String address,
List<OpenPort> openPorts = const [],
Expand Down Expand Up @@ -96,26 +95,6 @@ class ActiveHost extends Comparable<ActiveHost> {
Duration? get responseTime => _pingData.response?.time;
String get address => internetAddress.address;

@override
int get hashCode => address.hashCode;

@override
bool operator ==(Object o) => o is ActiveHost && address == o.address;

@override
int compareTo(ActiveHost other) {
return hostId.compareTo(other.hostId);
}

@override
String toString() {
return 'Address: $address, HostId: $hostId, Time: ${responseTime?.inMilliseconds}ms, port: ${openPorts.join(",")}';
}

Future<String> toStringFull() async {
return 'Address: $address, HostId: $hostId Time: ${responseTime?.inMilliseconds}ms, DeviceName: ${await deviceName}, HostName: ${await hostName}, MdnsInfo: ${await mdnsInfo}';
}

static PingData getPingData(String host) {
const int timeoutInSeconds = 1;

Expand Down Expand Up @@ -182,4 +161,24 @@ class ActiveHost extends Comparable<ActiveHost> {
}
return generic;
}

@override
int get hashCode => address.hashCode;

@override
bool operator ==(Object o) => o is ActiveHost && address == o.address;

@override
int compareTo(ActiveHost other) {
return hostId.compareTo(other.hostId);
}

@override
String toString() {
return 'Address: $address, HostId: $hostId, Time: ${responseTime?.inMilliseconds}ms, port: ${openPorts.join(",")}';
}

Future<String> toStringFull() async {
return 'Address: $address, HostId: $hostId Time: ${responseTime?.inMilliseconds}ms, DeviceName: ${await deviceName}, HostName: ${await hostName}, MdnsInfo: ${await mdnsInfo}';
}
}
2 changes: 1 addition & 1 deletion network_tools/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: network_tools
description: Networking Tools library which can help you discover open ports, devices on subnet and many other things.
version: 3.2.1
version: 3.2.2
issue_tracker: https://github.com/osociety/network_tools/issues
repository: https://github.com/osociety/network_tools/tree/main/network_tools

Expand Down
4 changes: 3 additions & 1 deletion network_tools_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Change Log

## 0.0.1

* TODO: Describe initial release.
* Initial support added for ping_ios_dart in network_tools
35 changes: 10 additions & 25 deletions network_tools_flutter/README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.

For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).

For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.
# Network Tools Flutter

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.
This package will add support for flutter only features in network_tools, network_tools will still be required to be added in pubspec.yaml.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.
```dart
import 'package:network_tools_flutter/network_tools.dart';

## Usage
```

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.
## Usage

```dart
const like = 'sample';
main() {
NetworkToolsFlutter.init();
}
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
Currently getAllPingableDevicesAsync() is not working on ios because of plugin registration
6 changes: 1 addition & 5 deletions network_tools_flutter/lib/network_tools_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
library network_tools_flutter;

/// A Calculator.
class Calculator {
/// Returns [value] plus 1.
int addOne(int value) => value + 1;
}
export 'src/host_scanner_flutter.dart';
95 changes: 95 additions & 0 deletions network_tools_flutter/lib/src/host_scanner_flutter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import 'dart:async';
import 'dart:isolate';
import 'dart:math';

import 'package:dart_ping_ios/dart_ping_ios.dart';
import 'package:network_tools/network_tools.dart';

/// Scans for all hosts in a subnet.
class HostScannerFlutter {
/// Scans for all hosts in a particular subnet (e.g., 192.168.1.0/24)
/// 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.
/// [resultsInAddressAscendingOrder] = false will return results faster but not in
static Future<Stream<ActiveHost>> getAllPingableDevices(
String subnet, {
int firstHostId = HostScanner.defaultFirstHostId,
int lastHostId = HostScanner.defaultLastHostId,
int timeoutInSeconds = 1,
ProgressCallback? progressCallback,
bool resultsInAddressAscendingOrder = true,
}) async {
const int scanRangeForIsolate = 51;
final StreamController<ActiveHost> activeHostsController =
StreamController<ActiveHost>();
final int lastValidSubnet = HostScanner.validateAndGetLastValidSubnet(
subnet, firstHostId, lastHostId);

for (int i = firstHostId;
i <= lastValidSubnet;
i += scanRangeForIsolate + 1) {
final limit = min(i + scanRangeForIsolate, lastValidSubnet);
final receivePort = ReceivePort();
final isolate = await Isolate.spawn(
HostScannerFlutter._startSearchingDevices, receivePort.sendPort);

receivePort.listen((message) {
if (message is SendPort) {
message.send([
subnet,
i.toString(),
limit.toString(),
timeoutInSeconds.toString(),
resultsInAddressAscendingOrder.toString()
]);
} else if (message is ActiveHost) {
progressCallback
?.call((i - firstHostId) * 100 / (lastValidSubnet - firstHostId));
activeHostsController.add(message);
} else if (message is String && message == 'Done') {
isolate.kill();
}
});
}
return activeHostsController.stream;
}

/// Will search devices in the network inside new isolate
// @pragma('vm:entry-point')
static Future<void> _startSearchingDevices(SendPort sendPort) async {
DartPingIOS.register();
final port = ReceivePort();
sendPort.send(port.sendPort);

await for (List message in port) {
final String subnetIsolate = message[0];
final int firstSubnetIsolate = int.parse(message[1]);
final int lastSubnetIsolate = int.parse(message[2]);
final int timeoutInSeconds = int.parse(message[3]);
final bool resultsInAddressAscendingOrder = message[4] == "true";

/// Will contain all the hosts that got discovered in the network, will
/// be use inorder to cancel on dispose of the page.
final Stream<ActiveHost> hostsDiscoveredInNetwork =
HostScanner.getAllPingableDevices(
subnetIsolate,
firstHostId: firstSubnetIsolate,
lastHostId: lastSubnetIsolate,
timeoutInSeconds: timeoutInSeconds,
resultsInAddressAscendingOrder: resultsInAddressAscendingOrder,
);

await for (final ActiveHost activeHostFound in hostsDiscoveredInNetwork) {
activeHostFound.deviceName.then((value) {
activeHostFound.mdnsInfo.then((value) {
activeHostFound.hostName.then((value) {
sendPort.send(activeHostFound);
});
});
});
}
sendPort.send('Done');
}
}
}
Loading