From e51474f3939e928d844f24f82be5a782e6184493 Mon Sep 17 00:00:00 2001 From: kdeme Date: Thu, 12 Sep 2019 19:27:43 +0200 Subject: [PATCH] improvements and clean-up --- wrappers/libnimbus.h | 4 ++-- wrappers/libnimbus.nim | 34 ++++++++++++++++++++++++++++------ wrappers/wrapper_example.c | 4 ++-- wrappers/wrapper_example.go | 27 ++++++++++++--------------- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/wrappers/libnimbus.h b/wrappers/libnimbus.h index d175e67a4..8a30b9840 100644 --- a/wrappers/libnimbus.h +++ b/wrappers/libnimbus.h @@ -60,8 +60,8 @@ void nimbus_add_peer(const char* nodeId); */ void nimbus_poll(); -void nimbus_post(const char* channel, const char* payload); -void nimbus_subscribe(const char* channel, received_msg_handler msg); +void nimbus_post_public(const char* channel, const char* payload); +void nimbus_join_public_chat(const char* channel, received_msg_handler msg); /* Whisper API */ diff --git a/wrappers/libnimbus.nim b/wrappers/libnimbus.nim index 4947d6653..a6d560a1b 100644 --- a/wrappers/libnimbus.nim +++ b/wrappers/libnimbus.nim @@ -74,7 +74,6 @@ let whisperKeys = newWhisperKeys() # TODO: Return filter ID if we ever want to unsubscribe proc subscribeChannel( channel: string, handler: proc (msg: ReceivedMessage) {.gcsafe.}) = - setupForeignThreadGc() var ctx: HMAC[sha256] var symKey: SymKey discard ctx.pbkdf2(channel, "", 65356, symKey) @@ -87,8 +86,8 @@ proc subscribeChannel( info "Subscribing to channel", channel, topic, symKey discard node.subscribeFilter(newFilter(symKey = some(symKey), - topics = @[topic]), - handler) + topics = @[topic]), + handler) # proc handler(msg: ReceivedMessage) {.gcsafe.} = # try: @@ -111,6 +110,7 @@ proc subscribeChannel( proc nimbus_start(port: uint16 = 30303) {.exportc.} = setupForeignThreadGc() + let address = Address( udpPort: port.Port, tcpPort: port.Port, ip: parseIpAddress("0.0.0.0")) @@ -135,12 +135,18 @@ proc nimbus_start(port: uint16 = 30303) {.exportc.} = asyncCheck node.peerPool.connectToNode(whisperNode) + tearDownForeignThreadGc() + proc nimbus_poll() {.exportc.} = setupForeignThreadGc() + poll() -# TODO: Consider better naming to understand how it relates to public channels etc -proc nimbus_subscribe(channel: cstring, handler: proc (msg: ptr CReceivedMessage) {.gcsafe, cdecl.}) {.exportc.} = + tearDownForeignThreadGc() + +proc nimbus_join_public_chat(channel: cstring, + handler: proc (msg: ptr CReceivedMessage) + {.gcsafe, cdecl.}) {.exportc.} = setupForeignThreadGc() if handler.isNil: @@ -161,10 +167,13 @@ proc nimbus_subscribe(channel: cstring, handler: proc (msg: ptr CReceivedMessage subscribeChannel($channel, c_handler) + tearDownForeignThreadGc() + # TODO: Add signing key as parameter # TODO: How would we do key management? In nimbus (like in rpc) or in status go? -proc nimbus_post(channel: cstring, payload: cstring) {.exportc.} = +proc nimbus_post_public(channel: cstring, payload: cstring) {.exportc.} = setupForeignThreadGc() + let encPrivateKey = initPrivateKey("5dc5381cae54ba3174dc0d46040fe11614d0cc94d41185922585198b4fcef9d3") var ctx: HMAC[sha256] @@ -185,26 +194,39 @@ proc nimbus_post(channel: cstring, payload: cstring) {.exportc.} = payload = npayload, powTarget = 0.002) + tearDownForeignThreadGc() + proc nimbus_add_peer(nodeId: cstring) {.exportc.} = setupForeignThreadGc() + var whisperENode: ENode discard initENode($nodeId, whisperENode) var whisperNode = newNode(whisperENode) asyncCheck node.peerPool.connectToNode(whisperNode) + tearDownForeignThreadGc() + # Whisper API (Similar to Whisper RPC API) # Mostly an example for now, lots of things to fix if continued like this. proc nimbus_string_to_topic(s: cstring): CTopic {.exportc.} = + setupForeignThreadGc() + let hash = digest(keccak256, $s) for i in 0..<4: result.topic[i] = hash.data[i] + tearDownForeignThreadGc() + proc nimbus_new_keypair(): cstring {.exportc.} = + setupForeignThreadGc() + result = generateRandomID() whisperKeys.asymKeys.add($result, newKeyPair()) + tearDownForeignThreadGc() + proc nimbus_add_keypair(key: PrivateKey): cstring = discard proc nimbus_delete_keypair(id: cstring) = discard proc nimbus_add_symkey(key: SymKey): cstring = discard diff --git a/wrappers/wrapper_example.c b/wrappers/wrapper_example.c index 730ef3547..d65abe772 100644 --- a/wrappers/wrapper_example.c +++ b/wrappers/wrapper_example.c @@ -24,7 +24,7 @@ int main(int argc, char* argv[]) { NimMain(); nimbus_start(30303); - nimbus_subscribe(channel, print_msg); + nimbus_join_public_chat(channel, print_msg); lastmsg = time(NULL); @@ -39,7 +39,7 @@ int main(int argc, char* argv[]) { msg, lastmsg * 1000 * 100, lastmsg * 1000, channel, msg); printf("Posting %s\n", buf); - nimbus_post(channel, buf); + nimbus_post_public(channel, buf); } nimbus_poll(); } diff --git a/wrappers/wrapper_example.go b/wrappers/wrapper_example.go index 2eb985fa9..61fdc8cff 100644 --- a/wrappers/wrapper_example.go +++ b/wrappers/wrapper_example.go @@ -37,24 +37,21 @@ func receiveHandler(msg *C.received_message) { func Start() { C.NimMain() - fmt.Println("[nim-status] Start 1") - fmt.Println(C.nimbus_start(30306)) - C.nimbus_subscribe(C.CString("status-test-go"), (C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo))) - fmt.Println("[nim-status] Start 2") + fmt.Println("[nim-status] Start Nimbus") + C.nimbus_start(30306) peer1 := "enode://2d3e27d7846564f9b964308038dfadd4076e4373ac938e020708ad8819fd4fd90e5eb8314140768f782db704cb313b60707b968f8b61108a6fecd705b041746d@192.168.0.33:30303" peer2 := "enode://4ea35352702027984a13274f241a56a47854a7fd4b3ba674a596cff917d3c825506431cf149f9f2312a293bb7c2b1cca55db742027090916d01529fe0729643b@206.189.243.178:443" - peer3 := "enode://94d2403d0c55b5c1627eb032c4c6ea8d30b523ae84661aafa18c539ce3af3f114a5bfe1a3cde7776988a6ab2906169dca8ce6a79e32d30c445629b24e6f59e0a@0.0.0.0:30303" - fmt.Println(C.nimbus_add_peer(C.CString(peer1))) - fmt.Println(C.nimbus_add_peer(C.CString(peer2))) - - fmt.Println(C.nimbus_add_peer(C.CString(peer3))) - + C.nimbus_add_peer(C.CString(peer1)) + C.nimbus_add_peer(C.CString(peer2)) + C.nimbus_add_peer(C.CString(peer3)) } -func ListenAndPost() { - fmt.Println("[nim-status] ListenAndPost 1") +func StatusListenAndPost(channel string) { + fmt.Println("[nim-status] Status Public ListenAndPost") + C.nimbus_join_public_chat(C.CString(channel), + (C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo))) i := 0 for { //fmt.Println("[nim-status] ListenAndPost (post @i==1000) i= ", i) @@ -62,10 +59,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-go\",\"~:text\",\"Message:%d\"]]]", i, t*100, t, i) + message := fmt.Sprintf("[\"~#c4\",[\"Message:%d\",\"text/plain\",\"~:public-group-user-message\",%d,%d,[\"^ \",\"~:chat-id\",\"%s\",\"~:text\",\"Message:%d\"]]]", i, t*100, t, channel, i) if i%1000 == 0 { fmt.Println("[nim-status] posting", message) - C.nimbus_post(C.CString("status-test-go"), C.CString(message)) + C.nimbus_post_public(C.CString(channel), C.CString(message)) } } } @@ -77,5 +74,5 @@ func main() { fmt.Println("GOMAXPROCS ", nprocs) Start() - ListenAndPost() + StatusListenAndPost("status-test-go") }