Skip to content

Commit

Permalink
Update network stack
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Oct 26, 2024
1 parent ed5d292 commit 414fd6c
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 90 deletions.
2 changes: 1 addition & 1 deletion api/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: butterfly_api
description: The Linwood Butterfly API
version: 2.3.0-beta.0
version: 2.2.2-rc.0
publish_to: none

environment:
Expand Down
2 changes: 1 addition & 1 deletion app/AppImageBuilder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ AppDir:
id: dev.linwood.butterfly
name: Linwood Butterfly
icon: dev.linwood.butterfly
version: 2.3.0-beta.0
version: 2.2.2-rc.0
exec: butterfly
exec_args: $@
apt:
Expand Down
6 changes: 3 additions & 3 deletions app/android/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GEM
artifactory (3.0.17)
atomos (0.1.3)
aws-eventstream (1.3.0)
aws-partitions (1.994.0)
aws-partitions (1.996.0)
aws-sdk-core (3.211.0)
aws-eventstream (~> 1, >= 1.3.0)
aws-partitions (~> 1, >= 1.992.0)
Expand Down Expand Up @@ -152,7 +152,7 @@ GEM
domain_name (~> 0.5)
httpclient (2.8.3)
jmespath (1.6.2)
json (2.7.3)
json (2.7.4)
jwt (2.9.3)
base64
mini_magick (4.13.2)
Expand Down Expand Up @@ -222,4 +222,4 @@ DEPENDENCIES
screengrab

BUNDLED WITH
2.5.22
2.5.21
2 changes: 1 addition & 1 deletion app/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pluginManagement {

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version '8.7.0' apply false
id "com.android.application" version '8.7.1' apply false
id "org.jetbrains.kotlin.android" version "1.9.22" apply false
}

Expand Down
2 changes: 1 addition & 1 deletion app/lib/cubits/current_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
}

