This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnotify.go
137 lines (108 loc) · 1.97 KB
/
notify.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
package spoe
import (
"github.com/pkg/errors"
)
type Arg struct {
Name string
Value interface{}
}
type ArgIterator struct {
b []byte
count int
Arg Arg
err error
}
func (i *ArgIterator) Next() bool {
if i.count == 0 {
return false
}
name, value, n, err := decodeKV(i.b)
if err != nil {
i.err = err
return false
}
i.b = i.b[n:]
i.Arg.Name = name
i.Arg.Value = value
i.count--
return true
}
func (i *ArgIterator) Map() map[string]interface{} {
res := make(map[string]interface{}, i.count)
for i.Next() {
res[i.Arg.Name] = i.Arg.Value
}
return res
}
func (i *ArgIterator) Count() int {
return i.count
}
type Message struct {
Name string
Args *ArgIterator
}
type MessageIterator struct {
b []byte
err error
Message Message
}
func NewMessageIterator(b []byte) *MessageIterator {
return &MessageIterator{
b: b,
Message: Message{
Args: &ArgIterator{
b: b,
},
},
}
}
func (i *MessageIterator) Error() error {
return i.err
}
func (i *MessageIterator) Next() bool {
if i.Message.Args != nil {
for i.Message.Args.Next() {
}
i.b = i.Message.Args.b
if i.Message.Args.err != nil {
i.err = i.Message.Args.err
return false
}
}
if len(i.b) == 0 {
return false
}
messageName, n, err := decodeString(i.b)
if err != nil {
i.err = err
return false
}
i.b = i.b[n:]
argCount := int(i.b[0])
i.b = i.b[1:]
i.Message.Args.b = i.b
i.Message.Args.count = argCount
i.Message.Name = messageName
return true
}
func (c *conn) handleNotify(f Frame, acks chan Frame) error {
messages := NewMessageIterator(f.data)
actions, err := c.handler(messages)
if err != nil {
return errors.Wrap(err, "handle notify") // TODO return proper response
}
f.ftype = frameTypeAgentACK
f.flags = frameFlagFin
f.data = f.originalData
off := 0
for _, a := range actions {
n, err := a.encode(f.data[off:])
if err != nil {
return errors.Wrap(err, "handle notify")
}
off += n
}
f.data = f.data[:off]
acks <- f
return nil
}