2017-09-25 18:22:57 +00:00
|
|
|
package signal
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-08-10 11:23:28 +00:00
|
|
|
/*
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
extern bool StatusServiceSignalEvent(const char *jsonEvent);
|
|
|
|
extern void SetEventCallback(void *cb);
|
|
|
|
*/
|
|
|
|
import "C"
|
2017-05-16 12:09:52 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-08-10 11:23:28 +00:00
|
|
|
|
2018-06-27 23:16:27 +00:00
|
|
|
"unsafe"
|
|
|
|
|
2017-11-28 13:17:15 +00:00
|
|
|
"sync"
|
2018-02-05 10:37:59 +00:00
|
|
|
|
2018-03-20 18:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
2018-05-03 07:35:58 +00:00
|
|
|
// All general log messages in this package should be routed through this logger.
|
|
|
|
var logger = log.New("package", "status-go/signal")
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-25 18:22:57 +00:00
|
|
|
// Envelope is a general signal sent upward from node to RN app
|
|
|
|
type Envelope struct {
|
2017-05-16 12:09:52 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Event interface{} `json:"event"`
|
|
|
|
}
|
|
|
|
|
2018-05-03 07:35:58 +00:00
|
|
|
// NewEnvelope creates new envlope of given type and event payload.
|
|
|
|
func NewEnvelope(typ string, event interface{}) *Envelope {
|
|
|
|
return &Envelope{
|
|
|
|
Type: typ,
|
|
|
|
Event: event,
|
|
|
|
}
|
2018-02-05 10:37:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 07:35:58 +00:00
|
|
|
// send sends application signal (in JSON) upwards to application (via default notification handler)
|
|
|
|
func send(typ string, event interface{}) {
|
|
|
|
signal := NewEnvelope(typ, event)
|
|
|
|
data, err := json.Marshal(&signal)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Marshalling signal envelope", "error", err)
|
2018-07-26 11:04:52 +00:00
|
|
|
return
|
2018-05-03 07:35:58 +00:00
|
|
|
}
|
2018-07-26 11:04:52 +00:00
|
|
|
|
2018-08-10 11:23:28 +00:00
|
|
|
str := C.CString(string(data))
|
|
|
|
C.StatusServiceSignalEvent(str)
|
|
|
|
C.free(unsafe.Pointer(str))
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeNotificationHandler defines a handler able to process incoming node events.
|
|
|
|
// Events are encoded as JSON strings.
|
|
|
|
type NodeNotificationHandler func(jsonEvent string)
|
|
|
|
|
|
|
|
var notificationHandler NodeNotificationHandler = TriggerDefaultNodeNotificationHandler
|
|
|
|
|
2017-11-28 13:17:15 +00:00
|
|
|
// notificationHandlerMutex guards notificationHandler for concurrent calls
|
|
|
|
var notificationHandlerMutex sync.RWMutex
|
|
|
|
|
2017-09-25 18:22:57 +00:00
|
|
|
// SetDefaultNodeNotificationHandler sets notification handler to invoke on Send
|
2017-05-16 12:09:52 +00:00
|
|
|
func SetDefaultNodeNotificationHandler(fn NodeNotificationHandler) {
|
2017-11-28 13:17:15 +00:00
|
|
|
notificationHandlerMutex.Lock()
|
2017-05-16 12:09:52 +00:00
|
|
|
notificationHandler = fn
|
2017-11-28 13:17:15 +00:00
|
|
|
notificationHandlerMutex.Unlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 18:22:57 +00:00
|
|
|
// ResetDefaultNodeNotificationHandler sets notification handler to default one
|
2017-05-25 13:14:52 +00:00
|
|
|
func ResetDefaultNodeNotificationHandler() {
|
2017-11-28 13:17:15 +00:00
|
|
|
notificationHandlerMutex.Lock()
|
2017-05-25 13:14:52 +00:00
|
|
|
notificationHandler = TriggerDefaultNodeNotificationHandler
|
2017-11-28 13:17:15 +00:00
|
|
|
notificationHandlerMutex.Unlock()
|
2017-05-25 13:14:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// TriggerDefaultNodeNotificationHandler triggers default notification handler (helpful in tests)
|
|
|
|
func TriggerDefaultNodeNotificationHandler(jsonEvent string) {
|
2018-06-12 07:49:38 +00:00
|
|
|
logger.Trace("Notification received", "event", jsonEvent)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 11:23:28 +00:00
|
|
|
//export NotifyNode
|
2017-10-20 09:06:22 +00:00
|
|
|
//nolint: golint
|
2018-08-10 11:23:28 +00:00
|
|
|
func NotifyNode(jsonEvent *C.char) {
|
2017-11-28 13:17:15 +00:00
|
|
|
notificationHandlerMutex.RLock()
|
2018-08-10 11:23:28 +00:00
|
|
|
defer notificationHandlerMutex.RUnlock()
|
|
|
|
notificationHandler(C.GoString(jsonEvent))
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 11:23:28 +00:00
|
|
|
//export TriggerTestSignal
|
2017-10-20 09:06:22 +00:00
|
|
|
//nolint: golint
|
2018-08-10 11:23:28 +00:00
|
|
|
func TriggerTestSignal() {
|
|
|
|
str := C.CString(`{"answer": 42}`)
|
|
|
|
C.StatusServiceSignalEvent(str)
|
|
|
|
C.free(unsafe.Pointer(str))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSignalEventCallback set callback
|
|
|
|
func SetSignalEventCallback(cb unsafe.Pointer) {
|
|
|
|
C.SetEventCallback(cb)
|
|
|
|
}
|