From a9c5cc0ce12e3e946eb804463ad786eff2a401d6 Mon Sep 17 00:00:00 2001 From: Chris K Date: Wed, 1 Feb 2023 17:20:25 +0300 Subject: [PATCH] Fix #20 : Added "unreachable" ErrorType and regex. --- dart_ping/lib/src/models/ping_error.dart | 5 ++++- dart_ping/lib/src/models/ping_parser.dart | 24 ++++++++++++++++++++--- dart_ping/lib/src/ping/linux_ping.dart | 14 +++++++++---- dart_ping/lib/src/ping/mac_ping.dart | 1 + dart_ping/lib/src/ping/windows_ping.dart | 3 ++- 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/dart_ping/lib/src/models/ping_error.dart b/dart_ping/lib/src/models/ping_error.dart index 2be68b7..4ddc407 100644 --- a/dart_ping/lib/src/models/ping_error.dart +++ b/dart_ping/lib/src/models/ping_error.dart @@ -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); @@ -74,6 +75,8 @@ enum ErrorType { return ErrorType.unknownHost; case 'No Reply': return ErrorType.noReply; + case 'Unreachable': + return ErrorType.unreachable; default: return ErrorType.unknown; } diff --git a/dart_ping/lib/src/models/ping_parser.dart b/dart_ping/lib/src/models/ping_parser.dart index 1dae31a..969cf0d 100644 --- a/dart_ping/lib/src/models/ping_parser.dart +++ b/dart_ping/lib/src/models/ping_parser.dart @@ -14,6 +14,7 @@ class PingParser { required this.summaryRgx, required this.timeoutStr, required this.unknownHostStr, + required this.unreachableStr, this.errorStr, }); @@ -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 get responseParser => StreamTransformer.fromHandlers( + StreamTransformer get responseParser => + StreamTransformer.fromHandlers( handleData: (data, sink) { // Timeout if (sequenceRgx != null && data.contains(timeoutStr)) { @@ -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( @@ -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)), ), ), ); @@ -121,6 +130,15 @@ class PingParser { ); } + // Host or Network Unreachable + if (data.contains(unreachableStr)) { + sink.add( + PingData( + error: PingError(ErrorType.unreachable), + ), + ); + } + // Other error if (errorStr != null && data.contains(errorStr!)) { sink.add( diff --git a/dart_ping/lib/src/ping/linux_ping.dart b/dart_ping/lib/src/ping/linux_ping.dart index f1fff7d..78777ba 100644 --- a/dart_ping/lib/src/ping/linux_ping.dart +++ b/dart_ping/lib/src/ping/linux_ping.dart @@ -26,12 +26,16 @@ class PingLinux extends BasePing implements Ping { static PingParser get _parser => PingParser( responseStr: RegExp(r'bytes from'), - responseRgx: RegExp(r'from (?.*): icmp_seq=(?\d+) ttl=(?\d+) time=(?