go-waku/library/mobile/api.go

163 lines
4.7 KiB
Go
Raw Normal View History

2023-08-10 13:30:38 +00:00
// Package gowaku implements gomobile bindings for go-waku. Contains a set of functions that
// are exported when go-waku is exported as libraries for mobile devices
package gowaku
import (
"github.com/waku-org/go-waku/library"
"github.com/waku-org/go-waku/waku/v2/protocol"
)
// NewNode initializes a waku node.
// Receives a JSON string containing the configuration, and use default values for those config items not specified
// Returns an instance id
func NewNode(instanceID uint, configJSON string) string {
instance := library.Init()
err := library.NewNode(instance, configJSON)
if err != nil {
_ = library.Free(instance)
}
return prepareJSONResponse(instance.ID, err)
2023-08-10 13:30:38 +00:00
}
// Start starts the waku node
func Start(instanceID uint) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
err = library.Start(instance)
2023-08-10 13:30:38 +00:00
return makeJSONResponse(err)
}
// Stop stops a waku node
func Stop(instanceID uint) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
err = library.Stop(instance)
return makeJSONResponse(err)
}
// Release resources allocated to a waku node
func Free(instanceID uint) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
err = library.Free(instance)
2023-08-10 13:30:38 +00:00
return makeJSONResponse(err)
}
// IsStarted is used to determine is a node is started or not
func IsStarted(instanceID uint) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
return prepareJSONResponse(library.IsStarted(instance), nil)
2023-08-10 13:30:38 +00:00
}
// PeerID is used to obtain the peer ID of the waku node
func PeerID(instanceID uint) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
peerID, err := library.PeerID(instance)
2023-08-10 13:30:38 +00:00
return prepareJSONResponse(peerID, err)
}
// ListenAddresses returns the multiaddresses the wakunode is listening to
func ListenAddresses(instanceID uint) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
addresses, err := library.ListenAddresses(instance)
2023-08-10 13:30:38 +00:00
return prepareJSONResponse(addresses, err)
}
// AddPeer adds a node multiaddress and protocol to the wakunode peerstore
func AddPeer(instanceID uint, address string, protocolID string) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
peerID, err := library.AddPeer(instance, address, protocolID)
2023-08-10 13:30:38 +00:00
return prepareJSONResponse(peerID, err)
}
// Connect is used to connect to a peer at multiaddress. if ms > 0, cancel the function execution if it takes longer than N milliseconds
func Connect(instanceID uint, address string, ms int) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
err = library.Connect(instance, address, ms)
2023-08-10 13:30:38 +00:00
return makeJSONResponse(err)
}
// ConnectPeerID is usedd to connect to a known peer by peerID. if ms > 0, cancel the function execution if it takes longer than N milliseconds
func ConnectPeerID(instanceID uint, peerID string, ms int) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
err = library.ConnectPeerID(instance, peerID, ms)
2023-08-10 13:30:38 +00:00
return makeJSONResponse(err)
}
// Disconnect closes a connection to a known peer by peerID
func Disconnect(instanceID uint, peerID string) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
err = library.Disconnect(instance, peerID)
2023-08-10 13:30:38 +00:00
return makeJSONResponse(err)
}
// PeerCnt returns the number of connected peers
func PeerCnt(instanceID uint) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
peerCnt, err := library.PeerCnt(instance)
2023-08-10 13:30:38 +00:00
return prepareJSONResponse(peerCnt, err)
}
// ContentTopic creates a content topic string according to RFC 23
func ContentTopic(applicationName string, applicationVersion string, contentTopicName string, encoding string) string {
contentTopic, _ := protocol.NewContentTopic(applicationName, applicationVersion, contentTopicName, encoding)
return contentTopic.String()
2023-08-10 13:30:38 +00:00
}
// DefaultPubsubTopic returns the default pubsub topic used in waku2: /waku/2/default-waku/proto
func DefaultPubsubTopic() string {
return library.DefaultPubsubTopic()
2023-08-10 13:30:38 +00:00
}
// Peers retrieves the list of peers known by the waku node
func Peers(instanceID uint) string {
instance, err := library.GetInstance(instanceID)
if err != nil {
return makeJSONResponse(err)
}
peers, err := library.Peers(instance)
2023-08-10 13:30:38 +00:00
return prepareJSONResponse(peers, err)
}