Make go wrapper_example subscribe work with c gateway function

This commit is contained in:
kdeme 2019-09-10 12:16:57 +02:00 committed by zah
parent 372f02dbcc
commit ef540fdb14
4 changed files with 35 additions and 8 deletions

View File

@ -86,7 +86,7 @@ wrappers: | build deps libnimbus.so go-checks
echo -e $(BUILD_MSG) "build/C_wrapper_example" && \
$(CC) wrappers/wrapper_example.c -Wl,-rpath,'$$ORIGIN' -Lbuild -lnimbus -lm -g -o build/C_wrapper_example
echo -e $(BUILD_MSG) "build/go_wrapper_example" && \
go build -linkshared -o build/go_wrapper_example wrappers/wrapper_example.go
go build -linkshared -o build/go_wrapper_example wrappers/wrapper_example.go wrappers/cfuncs.go
libnimbus.a: | build deps
echo -e $(BUILD_MSG) "build/$@" && \
@ -97,5 +97,5 @@ wrappers-static: | build deps libnimbus.a go-checks
echo -e $(BUILD_MSG) "build/C_wrapper_example_static" && \
$(CC) wrappers/wrapper_example.c -static -pthread -Lbuild -lnimbus -lm -ldl -lpcre -g -o build/C_wrapper_example_static
echo -e $(BUILD_MSG) "build/go_wrapper_example_static" && \
go build -ldflags "-linkmode external -extldflags '-static -ldl -lpcre'" -o build/go_wrapper_example_static wrappers/wrapper_example.go
go build -ldflags "-linkmode external -extldflags '-static -ldl -lpcre'" -o build/go_wrapper_example_static wrappers/wrapper_example.go wrappers/cfuncs.go

14
wrappers/cfuncs.go Normal file
View File

@ -0,0 +1,14 @@
package main
/*
#include "libnimbus.h"
// receiveHandler gateway function
void receiveHandler_cgo(received_message * msg)
{
void receiveHandler(received_message*);
receiveHandler(msg);
}
*/
import "C"

View File

@ -3,13 +3,15 @@
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include "libnimbus.h"
void NimMain();
void print_msg(received_message* msg) {
printf("Got message! %ld\n", msg->decodedLen);
// Note: early null chars will terminate string early
printf("received message %.*s\n", (int)msg->decodedLen, msg->decoded);
}
const char* channel = "status-test-c";

View File

@ -4,10 +4,16 @@ import (
"fmt"
"runtime"
"time"
"unsafe"
)
// #cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm
// #include "libnimbus.h"
/*
#cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm
#include "libnimbus.h"
void receiveHandler_cgo(received_message * msg); // Forward declaration.
*/
import "C"
// Arrange that main.main runs on main thread.
@ -24,11 +30,16 @@ func poll() {
}
}
//export receiveHandler
func receiveHandler(msg *C.received_message) {
fmt.Printf("[nim-status] received message %s\n", C.GoStringN((*C.char)(msg.decoded), (C.int)(msg.decodedLen)) )
}
func Start() {
C.NimMain()
fmt.Println("[nim-status] Start 1")
fmt.Println(C.nimbus_start(30306))
//C.nimbus_subscribe(C.CString("status-test-c"), nil)
C.nimbus_subscribe(C.CString("status-test-go"), (C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo)))
fmt.Println("[nim-status] Start 2")
peer1 := "enode://2d3e27d7846564f9b964308038dfadd4076e4373ac938e020708ad8819fd4fd90e5eb8314140768f782db704cb313b60707b968f8b61108a6fecd705b041746d@192.168.0.33:30303"
@ -51,10 +62,10 @@ func ListenAndPost() {
t := time.Now().UnixNano() / int64(time.Millisecond)
i = i + 1
time.Sleep(1 * time.Microsecond)
message := fmt.Sprintf("[\"~#c4\",[\"Message:%d\",\"text/plain\",\"~:public-group-user-message\",%d,%d,[\"^ \",\"~:chat-id\",\"status-test-c\",\"~:text\",\"Message:%d\"]]]", i, t*100, t, i)
message := fmt.Sprintf("[\"~#c4\",[\"Message:%d\",\"text/plain\",\"~:public-group-user-message\",%d,%d,[\"^ \",\"~:chat-id\",\"status-test-go\",\"~:text\",\"Message:%d\"]]]", i, t*100, t, i)
if i%1000 == 0 {
fmt.Println("[nim-status] posting", message)
C.nimbus_post(C.CString("status-test-c"), C.CString(message))
C.nimbus_post(C.CString("status-test-go"), C.CString(message))
}
}
}