Skip to content
Open
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
5 changes: 4 additions & 1 deletion dart_ping/lib/src/models/ping_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ enum ErrorType {
requestTimedOut('Request Timed Out'),
unknownHost('Unknown Host'),
unknown('Unknown Error'),
noReply('No Reply');
noReply('No Reply'),
unreachable('Host/Network Unreachable');

const ErrorType(this.message);

Expand All @@ -74,6 +75,8 @@ enum ErrorType {
return ErrorType.unknownHost;
case 'No Reply':
return ErrorType.noReply;
case 'Unreachable':
return ErrorType.unreachable;
default:
return ErrorType.unknown;
}
Expand Down
24 changes: 21 additions & 3 deletions dart_ping/lib/src/models/ping_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PingParser {
required this.summaryRgx,
required this.timeoutStr,
required this.unknownHostStr,
required this.unreachableStr,
this.errorStr,
});

Expand Down Expand Up @@ -41,11 +42,15 @@ class PingParser {
/// String used to detect an unknown host error
RegExp unknownHostStr;

/// String used to detect a host unreachable error
RegExp unreachableStr;

/// String(s) used to detect misc unknown error(s)
RegExp? errorStr;

// ignore: long-method
StreamTransformer<String, PingData> get responseParser => StreamTransformer<String, PingData>.fromHandlers(
StreamTransformer<String, PingData> get responseParser =>
StreamTransformer<String, PingData>.fromHandlers(
handleData: (data, sink) {
// Timeout
if (sequenceRgx != null && data.contains(timeoutStr)) {
Expand All @@ -70,7 +75,9 @@ class PingParser {
if (match == null) {
return;
}
var seq = match.groupNames.contains('seq') ? match.namedGroup('seq') : null;
var seq = match.groupNames.contains('seq')
? match.namedGroup('seq')
: null;
var ttl = match.namedGroup('ttl');
var time = match.namedGroup('time');
sink.add(
Expand Down Expand Up @@ -106,7 +113,9 @@ class PingParser {
summary: PingSummary(
transmitted: int.parse(tx),
received: int.parse(rx),
time: time == null ? null : Duration(milliseconds: int.parse(time)),
time: time == null
? null
: Duration(milliseconds: int.parse(time)),
),
),
);
Expand All @@ -121,6 +130,15 @@ class PingParser {
);
}

// Host or Network Unreachable
if (data.contains(unreachableStr)) {
Copy link
Owner

@point-source point-source Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These if statements should probably become if/else unless we can guarantee that the presence of one truthy contains() negates the others. Reason being that we don't want one line of ping output to result in two separate PingData events.

sink.add(
PingData(
error: PingError(ErrorType.unreachable),
),
);
}

// Other error
if (errorStr != null && data.contains(errorStr!)) {
sink.add(
Expand Down
14 changes: 10 additions & 4 deletions dart_ping/lib/src/ping/linux_ping.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ class PingLinux extends BasePing implements Ping {

static PingParser get _parser => PingParser(
responseStr: RegExp(r'bytes from'),
responseRgx: RegExp(r'from (?<ip>.*): icmp_seq=(?<seq>\d+) ttl=(?<ttl>\d+) time=(?<time>(\d+).?(\d+))'),
responseRgx: RegExp(
r'from (?<ip>.*): icmp_seq=(?<seq>\d+) ttl=(?<ttl>\d+) time=(?<time>(\d+).?(\d+))'),
sequenceRgx: RegExp(r'icmp_seq=(?<seq>\d+)'),
summaryStr: RegExp(r'packet loss'),
summaryRgx: RegExp(r'(?<tx>\d+) packets transmitted, (?<rx>\d+) received,.*time (?<time>\d+)ms'),
summaryRgx: RegExp(
r'(?<tx>\d+) packets transmitted, (?<rx>\d+) received,.*time (?<time>\d+)ms'),
timeoutStr: RegExp(r'no answer yet'),
unknownHostStr: RegExp(r'unknown host|service not known|failure in name'),
unknownHostStr:
RegExp(r'unknown host|service not known|failure in name'),
unreachableStr: RegExp(r'[Uu]nreachable'),
);

@override
Expand All @@ -52,6 +56,8 @@ class PingLinux extends BasePing implements Ping {

@override
Exception? throwExit(int exitCode) {
return exitCode > 1 ? Exception('Ping process exited with code: $exitCode') : null;
return exitCode > 1
? Exception('Ping process exited with code: $exitCode')
: null;
}
}
1 change: 1 addition & 0 deletions dart_ping/lib/src/ping/mac_ping.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PingMac extends BasePing implements Ping {
),
timeoutStr: RegExp(r'Request timeout'),
unknownHostStr: RegExp(r'Unknown host'),
unreachableStr: RegExp(r'[Uu]nreachable'),
);

@override
Expand Down
3 changes: 2 additions & 1 deletion dart_ping/lib/src/ping/windows_ping.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class PingWindows extends BasePing implements Ping {
summaryStr: RegExp(r'Lost'),
summaryRgx:
RegExp(r'Sent = (?<tx>\d+), Received = (?<rx>\d+), Lost = (?:\d+)'),
timeoutStr: RegExp(r'host unreachable|timed out'),
timeoutStr: RegExp(r'timed out'),
unknownHostStr: RegExp(r'could not find host'),
unreachableStr: RegExp(r'[Uu]nreachable'),
errorStr: RegExp(r'transmit failed'),
);

Expand Down