ffi respond to NewPeer event

This commit is contained in:
Dan Ballard 2021-02-05 16:32:35 -08:00
parent 0fea363e4d
commit a3e681b60b
3 changed files with 98 additions and 2 deletions

View File

@ -1,10 +1,14 @@
import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'package:path/path.dart' as path;
import 'package:ffi/ffi.dart';
import 'package:flutter_app/cwtch/cwtch.dart';
import '../model.dart';
/////////////////////
/// Cwtch API ///
/////////////////////
@ -42,11 +46,18 @@ typedef GetJsonBlobFromStrStrIntIntFn = Pointer<Utf8> Function(Pointer<Utf8>, in
typedef acn_events_function = Pointer<Utf8> Function();
typedef ACNEventsFn = Pointer<Utf8> Function();
typedef repaint_event_function = Int8 Function();
typedef RepaintEventFn = int Function();
class CwtchFfi implements Cwtch {
DynamicLibrary library;
Stream<bool> repaintProfileStream;
ProfileListState profileCN;
CwtchFfi() {
CwtchFfi(ProfileListState profs) {
library = DynamicLibrary.open("libCwtch.so");
profileCN = profs;
}
Future<void> Start() async {
@ -64,6 +75,65 @@ class CwtchFfi implements Cwtch {
// ignore: non_constant_identifier_names
final StartCwtch = startCwtchC.asFunction<StartCwtchFn>();
StartCwtch(Utf8.toUtf8(cwtchDir), cwtchDir.length, Utf8.toUtf8(""), 0);
// Test non timer getting events from libcwtch-go
var _receivePort = ReceivePort();
var _isolate = await Isolate.spawn(_checkAppbusEvents, _receivePort.sendPort);
_receivePort.listen((message) {
var env = jsonDecode(message);
var obj = env["Data"];
switch (env["EventType"]) {
case "NewPeer":
print(env["Data"]);
profileCN.add(ProfileInfoState(onion: obj["Identity"], nickname: obj["name"], imagePath: obj["picture"]));
break;
default: print("unhandled gomobile appbus event: ${env["EventType"]}");
}
});
//repaintProfileStream = pollRepaintProfileEvents();
}
/*
Stream<bool> GetRepaintProfileStream() {
return repaintProfileStream;
}
Stream<bool> pollRepaintProfileEvents() async* {
var getProfileRepaintEventC = library.lookup<NativeFunction<repaint_event_function>>("c_GetProfileRepaintEvent");
final GetProfileRepaintEvent = getProfileRepaintEventC.asFunction<RepaintEventFn>();
while (true) {
GetProfileRepaintEvent();
yield true;
}
}*/
// Test reader of a potential method for libcwtch-go to "push" methods into dart
// statis so it can be an isolate
static void _checkAppbusEvents(SendPort sendPort) async {
var stream = pollAppbusEvents();
await for (var value in stream) {
//print("_checkAppbuss events: $value");
sendPort.send(value);
}
}
// Steam of appbus events. should block in libcwtch-go GetAppbusEvent
// currently statis so the isolate tester can use it, shouldn't need to be static
// for other uses
static Stream<String> pollAppbusEvents() async* {
var library = DynamicLibrary.open("libCwtch.so");
var getAppbusEventC = library.lookup<NativeFunction<acn_events_function>>("c_GetAppBusEvent");
final GetAppbusEvent = getAppbusEventC.asFunction<ACNEventsFn>();
while (true) {
Pointer<Utf8> result = GetAppbusEvent();
String event = Utf8.fromUtf8(result);
yield event;
}
}
// ignore: non_constant_identifier_names

View File

@ -40,7 +40,7 @@ class FlwtchState extends State<Flwtch> {
if (Platform.isAndroid) {
cwtch = CwtchGomobile(profs);
} else {
cwtch = CwtchFfi();
cwtch = CwtchFfi(profs);
}
cwtch.Start().then((val) {
@ -50,6 +50,31 @@ class FlwtchState extends State<Flwtch> {
});
appStatus = AppModel(cwtch: cwtch);
/* // Timing issue? Start may not have inited cwtch yet when we ask for getProfiles...
}
void loadProfiles() {
cwtch.GetProfiles().then((profilesJson) {
if (profilesJson != null) {
setState(() {
jsonDecode(profilesJson).forEach((profile) {
ProfileModel profile1 = new ProfileModel();
profile1.onion = profile['onion'];
profile1.nickname = profile['name'];
profile1.creationDate = "4 jan 2020";
profile1.contacts = new HashMap<String, ContactModel>();
profile1.imagePath = profile['imagePath'];
profiles.putIfAbsent(profile1.onion, () => profile1);
});
});
}
});
}
ChangeNotifierProvider<OpaqueTheme> getOpaqueProvider() {
return ChangeNotifierProvider(create: (context) => OpaqueTheme(Opaque.dark)); */
}
ChangeNotifierProvider<OpaqueTheme> getOpaqueProvider() => ChangeNotifierProvider(create: (context) => OpaqueTheme(Opaque.dark));

View File

@ -83,6 +83,7 @@ add_custom_command(
${FLUTTER_TOOL_ENVIRONMENT}
"${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh"
linux-x64 ${CMAKE_BUILD_TYPE}
VERBATIM
)
add_custom_target(flutter_assemble DEPENDS
"${FLUTTER_LIBRARY}"