-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto.go
365 lines (309 loc) · 7.73 KB
/
proto.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package apc
import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"
)
const (
// RS is Message separator
RS byte = 0x1E
// ETB is IsIncomplete message separator
ETB byte = 0x17
// ETX is End of text
ETX byte = 0x03
)
type EventType byte
const (
EventTypeCommand EventType = 'C'
EventTypePending EventType = 'P'
EventTypeData EventType = 'D'
EventTypeResponse EventType = 'R'
EventTypeBusy EventType = 'B'
EventTypeNotification EventType = 'N'
)
type Event struct {
Keyword string
Type EventType
Client string
ProcessID uint32
InvokeID uint32
Segments []string
IsIncomplete bool
}
func (e Event) IsStart() bool {
if e.Type != EventTypeNotification ||
len(e.Segments) < 2 ||
e.Segments[0] != "0" ||
e.Segments[1] != "AGENT_STARTUP" {
return false
}
return true
}
func (e Event) IsPending() bool {
if e.Type != EventTypePending ||
len(e.Segments) < 2 ||
e.Segments[0] != "0" ||
e.Segments[1] != "S28833" {
return false
}
return true
}
func (e Event) IsDataMessage() bool {
if e.Type != EventTypeData ||
len(e.Segments) < 2 ||
e.Segments[0] != "0" {
return false
}
return true
}
func (e Event) IsSuccessfulResponse() bool {
if e.Type != EventTypeResponse ||
len(e.Segments) < 2 ||
e.Segments[0] != "0" ||
e.Segments[1] != "M00000" {
return false
}
return true
}
func (e Event) IsResponseError() bool {
if e.Type != EventTypeResponse ||
len(e.Segments) < 2 ||
e.Segments[0] != "1" ||
!strings.HasPrefix(e.Segments[1], "E") {
return false
}
return true
}
func (e Event) IsSuccessfulNotification() bool {
if e.Type != EventTypeNotification ||
len(e.Segments) < 2 ||
e.Segments[0] != "0" ||
e.Segments[1] != "M00000" {
return false
}
return true
}
func (e Event) IsNotificationError() bool {
if e.Type != EventTypeNotification ||
len(e.Segments) < 2 ||
e.Segments[0] != "1" ||
len(e.Segments[1]) != 6 ||
!strings.HasPrefix(e.Segments[1], "E") {
return false
}
return true
}
func (e Event) IsNotificationData() bool {
if e.Type != EventTypeNotification ||
len(e.Segments) < 2 ||
e.Segments[0] != "0" ||
e.Segments[1] != "M00001" {
return false
}
return true
}
func encodeCommand(keyword string, invokeID uint32, args ...string) ([]byte, error) {
// Checks
if len(keyword) > 20 {
return nil, errors.New("keyword should be less or equal to 20 bytes")
}
if len(strconv.Itoa(int(invokeID))) > 4 {
return nil, errors.New("invoke id should be less or equal to 4 bytes")
}
buf := bytes.NewBuffer(nil)
// Keyword; 20 bytes
buf.WriteString(fmt.Sprintf("%-20s", keyword))
// Type; 1 byte
buf.WriteByte('C')
// Client; 20 bytes
buf.WriteString(fmt.Sprintf("%-20s", "Golang"))
// Process ID; 6 bytes
buf.WriteString(fmt.Sprintf("%-6d", 0))
// Invoke ID; 4 bytes
buf.WriteString(fmt.Sprintf("%-4d", invokeID))
// Number of segments; 4 bytes
buf.WriteString(fmt.Sprintf("%-4d", len(args)))
if len(args) > 0 {
buf.WriteByte(RS)
for i, arg := range args {
buf.WriteString(arg)
if len(args)-1 != i {
buf.WriteByte(RS)
}
}
}
buf.WriteByte(ETX)
return buf.Bytes(), nil
}
type decodingError struct {
error
}
func newDecodingError(text string) error {
return &decodingError{errors.New(text)}
}
func IsDecodingError(err error) bool {
_, ok := err.(*decodingError)
return ok
}
func decodeEvent(raw string) (event Event, err error) {
if len(raw) < 56 {
return Event{}, newDecodingError("event len should be less or equal to 55 bytes")
}
event = Event{
Keyword: strings.TrimSpace(raw[:20]),
Type: EventType(raw[20]),
Client: strings.TrimSpace(raw[21:41]),
}
processID, err := strconv.Atoi(strings.TrimSpace(raw[41:47]))
if err != nil {
return Event{}, newDecodingError("cannot parse process id as int")
}
event.ProcessID = uint32(processID)
invokeID, err := strconv.Atoi(strings.TrimSpace(raw[47:51]))
if err != nil {
return Event{}, newDecodingError("cannot parse invoke id as int")
}
event.InvokeID = uint32(invokeID)
numberOfSegments, err := strconv.Atoi(strings.TrimSpace(raw[51:55]))
if err != nil {
return Event{}, newDecodingError("cannot parse number of segments as int")
}
if numberOfSegments > 0 && len(raw) > 56 {
segments := strings.Split(raw[56:], string(RS))
for i, s := range segments {
// Trim last byte if it reached the end
if i == len(segments)-1 {
if strings.HasSuffix(s, string(ETB)) {
event.IsIncomplete = true
s = strings.TrimSuffix(s, string(ETB))
}
if strings.HasSuffix(s, string(ETX)) {
s = strings.TrimSuffix(s, string(ETX))
}
}
event.Segments = append(event.Segments, s)
}
}
return
}
type AvayaError struct {
Code string
}
func (e AvayaError) Error() string {
return e.Code
}
func processRequest(r *request) ([]string, error) {
var (
dataSegments []string
batch bool
)
el:
for {
select {
case event := <-r.eventChan:
switch {
// Skip pending events
case event.IsPending():
continue
// Handle data messages and wait successful request
case event.IsDataMessage():
dataSegments = append(dataSegments, event.Segments[1:]...)
// If event is incomplete then mark it as a batch
if event.IsIncomplete {
batch = true
}
continue
case batch:
dataSegments = append(dataSegments, event.Segments...)
// If event is complete then unmark it as a batch
if !event.IsIncomplete {
batch = false
}
continue
// Break the loop in case of success
case event.IsSuccessfulResponse():
break el
// Return error immediately
case event.IsResponseError():
return nil, AvayaError{Code: event.Segments[1]}
default:
return nil, fmt.Errorf("unexpected event")
}
case <-r.context.Done():
return nil, r.context.Err()
}
}
return dataSegments, nil
}
type Notification struct {
Type NotificationType
Payload interface{}
}
type NotificationType string
const (
NotificationTypeCallNotify NotificationType = "AGTCallNotify"
NotificationTypeAutoReleaseLine NotificationType = "AGTAutoReleaseLine"
NotificationTypeJobEnd NotificationType = "AGTJobEnd"
NotificationTypeReceiveMessage NotificationType = "AGTReceiveMessage"
NotificationTypeJobTransRequest NotificationType = "AGTJobTransRequest"
NotificationTypeHeadsetConnBroken NotificationType = "AGTHeadsetConnBroken"
NotificationTypeSystemError NotificationType = "AGTSystemError"
)
func processNotifications(r *request, notifications chan<- Notification) {
var (
state int
fields map[string]string
message string
jobName string
)
for {
select {
case event := <-r.eventChan:
switch {
case event.IsNotificationData():
switch NotificationType(event.Keyword) {
case NotificationTypeCallNotify:
switch state {
case 0:
state++
case 1:
fields = make(map[string]string)
for _, s := range event.Segments[2:] {
parts := strings.Split(s, ",")
if len(parts) != 2 {
continue
}
fields[parts[0]] = parts[1]
}
state++
}
case NotificationTypeReceiveMessage:
message = event.Segments[2]
case NotificationTypeJobTransRequest:
jobName = event.Segments[2]
}
case event.IsSuccessfulNotification():
n := Notification{Type: NotificationType(event.Keyword)}
switch n.Type {
case NotificationTypeCallNotify:
n.Payload = fields
state = 0
fields = nil
case NotificationTypeReceiveMessage:
n.Payload = message
message = ""
case NotificationTypeJobTransRequest:
n.Payload = jobName
jobName = ""
}
notifications <- n
case event.IsNotificationError():
notifications <- Notification{Type: NotificationType(event.Keyword), Payload: event.Segments[1]}
}
case <-r.context.Done():
return
}
}
}