veilid/veilid-flutter/lib/veilid_state.dart

548 lines
13 KiB
Dart
Raw Normal View History

2023-05-14 00:36:52 +00:00
import 'dart:typed_data';
import 'package:change_case/change_case.dart';
import 'veilid_encoding.dart';
import 'veilid.dart';
//////////////////////////////////////
/// AttachmentState
enum AttachmentState {
detached,
attaching,
attachedWeak,
attachedGood,
attachedStrong,
fullyAttached,
overAttached,
2023-05-17 17:38:36 +00:00
detaching;
2023-05-14 00:36:52 +00:00
2023-05-15 15:33:32 +00:00
String toJson() {
2023-05-14 00:36:52 +00:00
return name.toPascalCase();
}
2023-05-17 17:38:36 +00:00
factory AttachmentState.fromJson(String j) {
return AttachmentState.values.byName(j.toCamelCase());
}
2023-05-14 00:36:52 +00:00
}
//////////////////////////////////////
/// VeilidLogLevel
enum VeilidLogLevel {
error,
warn,
info,
debug,
2023-05-17 17:38:36 +00:00
trace;
2023-05-14 00:36:52 +00:00
2023-05-15 15:33:32 +00:00
String toJson() {
2023-05-14 00:36:52 +00:00
return name.toPascalCase();
}
2023-05-17 17:38:36 +00:00
factory VeilidLogLevel.fromJson(String j) {
return VeilidLogLevel.values.byName(j.toCamelCase());
}
2023-05-14 00:36:52 +00:00
}
////////////
class LatencyStats {
TimestampDuration fastest;
TimestampDuration average;
TimestampDuration slowest;
LatencyStats({
required this.fastest,
required this.average,
required this.slowest,
});
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
2023-05-15 15:33:32 +00:00
'fastest': fastest.toJson(),
'average': average.toJson(),
'slowest': slowest.toJson(),
2023-05-14 00:36:52 +00:00
};
}
LatencyStats.fromJson(dynamic json)
: fastest = TimestampDuration.fromJson(json['fastest']),
average = TimestampDuration.fromJson(json['average']),
slowest = TimestampDuration.fromJson(json['slowest']);
}
////////////
class TransferStats {
BigInt total;
BigInt maximum;
BigInt average;
BigInt minimum;
TransferStats({
required this.total,
required this.maximum,
required this.average,
required this.minimum,
});
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
'total': total.toString(),
'maximum': maximum.toString(),
'average': average.toString(),
'minimum': minimum.toString(),
};
}
TransferStats.fromJson(dynamic json)
: total = BigInt.parse(json['total']),
maximum = BigInt.parse(json['maximum']),
average = BigInt.parse(json['average']),
minimum = BigInt.parse(json['minimum']);
}
////////////
class TransferStatsDownUp {
TransferStats down;
TransferStats up;
TransferStatsDownUp({
required this.down,
required this.up,
});
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
2023-05-15 15:33:32 +00:00
'down': down.toJson(),
'up': up.toJson(),
2023-05-14 00:36:52 +00:00
};
}
TransferStatsDownUp.fromJson(dynamic json)
: down = TransferStats.fromJson(json['down']),
up = TransferStats.fromJson(json['up']);
}
////////////
class RPCStats {
int messagesSent;
int messagesRcvd;
int questionsInFlight;
Timestamp? lastQuestion;
Timestamp? lastSeenTs;
Timestamp? firstConsecutiveSeenTs;
int recentLostAnswers;
int failedToSend;
RPCStats({
required this.messagesSent,
required this.messagesRcvd,
required this.questionsInFlight,
required this.lastQuestion,
required this.lastSeenTs,
required this.firstConsecutiveSeenTs,
required this.recentLostAnswers,
required this.failedToSend,
});
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
'messages_sent': messagesSent,
'messages_rcvd': messagesRcvd,
'questions_in_flight': questionsInFlight,
2023-05-15 15:33:32 +00:00
'last_question': lastQuestion?.toJson(),
'last_seen_ts': lastSeenTs?.toJson(),
'first_consecutive_seen_ts': firstConsecutiveSeenTs?.toJson(),
2023-05-14 00:36:52 +00:00
'recent_lost_answers': recentLostAnswers,
'failed_to_send': failedToSend,
};
}
RPCStats.fromJson(dynamic json)
: messagesSent = json['messages_sent'],
messagesRcvd = json['messages_rcvd'],
questionsInFlight = json['questions_in_flight'],
lastQuestion = json['last_question'] != null
? Timestamp.fromJson(json['last_question'])
: null,
lastSeenTs = json['last_seen_ts'] != null
? Timestamp.fromJson(json['last_seen_ts'])
: null,
firstConsecutiveSeenTs = json['first_consecutive_seen_ts'] != null
? Timestamp.fromJson(json['first_consecutive_seen_ts'])
: null,
recentLostAnswers = json['recent_lost_answers'],
failedToSend = json['failed_to_send'];
}
////////////
class PeerStats {
Timestamp timeAdded;
RPCStats rpcStats;
LatencyStats? latency;
TransferStatsDownUp transfer;
PeerStats({
required this.timeAdded,
required this.rpcStats,
required this.latency,
required this.transfer,
});
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
2023-05-15 15:33:32 +00:00
'time_added': timeAdded.toJson(),
'rpc_stats': rpcStats.toJson(),
'latency': latency?.toJson(),
'transfer': transfer.toJson(),
2023-05-14 00:36:52 +00:00
};
}
PeerStats.fromJson(dynamic json)
: timeAdded = Timestamp.fromJson(json['time_added']),
rpcStats = RPCStats.fromJson(json['rpc_stats']),
latency = json['latency'] != null
? LatencyStats.fromJson(json['latency'])
: null,
transfer = TransferStatsDownUp.fromJson(json['transfer']);
}
////////////
class PeerTableData {
List<TypedKey> nodeIds;
2023-05-17 17:38:36 +00:00
String peerAddress;
2023-05-14 00:36:52 +00:00
PeerStats peerStats;
PeerTableData({
required this.nodeIds,
required this.peerAddress,
required this.peerStats,
});
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
2023-05-15 15:33:32 +00:00
'node_ids': nodeIds.map((p) => p.toJson()).toList(),
2023-05-17 17:38:36 +00:00
'peer_address': peerAddress,
2023-05-15 15:33:32 +00:00
'peer_stats': peerStats.toJson(),
2023-05-14 00:36:52 +00:00
};
}
PeerTableData.fromJson(dynamic json)
: nodeIds = List<TypedKey>.from(
json['node_ids'].map((j) => TypedKey.fromJson(j))),
2023-05-17 17:38:36 +00:00
peerAddress = json['peer_address'],
2023-05-14 00:36:52 +00:00
peerStats = PeerStats.fromJson(json['peer_stats']);
}
//////////////////////////////////////
/// VeilidUpdate
abstract class VeilidUpdate {
factory VeilidUpdate.fromJson(dynamic json) {
switch (json["kind"]) {
case "Log":
{
return VeilidLog(
2023-05-17 17:38:36 +00:00
logLevel: VeilidLogLevel.fromJson(json["log_level"]),
2023-05-14 00:36:52 +00:00
message: json["message"],
backtrace: json["backtrace"]);
}
case "AppMessage":
{
return VeilidAppMessage(
sender: json["sender"], message: json["message"]);
}
case "AppCall":
{
return VeilidAppCall(
sender: json["sender"], message: json["message"], id: json["id"]);
}
case "Attachment":
{
return VeilidUpdateAttachment(
state: VeilidStateAttachment.fromJson(json));
}
case "Network":
{
return VeilidUpdateNetwork(state: VeilidStateNetwork.fromJson(json));
}
case "Config":
{
return VeilidUpdateConfig(state: VeilidStateConfig.fromJson(json));
}
case "RouteChange":
{
return VeilidUpdateRouteChange(
deadRoutes: List<String>.from(json['dead_routes'].map((j) => j)),
deadRemoteRoutes:
List<String>.from(json['dead_remote_routes'].map((j) => j)));
}
case "ValueChange":
{
return VeilidUpdateValueChange(
key: TypedKey.fromJson(json['key']),
subkeys: List<ValueSubkeyRange>.from(
json['subkeys'].map((j) => ValueSubkeyRange.fromJson(j))),
count: json['count'],
valueData: ValueData.fromJson(json['value_data']));
}
default:
{
throw VeilidAPIExceptionInternal(
"Invalid VeilidAPIException type: ${json['kind']}");
}
}
}
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson();
2023-05-14 00:36:52 +00:00
}
class VeilidLog implements VeilidUpdate {
final VeilidLogLevel logLevel;
final String message;
final String? backtrace;
//
VeilidLog({
required this.logLevel,
required this.message,
required this.backtrace,
});
@override
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
'kind': "Log",
2023-05-15 15:33:32 +00:00
'log_level': logLevel.toJson(),
2023-05-14 00:36:52 +00:00
'message': message,
'backtrace': backtrace
};
}
}
class VeilidAppMessage implements VeilidUpdate {
2023-05-15 00:50:28 +00:00
final TypedKey? sender;
2023-05-14 00:36:52 +00:00
final Uint8List message;
//
VeilidAppMessage({
required this.sender,
required this.message,
});
@override
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
'kind': "AppMessage",
'sender': sender,
'message': base64UrlNoPadEncode(message)
};
}
}
class VeilidAppCall implements VeilidUpdate {
final String? sender;
final Uint8List message;
final String id;
//
VeilidAppCall({
required this.sender,
required this.message,
required this.id,
});
@override
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
'kind': "AppMessage",
'sender': sender,
'message': base64UrlNoPadEncode(message),
'id': id,
};
}
}
class VeilidUpdateAttachment implements VeilidUpdate {
final VeilidStateAttachment state;
//
VeilidUpdateAttachment({required this.state});
@override
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
var jsonRep = state.toJson();
2023-05-14 00:36:52 +00:00
jsonRep['kind'] = "Attachment";
return jsonRep;
}
}
class VeilidUpdateNetwork implements VeilidUpdate {
final VeilidStateNetwork state;
//
VeilidUpdateNetwork({required this.state});
@override
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
var jsonRep = state.toJson();
2023-05-14 00:36:52 +00:00
jsonRep['kind'] = "Network";
return jsonRep;
}
}
class VeilidUpdateConfig implements VeilidUpdate {
final VeilidStateConfig state;
//
VeilidUpdateConfig({required this.state});
@override
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
var jsonRep = state.toJson();
2023-05-14 00:36:52 +00:00
jsonRep['kind'] = "Config";
return jsonRep;
}
}
class VeilidUpdateRouteChange implements VeilidUpdate {
final List<String> deadRoutes;
final List<String> deadRemoteRoutes;
//
VeilidUpdateRouteChange({
required this.deadRoutes,
required this.deadRemoteRoutes,
});
@override
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
'dead_routes': deadRoutes.map((p) => p).toList(),
'dead_remote_routes': deadRemoteRoutes.map((p) => p).toList()
};
}
}
class VeilidUpdateValueChange implements VeilidUpdate {
final TypedKey key;
final List<ValueSubkeyRange> subkeys;
final int count;
final ValueData valueData;
//
VeilidUpdateValueChange({
required this.key,
required this.subkeys,
required this.count,
required this.valueData,
});
@override
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
2023-05-15 15:33:32 +00:00
'key': key.toJson(),
'subkeys': subkeys.map((p) => p.toJson()).toList(),
2023-05-14 00:36:52 +00:00
'count': count,
2023-05-15 15:33:32 +00:00
'value_data': valueData.toJson(),
2023-05-14 00:36:52 +00:00
};
}
}
//////////////////////////////////////
/// VeilidStateAttachment
class VeilidStateAttachment {
final AttachmentState state;
final bool publicInternetReady;
final bool localNetworkReady;
VeilidStateAttachment(
this.state, this.publicInternetReady, this.localNetworkReady);
VeilidStateAttachment.fromJson(dynamic json)
2023-05-17 17:38:36 +00:00
: state = AttachmentState.fromJson(json['state']),
2023-05-14 00:36:52 +00:00
publicInternetReady = json['public_internet_ready'],
localNetworkReady = json['local_network_ready'];
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
2023-05-15 15:33:32 +00:00
'state': state.toJson(),
2023-05-14 00:36:52 +00:00
'public_internet_ready': publicInternetReady,
'local_network_ready': localNetworkReady,
};
}
}
//////////////////////////////////////
/// VeilidStateNetwork
class VeilidStateNetwork {
final bool started;
final BigInt bpsDown;
final BigInt bpsUp;
final List<PeerTableData> peers;
VeilidStateNetwork(
{required this.started,
required this.bpsDown,
required this.bpsUp,
required this.peers});
VeilidStateNetwork.fromJson(dynamic json)
: started = json['started'],
bpsDown = BigInt.parse(json['bps_down']),
bpsUp = BigInt.parse(json['bps_up']),
peers = List<PeerTableData>.from(
json['peers'].map((j) => PeerTableData.fromJson(j)));
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
'started': started,
'bps_down': bpsDown.toString(),
'bps_up': bpsUp.toString(),
2023-05-15 15:33:32 +00:00
'peers': peers.map((p) => p.toJson()).toList(),
2023-05-14 00:36:52 +00:00
};
}
}
//////////////////////////////////////
/// VeilidStateConfig
class VeilidStateConfig {
final Map<String, dynamic> config;
VeilidStateConfig({
required this.config,
});
VeilidStateConfig.fromJson(dynamic json) : config = json['config'];
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {'config': config};
}
}
//////////////////////////////////////
/// VeilidState
class VeilidState {
final VeilidStateAttachment attachment;
final VeilidStateNetwork network;
final VeilidStateConfig config;
VeilidState.fromJson(dynamic json)
: attachment = VeilidStateAttachment.fromJson(json['attachment']),
network = VeilidStateNetwork.fromJson(json['network']),
config = VeilidStateConfig.fromJson(json['config']);
2023-05-15 15:33:32 +00:00
Map<String, dynamic> toJson() {
2023-05-14 00:36:52 +00:00
return {
2023-05-15 15:33:32 +00:00
'attachment': attachment.toJson(),
'network': network.toJson(),
'config': config.toJson()
2023-05-14 00:36:52 +00:00
};
}
}