48 lines
1.3 KiB
Dart
48 lines
1.3 KiB
Dart
|
|
import 'dart:convert';
|
||
|
|
import 'dart:io';
|
||
|
|
|
||
|
|
import 'package:iperf3gui/domain/models/iperf3_result.dart';
|
||
|
|
import 'package:iperf3gui/domain/models/test_configuration.dart';
|
||
|
|
|
||
|
|
/// Service that runs iperf3 via [Process.spawn] and captures output.
|
||
|
|
class Iperf3Service {
|
||
|
|
final String _binaryPath;
|
||
|
|
|
||
|
|
// ignore: prefer_initializing_formals
|
||
|
|
Iperf3Service({required String binaryPath}) : _binaryPath = binaryPath;
|
||
|
|
|
||
|
|
/// Run iperf3 with the given configuration.
|
||
|
|
///
|
||
|
|
/// Returns an [Iperf3Result] with stdout/stderr and exit code.
|
||
|
|
Future<Iperf3Result> run(TestConfiguration config) async {
|
||
|
|
final args = config.toCliArgs();
|
||
|
|
|
||
|
|
final process = await Process.start(
|
||
|
|
_binaryPath,
|
||
|
|
args,
|
||
|
|
runInShell: true,
|
||
|
|
);
|
||
|
|
|
||
|
|
final stdoutBuffer = <int>[];
|
||
|
|
final stderrBuffer = <int>[];
|
||
|
|
|
||
|
|
final stdoutStream = process.stdout.transform(utf8.decoder).listen(
|
||
|
|
(data) => stdoutBuffer.addAll(utf8.encode(data)),
|
||
|
|
);
|
||
|
|
final stderrStream = process.stderr.transform(utf8.decoder).listen(
|
||
|
|
(data) => stderrBuffer.addAll(utf8.encode(data)),
|
||
|
|
);
|
||
|
|
|
||
|
|
await process.exitCode;
|
||
|
|
await stdoutStream.cancel();
|
||
|
|
await stderrStream.cancel();
|
||
|
|
|
||
|
|
final rawOutput = utf8.decode(stdoutBuffer) + utf8.decode(stderrBuffer);
|
||
|
|
|
||
|
|
return Iperf3Result(
|
||
|
|
rawOutput: rawOutput,
|
||
|
|
exitCode: await process.exitCode,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|