-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.go
612 lines (491 loc) · 14.6 KB
/
methods.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package apc
import (
"context"
"fmt"
"strconv"
"strings"
)
type arg struct {
key string
value string
}
func newArg(key, value string) arg {
return arg{
key: key,
value: value,
}
}
func newRequest(ctx context.Context) *request {
// Add cancellation context to parent one
ctx, cancel := context.WithCancel(ctx)
// Create dedicated event channel for this request
return &request{
context: ctx,
cancel: cancel,
// Usually one request needs two events: data and response
eventChan: make(chan Event, 2),
}
}
func (c *Client) invokeCommand(ctx context.Context, keyword string, args ...arg) (*request, uint32, error) {
invokeID := c.invokeIDPool.Get()
if c.state.Load() != ConnOK {
return nil, invokeID, ErrConnectionClosed
}
fields := map[string]interface{}{
"type": string(EventTypeCommand),
"keyword": keyword,
"invoke_id": invokeID,
}
var flatArgs []string
if len(args) > 0 {
flatArgs = make([]string, 0, len(args))
for _, arg := range args {
flatArgs = append(flatArgs, arg.value)
fields[arg.key] = arg.value
}
}
fields["segments"] = flatArgs
// Encode command
b, err := encodeCommand(keyword, invokeID, flatArgs...)
if err != nil {
return nil, invokeID, fmt.Errorf("cannot encode command: %w", err)
}
c.logger.log(newLogEntry(LogLevelDebug, "Command has encoded.", map[string]interface{}{"raw": string(b)}))
// Create the request and place it into the requests map;
// it should be done BEFORE writing a command into connection to avoid the situation while server responds
// so quickly that events being just skipped before processing goroutine even started
r := newRequest(ctx)
c.mu.Lock()
c.requests[invokeID] = r
c.mu.Unlock()
// Write command to connection
if _, err := c.conn.Write(b); err != nil {
return nil, invokeID, fmt.Errorf("cannot write command: %w", err)
}
c.logger.log(newLogEntry(LogLevelInfo, "Command has sent.", fields))
return r, invokeID, nil
}
func (c *Client) destroyCommand(invokeID uint32) {
c.mu.RLock()
_, ok := c.requests[invokeID]
c.mu.RUnlock()
// in case of executeCommand func returned an error just release invoke id from pool
if !ok {
c.invokeIDPool.Release(invokeID)
return
}
// Delete request from pool
c.mu.Lock()
delete(c.requests, invokeID)
c.mu.Unlock()
// Finally release invoke ID
c.invokeIDPool.Release(invokeID)
}
func (c *Client) Logon(ctx context.Context, agentName string, password string) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTLogon", newArg("agent_name", agentName), newArg("password", password), newArg("version", "GOLANG_0.0.3"))
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTLogon command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) ReserveHeadset(ctx context.Context, headsetID int) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTReserveHeadset", newArg("headset_id", strconv.Itoa(headsetID)))
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTReserveHeadset command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) ConnectHeadset(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTConnHeadset")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTConnHeadset command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
type JobType byte
const (
JobTypeAll JobType = 'A'
JobTypeBlend JobType = 'B'
JobTypeOutbound JobType = 'O'
JobTypeInbound JobType = 'I'
JobTypeManaged JobType = 'M'
)
type Job struct {
Type JobType
Name string
Status StatusType
}
type StatusType byte
const (
StatusTypeInactive StatusType = 'I'
StatusTypeActive StatusType = 'A'
)
func (c *Client) ListJobs(ctx context.Context, jobType JobType) ([]Job, error) {
r, invokeID, err := c.invokeCommand(ctx, "AGTListJobs", newArg("job_type", string([]byte{byte(jobType)})))
defer c.destroyCommand(invokeID)
if err != nil {
return nil, fmt.Errorf("error while executing AGTListJobs command: %w", err)
}
rawSegments, err := processRequest(r)
if err != nil {
return nil, err
}
jobs := make([]Job, 0, len(rawSegments))
for _, segment := range rawSegments {
jobParts := strings.Split(segment, ",")
if len(jobParts) == 3 {
jobs = append(jobs, Job{
Type: JobType(jobParts[0][0]),
Name: jobParts[1],
Status: StatusType(jobParts[2][0]),
})
}
}
return jobs, nil
}
func (c *Client) ListCallLists(ctx context.Context) ([]string, error) {
r, invokeID, err := c.invokeCommand(ctx, "AGTListCallLists")
defer c.destroyCommand(invokeID)
if err != nil {
return nil, fmt.Errorf("error while executing AGTListCallLists command: %w", err)
}
rawSegments, err := processRequest(r)
if err != nil {
return nil, err
}
callLists := make([]string, 0, len(rawSegments))
for _, segment := range rawSegments {
callLists = append(callLists, segment)
}
return callLists, nil
}
func (c *Client) ListCallFields(ctx context.Context, listName string) ([]string, error) {
r, invokeID, err := c.invokeCommand(ctx, "AGTListCallFields", newArg("list_name", listName))
defer c.destroyCommand(invokeID)
if err != nil {
return nil, fmt.Errorf("error while executing AGTListCallFields command: %w", err)
}
rawSegments, err := processRequest(r)
if err != nil {
return nil, err
}
callFields := make([]string, 0, len(rawSegments))
for _, segment := range rawSegments {
callFields = append(callFields, segment)
}
return callFields, nil
}
func (c *Client) AttachJob(ctx context.Context, jobName string) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTAttachJob", newArg("job_name", jobName))
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTAttachJob command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
type ListType byte
const (
ListTypeOutbound ListType = 'O'
ListTypeInbound ListType = 'I'
)
type DataField struct {
Name string
}
func (c *Client) ListDataFields(ctx context.Context, listType ListType) ([]DataField, error) {
r, invokeID, err := c.invokeCommand(ctx, "AGTListDataFields", newArg("list_type", string([]byte{byte(listType)})))
defer c.destroyCommand(invokeID)
if err != nil {
return nil, fmt.Errorf("error while executing AGTListDataFields command: %w", err)
}
rawSegments, err := processRequest(r)
if err != nil {
return nil, err
}
dataFields := make([]DataField, 0, len(rawSegments))
for _, segment := range rawSegments {
dataFieldParts := strings.Split(segment, ",")
if len(dataFieldParts) == 4 {
dataFields = append(dataFields, DataField{
Name: dataFieldParts[0],
})
}
}
return dataFields, nil
}
func (c *Client) SetNotifyKeyField(ctx context.Context, listType ListType, fieldName string) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTSetNotifyKeyField", newArg("list_type", string([]byte{byte(listType)})), newArg("field_name", fieldName))
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTSetNotifyKeyField command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) SetDataField(ctx context.Context, listType ListType, fieldName string) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTSetDataField", newArg("list_type", string([]byte{byte(listType)})), newArg("field_name", fieldName))
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTSetDataField command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) AvailWork(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTAvailWork")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTAvailWork command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) ReadyNextItem(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTReadyNextItem")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTReadyNextItem command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) ListKeys(ctx context.Context) ([]string, error) {
r, invokeID, err := c.invokeCommand(ctx, "AGTListKeys")
defer c.destroyCommand(invokeID)
if err != nil {
return nil, fmt.Errorf("error while executing AGTListKeys command: %w", err)
}
rawSegments, err := processRequest(r)
if err != nil {
return nil, err
}
keys := make([]string, 0, len(rawSegments))
for _, segment := range rawSegments {
keys = append(keys, segment)
}
return keys, nil
}
func (c *Client) ReleaseLine(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTReleaseLine")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTReleaseLine command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) FinishedItem(ctx context.Context, compCode int) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTFinishedItem", newArg("comp_code", strconv.Itoa(compCode)))
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTFinishedItem command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) NoFurtherWork(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTNoFurtherWork")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTNoFurtherWork command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) DetachJob(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTDetachJob")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTDetachJob command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) DisconnectHeadset(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTDisconnHeadset")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTDisconnHeadset command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) FreeHeadset(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTFreeHeadset")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTFreeHeadset command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
// Logoff sends ATGLogoff command, then Proactive Control server terminates session
func (c *Client) Logoff(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTLogoff")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTLogoff command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) EchoOn(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTEchoOn")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTEchoOn command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) EchoOff(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTEchoOff")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTEchoOff command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) LogIoStart(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTLogIoStart")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTLogIoStart command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
func (c *Client) LogIoStop(ctx context.Context) error {
r, invokeID, err := c.invokeCommand(ctx, "AGTLogIoStop")
defer c.destroyCommand(invokeID)
if err != nil {
return fmt.Errorf("error while executing AGTLogIoStop command: %w", err)
}
if _, err := processRequest(r); err != nil {
return err
}
return nil
}
type State struct {
Type StateType
JobName string
}
type StateType string
const (
StateTypeOnCall StateType = "S70000"
StateTypeReadyForCall StateType = "S70001"
StateTypeHasJoinedJob StateType = "S70002"
StateTypeHasSelectedJob StateType = "S70003"
StateTypeLoggedOn StateType = "S70004"
)
func (c *Client) ListState(ctx context.Context) (*State, error) {
r, invokeID, err := c.invokeCommand(ctx, "AGTListState")
defer c.destroyCommand(invokeID)
if err != nil {
return nil, fmt.Errorf("error while executing AGTListState command: %w", err)
}
rawSegments, err := processRequest(r)
if err != nil {
return nil, err
}
if rawSegments == nil || len(rawSegments) != 1 {
return nil, fmt.Errorf("invalid segment")
}
parts := strings.Split(rawSegments[0], ",")
if len(parts) > 2 {
return nil, fmt.Errorf("invalid segment")
}
var jobName string
if len(parts) == 2 {
jobName = parts[1]
}
return &State{
Type: StateType(parts[0]),
JobName: jobName,
}, nil
}
type Field struct {
Name string
Type FieldType
Length int
Value string
}
type FieldType string
const (
FieldTypeAlphanumeric FieldType = "A"
FieldTypeNumeric FieldType = "N"
FieldTypeDate FieldType = "D"
FieldTypeCurrency FieldType = "$"
FieldTypeFutureUse FieldType = "F"
)
func (c *Client) ReadField(ctx context.Context, listType ListType, fieldName string) (*Field, error) {
r, invokeID, err := c.invokeCommand(ctx, "AGTReadField", newArg("list_type", string([]byte{byte(listType)})), newArg("field_name", fieldName))
defer c.destroyCommand(invokeID)
if err != nil {
return nil, fmt.Errorf("error while executing AGTSetDataField command: %w", err)
}
rawSegments, err := processRequest(r)
if err != nil {
return nil, err
}
if rawSegments == nil || len(rawSegments) != 2 || rawSegments[0] != "M00001" {
return nil, fmt.Errorf("invalid segment")
}
parts := strings.Split(rawSegments[1], ",")
if len(parts) != 4 {
return nil, fmt.Errorf("invalid segment")
}
length, err := strconv.Atoi(parts[2])
if err != nil {
return nil, fmt.Errorf("cannot convert field length: %w", err)
}
return &Field{
Name: parts[0],
Type: FieldType(parts[1]),
Length: length,
Value: parts[3],
}, nil
}