/
packet.go
100 lines (86 loc) · 2.41 KB
/
packet.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package bund
import (
"fmt"
"github.com/google/uuid"
"github.com/vmihailenco/msgpack"
"github.com/pieterclaerhout/go-log"
"github.com/vulogov/monitoringbund/internal/conf"
"github.com/vulogov/monitoringbund/internal/signal"
)
type NRBundPacket struct {
Id string
Uri string
PktId string
OrigName string
OrgRole string
PktClass string
PktKey string
Args []string
Res map[string]interface{}
Value []byte
}
func (msg *NRBundPacket) ApplicationId() string {
return fmt.Sprintf("%v:%v:%v", msg.Id, msg.OrigName, msg.OrgRole)
}
func NewBundPacket(uri string, orole string, pktclass string, pktkey string, args []string, value []byte, ret map[string]interface{}) *NRBundPacket {
res := new(NRBundPacket)
res.PktId = uuid.New().String()
res.Id = *conf.Id
res.Uri = uri
res.OrigName = *conf.Name
res.OrgRole = orole
res.PktClass = pktclass
res.PktKey = pktkey
res.Args = args
res.Res = ret
res.Value = value
return res
}
func Marshal(uri string, orole string, pktclass string, pktkey string, args []string, value []byte, ret map[string]interface{}) ([]byte, error) {
res := NewBundPacket(uri, orole, pktclass, pktkey, args, value, ret)
return msgpack.Marshal(res)
}
func MarshalPacket(pkt *NRBundPacket) ([]byte, error) {
return msgpack.Marshal(pkt)
}
func UnMarshal(data []byte) *NRBundPacket {
res := new(NRBundPacket)
err := msgpack.Unmarshal(data, res)
if err != nil {
log.Errorf("[ PACKET ] %v", err)
return nil
}
return res
}
func IfSTOP(msg *NRBundPacket) bool {
if msg.PktClass == "SYS" && msg.PktKey == "STOP" {
signal.ExitRequest()
DoContinue = false
return true
}
return false
}
func IfMSG(msg *NRBundPacket) bool {
if msg.PktClass == "SYS" && msg.PktKey == "MSG" {
return true
}
return false
}
func IfSYNC(msg *NRBundPacket) bool {
if msg.PktClass == "SYS" && msg.PktKey == "SYNC" {
return true
}
return false
}
func MakeSync(orole string) ([]byte, error) {
return Marshal("", orole, "SYS", "SYNC", nil, nil, nil)
}
func MakeStop(orole string) ([]byte, error) {
return Marshal("", orole, "SYS", "STOP", nil, nil, nil)
}
func MakeMsg(msg string) ([]byte, error) {
return Marshal("", ApplicationType, "SYS", "MSG", nil, []byte(msg), nil)
}
func MakeScript(uri string, orole string, script []byte, args []string, res map[string]interface{}) ([]byte, error) {
return Marshal(uri, orole, "SYS", "Agitator", args, script, res)
}