-
Notifications
You must be signed in to change notification settings - Fork 8
/
event.go
159 lines (123 loc) · 2.73 KB
/
event.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package zerorpc
import (
"errors"
uuid "github.com/nu7hatch/gouuid"
"github.com/ugorji/go/codec"
)
// ZeroRPC protocol version
const ProtocolVersion = 3
// Event representation
type Event struct {
Header map[string]interface{}
Name string
Args []interface{}
}
// Returns a pointer to a new event,
// a UUID V4 message_id is generated
func newEvent(name string, args ...interface{}) (*Event, error) {
id, err := uuid.NewV4()
if err != nil {
return nil, err
}
header := make(map[string]interface{})
header["message_id"] = id.String()
header["v"] = ProtocolVersion
e := Event{
Header: header,
Name: name,
Args: args,
}
return &e, nil
}
// Packs an event into MsgPack bytes
func (e *Event) packBytes() ([]byte, error) {
data := make([]interface{}, 2)
data[0] = e.Header
data[1] = e.Name
for _, a := range e.Args {
data = append(data, a)
}
var buf []byte
enc := codec.NewEncoderBytes(&buf, &codec.MsgpackHandle{})
if err := enc.Encode(data); err != nil {
return nil, err
}
return buf, nil
}
// Unpacks an event fom MsgPack bytes
func unPackBytes(b []byte) (*Event, error) {
var mh codec.MsgpackHandle
var v interface{}
dec := codec.NewDecoderBytes(b, &mh)
err := dec.Decode(&v)
if err != nil {
return nil, err
}
// get the event headers
h, ok := v.([]interface{})[0].(map[interface{}]interface{})
if !ok {
return nil, errors.New("zerorpc/event interface conversion error")
}
header := make(map[string]interface{})
for k, v := range h {
switch t := v.(type) {
case []byte:
header[k.(string)] = string(t)
default:
header[k.(string)] = t
}
}
// get the event name
n, ok := v.([]interface{})[1].([]byte)
if !ok {
return nil, errors.New("zerorpc/event interface conversion error")
}
// get the event args
args := make([]interface{}, 0)
for i := 2; i < len(v.([]interface{})); i++ {
t := v.([]interface{})[i]
switch t.(type) {
case []interface{}:
for _, a := range t.([]interface{}) {
args = append(args, convertValue(a))
}
default:
args = append(args, convertValue(t))
}
}
e := Event{
Header: header,
Name: string(n),
Args: args,
}
return &e, nil
}
// Returns a pointer to a new heartbeat event
func newHeartbeatEvent() (*Event, error) {
ev, err := newEvent("_zpc_hb", nil)
if err != nil {
return nil, err
}
return ev, nil
}
// converts an interface{} to a type
func convertValue(v interface{}) interface{} {
var out interface{}
switch t := v.(type) {
case []byte:
out = string(t)
case []interface{}:
for i, x := range t {
t[i] = convertValue(x)
}
out = t
case map[interface{}]interface{}:
for key, val := range v.(map[interface{}]interface{}) {
t[key] = convertValue(val)
}
out = t
default:
out = t
}
return out
}