Skip to content

Commit

Permalink
feat: add rust process_runner integration
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Dec 3, 2023
1 parent 7b83ab6 commit bb5501b
Show file tree
Hide file tree
Showing 16 changed files with 615 additions and 96 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
init:
cd rust_ffi && make init
cargo install flutter_rust_bridge_codegen
flutter pub add --dev ffigen && flutter pub add ffi

generate_rust_bridge:
flutter_rust_bridge_codegen -r rust_ffi/src/api.rs -d lib/ffi/rust_ffi/rust_ffi.dart

format:
cd rust_ffi && make format
dart fix --apply
dart format .
30 changes: 29 additions & 1 deletion lib/bloc/gebura/gebura_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'package:bloc/bloc.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge.dart';
import 'package:path/path.dart';
import 'package:tuihub_protos/librarian/sephirah/v1/gebura.pb.dart';
import 'package:tuihub_protos/librarian/v1/common.pb.dart';

import '../../common/bloc_event_status_mixin.dart';
import '../../ffi/native_ffi.dart';
import '../../model/gebura_model.dart';
import '../../repo/grpc/api_helper.dart';
import '../../repo/local/gebura.dart';
Expand All @@ -24,7 +27,6 @@ class GeburaBloc extends Bloc<GeburaEvent, GeburaState> {
});

on<GeburaPurchasedAppsLoadEvent>((event, emit) async {
debugPrint('GeburaPurchasedAppsLoadEvent');
emit(GeburaPurchasedAppsLoadState(state, EventStatus.processing));
final resp = await _api.doRequest(
(client) => client.getPurchasedApps,
Expand Down Expand Up @@ -177,6 +179,32 @@ class GeburaBloc extends Bloc<GeburaEvent, GeburaState> {
emit(GeburaAssignAppPackageState(state, EventStatus.success,
msg: resp.error));
}, transformer: droppable());

on<GeburaRunAppEvent>((event, emit) async {
emit(GeburaRunAppState(state, event.appID, EventStatus.processing));
final setting = _repo.getAppLauncherSetting(event.appID.id.toInt());
if (setting == null) {
emit(GeburaRunAppState(state, event.appID, EventStatus.failed,
msg: '请先设置应用路径'));
return;
}
try {
final (start, end, suceess) = await NativeFunc.processRunner(
'', setting.path, '', dirname(setting.path), 1, 1000);
if (!suceess) {
emit(GeburaRunAppState(state, event.appID, EventStatus.failed,
msg: '应用未正常退出'));
return;
}
emit(GeburaRunAppState(state, event.appID, EventStatus.success,
startTime: DateTime.fromMillisecondsSinceEpoch(start * 1000),
endTime: DateTime.fromMillisecondsSinceEpoch(end * 1000)));
} catch (e) {
emit(GeburaRunAppState(state, event.appID, EventStatus.failed,
msg: '启动器错误 ${e is FrbAnyhowException ? e.anyhow : e}'));
return;
}
});
}

AppLauncherSetting? getAppLauncherSetting(InternalID id) {
Expand Down
6 changes: 6 additions & 0 deletions lib/bloc/gebura/gebura_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ final class GeburaAssignAppPackageEvent extends GeburaEvent {

GeburaAssignAppPackageEvent(this.appPackageID, this.appID);
}

final class GeburaRunAppEvent extends GeburaEvent {
final InternalID appID;

GeburaRunAppEvent(this.appID);
}
17 changes: 17 additions & 0 deletions lib/bloc/gebura/gebura_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,20 @@ class GeburaAssignAppPackageState extends GeburaState with EventStatusMixin {
@override
final String? msg;
}

class GeburaRunAppState extends GeburaState with EventStatusMixin {
GeburaRunAppState(GeburaState state, this.appID, this.statusCode,
{this.startTime, this.endTime, this.msg})
: super() {
_from(state);
}

final InternalID appID;
final DateTime? startTime;
final DateTime? endTime;

@override
final EventStatus? statusCode;
@override
final String? msg;
}
21 changes: 18 additions & 3 deletions lib/ffi/native_ffi.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:ffi';
import 'dart:io';

import 'rust_ffi/rust_ffi.dart';

class NativeFFI {
Expand Down Expand Up @@ -30,8 +31,22 @@ class NativeFFI {
class NativeFunc {
static final _ffi = RustFfiImpl(NativeFFI.dyLib);

static Future<int> add(int left, int right) async {
final int sum = await _ffi.add(left: left, right: right);
return sum;
static Future<(int, int, bool)> processRunner(
String name,
String executePath,
String monitorPath,
String workingDir,
int sleepCount,
int sleepMillis,
) async {
return _ffi.processRunner(
name: name,
executePath: executePath,
monitorPath: monitorPath,
workingDir: workingDir,
sleepCount: sleepCount,
sleepMillis: sleepMillis,
mode: TraceMode.Simple,
);
}
}
Loading

0 comments on commit bb5501b

Please sign in to comment.