2023-01-09 22:50:34 -05:00
|
|
|
import 'dart:async';
|
2023-07-26 14:20:29 -04:00
|
|
|
|
2023-01-09 22:50:34 -05:00
|
|
|
import 'package:veilid/veilid.dart';
|
2023-07-26 14:20:29 -04:00
|
|
|
|
2023-09-26 18:46:02 -04:00
|
|
|
import 'providers/connection_state.dart';
|
2023-09-26 22:32:13 -04:00
|
|
|
import 'tools/tools.dart';
|
2023-09-26 18:46:02 -04:00
|
|
|
import 'veilid_support/src/config.dart';
|
|
|
|
import 'veilid_support/src/veilid_log.dart';
|
2023-01-09 22:50:34 -05:00
|
|
|
|
|
|
|
class Processor {
|
2023-07-26 14:20:29 -04:00
|
|
|
Processor();
|
|
|
|
String _veilidVersion = '';
|
2023-01-09 22:50:34 -05:00
|
|
|
bool _startedUp = false;
|
|
|
|
Stream<VeilidUpdate>? _updateStream;
|
|
|
|
Future<void>? _updateProcessor;
|
|
|
|
|
|
|
|
Future<void> startup() async {
|
|
|
|
if (_startedUp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_veilidVersion = Veilid.instance.veilidVersionString();
|
|
|
|
} on Exception {
|
|
|
|
_veilidVersion = 'Failed to get veilid version.';
|
|
|
|
}
|
|
|
|
|
2023-07-26 14:20:29 -04:00
|
|
|
log.info('Veilid version: $_veilidVersion');
|
2023-07-22 23:29:10 -04:00
|
|
|
|
2023-01-09 22:50:34 -05:00
|
|
|
// In case of hot restart shut down first
|
|
|
|
try {
|
|
|
|
await Veilid.instance.shutdownVeilidCore();
|
|
|
|
} on Exception {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2023-07-26 14:20:29 -04:00
|
|
|
final updateStream =
|
2023-01-09 22:50:34 -05:00
|
|
|
await Veilid.instance.startupVeilidCore(await getVeilidChatConfig());
|
|
|
|
_updateStream = updateStream;
|
|
|
|
_updateProcessor = processUpdates();
|
|
|
|
_startedUp = true;
|
|
|
|
|
|
|
|
await Veilid.instance.attach();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> shutdown() async {
|
|
|
|
if (!_startedUp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await Veilid.instance.shutdownVeilidCore();
|
|
|
|
if (_updateProcessor != null) {
|
|
|
|
await _updateProcessor;
|
|
|
|
}
|
|
|
|
_updateProcessor = null;
|
|
|
|
_updateStream = null;
|
|
|
|
_startedUp = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> processUpdateAttachment(
|
|
|
|
VeilidUpdateAttachment updateAttachment) async {
|
|
|
|
//loggy.info("Attachment: ${updateAttachment.json}");
|
|
|
|
|
|
|
|
// Set connection meter and ui state for connection state
|
2023-08-17 18:10:24 -04:00
|
|
|
var cs = GlobalConnectionState.detached;
|
2023-01-09 22:50:34 -05:00
|
|
|
var checkPublicInternet = false;
|
2023-07-06 22:52:02 -04:00
|
|
|
switch (updateAttachment.state) {
|
2023-01-09 22:50:34 -05:00
|
|
|
case AttachmentState.detached:
|
2023-08-17 18:10:24 -04:00
|
|
|
cs = GlobalConnectionState.detached;
|
2023-01-09 22:50:34 -05:00
|
|
|
break;
|
|
|
|
case AttachmentState.detaching:
|
2023-08-17 18:10:24 -04:00
|
|
|
cs = GlobalConnectionState.detaching;
|
2023-01-09 22:50:34 -05:00
|
|
|
break;
|
|
|
|
case AttachmentState.attaching:
|
2023-08-17 18:10:24 -04:00
|
|
|
cs = GlobalConnectionState.attaching;
|
2023-01-09 22:50:34 -05:00
|
|
|
break;
|
|
|
|
case AttachmentState.attachedWeak:
|
|
|
|
checkPublicInternet = true;
|
2023-08-17 18:10:24 -04:00
|
|
|
cs = GlobalConnectionState.attachedWeak;
|
2023-01-09 22:50:34 -05:00
|
|
|
break;
|
|
|
|
case AttachmentState.attachedGood:
|
|
|
|
checkPublicInternet = true;
|
2023-08-17 18:10:24 -04:00
|
|
|
cs = GlobalConnectionState.attachedGood;
|
2023-01-09 22:50:34 -05:00
|
|
|
break;
|
|
|
|
case AttachmentState.attachedStrong:
|
|
|
|
checkPublicInternet = true;
|
2023-08-17 18:10:24 -04:00
|
|
|
cs = GlobalConnectionState.attachedStrong;
|
2023-01-09 22:50:34 -05:00
|
|
|
break;
|
|
|
|
case AttachmentState.fullyAttached:
|
|
|
|
checkPublicInternet = true;
|
2023-08-17 18:10:24 -04:00
|
|
|
cs = GlobalConnectionState.fullyAttached;
|
2023-01-09 22:50:34 -05:00
|
|
|
break;
|
|
|
|
case AttachmentState.overAttached:
|
|
|
|
checkPublicInternet = true;
|
2023-08-17 18:10:24 -04:00
|
|
|
cs = GlobalConnectionState.overAttached;
|
2023-01-09 22:50:34 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (checkPublicInternet) {
|
2023-07-06 22:52:02 -04:00
|
|
|
if (!updateAttachment.publicInternetReady) {
|
2023-08-17 18:10:24 -04:00
|
|
|
cs = GlobalConnectionState.attaching;
|
2023-01-09 22:50:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 21:00:22 -04:00
|
|
|
globalConnectionState.state = cs;
|
2023-01-09 22:50:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> processUpdateConfig(VeilidUpdateConfig updateConfig) async {
|
|
|
|
//loggy.info("Config: ${updateConfig.json}");
|
|
|
|
// xxx: store in flutterflow local state? do we need this for anything?
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> processUpdateNetwork(VeilidUpdateNetwork updateNetwork) async {
|
|
|
|
//loggy.info("Network: ${updateNetwork.json}");
|
|
|
|
// xxx: store in flutterflow local state? do we need this for anything?
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> processUpdates() async {
|
2023-07-26 14:20:29 -04:00
|
|
|
final stream = _updateStream;
|
2023-01-09 22:50:34 -05:00
|
|
|
if (stream != null) {
|
|
|
|
await for (final update in stream) {
|
|
|
|
if (update is VeilidLog) {
|
|
|
|
await processLog(update);
|
|
|
|
} else if (update is VeilidUpdateAttachment) {
|
|
|
|
await processUpdateAttachment(update);
|
|
|
|
} else if (update is VeilidUpdateConfig) {
|
|
|
|
await processUpdateConfig(update);
|
|
|
|
} else if (update is VeilidUpdateNetwork) {
|
|
|
|
await processUpdateNetwork(update);
|
|
|
|
} else if (update is VeilidAppMessage) {
|
2023-07-26 14:20:29 -04:00
|
|
|
log.info('AppMessage: ${update.toJson()}');
|
2023-01-09 22:50:34 -05:00
|
|
|
} else if (update is VeilidAppCall) {
|
2023-07-26 14:20:29 -04:00
|
|
|
log.info('AppCall: ${update.toJson()}');
|
2023-01-09 22:50:34 -05:00
|
|
|
} else {
|
2023-07-26 14:20:29 -04:00
|
|
|
log.trace('Update: ${update.toJson()}');
|
2023-01-09 22:50:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|