Future<void> updateNetworkingState(DocumentBloc bloc,
[Map<ConnectionId?, NetworkingUser>? current]) async {
[Map<Channel?, NetworkingUser>? current]) async {
final blocState = bloc.state;
if (blocState is! DocumentLoadSuccess) return;
final users = (current ?? state.networkingService.users).values.toList();
Expand Down
2 changes: 1 addition & 1 deletion app/lib/dialogs/collaboration/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ViewCollaborationDialog extends StatelessWidget {
onTap: () => saveToClipboard(context, address),
),
const Divider(),
StreamBuilder<Set<ConnectionId>>(
StreamBuilder<Set<Channel>>(
stream: service.connectionsStream,
builder: (context, snapshot) {
final connections = snapshot.data ?? {};
Expand Down
139 changes: 79 additions & 60 deletions app/lib/services/network.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';

Expand Down Expand Up @@ -34,7 +35,7 @@ enum NetworkingType {

const kDefaultPort = 28005;
const kTimeout = Duration(seconds: 10);
typedef NetworkingState = (NetworkerBase, RpcPlugin);
typedef NetworkingState = (NetworkerBase, RpcNetworkerPipe);

@freezed
class NetworkingInitMessage with _$NetworkingInitMessage {
Expand All @@ -55,23 +56,29 @@ class NetworkingUser with _$NetworkingUser {
_$NetworkingUserFromJson(json);
}

enum NetworkEvent {
event,
init,
connections,
user,
}

class NetworkingService {
DocumentBloc? _bloc;
final BehaviorSubject<NetworkingState?> _subject =
BehaviorSubject.seeded(null);
final BehaviorSubject<Set<ConnectionId>> _connections =
BehaviorSubject.seeded({});
final BehaviorSubject<Map<ConnectionId?, NetworkingUser>> _users =
final BehaviorSubject<Set<Channel>> _connections = BehaviorSubject.seeded({});
final BehaviorSubject<Map<Channel?, NetworkingUser>> _users =
BehaviorSubject.seeded({});

Stream<NetworkingState?> get stream => _subject.stream;
NetworkingState? get state => _subject.value;

Stream<Set<ConnectionId>> get connectionsStream => _connections.stream;
Set<ConnectionId> get connections => _connections.value;
Stream<Set<Channel>> get connectionsStream => _connections.stream;
Set<Channel> get connections => _connections.value;

Stream<Map<ConnectionId?, NetworkingUser>> get usersStream => _users.stream;
Map<ConnectionId?, NetworkingUser> get users => _users.value;
Stream<Map<Channel?, NetworkingUser>> get usersStream => _users.stream;
Map<Channel?, NetworkingUser> get users => _users.value;

NetworkingService();

Expand All @@ -83,30 +90,30 @@ class NetworkingService {

Future<void> createSocketServer([String? address, int? port]) async {
closeNetworking();
final httpServer = await HttpServer.bind(
address != null
? InternetAddress(address, type: InternetAddressType.any)
: InternetAddress.anyIPv4,
port ?? kDefaultPort,
);
final server = NetworkerSocketServer(httpServer);
final rpc = RpcNetworkerServerPlugin();
final server = NetworkerSocketServer(
address != null
? InternetAddress(address, type: InternetAddressType.any)
: InternetAddress.anyIPv4,
port ?? kDefaultPort);
final rpc = RpcServerNetworkerPipe();
_setupRpc(rpc, server);
void sendConnections() {
rpc.sendMessage(RpcRequest(
kNetworkerConnectionIdAny, 'connections', server.connectionIds));
rpc.callFunction(NetworkEvent.connections.index,
Uint8List.fromList(jsonEncode(connections.toList()).codeUnits));
}

server.connect.listen((event) {
server.clientConnect.listen((event) {
final state = _bloc?.state;
rpc.sendMessage(
RpcRequest(event, 'init', NetworkingInitMessage(state?.saveBytes())));
rpc.callFunction(
NetworkEvent.init.index,
Uint8List.fromList(
jsonEncode(NetworkingInitMessage(state?.saveBytes())).codeUnits));
sendConnections();
});
server.disconnect.listen((event) {
server.clientDisconnect.listen((event) {
sendConnections();
});
server.addPlugin(rpc);
server.connect(rpc);
_subject.add((server, rpc));
}

Expand All @@ -116,17 +123,19 @@ class NetworkingService {
uri = uri.replace(port: kDefaultPort);
}
final client = NetworkerSocketClient(uri);
final rpc = RpcNetworkerPlugin();
final rpc = RpcClientNetworkerPipe();
_setupRpc(rpc, client);
final completer = Completer<Uint8List?>();
rpc.addFunction(
'init',
RpcFunction(RpcType.authority, (message) {
final init = NetworkingInitMessage.fromJson(message.message);
completer.complete(
init.data == null ? null : Uint8List.fromList(init.data!));
}));
client.addPlugin(RawJsonNetworkerPlugin()..addPlugin(rpc));
rpc
.registerFunction(NetworkEvent.init.index,
mode: RpcNetworkerMode.authority)
.connect(RawJsonNetworkerPlugin()
..read.listen((message) {
final init = NetworkingInitMessage.fromJson(message.data);
completer.complete(
init.data == null ? null : Uint8List.fromList(init.data!));
}));
client.connect(rpc);
_subject.add((client, rpc));
return completer.future.timeout(kTimeout);
}
Expand All @@ -138,43 +147,53 @@ class NetworkingService {
_users.add({});
}

void _setupRpc(RpcPlugin rpc, NetworkerBase networker) {
rpc.addFunction(
'event',
RpcFunction(RpcType.any, (message) {
final event = DocumentEvent.fromJson(message.message);
onMessage(event);
}));
rpc.addFunction(
'connections',
RpcFunction(RpcType.authority, (message) {
final ids = Set<ConnectionId>.from(message.message);
_connections.add(ids);
_users.add(Map.from(_users.value)
..removeWhere((key, value) => !ids.contains(key)));
}, true));
rpc.addFunction(
'user',
RpcFunction(RpcType.any, (message) {
final user = NetworkingUser.fromJson(message.message);
final users = Map<ConnectionId?, NetworkingUser>.from(_users.value)
..[message.client] = user;
_users.add(users);
_bloc?.state.currentIndexCubit?.updateNetworkingState(_bloc!, users);
}));
void _setupRpc(RpcNetworkerPipe rpc, NetworkerBase networker) {
rpc
.registerFunction(NetworkEvent.event.index, mode: RpcNetworkerMode.any)
.connect(RawJsonNetworkerPlugin()
..read.listen((message) {
final event = DocumentEvent.fromJson(message.data);
onMessage(event);
}));
rpc
.registerFunction(
NetworkEvent.connections.index,
mode: RpcNetworkerMode.authority,
)
.connect(RawJsonNetworkerPlugin()
..read.listen((message) {
final ids = Set<Channel>.from(message.data);
_connections.add(ids);
_users.add(Map.from(_users.value)
..removeWhere((key, value) => !ids.contains(key)));
}));
rpc
.registerFunction(
NetworkEvent.user.index,
mode: RpcNetworkerMode.any,
)
.connect(RawJsonNetworkerPlugin()
..read.listen((message) {
final user = NetworkingUser.fromJson(message.data);
final users = Map<Channel?, NetworkingUser>.from(_users.value)
..[message.channel] = user;
_users.add(users);
_bloc?.state.currentIndexCubit
?.updateNetworkingState(_bloc!, users);
}));
}

void sendUser(NetworkingUser user) {
state?.$2.sendMessage(
RpcRequest(kNetworkerConnectionIdAny, 'user', user.toJson()));
state?.$2.callFunction(NetworkEvent.user.index,
Uint8List.fromList(jsonEncode(user.toJson()).codeUnits));
}

bool _externalEvent = false;

void onEvent(DocumentEvent event) {
if (!event.shouldSync() || _externalEvent) return;
state?.$2.sendMessage(
RpcRequest(kNetworkerConnectionIdAny, 'event', event.toJson()));
state?.$2.callFunction(NetworkEvent.event.index,
Uint8List.fromList(jsonEncode(event.toJson()).codeUnits));
}

void onMessage(DocumentEvent event) {
Expand Down
2 changes: 1 addition & 1 deletion app/linux/debian/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: linwood-butterfly
Version: 2.3.0-beta.0
Version: 2.2.2-rc.0
Section: base
Priority: optional
Homepage: https://github.com/LinwoodDev/butterfly
Expand Down
22 changes: 11 additions & 11 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ packages:
path: "../api"
relative: true
source: path
version: "2.3.0-beta.0"
version: "2.2.2-rc.0"
camera:
dependency: "direct main"
description:
Expand Down Expand Up @@ -824,7 +824,7 @@ packages:
path: "packages/lw_file_system"
ref: "71ebfd76ef24a9b21b2e909d5802e9fb01357317"
resolved-ref: "71ebfd76ef24a9b21b2e909d5802e9fb01357317"
url: "https://github.com/LinwoodDev/dart_pkgs.git"
url: "https://github.com/LinwoodDev/dart_pkgs"
source: git
version: "1.0.0"
lw_file_system_api:
Expand All @@ -842,7 +842,7 @@ packages:
path: "packages/lw_sysapi"
ref: "21808e89bb6239f0b39367d0c00b0b14f5273992"
resolved-ref: "21808e89bb6239f0b39367d0c00b0b14f5273992"
url: "https://github.com/LinwoodDev/dart_pkgs.git"
url: "https://github.com/LinwoodDev/dart_pkgs"
source: git
version: "0.0.1"
macros:
Expand Down Expand Up @@ -883,7 +883,7 @@ packages:
path: "packages/material_leap"
ref: "18cd4cd6cb9e8663036eb59bb431f8ac65827788"
resolved-ref: "18cd4cd6cb9e8663036eb59bb431f8ac65827788"
url: "https://github.com/LinwoodDev/dart_pkgs.git"
url: "https://github.com/LinwoodDev/dart_pkgs"
source: git
version: "0.0.1"
meta:
Expand Down Expand Up @@ -914,18 +914,18 @@ packages:
dependency: "direct main"
description:
path: "packages/networker/networker"
ref: "6617b9bbfb481ef59b1a6921f1676bb90d73ecf9"
resolved-ref: "6617b9bbfb481ef59b1a6921f1676bb90d73ecf9"
url: "https://github.com/LinwoodDev/dart_pkgs.git"
ref: "5a3658cad80fcb8594b8be6f84460e77607eb959"
resolved-ref: "5a3658cad80fcb8594b8be6f84460e77607eb959"
url: "https://github.com/LinwoodDev/dart_pkgs"
source: git
version: "1.0.0"
networker_socket:
dependency: "direct main"
description:
path: "packages/networker/networker_socket"
ref: fce8dc67ec282861bf629e32c404c52921f54bbd
resolved-ref: fce8dc67ec282861bf629e32c404c52921f54bbd
url: "https://github.com/LinwoodDev/dart_pkgs.git"
ref: "40b8553ab86ab4f06d9f689268a65d65ed936fda"
resolved-ref: "40b8553ab86ab4f06d9f689268a65d65ed936fda"
url: "https://github.com/LinwoodDev/dart_pkgs"
source: git
version: "1.0.0"
nm:
Expand Down Expand Up @@ -1640,4 +1640,4 @@ packages:
version: "3.1.2"
sdks:
dart: ">=3.5.0 <4.0.0"
flutter: ">=3.24.3"
flutter: ">=3.24.4"
18 changes: 9 additions & 9 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ publish_to: none
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 2.3.0-beta.0+120
version: 2.2.2-rc.0+120

environment:
sdk: ">=3.4.0 <4.0.0"
flutter: 3.24.3
flutter: 3.24.4

dependencies:
flutter:
Expand Down Expand Up @@ -68,27 +68,27 @@ dependencies:
ref: 6228363ff19e95aa16055dd493533d4c01fa2209
material_leap:
git:
url: https://github.com/LinwoodDev/dart_pkgs.git
url: https://github.com/LinwoodDev/dart_pkgs
ref: 18cd4cd6cb9e8663036eb59bb431f8ac65827788
path: packages/material_leap
lw_sysapi:
git:
url: https://github.com/LinwoodDev/dart_pkgs.git
url: https://github.com/LinwoodDev/dart_pkgs
ref: 21808e89bb6239f0b39367d0c00b0b14f5273992
path: packages/lw_sysapi
networker:
git:
url: https://github.com/LinwoodDev/dart_pkgs.git
ref: 6617b9bbfb481ef59b1a6921f1676bb90d73ecf9
url: https://github.com/LinwoodDev/dart_pkgs
ref: 5a3658cad80fcb8594b8be6f84460e77607eb959
path: packages/networker/networker
networker_socket:
git:
url: https://github.com/LinwoodDev/dart_pkgs.git
ref: fce8dc67ec282861bf629e32c404c52921f54bbd
url: https://github.com/LinwoodDev/dart_pkgs
ref: 40b8553ab86ab4f06d9f689268a65d65ed936fda
path: packages/networker/networker_socket
lw_file_system:
git:
url: https://github.com/LinwoodDev/dart_pkgs.git
url: https://github.com/LinwoodDev/dart_pkgs
ref: 71ebfd76ef24a9b21b2e909d5802e9fb01357317
path: packages/lw_file_system
flutter_localized_locales: ^2.0.5
Expand Down
Loading

0 comments on commit 414fd6c

Please sign in to comment.