Skip to content

Commit

Permalink
fix: simplify waku_new
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Dec 15, 2023
1 parent 7f5f8ed commit b3c5469
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
10 changes: 4 additions & 6 deletions examples/c-bindings/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,14 @@ void callBack(int ret, const char *signal, void *user_data)

int main(int argc, char *argv[])
{
void* ctx = waku_init();

// Set callback to be executed each time a message is received
waku_set_event_callback(ctx, callBack);

// configJSON can be NULL too to use defaults. Any value not defined will have
// a default set
char *configJSON = "{\"host\": \"0.0.0.0\", \"port\": 60000, "
"\"logLevel\":\"error\", \"store\":true}";
waku_new(ctx, configJSON, on_error, NULL);
void* ctx = waku_new(configJSON, on_error, NULL);

// Set callback to be executed each time a message is received
waku_set_event_callback(ctx, callBack);

// Start the node, enabling the waku protocols
waku_start(ctx, on_error, NULL);
Expand Down
38 changes: 22 additions & 16 deletions library/c/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ import (

func main() {}

// Allocate memory for a waku node. The result of this function must be passed
// to all waku_* functions
//
//export waku_init
func waku_init() unsafe.Pointer {
cid := C.malloc(C.size_t(unsafe.Sizeof(uintptr(0))))
pid := (*uint)(cid)
instance := library.Init()
*pid = instance.ID
return cid
}

// Initialize a waku node. Receives a JSON string containing the configuration
// for the node. It can be NULL. Example configuration:
// ```
Expand Down Expand Up @@ -100,14 +88,23 @@ func waku_init() unsafe.Pointer {
// - dns4DomainName: the domain name resolving to the node's public IPv4 address.
//
//export waku_new
func waku_new(ctx unsafe.Pointer, configJSON *C.char, cb C.WakuCallBack, userData unsafe.Pointer) C.int {
instance, err := getInstance(ctx)
func waku_new(configJSON *C.char, cb C.WakuCallBack, userData unsafe.Pointer) unsafe.Pointer {
if cb == nil {
panic("error: missing callback in waku_new")
}

cid := C.malloc(C.size_t(unsafe.Sizeof(uintptr(0))))
pid := (*uint)(cid)
instance := library.Init()
*pid = instance.ID

err := library.NewNode(instance, C.GoString(configJSON))
if err != nil {
onError(err, cb, userData)
return nil
}

err = library.NewNode(instance, C.GoString(configJSON))
return onError(err, cb, userData)
return cid
}

// Starts the waku node
Expand Down Expand Up @@ -146,6 +143,15 @@ func waku_free(ctx unsafe.Pointer, onErr C.WakuCallBack, userData unsafe.Pointer
}

err = library.Stop(instance)
if err != nil {
return onError(err, onErr, userData)
}

err = library.Free(instance)
if err == nil {
C.free(ctx)
}

return onError(err, onErr, userData)
}

Expand Down
19 changes: 7 additions & 12 deletions library/mobile/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@ import (
"github.com/waku-org/go-waku/waku/v2/protocol"
)

// Allocate resources for a waku node
func Init() uint {
instance := library.Init()
return instance.ID
}

// NewNode initializes a waku node. Receives a JSON string containing the configuration, and use default values for those config items not specified
// 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, err := library.GetInstance(instanceID)
instance := library.Init()
err := library.NewNode(instance, configJSON)
if err != nil {
return makeJSONResponse(err)
_ = library.Free(instance)
}

err = library.NewNode(instance, configJSON)
return makeJSONResponse(err)
return prepareJSONResponse(instance.ID, err)
}

// Start starts the waku node
Expand Down

0 comments on commit b3c5469

Please sign in to comment.