improvements and clean-up

This commit is contained in:
kdeme 2019-09-12 19:27:43 +02:00 committed by zah
parent 23baeaa15d
commit e51474f393
4 changed files with 44 additions and 25 deletions

View File

@ -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 */

View File

@ -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)
@ -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

View File

@ -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();
}

View File

@ -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")
}