2017-09-25 18:22:57 +00:00
|
|
|
package signal
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
extern bool StatusServiceSignalEvent(const char *jsonEvent);
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-08-10 13:35:58 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/geth/log"
|
2017-11-28 13:17:15 +00:00
|
|
|
"sync"
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// EventNodeStarted is triggered when underlying node is started
|
|
|
|
EventNodeStarted = "node.started"
|
|
|
|
|
|
|
|
// EventNodeReady is triggered when underlying node is fully ready
|
|
|
|
// (consider backend to be fully registered)
|
|
|
|
EventNodeReady = "node.ready"
|
|
|
|
|
|
|
|
// EventNodeStopped is triggered when underlying node is fully stopped
|
|
|
|
EventNodeStopped = "node.stopped"
|
|
|
|
|
|
|
|
// EventNodeCrashed is triggered when node crashes
|
|
|
|
EventNodeCrashed = "node.crashed"
|
|
|
|
|
|
|
|
// EventChainDataRemoved is triggered when node's chain data is removed
|
|
|
|
EventChainDataRemoved = "chaindata.removed"
|
|
|
|
)
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeCrashEvent is special kind of error, used to report node crashes
|
|
|
|
type NodeCrashEvent struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
log.Info("Notification received", "event", jsonEvent)
|
|
|
|
}
|
|
|
|
|
2017-09-25 18:22:57 +00:00
|
|
|
// Send sends application signal (JSON, normally) upwards to application (via default notification handler)
|
|
|
|
func Send(signal Envelope) {
|
2017-05-16 12:09:52 +00:00
|
|
|
data, _ := json.Marshal(&signal)
|
|
|
|
C.StatusServiceSignalEvent(C.CString(string(data)))
|
|
|
|
}
|
|
|
|
|
|
|
|
//export NotifyNode
|
2017-10-20 09:06:22 +00:00
|
|
|
//nolint: golint
|
|
|
|
func NotifyNode(jsonEvent *C.char) {
|
2017-11-28 13:17:15 +00:00
|
|
|
notificationHandlerMutex.RLock()
|
|
|
|
defer notificationHandlerMutex.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
notificationHandler(C.GoString(jsonEvent))
|
|
|
|
}
|
|
|
|
|
|
|
|
//export TriggerTestSignal
|
2017-10-20 09:06:22 +00:00
|
|
|
//nolint: golint
|
|
|
|
func TriggerTestSignal() {
|
2017-05-16 12:09:52 +00:00
|
|
|
C.StatusServiceSignalEvent(C.CString(`{"answer": 42}`))
|
|
|
|
}
|