LCOV - code coverage report

Current view
top level - /src - worker_request.dart
Test
lcov.info
Date
2022-04-02
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines424397.7%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'cancellation_token.dart';
2import 'channel.dart';
3import 'squadron.dart';
4import 'squadron_error.dart';
5import 'worker.dart';
6
7/// Class used to communicate from a [Channel] to the [Worker]. Typically a [WorkerRequest] consists of a command ID
8/// and a list of arguments. The [command] ID is used by the [Worker] to dispatch the [WorkerRequest] to the method
9/// responsible for handling it. The [WorkerRequest] is effectively sent to the [Worker] by calling the
10/// [WorkerRequest.send] or [WorkerRequest.stream] method. These methods will serialize the [WorkerRequest] as a
11/// [Map] to be transfered from the client to the worker and contains:
12/// * the serialized [Channel] to communicate with the [Worker]
13/// * the [command] ID
14/// * the command's arguments [args]
15/// The command's arguments are passed as a list and should only contain primitive values or objects that can be
16/// transfered across workers. For applications running on a VM platform, Dart objects should be safe according to
17/// Dart's documentation of [SendPort.send]. The worker is responsible for deserializing the messages it receives
18/// using the [WorkerRequest.deserialize] constructor. [WorkerRequest] also implements specific requests used
19/// for worker startup, token cancellation, and worker termination.
20class WorkerRequest {
21 static const _noArgs = [];
22
231 WorkerRequest._(dynamic channelInfo, this.command, this.id, this.args,
24 this.logLevel, this._cancelToken)
251 : client = WorkerChannel.deserialize(channelInfo);
26
27 /// Creates a new request with the specified [command] ID and optional arguments.
282 WorkerRequest(dynamic channelInfo, int command,
29 [List args = _noArgs, CancellationToken? cancelToken])
302 : this._(channelInfo, command, null, args, null, cancelToken);
31
32 /// Creates a new start request.
332 WorkerRequest.start(dynamic channelInfo, String id, [List args = _noArgs])
343 : this._(channelInfo, _connectCommand, id, args, Squadron.logLevel, null);
35
36 /// Creates a new cancel request.
372 WorkerRequest.cancel(CancellationToken cancelToken)
382 : this._(null, _cancelCommand, null, _noArgs, null, cancelToken);
39
40 /// Creates a new termination request.
412 WorkerRequest.stop()
421 : this._(
431 null, _terminateCommand, null, _noArgs, null, null);
44
45 static const _$client = 'a';
46 static const _$command = 'b';
47 static const _$args = 'c';
48 static const _$token = 'd';
49 static const _$id = 'e';
50 static const _$logLevel = 'f';
51
52 /// Creates a new [WorkerRequest] from a message received by the worker.
531 static WorkerRequest? deserialize(Map? message) => (message == null)
54 ? null
551 : WorkerRequest._(
562 message[_$client],
572 message[_$command],
582 message[_$id],
592 message[_$args] ?? const [],
602 message[_$logLevel],
613 CancellationToken.deserialize(message[_$token]));
62
63 /// [WorkerRequest] serialization.
642 Map<String, dynamic> serialize() {
652 if (terminate) {
661 return const {_$command: _terminateCommand};
672 } else if (connect) {
682 return {
694 _$client: client?.serialize(),
701 _$command: _connectCommand,
713 _$id: id,
723 _$logLevel: logLevel,
735 if (args.isNotEmpty) _$args: args,
74 };
75 } else {
762 return {
775 if (client != null) _$client: client?.serialize(),
782 _$command: command,
795 if (args.isNotEmpty) _$args: args,
805 if (_cancelToken != null) _$token: _cancelToken!.serialize(),
81 };
82 }
831 }
84
85 /// The client's [WorkerChannel].
86 final WorkerChannel? client;
87
88 /// Cancellation token.
892 CancellationToken? get cancelToken => _cancelToken;
90 CancellationToken? _cancelToken;
91
92 /// The [command]'s ID.
93 final int command;
94
95 /// The command's arguments, if any.
96 final List args;
97
98 /// The worker id set by the caller, used for logging/debugging purpose.
99 /// This is only used for connection commands.
100 final String? id;
101
102 /// The current Squadron log level.
103 /// This is set automaticallt and only used for connection commands.
104 final int? logLevel;
105
106 /// flag for start requests.
1073 bool get connect => command == _connectCommand;
108
109 /// flag for cancel requests.
1103 bool get cancel => command == _cancelCommand;
111
112 /// flag for termination requests.
1134 bool get terminate => command == _terminateCommand;
114
115 static const int _connectCommand = -1;
116 static const int _cancelCommand = -2;
117 static const int _terminateCommand = -3;
118}
119
120// private implementation internal to Squadron
121extension WorkerRequestExt on WorkerRequest {
1221 void overrideCancelToken(CancellationToken token) {
1235 if (_cancelToken == null || _cancelToken!.id != token.id) {
1240 throw newSquadronError('cancellation token mismatch');
125 }
1261 _cancelToken = token;
127 }
128}
Choose Features