refactor: depend on an Iperf3Runner interface instead of the service

The presentation layer imported Iperf3Service directly, so the UI was welded
to the MethodChannel transport: the view model named it, and the screen
constructed one. Dependency direction was already inward-only, but the UI
depended on an implementation rather than a contract.

Iperf3Runner states that contract in the domain layer in five members, in pure
Dart with no Flutter import, and Iperf3Service implements it. The view model
and screen now take an Iperf3Runner; main.dart is the only file that names a
concrete one, which makes it the composition root.

Two consequences worth having. The UI can be driven against an in-memory
runner with no MethodChannel, native library or device involved. And a test
double implements five stable members rather than tracking every public member
the transport happens to expose — the previous double, forced to implement the
concrete class, broke twice while this branch's work was going on.

Also drops flutter/foundation from the data layer: debugPrint gives way to
dart:developer, leaving flutter/services (where MethodChannel lives) as that
layer's only Flutter dependency.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-26 19:56:03 +09:00
parent defd64a073
commit 314a3ea059
5 changed files with 85 additions and 30 deletions
+27 -14
View File
@@ -1,12 +1,13 @@
import 'dart:async';
import 'dart:developer' as developer;
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:iperf3gui/domain/models/iperf3_event.dart';
import 'package:iperf3gui/domain/models/iperf3_result.dart';
import 'package:iperf3gui/domain/models/test_configuration.dart';
import 'package:iperf3gui/domain/services/iperf3_runner.dart';
/// Service that runs iperf3 via JNI (native library).
/// [Iperf3Runner] backed by the native iperf3 library over a MethodChannel.
///
/// On Android, iperf3 is compiled into the app as a shared library (.so).
/// This avoids SELinux restrictions on executing ELF binaries from assets.
@@ -16,13 +17,17 @@ import 'package:iperf3gui/domain/models/test_configuration.dart';
/// they are parsed and published on [events] while the test runs. [run]
/// resolves once the native layer reports completion, carrying the raw JSON
/// lines as the result's output.
class Iperf3Service {
class Iperf3Service implements Iperf3Runner {
Iperf3Service() {
_channel.setMethodCallHandler(_handleNativeCall);
}
static const MethodChannel _channel =
MethodChannel('com.novoyuuparosk.iperf3gui/iperf');
/// A channel's handler is keyed by channel name in the binary messenger, so
/// it is process-global: constructing a second instance would take the
/// handler away from the first. The app composes exactly one, in main().
final MethodChannel _channel = const MethodChannel(_channelName);
static const String _channelName = 'com.novoyuuparosk.iperf3gui/iperf';
/// Exit code reported when the native layer signalled an error.
static const int exitCodeNativeError = 1;
@@ -41,6 +46,7 @@ class Iperf3Service {
StreamController<Iperf3Event>.broadcast();
/// Live reports, one event per native report. Use this to render progress.
@override
Stream<Iperf3Event> get events => _eventController.stream;
/// Raw JSON lines received during the current (or most recent) run.
@@ -51,6 +57,7 @@ class Iperf3Service {
/// Whether iperf3 is currently running.
bool _isRunning = false;
@override
bool get isRunning => _isRunning;
/// Whether the native layer reported an error during this run.
@@ -60,17 +67,19 @@ class Iperf3Service {
bool _stopRequested = false;
/// Cancel any running test.
@override
Future<void> stop() async {
if (!_isRunning) return;
_stopRequested = true;
try {
await _channel.invokeMethod('stopIperf');
} catch (e) {
debugPrint('Iperf3Service: error stopping: $e');
_log('Iperf3Service: error stopping: $e');
}
}
/// Release the stream controller. Call from the owner's dispose().
@override
Future<void> dispose() async {
_channel.setMethodCallHandler(null);
await _eventController.close();
@@ -79,6 +88,7 @@ class Iperf3Service {
/// Run iperf3 with the given configuration.
///
/// Returns an [Iperf3Result] with the full output and an exit code.
@override
Future<Iperf3Result> run(TestConfiguration config) async {
if (_isRunning) {
return const Iperf3Result(
@@ -93,7 +103,7 @@ class Iperf3Service {
_rawLines.clear();
final args = config.toCliArgs();
debugPrint('Iperf3Service: running: ${args.join(" ")}');
_log('Iperf3Service: running: ${args.join(" ")}');
final completer = Completer<Iperf3Result>();
_runCompleter = completer;
@@ -101,8 +111,8 @@ class Iperf3Service {
try {
await _channel.invokeMethod('runIperf', {'args': args});
} catch (e, stackTrace) {
debugPrint('Iperf3Service exception: $e');
debugPrint('Iperf3Service stack: $stackTrace');
_log('Iperf3Service exception: $e');
_log('Iperf3Service stack: $stackTrace');
_finishRun();
return Iperf3Result(
rawOutput: 'Error starting iperf3: $e',
@@ -116,11 +126,11 @@ class Iperf3Service {
return await completer.future.timeout(
config.duration + const Duration(seconds: 45),
onTimeout: () {
debugPrint('Iperf3Service: timeout, stopping test');
_log('Iperf3Service: timeout, stopping test');
// Ask the native layer to wind down, then release local state so a
// stuck run can never lock the UI out of starting another test.
_channel.invokeMethod('stopIperf').catchError((Object e) {
debugPrint('Iperf3Service: error stopping after timeout: $e');
_log('Iperf3Service: error stopping after timeout: $e');
return null;
});
_publish(const Iperf3Error('Timed out waiting for iperf3 to finish.'));
@@ -131,7 +141,7 @@ class Iperf3Service {
},
);
} catch (e) {
debugPrint('Iperf3Service: error waiting for completion: $e');
_log('Iperf3Service: error waiting for completion: $e');
return Iperf3Result(
rawOutput: 'Test failed: $e',
exitCode: exitCodeStartFailed,
@@ -163,7 +173,7 @@ class Iperf3Service {
_publish(Iperf3Error(error));
}
case 'onComplete':
debugPrint('Iperf3Service: test completed');
_log('Iperf3Service: test completed');
final completer = _runCompleter;
if (completer != null && !completer.isCompleted) {
completer.complete(
@@ -177,7 +187,7 @@ class Iperf3Service {
);
}
default:
debugPrint('Iperf3Service: unhandled native call ${call.method}');
_log('Iperf3Service: unhandled native call ${call.method}');
}
}
@@ -197,3 +207,6 @@ class Iperf3Service {
_runCompleter = null;
}
}
void _log(String message) =>
developer.log(message, name: 'Iperf3Service');