-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipc.go
58 lines (49 loc) · 1.38 KB
/
ipc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package rubik
import (
"fmt"
"reflect"
"time"
"github.com/rubikorg/rubik/pkg"
)
// IpcMessage is the structure using which Rubik services
// communicates with each other
type IpcMessage struct {
Type interface{}
Func func(interface{})
}
type ipcModem struct {
wsMap map[string]string
msgRx map[string]IpcMessage
}
// Send transmits a message using the ipcModem to the given service
// the message is identified by the receiver using the msgType argument
func (ipc ipcModem) Send(msgType string, service string, message interface{}) {
if s, ok := ipc.wsMap[service]; ok {
txClient := NewClient(s, time.Second*30)
ipcRxEn := IpcRxEntity{
Message: msgType,
Body: message,
}
ipcRxEn.PointTo = "/rubik/msg/rx/:message"
ipcRxEn.Params = []string{msgType}
_, err := txClient.Post(ipcRxEn)
if err != nil {
pkg.ErrorMsg(err.Error())
}
fmt.Printf("Message: %s, published\n", msgType)
return
}
pkg.ErrorMsg(fmt.Sprintf("%s is not present in this workspace", service))
}
// OnMessage registers a IpcMessage handler for the given
// message type
func (ipc ipcModem) OnMessage(msgType string, ipcMp IpcMessage) {
if reflect.TypeOf(ipcMp.Type).Kind() != reflect.Ptr {
panic(fmt.Errorf("OnMessage: %s has IpcMessage type as non-pointer", msgType))
}
ipc.msgRx[msgType] = ipcMp
}
var Ipc = ipcModem{
wsMap: make(map[string]string),
msgRx: make(map[string]IpcMessage),
}