2019-09-12 16:32:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2019-11-04 12:03:28 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2019-09-12 16:32:07 +00:00
|
|
|
#cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm
|
|
|
|
#include "libnimbus.h"
|
|
|
|
|
2019-10-30 11:18:11 +00:00
|
|
|
void receiveHandler_cgo(received_message * msg, void* udata); // Forward declaration.
|
2019-09-12 16:32:07 +00:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
// Arrange that main.main runs on main thread.
|
|
|
|
func init() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
}
|
|
|
|
|
|
|
|
func poll() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
fmt.Println("POLLING")
|
|
|
|
time.Sleep(1 * time.Microsecond)
|
|
|
|
C.nimbus_poll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//export receiveHandler
|
2019-10-30 11:18:11 +00:00
|
|
|
func receiveHandler(msg *C.received_message, udata unsafe.Pointer) {
|
2019-11-04 12:03:28 +00:00
|
|
|
receivedMsg := C.GoBytes(unsafe.Pointer(msg.decoded), C.int(msg.decodedLen))
|
|
|
|
fmt.Printf("[nim-status] received message %s\n", string(receivedMsg))
|
2019-09-12 16:32:07 +00:00
|
|
|
fmt.Printf("[nim-status] source public key %x\n", msg.source)
|
2019-10-30 11:18:11 +00:00
|
|
|
msgCount := (*int)(udata)
|
|
|
|
*msgCount += 1
|
|
|
|
fmt.Printf("[nim-status] message count %d\n", *msgCount)
|
2019-09-12 16:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Start() {
|
|
|
|
C.NimMain()
|
|
|
|
fmt.Println("[nim-status] Start Nimbus")
|
2019-11-04 12:56:24 +00:00
|
|
|
C.nimbus_start(30306, false, false, 0.002)
|
2019-09-12 16:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func StatusListenAndPost(channel string) {
|
|
|
|
fmt.Println("[nim-status] Status Public ListenAndPost")
|
|
|
|
|
2019-11-04 12:03:28 +00:00
|
|
|
channelC := C.CString(channel)
|
|
|
|
defer C.free(unsafe.Pointer(channelC))
|
|
|
|
|
|
|
|
symKeyId := C.GoString(C.nimbus_add_symkey_from_password(channelC))
|
2019-09-12 16:32:07 +00:00
|
|
|
asymKeyId := C.GoString(C.nimbus_new_keypair())
|
|
|
|
|
2019-11-04 12:03:28 +00:00
|
|
|
var msgCount int = 0
|
|
|
|
|
2019-09-12 16:32:07 +00:00
|
|
|
options := C.filter_options{symKeyID: C.CString(symKeyId),
|
|
|
|
minPow: 0.002,
|
2019-11-04 12:03:28 +00:00
|
|
|
topic: C.nimbus_channel_to_topic(channelC).topic}
|
2019-10-30 11:18:11 +00:00
|
|
|
filterId := C.GoString(C.nimbus_subscribe_filter(&options,
|
|
|
|
(C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo)),
|
|
|
|
unsafe.Pointer(&msgCount)))
|
|
|
|
fmt.Printf("[nim-status] filter subscribed, id: %s\n", filterId)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-04 12:03:28 +00:00
|
|
|
symKeyIdC := C.CString(symKeyId)
|
|
|
|
defer C.free(unsafe.Pointer(symKeyIdC))
|
|
|
|
asymKeyIdC := C.CString(asymKeyId)
|
|
|
|
defer C.free(unsafe.Pointer(asymKeyIdC))
|
|
|
|
|
|
|
|
postMessage := C.post_message{symKeyID: symKeyIdC,
|
|
|
|
sourceID: asymKeyIdC,
|
2019-09-12 16:32:07 +00:00
|
|
|
ttl: 20,
|
2019-11-04 12:03:28 +00:00
|
|
|
topic: C.nimbus_channel_to_topic(channelC).topic,
|
2019-09-12 16:32:07 +00:00
|
|
|
powTarget: 0.002,
|
|
|
|
powTime: 1.0}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
C.nimbus_poll()
|
|
|
|
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\",\"%s\",\"~:text\",\"Message:%d\"]]]", i, t*100, t, channel, i)
|
|
|
|
if i%1000 == 0 {
|
2019-11-04 12:03:28 +00:00
|
|
|
fmt.Printf("[nim-status] posting msg number %d: %s\n", msgCount, message)
|
|
|
|
postMessage.payload = (*C.uint8_t)(C.CBytes([]byte(message)))
|
|
|
|
postMessage.payloadLen = (C.size_t)(len([]byte(message)))
|
|
|
|
defer C.free(unsafe.Pointer(postMessage.payload))
|
|
|
|
if C.nimbus_post(&postMessage) == false {
|
|
|
|
fmt.Println("[nim-status] message could not be added to queue")
|
|
|
|
}
|
2019-09-12 16:32:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
nprocs := runtime.GOMAXPROCS(0)
|
|
|
|
fmt.Println("GOMAXPROCS ", nprocs)
|
|
|
|
|
|
|
|
Start()
|
|
|
|
StatusListenAndPost("status-test-go")
|
|
|
|
}
|