go-waku/library/c/api_relay.go

78 lines
2.7 KiB
Go
Raw Normal View History

2022-04-03 00:22:42 +00:00
package main
2023-08-10 13:30:38 +00:00
/*
#include <cgo_utils.h>
*/
import "C"
2023-10-28 23:37:53 +00:00
import (
"unsafe"
"github.com/waku-org/go-waku/library"
)
2022-04-03 00:22:42 +00:00
// Determine if there are enough peers to publish a message on a topic. Use NULL
// to verify the number of peers in the default pubsub topic
2023-03-29 19:20:31 +00:00
//
//export waku_relay_enough_peers
2023-10-28 23:37:53 +00:00
func waku_relay_enough_peers(topic *C.char, cb C.WakuCallBack, userData unsafe.Pointer) C.int {
2023-08-10 13:30:38 +00:00
return singleFnExec(func() (string, error) {
result, err := library.RelayEnoughPeers(C.GoString(topic))
if result {
return "true", err
}
return "false", err
2023-10-28 23:37:53 +00:00
}, cb, userData)
2022-04-03 00:22:42 +00:00
}
2023-10-28 23:37:53 +00:00
// Publish a message using waku relay and returns the message ID. Use NULL for topic to derive the pubsub topic from the contentTopic.
2022-04-03 00:22:42 +00:00
// If ms is greater than 0, the broadcast of the message must happen before the timeout
2022-05-06 18:13:10 +00:00
// (in milliseconds) is reached, or an error will be returned.
2023-03-29 19:20:31 +00:00
//
//export waku_relay_publish
2023-10-28 23:37:53 +00:00
func waku_relay_publish(messageJSON *C.char, topic *C.char, ms C.int, cb C.WakuCallBack, userData unsafe.Pointer) C.int {
2023-08-10 13:30:38 +00:00
return singleFnExec(func() (string, error) {
return library.RelayPublish(C.GoString(messageJSON), C.GoString(topic), int(ms))
2023-10-28 23:37:53 +00:00
}, cb, userData)
2022-04-03 00:22:42 +00:00
}
2023-10-28 23:37:53 +00:00
// Subscribe to WakuRelay to receive messages matching a content filter.
// filterJSON must contain a JSON with this format:
//
// {
// "pubsubTopic": "the pubsub topic" // optional if using autosharding, mandatory if using static or named sharding.
// "contentTopics": ["the content topic"] // optional
// }
//
// When a message is received, a "message" event is emitted containing the message and pubsub topic in which
2022-04-14 15:41:44 +00:00
// the message was received
2023-03-29 19:20:31 +00:00
//
//export waku_relay_subscribe
2023-10-28 23:37:53 +00:00
func waku_relay_subscribe(filterJSON *C.char, cb C.WakuCallBack, userData unsafe.Pointer) C.int {
err := library.RelaySubscribe(C.GoString(filterJSON))
return onError(err, cb, userData)
2022-04-03 00:22:42 +00:00
}
2023-03-29 19:20:31 +00:00
// Returns a json response with the list of pubsub topics the node
// is subscribed to in WakuRelay
//
//export waku_relay_topics
2023-10-28 23:37:53 +00:00
func waku_relay_topics(cb C.WakuCallBack, userData unsafe.Pointer) C.int {
2023-08-10 13:30:38 +00:00
return singleFnExec(func() (string, error) {
return library.RelayTopics()
2023-10-28 23:37:53 +00:00
}, cb, userData)
2023-03-29 19:20:31 +00:00
}
2023-10-28 23:37:53 +00:00
// Closes the pubsub subscription to stop receiving messages matching a content filter
// filterJSON must contain a JSON with this format:
//
// {
// "pubsubTopic": "the pubsub topic" // optional if using autosharding, mandatory if using static or named sharding.
// "contentTopics": ["the content topic"] // optional
// }
2023-03-29 19:20:31 +00:00
//
//export waku_relay_unsubscribe
2023-10-28 23:37:53 +00:00
func waku_relay_unsubscribe(filterJSON *C.char, cb C.WakuCallBack, userData unsafe.Pointer) C.int {
err := library.RelayUnsubscribe(C.GoString(filterJSON))
return onError(err, cb, userData)
2022-04-03 00:22:42 +00:00
}