-
Notifications
You must be signed in to change notification settings - Fork 5
/
processor.go
199 lines (183 loc) · 4.49 KB
/
processor.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package znp
import (
"errors"
"fmt"
"time"
"github.com/dyrkin/unp-go"
"github.com/dyrkin/bin"
"github.com/dyrkin/znp-go/reflection"
"github.com/dyrkin/znp-go/request"
)
func (znp *Znp) Start() {
startProcessors(znp)
startIncomingFrameLoop(znp)
znp.started = true
}
func (znp *Znp) Stop() {
znp.started = false
}
func (znp *Znp) ProcessRequest(commandType unp.CommandType, subsystem unp.Subsystem, command byte, req interface{}, resp interface{}) error {
frame := &unp.Frame{
CommandType: commandType,
Subsystem: subsystem,
Command: command,
Payload: bin.Encode(req),
}
return processFrame(znp, frame, resp)
}
func processFrame(znp *Znp, frame *unp.Frame, resp interface{}) (err error) {
if !znp.started {
panic("Znp is not started. Call znp.Start() before")
}
completed := make(chan bool, 1)
go func() {
switch frame.CommandType {
case unp.C_SREQ:
outgoing := request.NewSync(frame)
znp.outbound <- outgoing
select {
case frame := <-outgoing.SyncRsp():
bin.Decode(frame.Payload, resp)
case err = <-outgoing.SyncErr():
}
case unp.C_AREQ:
outgoing := request.NewAsync(frame)
znp.outbound <- outgoing
default:
err = fmt.Errorf("unsupported command type: %s ", frame.CommandType)
}
completed <- true
}()
<-completed
return
}
func startProcessors(znp *Znp) {
syncRsp := make(chan *unp.Frame)
syncErr := make(chan error)
syncRequestProcessor := makeSyncRequestProcessor(znp, syncRsp, syncErr)
asyncRequestProcessor := makeAsyncRequestProcessor(znp)
syncResponseProcessor := makeSyncResponseProcessor(syncRsp, syncErr)
asyncResponseProcessor := makeAsyncResponseProcessor(znp)
outgoingProcessor := func() {
for znp.started {
select {
case outgoing := <-znp.outbound:
switch req := outgoing.(type) {
case *request.Sync:
syncRequestProcessor(req)
case *request.Async:
asyncRequestProcessor(req)
}
}
}
}
incomingProcessor := func() {
for znp.started {
select {
case frame := <-znp.inbound:
switch frame.CommandType {
case unp.C_SRSP:
syncResponseProcessor(frame)
case unp.C_AREQ:
asyncResponseProcessor(frame)
default:
select {
case znp.errors <- fmt.Errorf("unsupported frame received type: %v ", frame):
default:
}
}
}
}
}
go incomingProcessor()
go outgoingProcessor()
}
func startIncomingFrameLoop(znp *Znp) {
incomingLoop := func() {
for znp.started {
frame, err := znp.u.ReadFrame()
if err != nil {
select {
case znp.errors <- err:
default:
}
} else {
logFrame(frame, znp.inFramesLog)
znp.inbound <- frame
}
}
}
go incomingLoop()
}
func makeSyncRequestProcessor(znp *Znp, syncRsp chan *unp.Frame, syncErr chan error) func(req *request.Sync) {
return func(req *request.Sync) {
frame := req.Frame()
deadline := time.NewTimer(5 * time.Second)
logFrame(frame, znp.outFramesLog)
err := znp.u.WriteFrame(frame)
if err != nil {
req.SyncErr() <- err
return
}
select {
case _ = <-deadline.C:
if !deadline.Stop() {
req.SyncErr() <- fmt.Errorf("timed out while waiting response for command: 0x%x sent to subsystem: %s ", frame.Command, frame.Subsystem)
}
case response := <-syncRsp:
deadline.Stop()
req.SyncRsp() <- response
case err := <-syncErr:
deadline.Stop()
req.SyncErr() <- err
}
}
}
func makeAsyncRequestProcessor(znp *Znp) func(req *request.Async) {
return func(req *request.Async) {
logFrame(req.Frame(), znp.outFramesLog)
znp.u.WriteFrame(req.Frame())
}
}
var errorMessages = map[uint8]string{
1: "Invalid subsystem",
2: "Invalid command ID",
3: "Invalid parameter",
4: "Invalid length",
}
func makeSyncResponseProcessor(syncRsp chan *unp.Frame, syncErr chan error) func(frame *unp.Frame) {
return func(frame *unp.Frame) {
if frame.Subsystem == unp.S_RES0 && frame.Command == 0 {
errorCode := frame.Payload[0]
syncErr <- errors.New(errorMessages[errorCode])
} else {
syncRsp <- frame
}
}
}
func makeAsyncResponseProcessor(znp *Znp) func(frame *unp.Frame) {
return func(frame *unp.Frame) {
key := key{frame.Subsystem, frame.Command}
if value, ok := asyncCommandRegistry[key]; ok {
cp := reflection.Copy(value)
bin.Decode(frame.Payload, cp)
select {
case znp.asyncInbound <- cp:
default:
}
} else {
select {
case znp.errors <- fmt.Errorf("unknown async command received: %v", frame):
default:
}
}
}
}
func logFrame(frame *unp.Frame, logger chan *unp.Frame) {
go func() {
select {
case logger <- frame:
default:
}
}()
}