From 3a695f708e7c5cc88be12936d876140a3d40bf41 Mon Sep 17 00:00:00 2001 From: Muzzammil Shahid Date: Wed, 22 May 2024 19:11:00 +0500 Subject: [PATCH] Add functions for basic wamp actions --- lib/constants.dart | 5 +++ lib/screens/mobile/mobile_home.dart | 6 +-- lib/wamp_actions.dart | 63 +++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 lib/wamp_actions.dart diff --git a/lib/constants.dart b/lib/constants.dart index 15fe118..b40f8a7 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -13,3 +13,8 @@ Color closeIconColor = Colors.redAccent; const double iconSize = 15; const double largeIconSize = 20; const double horizontalPadding = 10; + +/// Serializers /// +const jsonSerializer = "JSON"; +const cborSerializer = "CBOR"; +const msgPackSerializer = "MsgPack"; diff --git a/lib/screens/mobile/mobile_home.dart b/lib/screens/mobile/mobile_home.dart index 690a0eb..f4ce8ad 100644 --- a/lib/screens/mobile/mobile_home.dart +++ b/lib/screens/mobile/mobile_home.dart @@ -260,9 +260,9 @@ class _MobileHomeScaffoldState extends State with TickerProv value: _tabData[index].selectedSerializer.isEmpty ? null : _tabData[index].selectedSerializer, hint: const Text("Serializers"), items: [ - "JSON", - "CBOR", - "MsgPack", + jsonSerializer, + cborSerializer, + msgPackSerializer, ].map((String value) { return DropdownMenuItem( value: value, diff --git a/lib/wamp_actions.dart b/lib/wamp_actions.dart new file mode 100644 index 0000000..344326d --- /dev/null +++ b/lib/wamp_actions.dart @@ -0,0 +1,63 @@ +import "package:wampproto/auth.dart"; +import "package:wampproto/serializers.dart"; +import "package:xconn/exports.dart"; + +import "package:xconn_ui/constants.dart"; + +Serializer _getSerializer(String? serializerString) { + switch (serializerString) { + case jsonSerializer: + return JSONSerializer(); + case cborSerializer: + return CBORSerializer(); + case msgPackSerializer: + return MsgPackSerializer(); + + default: + throw Exception("invalid serializer $serializerString"); + } +} + +Future connect( + String url, + String realm, + String serializerStr, { + String? authid, + String? authrole, + String? ticket, + String? secret, + String? privateKey, +}) async { + var serializer = _getSerializer(serializerStr); + late Client client; + + if (ticket != null) { + client = Client(serializer: serializer, authenticator: TicketAuthenticator(ticket, authid ?? "")); + } else if (secret != null) { + client = Client(serializer: serializer, authenticator: WAMPCRAAuthenticator(secret, authid ?? "", {})); + } else if (privateKey != null) { + client = Client(serializer: serializer, authenticator: CryptoSignAuthenticator(authid ?? "", privateKey)); + } else { + client = Client(serializer: serializer); + } + + return client.connect(url, realm); +} + +Future register(Session session, String procedure) { + return session.register(procedure, (Invocation inv) { + return Result(args: inv.args, kwargs: inv.kwargs); + }); +} + +Future call(Session session, String procedure, {List? args, Map? kwargs}) { + return session.call(procedure, args: args, kwargs: kwargs); +} + +Future subscribe(Session session, String topic) { + return session.subscribe(topic, (Event event) {}); +} + +Future? publish(Session session, String topic, {List? args, Map? kwargs}) { + return session.publish(topic, args: args, kwargs: kwargs); +}