veilid/veilid-flutter/lib/veilid_state.dart

302 lines
8.1 KiB
Dart
Raw Normal View History

2023-05-29 14:24:57 -05:00
import 'dart:typed_data';
import 'package:change_case/change_case.dart';
2023-07-05 17:48:06 -05:00
import 'package:freezed_annotation/freezed_annotation.dart';
2023-05-29 14:24:57 -05:00
import 'veilid.dart';
2023-07-05 17:48:06 -05:00
part 'veilid_state.freezed.dart';
part 'veilid_state.g.dart';
2023-05-29 14:24:57 -05:00
//////////////////////////////////////
/// AttachmentState
enum AttachmentState {
detached,
attaching,
attachedWeak,
attachedGood,
attachedStrong,
fullyAttached,
overAttached,
detaching;
2023-07-25 00:04:22 -05:00
factory AttachmentState.fromJson(dynamic j) =>
AttachmentState.values.byName((j as String).toCamelCase());
2023-07-30 14:57:06 -05:00
String toJson() => name.toPascalCase();
2023-05-29 14:24:57 -05:00
}
//////////////////////////////////////
/// VeilidLogLevel
enum VeilidLogLevel {
error,
warn,
info,
debug,
trace;
2023-07-25 00:04:22 -05:00
factory VeilidLogLevel.fromJson(dynamic j) =>
VeilidLogLevel.values.byName((j as String).toCamelCase());
2023-07-30 14:57:06 -05:00
String toJson() => name.toPascalCase();
2023-05-29 14:24:57 -05:00
}
////////////
2023-07-05 17:48:06 -05:00
@freezed
class LatencyStats with _$LatencyStats {
const factory LatencyStats({
required TimestampDuration fastest,
required TimestampDuration average,
required TimestampDuration slowest,
}) = _LatencyStats;
2023-07-25 00:04:22 -05:00
factory LatencyStats.fromJson(dynamic json) =>
_$LatencyStatsFromJson(json as Map<String, dynamic>);
2023-05-29 14:24:57 -05:00
}
////////////
2023-07-05 17:48:06 -05:00
@freezed
class TransferStats with _$TransferStats {
const factory TransferStats({
required BigInt total,
required BigInt maximum,
required BigInt average,
required BigInt minimum,
}) = _TransferStats;
2023-07-25 00:04:22 -05:00
factory TransferStats.fromJson(dynamic json) =>
_$TransferStatsFromJson(json as Map<String, dynamic>);
2023-05-29 14:24:57 -05:00
}
////////////
2023-07-05 17:48:06 -05:00
@freezed
class TransferStatsDownUp with _$TransferStatsDownUp {
const factory TransferStatsDownUp({
required TransferStats down,
required TransferStats up,
}) = _TransferStatsDownUp;
2023-07-25 00:04:22 -05:00
factory TransferStatsDownUp.fromJson(dynamic json) =>
_$TransferStatsDownUpFromJson(json as Map<String, dynamic>);
2023-05-29 14:24:57 -05:00
}
////////////
2024-10-10 19:16:39 -05:00
@freezed
class StateStats with _$StateStats {
const factory StateStats({
required TimestampDuration span,
required TimestampDuration reliable,
required TimestampDuration unreliable,
required TimestampDuration dead,
required TimestampDuration punished,
required StateReasonStats reason,
}) = _StateStats;
factory StateStats.fromJson(dynamic json) =>
_$StateStatsFromJson(json as Map<String, dynamic>);
}
////////////
@freezed
class StateReasonStats with _$StateReasonStats {
const factory StateReasonStats({
required TimestampDuration canNotSend,
required TimestampDuration tooManyLostAnswers,
required TimestampDuration noPingResponse,
required TimestampDuration failedToSend,
required TimestampDuration lostAnswers,
required TimestampDuration notSeenConsecutively,
required TimestampDuration inUnreliablePingSpan,
}) = _StateReasonStats;
factory StateReasonStats.fromJson(dynamic json) =>
_$StateReasonStatsFromJson(json as Map<String, dynamic>);
}
////////////
@freezed
class AnswerStats with _$AnswerStats {
const factory AnswerStats({
required TimestampDuration span,
required int questions,
required int answers,
required int lostAnswers,
required int consecutiveAnswersMaximum,
required int consecutiveAnswersAverage,
required int consecutiveAnswersMinimum,
required int consecutiveLostAnswersMaximum,
required int consecutiveLostAnswersAverage,
required int consecutiveLostAnswersMinimum,
}) = _AnswerStats;
factory AnswerStats.fromJson(dynamic json) =>
_$AnswerStatsFromJson(json as Map<String, dynamic>);
}
////////////
2023-07-05 17:48:06 -05:00
@freezed
class RPCStats with _$RPCStats {
const factory RPCStats({
required int messagesSent,
required int messagesRcvd,
required int questionsInFlight,
2024-10-10 19:16:39 -05:00
required Timestamp? lastQuestionTs,
2023-07-05 17:48:06 -05:00
required Timestamp? lastSeenTs,
required Timestamp? firstConsecutiveSeenTs,
required int recentLostAnswers,
required int failedToSend,
2024-10-10 19:16:39 -05:00
required AnswerStats answer,
2023-07-05 17:48:06 -05:00
}) = _RPCStats;
2023-07-25 00:04:22 -05:00
factory RPCStats.fromJson(dynamic json) =>
_$RPCStatsFromJson(json as Map<String, dynamic>);
2023-05-29 14:24:57 -05:00
}
////////////
2023-07-05 17:48:06 -05:00
@freezed
class PeerStats with _$PeerStats {
const factory PeerStats({
required Timestamp timeAdded,
required RPCStats rpcStats,
2023-07-30 14:57:06 -05:00
required TransferStatsDownUp transfer,
2024-10-10 19:16:39 -05:00
required StateStats state,
2023-07-30 14:57:06 -05:00
LatencyStats? latency,
2023-07-05 17:48:06 -05:00
}) = _PeerStats;
2023-07-25 00:04:22 -05:00
factory PeerStats.fromJson(dynamic json) =>
_$PeerStatsFromJson(json as Map<String, dynamic>);
2023-05-29 14:24:57 -05:00
}
////////////
2023-07-05 17:48:06 -05:00
@freezed
class PeerTableData with _$PeerTableData {
const factory PeerTableData({
required List<TypedKey> nodeIds,
required String peerAddress,
required PeerStats peerStats,
}) = _PeerTableData;
2023-07-25 00:04:22 -05:00
factory PeerTableData.fromJson(dynamic json) =>
_$PeerTableDataFromJson(json as Map<String, dynamic>);
2023-05-29 14:24:57 -05:00
}
//////////////////////////////////////
/// VeilidUpdate
2023-07-05 22:53:08 -05:00
@Freezed(unionKey: 'kind', unionValueCase: FreezedUnionCase.pascal)
sealed class VeilidUpdate with _$VeilidUpdate {
const factory VeilidUpdate.log({
required VeilidLogLevel logLevel,
required String message,
String? backtrace,
}) = VeilidLog;
const factory VeilidUpdate.appMessage({
2024-03-17 09:54:37 -05:00
@Uint8ListJsonConverter.jsIsArray() required Uint8List message,
2023-07-30 14:57:06 -05:00
TypedKey? sender,
2024-03-07 23:17:38 -06:00
String? routeId,
2023-07-05 22:53:08 -05:00
}) = VeilidAppMessage;
const factory VeilidUpdate.appCall({
2024-03-17 09:54:37 -05:00
@Uint8ListJsonConverter.jsIsArray() required Uint8List message,
2023-07-30 14:57:06 -05:00
required String callId,
TypedKey? sender,
2024-03-07 23:17:38 -06:00
String? routeId,
2023-07-05 22:53:08 -05:00
}) = VeilidAppCall;
const factory VeilidUpdate.attachment(
{required AttachmentState state,
required bool publicInternetReady,
2024-10-19 10:18:26 -05:00
required bool localNetworkReady,
required TimestampDuration uptime,
required TimestampDuration? attachedUptime}) = VeilidUpdateAttachment;
2023-07-05 22:53:08 -05:00
const factory VeilidUpdate.network(
{required bool started,
required BigInt bpsDown,
required BigInt bpsUp,
required List<PeerTableData> peers}) = VeilidUpdateNetwork;
const factory VeilidUpdate.config({
required VeilidConfig config,
}) = VeilidUpdateConfig;
const factory VeilidUpdate.routeChange({
required List<String> deadRoutes,
required List<String> deadRemoteRoutes,
}) = VeilidUpdateRouteChange;
const factory VeilidUpdate.valueChange({
required TypedKey key,
required List<ValueSubkeyRange> subkeys,
required int count,
required ValueData? value,
2023-07-05 22:53:08 -05:00
}) = VeilidUpdateValueChange;
2024-02-16 10:19:26 -06:00
factory VeilidUpdate.fromJson(dynamic json) =>
_$VeilidUpdateFromJson(json as Map<String, dynamic>);
2023-05-29 14:24:57 -05:00
}
//////////////////////////////////////
/// VeilidStateAttachment
2023-07-05 22:53:08 -05:00
@freezed
class VeilidStateAttachment with _$VeilidStateAttachment {
const factory VeilidStateAttachment(
{required AttachmentState state,
required bool publicInternetReady,
2024-10-19 10:18:26 -05:00
required bool localNetworkReady,
required TimestampDuration uptime,
required TimestampDuration? attachedUptime}) = _VeilidStateAttachment;
2023-07-05 22:53:08 -05:00
2023-07-25 00:04:22 -05:00
factory VeilidStateAttachment.fromJson(dynamic json) =>
_$VeilidStateAttachmentFromJson(json as Map<String, dynamic>);
2023-05-29 14:24:57 -05:00
}
//////////////////////////////////////
/// VeilidStateNetwork
2023-07-05 22:53:08 -05:00
@freezed
class VeilidStateNetwork with _$VeilidStateNetwork {
const factory VeilidStateNetwork(
{required bool started,
required BigInt bpsDown,
required BigInt bpsUp,
required List<PeerTableData> peers}) = _VeilidStateNetwork;
2023-07-25 00:04:22 -05:00
factory VeilidStateNetwork.fromJson(dynamic json) =>
_$VeilidStateNetworkFromJson(json as Map<String, dynamic>);
2023-05-29 14:24:57 -05:00
}
//////////////////////////////////////
/// VeilidStateConfig
2023-07-05 22:53:08 -05:00
@freezed
class VeilidStateConfig with _$VeilidStateConfig {
const factory VeilidStateConfig({
required VeilidConfig config,
}) = _VeilidStateConfig;
2023-05-29 14:24:57 -05:00
2023-07-25 00:04:22 -05:00
factory VeilidStateConfig.fromJson(dynamic json) =>
_$VeilidStateConfigFromJson(json as Map<String, dynamic>);
2023-05-29 14:24:57 -05:00
}
//////////////////////////////////////
/// VeilidState
2023-07-05 22:53:08 -05:00
@freezed
class VeilidState with _$VeilidState {
const factory VeilidState({
required VeilidStateAttachment attachment,
required VeilidStateNetwork network,
required VeilidStateConfig config,
}) = _VeilidState;
2023-07-25 00:04:22 -05:00
factory VeilidState.fromJson(dynamic json) =>
_$VeilidStateFromJson(json as Map<String, dynamic>);
2023-05-29 14:24:57 -05:00
}