2019-07-31 08:05:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
2019-09-10 10:16:57 +00:00
|
|
|
"unsafe"
|
2019-07-31 08:05:22 +00:00
|
|
|
)
|
|
|
|
|
2019-09-10 10:16:57 +00:00
|
|
|
/*
|
2019-11-13 11:04:38 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2020-02-09 16:31:37 +00:00
|
|
|
// Passing "-lnimbus" to the Go linker through "-extldflags" is not enough. We need it in here, for some reason.
|
|
|
|
#cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus
|
2019-09-10 10:16:57 +00:00
|
|
|
#include "libnimbus.h"
|
|
|
|
|
|
|
|
void receiveHandler_cgo(received_message * msg); // Forward declaration.
|
|
|
|
*/
|
2019-07-31 08:05:22 +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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 10:16:57 +00:00
|
|
|
//export receiveHandler
|
|
|
|
func receiveHandler(msg *C.received_message) {
|
2019-11-04 12:56:24 +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-10 10:16:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 08:05:22 +00:00
|
|
|
func Start() {
|
|
|
|
C.NimMain()
|
2019-09-12 17:27:43 +00:00
|
|
|
fmt.Println("[nim-status] Start Nimbus")
|
2019-11-13 11:04:38 +00:00
|
|
|
if C.nimbus_start(30306, true, false, 0.002, nil, false) == false {
|
|
|
|
panic("Can't start nimbus")
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1 := C.CString("enode://2d3e27d7846564f9b964308038dfadd4076e4373ac938e020708ad8819fd4fd90e5eb8314140768f782db704cb313b60707b968f8b61108a6fecd705b041746d@192.168.0.33:30303")
|
2019-11-18 20:55:09 +00:00
|
|
|
defer C.free(unsafe.Pointer(peer1))
|
2019-11-13 11:04:38 +00:00
|
|
|
C.nimbus_add_peer(peer1)
|
2019-07-31 08:05:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 17:27:43 +00:00
|
|
|
func StatusListenAndPost(channel string) {
|
|
|
|
fmt.Println("[nim-status] Status Public ListenAndPost")
|
2019-11-13 11:04:38 +00:00
|
|
|
channelC := C.CString(channel)
|
|
|
|
defer C.free(unsafe.Pointer(channelC))
|
|
|
|
|
|
|
|
C.nimbus_join_public_chat(channelC,
|
2019-09-12 17:27:43 +00:00
|
|
|
(C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo)))
|
2019-07-31 08:05:22 +00:00
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
//fmt.Println("[nim-status] ListenAndPost (post @i==1000) i= ", i)
|
|
|
|
C.nimbus_poll()
|
|
|
|
t := time.Now().UnixNano() / int64(time.Millisecond)
|
|
|
|
i = i + 1
|
|
|
|
time.Sleep(1 * time.Microsecond)
|
2019-09-12 17:27:43 +00:00
|
|
|
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)
|
2019-07-31 08:05:22 +00:00
|
|
|
if i%1000 == 0 {
|
|
|
|
fmt.Println("[nim-status] posting", message)
|
2019-11-13 11:04:38 +00:00
|
|
|
messageC := C.CString(message)
|
|
|
|
C.nimbus_post_public(channelC, messageC)
|
|
|
|
C.free(unsafe.Pointer(messageC))
|
2019-07-31 08:05:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("Hi main")
|
|
|
|
|
|
|
|
nprocs := runtime.GOMAXPROCS(0)
|
|
|
|
fmt.Println("GOMAXPROCS ", nprocs)
|
|
|
|
|
|
|
|
Start()
|
2019-09-12 17:27:43 +00:00
|
|
|
StatusListenAndPost("status-test-go")
|
2019-07-31 08:05:22 +00:00
|
|
|
}
|