add writer to set_dht_value, allow multiple open_dht_record

This commit is contained in:
Christien Rioux 2024-02-21 20:18:07 -05:00
parent fac9937cf4
commit 0c3271b3b9
19 changed files with 161 additions and 66 deletions

View file

@ -312,11 +312,13 @@ abstract class VeilidRoutingContext {
// DHT Operations
Future<DHTRecordDescriptor> createDHTRecord(DHTSchema schema,
{CryptoKind kind = 0});
Future<DHTRecordDescriptor> openDHTRecord(TypedKey key, KeyPair? writer);
Future<DHTRecordDescriptor> openDHTRecord(TypedKey key, {KeyPair? writer});
Future<void> closeDHTRecord(TypedKey key);
Future<void> deleteDHTRecord(TypedKey key);
Future<ValueData?> getDHTValue(TypedKey key, int subkey, bool forceRefresh);
Future<ValueData?> setDHTValue(TypedKey key, int subkey, Uint8List data);
Future<ValueData?> getDHTValue(TypedKey key, int subkey,
{bool forceRefresh = false});
Future<ValueData?> setDHTValue(TypedKey key, int subkey, Uint8List data,
{KeyPair? writer});
Future<Timestamp> watchDHTValues(TypedKey key,
{List<ValueSubkeyRange>? subkeys, Timestamp? expiration, int? count});
Future<bool> cancelDHTWatch(TypedKey key, {List<ValueSubkeyRange>? subkeys});

View file

@ -81,9 +81,9 @@ typedef _RoutingContextDeleteDHTRecordDart = void Function(
typedef _RoutingContextGetDHTValueDart = void Function(
int, int, Pointer<Utf8>, int, bool);
// fn routing_context_set_dht_value(port: i64,
// id: u32, key: FfiStr, subkey: u32, data: FfiStr)
// id: u32, key: FfiStr, subkey: u32, data: FfiStr, writer: FfiStr)
typedef _RoutingContextSetDHTValueDart = void Function(
int, int, Pointer<Utf8>, int, Pointer<Utf8>);
int, int, Pointer<Utf8>, int, Pointer<Utf8>, Pointer<Utf8>);
// fn routing_context_watch_dht_values(port: i64,
// id: u32, key: FfiStr, subkeys: FfiStr, expiration: FfiStr, count: u32)
typedef _RoutingContextWatchDHTValuesDart = void Function(
@ -603,8 +603,8 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext {
}
@override
Future<DHTRecordDescriptor> openDHTRecord(
TypedKey key, KeyPair? writer) async {
Future<DHTRecordDescriptor> openDHTRecord(TypedKey key,
{KeyPair? writer}) async {
_ctx.ensureValid();
final nativeKey = jsonEncode(key).toNativeUtf8();
final nativeWriter =
@ -641,8 +641,8 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext {
}
@override
Future<ValueData?> getDHTValue(
TypedKey key, int subkey, bool forceRefresh) async {
Future<ValueData?> getDHTValue(TypedKey key, int subkey,
{bool forceRefresh = false}) async {
_ctx.ensureValid();
final nativeKey = jsonEncode(key).toNativeUtf8();
final recvPort = ReceivePort('routing_context_get_dht_value');
@ -655,16 +655,18 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext {
}
@override
Future<ValueData?> setDHTValue(
TypedKey key, int subkey, Uint8List data) async {
Future<ValueData?> setDHTValue(TypedKey key, int subkey, Uint8List data,
{KeyPair? writer}) async {
_ctx.ensureValid();
final nativeKey = jsonEncode(key).toNativeUtf8();
final nativeData = base64UrlNoPadEncode(data).toNativeUtf8();
final nativeWriter =
writer != null ? jsonEncode(writer).toNativeUtf8() : nullptr;
final recvPort = ReceivePort('routing_context_set_dht_value');
final sendPort = recvPort.sendPort;
_ctx.ffi._routingContextSetDHTValue(
sendPort.nativePort, _ctx.id!, nativeKey, subkey, nativeData);
_ctx.ffi._routingContextSetDHTValue(sendPort.nativePort, _ctx.id!,
nativeKey, subkey, nativeData, nativeWriter);
final valueData =
await processFutureOptJson(ValueData.fromJson, recvPort.first);
return valueData;
@ -1236,7 +1238,8 @@ class VeilidFFI extends Veilid {
Void Function(Int64, Uint32, Pointer<Utf8>, Uint32, Bool),
_RoutingContextGetDHTValueDart>('routing_context_get_dht_value'),
_routingContextSetDHTValue = dylib.lookupFunction<
Void Function(Int64, Uint32, Pointer<Utf8>, Uint32, Pointer<Utf8>),
Void Function(Int64, Uint32, Pointer<Utf8>, Uint32, Pointer<Utf8>,
Pointer<Utf8>),
_RoutingContextSetDHTValueDart>('routing_context_set_dht_value'),
_routingContextWatchDHTValues = dylib.lookupFunction<
Void Function(Int64, Uint32, Pointer<Utf8>, Pointer<Utf8>,

View file

@ -129,8 +129,8 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
}
@override
Future<DHTRecordDescriptor> openDHTRecord(
TypedKey key, KeyPair? writer) async {
Future<DHTRecordDescriptor> openDHTRecord(TypedKey key,
{KeyPair? writer}) async {
final id = _ctx.requireId();
return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util
.callMethod(wasm, 'routing_context_open_dht_record', [
@ -155,8 +155,8 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
}
@override
Future<ValueData?> getDHTValue(
TypedKey key, int subkey, bool forceRefresh) async {
Future<ValueData?> getDHTValue(TypedKey key, int subkey,
{bool forceRefresh = false}) async {
final id = _ctx.requireId();
final opt = await _wrapApiPromise<String?>(js_util.callMethod(
wasm,
@ -170,13 +170,17 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
}
@override
Future<ValueData?> setDHTValue(
TypedKey key, int subkey, Uint8List data) async {
Future<ValueData?> setDHTValue(TypedKey key, int subkey, Uint8List data,
{KeyPair? writer}) async {
final id = _ctx.requireId();
final opt = await _wrapApiPromise<String?>(js_util.callMethod(
wasm,
'routing_context_set_dht_value',
[id, jsonEncode(key), subkey, base64UrlNoPadEncode(data)]));
final opt = await _wrapApiPromise<String?>(
js_util.callMethod(wasm, 'routing_context_set_dht_value', [
id,
jsonEncode(key),
subkey,
base64UrlNoPadEncode(data),
if (writer != null) jsonEncode(writer) else null
]));
if (opt == null) {
return null;
}