From 0ed10a1b11d1fe1ebc8e61626d27c72a57c92de7 Mon Sep 17 00:00:00 2001 From: Paul Kepinski Date: Mon, 4 Nov 2024 08:35:30 +0100 Subject: [PATCH] Isolate message self handle (#14) --- .../sane/lib/src/isolate_messages/cancel.dart | 11 +- .../sane/lib/src/isolate_messages/close.dart | 12 +- .../control_button_option.dart | 15 +- .../src/isolate_messages/control_option.dart | 57 +++- .../sane/lib/src/isolate_messages/exit.dart | 9 +- .../get_all_option_descriptors.dart | 12 +- .../lib/src/isolate_messages/get_devices.dart | 8 + .../get_option_descriptor.dart | 12 +- .../src/isolate_messages/get_parameters.dart | 12 +- .../sane/lib/src/isolate_messages/init.dart | 10 +- .../lib/src/isolate_messages/interface.dart | 8 +- .../sane/lib/src/isolate_messages/open.dart | 8 + .../sane/lib/src/isolate_messages/read.dart | 12 +- .../lib/src/isolate_messages/set_io_mode.dart | 11 +- .../sane/lib/src/isolate_messages/start.dart | 11 +- packages/sane/lib/src/sane_isolate.dart | 280 ++---------------- 16 files changed, 202 insertions(+), 286 deletions(-) diff --git a/packages/sane/lib/src/isolate_messages/cancel.dart b/packages/sane/lib/src/isolate_messages/cancel.dart index 1c75179..4b5d65b 100644 --- a/packages/sane/lib/src/isolate_messages/cancel.dart +++ b/packages/sane/lib/src/isolate_messages/cancel.dart @@ -1,10 +1,17 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; import 'package:sane/src/structures.dart'; class CancelMessage implements IsolateMessage { - CancelMessage({required this.handle}); + CancelMessage({required this.saneHandle}); - final SaneHandle handle; + final SaneHandle saneHandle; + + @override + Future handle(Sane sane) async { + await sane.cancel(saneHandle); + return CancelResponse(); + } } class CancelResponse implements IsolateResponse {} diff --git a/packages/sane/lib/src/isolate_messages/close.dart b/packages/sane/lib/src/isolate_messages/close.dart index 609d37b..028f854 100644 --- a/packages/sane/lib/src/isolate_messages/close.dart +++ b/packages/sane/lib/src/isolate_messages/close.dart @@ -1,10 +1,16 @@ +import 'package:sane/sane.dart'; import 'package:sane/src/isolate_messages/interface.dart'; -import 'package:sane/src/structures.dart'; class CloseMessage implements IsolateMessage { - CloseMessage({required this.handle}); + CloseMessage({required this.saneHandle}); - final SaneHandle handle; + final SaneHandle saneHandle; + + @override + Future handle(Sane sane) async { + await sane.close(saneHandle); + return CloseResponse(); + } } class CloseResponse implements IsolateResponse {} diff --git a/packages/sane/lib/src/isolate_messages/control_button_option.dart b/packages/sane/lib/src/isolate_messages/control_button_option.dart index b5ba505..8bff2f2 100644 --- a/packages/sane/lib/src/isolate_messages/control_button_option.dart +++ b/packages/sane/lib/src/isolate_messages/control_button_option.dart @@ -1,14 +1,25 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; import 'package:sane/src/structures.dart'; class ControlButtonOptionMessage implements IsolateMessage { ControlButtonOptionMessage({ - required this.handle, + required this.saneHandle, required this.index, }); - final SaneHandle handle; + final SaneHandle saneHandle; final int index; + + @override + Future handle(Sane sane) async { + return ControlButtonOptionResponse( + result: await sane.controlButtonOption( + handle: saneHandle, + index: index, + ), + ); + } } class ControlButtonOptionResponse implements IsolateResponse { diff --git a/packages/sane/lib/src/isolate_messages/control_option.dart b/packages/sane/lib/src/isolate_messages/control_option.dart index ef8c366..5b33f39 100644 --- a/packages/sane/lib/src/isolate_messages/control_option.dart +++ b/packages/sane/lib/src/isolate_messages/control_option.dart @@ -1,22 +1,67 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; import 'package:sane/src/structures.dart'; -class ControlOptionMessage implements IsolateMessage { - ControlOptionMessage({ - required this.handle, +class ControlValueOptionMessage implements IsolateMessage { + ControlValueOptionMessage({ + required this.saneHandle, required this.index, required this.action, this.value, }); - final SaneHandle handle; + final SaneHandle saneHandle; final int index; final SaneAction action; final T? value; + + @override + Future handle(Sane sane) async { + switch (value) { + case final bool value: + return ControlValueOptionResponse( + result: await sane.controlBoolOption( + handle: saneHandle, + index: index, + action: action, + value: value, + ), + ); + case final int value: + return ControlValueOptionResponse( + result: await sane.controlIntOption( + handle: saneHandle, + index: index, + action: action, + value: value, + ), + ); + case final double value: + return ControlValueOptionResponse( + result: await sane.controlFixedOption( + handle: saneHandle, + index: index, + action: action, + value: value, + ), + ); + case final String value: + return ControlValueOptionResponse( + result: await sane.controlStringOption( + handle: saneHandle, + index: index, + action: action, + value: value, + ), + ); + default: + throw Exception('Invalid value type.'); + } + } } -class ControlOptionResponse implements IsolateResponse { - ControlOptionResponse({required this.result}); +class ControlValueOptionResponse implements IsolateResponse { + ControlValueOptionResponse({required this.result}); final SaneOptionResult result; } diff --git a/packages/sane/lib/src/isolate_messages/exit.dart b/packages/sane/lib/src/isolate_messages/exit.dart index 0e0b9c3..0e797e2 100644 --- a/packages/sane/lib/src/isolate_messages/exit.dart +++ b/packages/sane/lib/src/isolate_messages/exit.dart @@ -1,5 +1,12 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; -class ExitMessage implements IsolateMessage {} +class ExitMessage implements IsolateMessage { + @override + Future handle(Sane sane) async { + await sane.exit(); + return ExitResponse(); + } +} class ExitResponse implements IsolateResponse {} diff --git a/packages/sane/lib/src/isolate_messages/get_all_option_descriptors.dart b/packages/sane/lib/src/isolate_messages/get_all_option_descriptors.dart index 51dca6a..b1da559 100644 --- a/packages/sane/lib/src/isolate_messages/get_all_option_descriptors.dart +++ b/packages/sane/lib/src/isolate_messages/get_all_option_descriptors.dart @@ -1,10 +1,18 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; import 'package:sane/src/structures.dart'; class GetAllOptionDescriptorsMessage implements IsolateMessage { - GetAllOptionDescriptorsMessage({required this.handle}); + GetAllOptionDescriptorsMessage({required this.saneHandle}); - final SaneHandle handle; + final SaneHandle saneHandle; + + @override + Future handle(Sane sane) async { + return GetAllOptionDescriptorsResponse( + optionDescriptors: await sane.getAllOptionDescriptors(saneHandle), + ); + } } class GetAllOptionDescriptorsResponse implements IsolateResponse { diff --git a/packages/sane/lib/src/isolate_messages/get_devices.dart b/packages/sane/lib/src/isolate_messages/get_devices.dart index d7e764c..5faab61 100644 --- a/packages/sane/lib/src/isolate_messages/get_devices.dart +++ b/packages/sane/lib/src/isolate_messages/get_devices.dart @@ -1,10 +1,18 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; import 'package:sane/src/structures.dart'; class GetDevicesMessage implements IsolateMessage { GetDevicesMessage({required this.localOnly}); final bool localOnly; + + @override + Future handle(Sane sane) async { + return GetDevicesResponse( + devices: await sane.getDevices(localOnly: localOnly), + ); + } } class GetDevicesResponse implements IsolateResponse { diff --git a/packages/sane/lib/src/isolate_messages/get_option_descriptor.dart b/packages/sane/lib/src/isolate_messages/get_option_descriptor.dart index 7dca91b..09a639b 100644 --- a/packages/sane/lib/src/isolate_messages/get_option_descriptor.dart +++ b/packages/sane/lib/src/isolate_messages/get_option_descriptor.dart @@ -1,14 +1,22 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; import 'package:sane/src/structures.dart'; class GetOptionDescriptorMessage implements IsolateMessage { GetOptionDescriptorMessage({ - required this.handle, + required this.saneHandle, required this.index, }); - final SaneHandle handle; + final SaneHandle saneHandle; final int index; + + @override + Future handle(Sane sane) async { + return GetOptionDescriptorResponse( + optionDescriptor: await sane.getOptionDescriptor(saneHandle, index), + ); + } } class GetOptionDescriptorResponse implements IsolateResponse { diff --git a/packages/sane/lib/src/isolate_messages/get_parameters.dart b/packages/sane/lib/src/isolate_messages/get_parameters.dart index 8b2e3f7..93cf5c9 100644 --- a/packages/sane/lib/src/isolate_messages/get_parameters.dart +++ b/packages/sane/lib/src/isolate_messages/get_parameters.dart @@ -1,10 +1,18 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; import 'package:sane/src/structures.dart'; class GetParametersMessage implements IsolateMessage { - GetParametersMessage({required this.handle}); + GetParametersMessage({required this.saneHandle}); - final SaneHandle handle; + final SaneHandle saneHandle; + + @override + Future handle(Sane sane) async { + return GetParametersResponse( + parameters: await sane.getParameters(saneHandle), + ); + } } class GetParametersResponse implements IsolateResponse { diff --git a/packages/sane/lib/src/isolate_messages/init.dart b/packages/sane/lib/src/isolate_messages/init.dart index f1c6a6c..e591045 100644 --- a/packages/sane/lib/src/isolate_messages/init.dart +++ b/packages/sane/lib/src/isolate_messages/init.dart @@ -1,6 +1,14 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; -class InitMessage implements IsolateMessage {} +class InitMessage implements IsolateMessage { + @override + Future handle(Sane sane) async { + return InitResponse( + versionCode: await sane.init(), + ); + } +} class InitResponse implements IsolateResponse { InitResponse({required this.versionCode}); diff --git a/packages/sane/lib/src/isolate_messages/interface.dart b/packages/sane/lib/src/isolate_messages/interface.dart index e511d98..24740d9 100644 --- a/packages/sane/lib/src/isolate_messages/interface.dart +++ b/packages/sane/lib/src/isolate_messages/interface.dart @@ -1,3 +1,7 @@ -interface class IsolateMessage {} +import 'package:sane/src/sane.dart'; -interface class IsolateResponse {} +abstract interface class IsolateMessage { + Future handle(Sane sane); +} + +abstract interface class IsolateResponse {} diff --git a/packages/sane/lib/src/isolate_messages/open.dart b/packages/sane/lib/src/isolate_messages/open.dart index bd7f9b8..dca171a 100644 --- a/packages/sane/lib/src/isolate_messages/open.dart +++ b/packages/sane/lib/src/isolate_messages/open.dart @@ -1,10 +1,18 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; import 'package:sane/src/structures.dart'; class OpenMessage implements IsolateMessage { OpenMessage({required this.deviceName}); final String deviceName; + + @override + Future handle(Sane sane) async { + return OpenResponse( + handle: await sane.open(deviceName), + ); + } } class OpenResponse implements IsolateResponse { diff --git a/packages/sane/lib/src/isolate_messages/read.dart b/packages/sane/lib/src/isolate_messages/read.dart index babe139..f375463 100644 --- a/packages/sane/lib/src/isolate_messages/read.dart +++ b/packages/sane/lib/src/isolate_messages/read.dart @@ -1,16 +1,24 @@ import 'dart:typed_data'; import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; import 'package:sane/src/structures.dart'; class ReadMessage implements IsolateMessage { ReadMessage({ - required this.handle, + required this.saneHandle, required this.bufferSize, }); - final SaneHandle handle; + final SaneHandle saneHandle; final int bufferSize; + + @override + Future handle(Sane sane) async { + return ReadResponse( + bytes: await sane.read(saneHandle, bufferSize), + ); + } } class ReadResponse implements IsolateResponse { diff --git a/packages/sane/lib/src/isolate_messages/set_io_mode.dart b/packages/sane/lib/src/isolate_messages/set_io_mode.dart index 44ee834..7478351 100644 --- a/packages/sane/lib/src/isolate_messages/set_io_mode.dart +++ b/packages/sane/lib/src/isolate_messages/set_io_mode.dart @@ -1,14 +1,21 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; import 'package:sane/src/structures.dart'; class SetIOModeMessage implements IsolateMessage { SetIOModeMessage({ - required this.handle, + required this.saneHandle, required this.ioMode, }); - final SaneHandle handle; + final SaneHandle saneHandle; final SaneIOMode ioMode; + + @override + Future handle(Sane sane) async { + await sane.setIOMode(saneHandle, ioMode); + return SetIOModeResponse(); + } } class SetIOModeResponse implements IsolateResponse {} diff --git a/packages/sane/lib/src/isolate_messages/start.dart b/packages/sane/lib/src/isolate_messages/start.dart index 7eb83ed..fe961b2 100644 --- a/packages/sane/lib/src/isolate_messages/start.dart +++ b/packages/sane/lib/src/isolate_messages/start.dart @@ -1,10 +1,17 @@ import 'package:sane/src/isolate_messages/interface.dart'; +import 'package:sane/src/sane.dart'; import 'package:sane/src/structures.dart'; class StartMessage implements IsolateMessage { - StartMessage({required this.handle}); + StartMessage({required this.saneHandle}); - final SaneHandle handle; + final SaneHandle saneHandle; + + @override + Future handle(Sane sane) async { + await sane.start(saneHandle); + return StartResponse(); + } } class StartResponse implements IsolateResponse {} diff --git a/packages/sane/lib/src/sane_isolate.dart b/packages/sane/lib/src/sane_isolate.dart index 73721d2..3794dca 100644 --- a/packages/sane/lib/src/sane_isolate.dart +++ b/packages/sane/lib/src/sane_isolate.dart @@ -106,7 +106,7 @@ class SaneIsolate implements Sane { Future close(SaneHandle handle) async { await _sendMessage( CloseMessage( - handle: handle, + saneHandle: handle, ), ); } @@ -118,7 +118,7 @@ class SaneIsolate implements Sane { ) async { final response = await _sendMessage( GetOptionDescriptorMessage( - handle: handle, + saneHandle: handle, index: index, ), ) as GetOptionDescriptorResponse; @@ -132,7 +132,7 @@ class SaneIsolate implements Sane { ) async { final response = await _sendMessage( GetAllOptionDescriptorsMessage( - handle: handle, + saneHandle: handle, ), ) as GetAllOptionDescriptorsResponse; @@ -147,13 +147,13 @@ class SaneIsolate implements Sane { bool? value, }) async { final response = await _sendMessage( - ControlOptionMessage( - handle: handle, + ControlValueOptionMessage( + saneHandle: handle, index: index, action: action, value: value, ), - ) as ControlOptionResponse; + ) as ControlValueOptionResponse; return response.result; } @@ -166,13 +166,13 @@ class SaneIsolate implements Sane { int? value, }) async { final response = await _sendMessage( - ControlOptionMessage( - handle: handle, + ControlValueOptionMessage( + saneHandle: handle, index: index, action: action, value: value, ), - ) as ControlOptionResponse; + ) as ControlValueOptionResponse; return response.result; } @@ -185,13 +185,13 @@ class SaneIsolate implements Sane { double? value, }) async { final response = await _sendMessage( - ControlOptionMessage( - handle: handle, + ControlValueOptionMessage( + saneHandle: handle, index: index, action: action, value: value, ), - ) as ControlOptionResponse; + ) as ControlValueOptionResponse; return response.result; } @@ -204,13 +204,13 @@ class SaneIsolate implements Sane { String? value, }) async { final response = await _sendMessage( - ControlOptionMessage( - handle: handle, + ControlValueOptionMessage( + saneHandle: handle, index: index, action: action, value: value, ), - ) as ControlOptionResponse; + ) as ControlValueOptionResponse; return response.result; } @@ -222,7 +222,7 @@ class SaneIsolate implements Sane { }) async { final response = await _sendMessage( ControlButtonOptionMessage( - handle: handle, + saneHandle: handle, index: index, ), ) as ControlButtonOptionResponse; @@ -234,7 +234,7 @@ class SaneIsolate implements Sane { Future getParameters(SaneHandle handle) async { final response = await _sendMessage( GetParametersMessage( - handle: handle, + saneHandle: handle, ), ) as GetParametersResponse; @@ -245,7 +245,7 @@ class SaneIsolate implements Sane { Future start(SaneHandle handle) async { await _sendMessage( StartMessage( - handle: handle, + saneHandle: handle, ), ); } @@ -254,7 +254,7 @@ class SaneIsolate implements Sane { Future read(SaneHandle handle, int bufferSize) async { final response = await _sendMessage( ReadMessage( - handle: handle, + saneHandle: handle, bufferSize: bufferSize, ), ) as ReadResponse; @@ -266,7 +266,7 @@ class SaneIsolate implements Sane { Future cancel(SaneHandle handle) async { await _sendMessage( CancelMessage( - handle: handle, + saneHandle: handle, ), ); } @@ -277,7 +277,7 @@ class SaneIsolate implements Sane { SaneIOMode ioMode, ) async { await _sendMessage( - SetIOModeMessage(handle: handle, ioMode: ioMode), + SetIOModeMessage(saneHandle: handle, ioMode: ioMode), ); } } @@ -296,12 +296,12 @@ void _isolateEntryPoint(_IsolateEntryPointArgs args) { final isolateReceivePort = ReceivePort(); args.mainSendPort.send(isolateReceivePort.sendPort); - final messageHandler = MessageHandler(sane: args.sane); + final sane = args.sane; isolateReceivePort.listen((envellope) async { envellope = envellope as _IsolateMessageEnveloppe; envellope.replyPort.send( - await messageHandler.handleMessage(envellope.message), + await envellope.message.handle(sane), ); }); } @@ -315,237 +315,3 @@ class _IsolateMessageEnveloppe { final SendPort replyPort; final IsolateMessage message; } - -class MessageHandler { - MessageHandler({required Sane sane}) : _sane = sane; - - final Sane _sane; - - Future handleMessage( - IsolateMessage message, - ) async { - switch (message) { - case InitMessage _: - return await _handleSaneInitMessage(); - - case ExitMessage _: - return await _handleSaneExitMessage(); - - case final GetDevicesMessage message: - return await _handleSaneGetDevicesMessages(message); - - case final OpenMessage message: - return await _handleSaneOpenMessage(message); - - case final CloseMessage message: - return await _handleSaneCloseMessage(message); - - case final GetOptionDescriptorMessage message: - return await _handleSaneGetOptionDescriptorMessage(message); - - case final GetAllOptionDescriptorsMessage message: - return await _handleSaneGetAllOptionDescriptorsMessage(message); - - case final ControlOptionMessage message: - return await _handleSaneControlBoolOptionMessage(message); - - case final ControlOptionMessage message: - return await _handleSaneControlIntOptionMessage(message); - - case final ControlOptionMessage message: - return await _handleSaneControlFixedOptionMessage(message); - - case final ControlOptionMessage message: - return await _handleSaneControlStringOptionMessage(message); - - case final ControlButtonOptionMessage message: - return await _handleSaneControlButtonOptionMessage(message); - - case final GetParametersMessage message: - return await _handleSaneGetParametersMessage(message); - - case final StartMessage message: - return await _handleSaneStartMessage(message); - - case final ReadMessage message: - return await _handleSaneReadMessage(message); - - case final CancelMessage message: - return await _handleSaneCancelMessage(message); - - case final SetIOModeMessage message: - return await _handleSaneSetIOModeMessage(message); - - default: - throw Exception('No handler for this message.'); - } - } - - Future _handleSaneInitMessage() async { - return InitResponse( - versionCode: await _sane.init(), - ); - } - - Future _handleSaneExitMessage() async { - await _sane.exit(); - return ExitResponse(); - } - - Future _handleSaneGetDevicesMessages( - GetDevicesMessage message, - ) async { - return GetDevicesResponse( - devices: await _sane.getDevices( - localOnly: message.localOnly, - ), - ); - } - - Future _handleSaneOpenMessage( - OpenMessage message, - ) async { - return OpenResponse( - handle: await _sane.open( - message.deviceName, - ), - ); - } - - Future _handleSaneCloseMessage( - CloseMessage message, - ) async { - await _sane.close(message.handle); - return CloseResponse(); - } - - Future _handleSaneGetOptionDescriptorMessage( - GetOptionDescriptorMessage message, - ) async { - return GetOptionDescriptorResponse( - optionDescriptor: await _sane.getOptionDescriptor( - message.handle, - message.index, - ), - ); - } - - Future - _handleSaneGetAllOptionDescriptorsMessage( - GetAllOptionDescriptorsMessage message, - ) async { - return GetAllOptionDescriptorsResponse( - optionDescriptors: await _sane.getAllOptionDescriptors( - message.handle, - ), - ); - } - - Future> _handleSaneControlBoolOptionMessage( - ControlOptionMessage message, - ) async { - return ControlOptionResponse( - result: await _sane.controlBoolOption( - handle: message.handle, - index: message.index, - action: message.action, - value: message.value, - ), - ); - } - - Future> _handleSaneControlIntOptionMessage( - ControlOptionMessage message, - ) async { - return ControlOptionResponse( - result: await _sane.controlIntOption( - handle: message.handle, - index: message.index, - action: message.action, - value: message.value, - ), - ); - } - - Future> _handleSaneControlFixedOptionMessage( - ControlOptionMessage message, - ) async { - return ControlOptionResponse( - result: await _sane.controlFixedOption( - handle: message.handle, - index: message.index, - action: message.action, - value: message.value, - ), - ); - } - - Future> _handleSaneControlStringOptionMessage( - ControlOptionMessage message, - ) async { - return ControlOptionResponse( - result: await _sane.controlStringOption( - handle: message.handle, - index: message.index, - action: message.action, - value: message.value, - ), - ); - } - - Future _handleSaneControlButtonOptionMessage( - ControlButtonOptionMessage message, - ) async { - return ControlButtonOptionResponse( - result: await _sane.controlButtonOption( - handle: message.handle, - index: message.index, - ), - ); - } - - Future _handleSaneGetParametersMessage( - GetParametersMessage message, - ) async { - return GetParametersResponse( - parameters: await _sane.getParameters( - message.handle, - ), - ); - } - - Future _handleSaneStartMessage( - StartMessage message, - ) async { - await _sane.start(message.handle); - return StartResponse(); - } - - Future _handleSaneReadMessage( - ReadMessage message, - ) async { - return ReadResponse( - bytes: await _sane.read( - message.handle, - message.bufferSize, - ), - ); - } - - Future _handleSaneCancelMessage( - CancelMessage message, - ) async { - await _sane.cancel(message.handle); - return CancelResponse(); - } - - Future _handleSaneSetIOModeMessage( - SetIOModeMessage message, - ) async { - await _sane.setIOMode( - message.handle, - message.ioMode, - ); - return SetIOModeResponse(); - } -}