network debugging

This commit is contained in:
John Smith
2022-03-08 22:32:12 -05:00
parent 98799b4d3a
commit 64ea00f8cc
21 changed files with 891 additions and 345 deletions

View File

@@ -88,21 +88,21 @@ Future<VeilidConfig> getDefaultVeilidConfig() async {
udp: VeilidConfigUDP(
enabled: !kIsWeb,
socketPoolSize: 0,
listenAddress: "[::]:5150",
listenAddress: "",
publicAddress: null,
),
tcp: VeilidConfigTCP(
connect: !kIsWeb,
listen: !kIsWeb,
maxConnections: 32,
listenAddress: "[::]:5150",
listenAddress: "",
publicAddress: null,
),
ws: VeilidConfigWS(
connect: true,
listen: !kIsWeb,
maxConnections: 16,
listenAddress: "[::]:5150",
listenAddress: "",
path: "ws",
url: null,
),
@@ -110,7 +110,7 @@ Future<VeilidConfig> getDefaultVeilidConfig() async {
connect: true,
listen: false,
maxConnections: 16,
listenAddress: "[::]:5150",
listenAddress: "",
path: "ws",
url: null,
),

View File

@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@@ -24,6 +25,7 @@ LogOptions getLogOptions(LogLevel? level) {
}
void setRootLogLevel(LogLevel? level) {
print("setRootLogLevel: $level");
Loggy('').level = getLogOptions(level);
}
@@ -62,6 +64,7 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> with UiLoggy {
String _veilidVersion = 'Unknown';
Stream<VeilidUpdate>? _updateStream;
Future<void>? _updateProcessor;
@override
void initState() {
@@ -95,6 +98,39 @@ class _MyAppState extends State<MyApp> with UiLoggy {
});
}
Future<void> processUpdateLog(VeilidUpdateLog update) async {
switch (update.logLevel) {
case VeilidLogLevel.error:
loggy.error(update.message);
break;
case VeilidLogLevel.warn:
loggy.warning(update.message);
break;
case VeilidLogLevel.info:
loggy.info(update.message);
break;
case VeilidLogLevel.debug:
loggy.debug(update.message);
break;
case VeilidLogLevel.trace:
loggy.trace(update.message);
break;
}
}
Future<void> processUpdates() async {
var stream = _updateStream;
if (stream != null) {
await for (final update in stream) {
if (update is VeilidUpdateLog) {
await processUpdateLog(update);
} else {
loggy.trace("Update: " + update.toString());
}
}
}
}
@override
Widget build(BuildContext context) {
final ButtonStyle buttonStyle =
@@ -116,16 +152,32 @@ class _MyAppState extends State<MyApp> with UiLoggy {
child: Row(children: [
ElevatedButton(
style: buttonStyle,
onPressed: () async {
//var await Veilid.instance.startupVeilidCore(await getDefaultVeilidConfig())
// setState(() {
// };
},
onPressed: _updateStream != null
? null
: () async {
var updateStream = Veilid.instance.startupVeilidCore(
await getDefaultVeilidConfig());
setState(() {
_updateStream = updateStream;
_updateProcessor = processUpdates();
});
},
child: const Text('Startup'),
),
ElevatedButton(
style: buttonStyle,
onPressed: () {},
onPressed: _updateStream == null
? null
: () async {
await Veilid.instance.shutdownVeilidCore();
if (_updateProcessor != null) {
await _updateProcessor;
}
setState(() {
_updateProcessor = null;
_updateStream = null;
});
},
child: const Text('Shutdown'),
),
])),

View File

@@ -191,39 +191,47 @@ Future<void> processFutureVoid(Future<dynamic> future) {
}
Stream<T> processStreamJson<T>(
T Function(Map<String, dynamic>) jsonConstructor, Stream<dynamic> stream) {
return stream.map((value) {
final list = value as List<dynamic>;
switch (list[0] as int) {
case messageErr:
{
throw VeilidAPIExceptionInternal("Internal API Error: ${list[1]}");
}
case messageOkJson:
{
if (list[1] == null) {
throw VeilidAPIExceptionInternal("Null MESSAGE_OK_JSON value");
T Function(Map<String, dynamic>) jsonConstructor, ReceivePort port) async* {
try {
await for (var value in port) {
final list = value as List<dynamic>;
switch (list[0] as int) {
case messageStreamItemJson:
{
if (list[1] == null) {
throw VeilidAPIExceptionInternal(
"Null MESSAGE_STREAM_ITEM_JSON value");
}
var ret = jsonDecode(list[1] as String);
yield jsonConstructor(ret);
break;
}
var ret = jsonDecode(list[1] as String);
return jsonConstructor(ret);
}
case messageErrJson:
{
throw VeilidAPIException.fromJson(jsonDecode(list[1]));
}
default:
{
throw VeilidAPIExceptionInternal(
"Unexpected async return message type: ${list[0]}");
}
case messageStreamAbort:
{
port.close();
throw VeilidAPIExceptionInternal("Internal API Error: ${list[1]}");
}
case messageStreamAbortJson:
{
port.close();
throw VeilidAPIException.fromJson(jsonDecode(list[1]));
}
case messageStreamClose:
{
port.close();
break;
}
default:
{
throw VeilidAPIExceptionInternal(
"Unexpected async return message type: ${list[0]}");
}
}
}
}).handleError((e) {
} catch (e) {
// Wrap all other errors in VeilidAPIExceptionInternal
throw VeilidAPIExceptionInternal(e.toString());
}, test: (e) {
// Pass errors that are already VeilidAPIException through without wrapping
return e is! VeilidAPIException;
});
}
}
// FFI implementation of high level Veilid API
@@ -287,7 +295,7 @@ class VeilidFFI implements Veilid {
final recvPort = ReceivePort("get_veilid_state");
final sendPort = recvPort.sendPort;
_getVeilidState(sendPort.nativePort);
return processFutureJson(VeilidState.fromJson, recvPort.single);
return processFutureJson(VeilidState.fromJson, recvPort.first);
}
@override
@@ -297,7 +305,7 @@ class VeilidFFI implements Veilid {
final sendPort = recvPort.sendPort;
_changeApiLogLevel(sendPort.nativePort, nativeLogLevel);
malloc.free(nativeLogLevel);
return processFutureVoid(recvPort.single);
return processFutureVoid(recvPort.first);
}
@override
@@ -305,7 +313,7 @@ class VeilidFFI implements Veilid {
final recvPort = ReceivePort("shutdown_veilid_core");
final sendPort = recvPort.sendPort;
_shutdownVeilidCore(sendPort.nativePort);
return processFutureVoid(recvPort.single);
return processFutureVoid(recvPort.first);
}
@override
@@ -314,7 +322,7 @@ class VeilidFFI implements Veilid {
final recvPort = ReceivePort("debug");
final sendPort = recvPort.sendPort;
_debug(sendPort.nativePort, nativeCommand);
return processFuturePlain(recvPort.single);
return processFuturePlain(recvPort.first);
}
@override

View File

@@ -51,36 +51,36 @@ impl DartIsolateWrapper {
});
}
pub fn result<T: IntoDart, E: Serialize>(&self, result: Result<T, E>) -> bool {
pub fn result<T: IntoDart, E: Serialize>(self, result: Result<T, E>) -> bool {
match result {
Ok(v) => self.ok(v),
Err(e) => self.err_json(e),
}
}
pub fn result_json<T: Serialize, E: Serialize>(&self, result: Result<T, E>) -> bool {
pub fn result_json<T: Serialize, E: Serialize>(self, result: Result<T, E>) -> bool {
match result {
Ok(v) => self.ok_json(v),
Err(e) => self.err_json(e),
}
}
pub fn ok<T: IntoDart>(&self, value: T) -> bool {
pub fn ok<T: IntoDart>(self, value: T) -> bool {
self.isolate
.post(vec![MESSAGE_OK.into_dart(), value.into_dart()])
}
pub fn ok_json<T: Serialize>(&self, value: T) -> bool {
pub fn ok_json<T: Serialize>(self, value: T) -> bool {
self.isolate.post(vec![
MESSAGE_OK_JSON.into_dart(),
serialize_json(value).into_dart(),
])
}
// pub fn err<E: IntoDart>(&self, error: E) -> bool {
// pub fn err<E: IntoDart>(self, error: E) -> bool {
// self.isolate
// .post(vec![MESSAGE_ERR.into_dart(), error.into_dart()])
// }
pub fn err_json<E: Serialize>(&self, error: E) -> bool {
pub fn err_json<E: Serialize>(self, error: E) -> bool {
self.isolate.post(vec![
MESSAGE_ERR_JSON.into_dart(),
serialize_json(error).into_dart(),
@@ -88,21 +88,35 @@ impl DartIsolateWrapper {
}
}
struct DartIsolateStreamInner {
pub isolate: Option<Isolate>,
}
impl Drop for DartIsolateStreamInner {
fn drop(&mut self) {
if let Some(isolate) = self.isolate {
isolate.post(vec![MESSAGE_STREAM_CLOSE.into_dart()]);
}
}
}
#[derive(Clone)]
pub struct DartIsolateStream {
isolate: Arc<Mutex<Option<Isolate>>>,
inner: Arc<Mutex<DartIsolateStreamInner>>,
}
impl DartIsolateStream {
pub fn new(port: i64) -> Self {
DartIsolateStream {
isolate: Arc::new(Mutex::new(Some(Isolate::new(port)))),
inner: Arc::new(Mutex::new(DartIsolateStreamInner {
isolate: Some(Isolate::new(port)),
})),
}
}
// pub fn item<T: IntoDart>(&self, value: T) -> bool {
// let isolate = self.isolate.lock();
// if let Some(isolate) = &*isolate {
// let mut inner = self.inner.lock();
// if let Some(isolate) = inner.isolate.take() {
// isolate.post(vec![MESSAGE_STREAM_ITEM.into_dart(), value.into_dart()])
// } else {
// false
@@ -110,8 +124,8 @@ impl DartIsolateStream {
// }
pub fn item_json<T: Serialize>(&self, value: T) -> bool {
let isolate = self.isolate.lock();
if let Some(isolate) = &*isolate {
let inner = self.inner.lock();
if let Some(isolate) = &inner.isolate {
isolate.post(vec![
MESSAGE_STREAM_ITEM_JSON.into_dart(),
serialize_json(value).into_dart(),
@@ -122,8 +136,8 @@ impl DartIsolateStream {
}
// pub fn abort<E: IntoDart>(self, error: E) -> bool {
// let mut isolate = self.isolate.lock();
// if let Some(isolate) = isolate.take() {
// let mut inner = self.inner.lock();
// if let Some(isolate) = inner.isolate.take() {
// isolate.post(vec![MESSAGE_STREAM_ABORT.into_dart(), error.into_dart()])
// } else {
// false
@@ -131,8 +145,8 @@ impl DartIsolateStream {
// }
pub fn abort_json<E: Serialize>(self, error: E) -> bool {
let mut isolate = self.isolate.lock();
if let Some(isolate) = isolate.take() {
let mut inner = self.inner.lock();
if let Some(isolate) = inner.isolate.take() {
isolate.post(vec![
MESSAGE_STREAM_ABORT_JSON.into_dart(),
serialize_json(error).into_dart(),
@@ -143,20 +157,11 @@ impl DartIsolateStream {
}
pub fn close(self) -> bool {
let mut isolate = self.isolate.lock();
if let Some(isolate) = isolate.take() {
let mut inner = self.inner.lock();
if let Some(isolate) = inner.isolate.take() {
isolate.post(vec![MESSAGE_STREAM_CLOSE.into_dart()])
} else {
false
}
}
}
impl Drop for DartIsolateStream {
fn drop(&mut self) {
let mut isolate = self.isolate.lock();
if let Some(isolate) = isolate.take() {
isolate.post(vec![MESSAGE_STREAM_CLOSE.into_dart()]);
}
}
}