forked from cgrates/fsock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsock.go
564 lines (512 loc) · 15.8 KB
/
fsock.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
/*
fsock.go is released under the MIT License <http://www.opensource.org/licenses/mit-license.php
Copyright (C) ITsysCOM. All Rights Reserved.
Provides FreeSWITCH socket communication.
*/
package fsock
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net"
"reflect"
"strconv"
"strings"
"sync"
"time"
)
var (
ErrConnectionPoolTimeout = errors.New("ConnectionPool timeout")
)
// NewFSock connects to FS and starts buffering input
func NewFSock(fsaddr, fspaswd string, reconnects int, maxReconnectInterval time.Duration,
delayFunc func(time.Duration, time.Duration) func() time.Duration,
eventHandlers map[string][]func(string, int), eventFilters map[string][]string,
l logger, connIdx int, bgapiSup bool) (fsock *FSock, err error) {
if l == nil || (reflect.ValueOf(l).Kind() == reflect.Ptr && reflect.ValueOf(l).IsNil()) {
l = nopLogger{}
}
fsock = &FSock{
fsMutex: new(sync.RWMutex),
connIdx: connIdx,
fsaddress: fsaddr,
fspaswd: fspaswd,
eventHandlers: eventHandlers,
eventFilters: eventFilters,
backgroundChans: make(map[string]chan string),
cmdChan: make(chan string),
reconnects: reconnects,
maxReconnectInterval: maxReconnectInterval,
delayFunc: delayFunc,
logger: l,
bgapiSup: bgapiSup,
}
if err = fsock.Connect(); err != nil {
return nil, err
}
return
}
// FSock reperesents the connection to FreeSWITCH Socket
type FSock struct {
conn net.Conn
fsMutex *sync.RWMutex
connIdx int // Indetifier for the component using this instance of FSock, optional
buffer *bufio.Reader
fsaddress string
fspaswd string
eventHandlers map[string][]func(string, int) // eventStr, connId
eventFilters map[string][]string
backgroundChans map[string]chan string
cmdChan chan string
reconnects int
maxReconnectInterval time.Duration
delayFunc func(time.Duration, time.Duration) func() time.Duration // used to create/reset the delay function
stopReadEvents chan struct{} // Keep a reference towards forkedReadEvents so we can stop them whenever necessary
errReadEvents chan error
logger logger
bgapiSup bool
}
// Connect or reconnect
func (fs *FSock) Connect() error {
if fs.stopReadEvents != nil {
close(fs.stopReadEvents) // we have read events already processing, request stop
}
// Reinit readEvents channels so we avoid concurrency issues between goroutines
fs.stopReadEvents = make(chan struct{})
fs.errReadEvents = make(chan error)
return fs.connect()
}
func (fs *FSock) connect() (err error) {
if fs.Connected() {
fs.Disconnect()
}
var conn net.Conn
if conn, err = net.Dial("tcp", fs.fsaddress); err != nil {
fs.logger.Err(fmt.Sprintf("<FSock> Attempt to connect to FreeSWITCH, received: %s", err.Error()))
return
}
fs.fsMutex.Lock()
fs.conn = conn
fs.fsMutex.Unlock()
fs.logger.Info("<FSock> Successfully connected to FreeSWITCH!")
// Connected, init buffer, auth and subscribe to desired events and filters
fs.fsMutex.RLock()
fs.buffer = bufio.NewReaderSize(fs.conn, 8192) // reinit buffer
fs.fsMutex.RUnlock()
var authChg string
if authChg, err = fs.readHeaders(); err != nil {
return fmt.Errorf("Received error<%s> when receiving the auth challenge", err)
}
if !strings.Contains(authChg, "auth/request") {
return errors.New("No auth challenge received")
}
if err = fs.auth(); err != nil { // Auth did not succeed
return
}
if err = fs.filterEvents(fs.eventFilters, fs.bgapiSup); err != nil {
return
}
// Subscribe to events handled by event handlers
if err = fs.eventsPlain(getMapKeys(fs.eventHandlers), fs.bgapiSup); err != nil {
return
}
go fs.readEvents() // Fork read events in it's own goroutine
return
}
// Connected checks if socket connected. Can be extended with pings
func (fs *FSock) Connected() (ok bool) {
fs.fsMutex.RLock()
ok = (fs.conn != nil)
fs.fsMutex.RUnlock()
return
}
// Disconnect disconnects from socket
func (fs *FSock) Disconnect() (err error) {
fs.fsMutex.Lock()
if fs.conn != nil {
fs.logger.Info("<FSock> Disconnecting from FreeSWITCH!")
err = fs.conn.Close()
fs.conn = nil
}
fs.fsMutex.Unlock()
return
}
// ReconnectIfNeeded if not connected, attempt reconnect if allowed
func (fs *FSock) ReconnectIfNeeded() (err error) {
if fs.Connected() { // No need to reconnect
return
}
delay := fs.delayFunc(time.Second, fs.maxReconnectInterval)
for i := 0; fs.reconnects == -1 || i < fs.reconnects; i++ { // Maximum reconnects reached, -1 for infinite reconnects
if err = fs.connect(); err == nil && fs.Connected() {
break // No error or unrelated to connection
}
time.Sleep(delay())
}
if err == nil && !fs.Connected() {
return errors.New("Not connected to FreeSWITCH")
}
return // nil or last error in the loop
}
func (fs *FSock) send(cmd string) (err error) {
fs.fsMutex.RLock()
defer fs.fsMutex.RUnlock()
if _, err = fs.conn.Write([]byte(cmd)); err != nil {
fs.logger.Err(fmt.Sprintf("<FSock> Cannot write command to socket <%s>", err.Error()))
}
return
}
// Auth to FS
func (fs *FSock) auth() (err error) {
if err = fs.send("auth " + fs.fspaswd + "\n\n"); err != nil {
return
}
var rply string
if rply, err = fs.readHeaders(); err != nil {
return
}
if !strings.Contains(rply, "Reply-Text: +OK accepted") {
return fmt.Errorf("Unexpected auth reply received: <%s>", rply)
}
return
}
func (fs *FSock) sendCmd(cmd string) (rply string, err error) {
if err = fs.ReconnectIfNeeded(); err != nil {
return
}
if err = fs.send(cmd + "\n"); err != nil {
return
}
rply = <-fs.cmdChan
if strings.Contains(rply, "-ERR") {
return "", errors.New(strings.TrimSpace(rply))
}
return
}
// Generic proxy for commands
func (fs *FSock) SendCmd(cmdStr string) (string, error) {
return fs.sendCmd(cmdStr + "\n")
}
func (fs *FSock) SendCmdWithArgs(cmd string, args map[string]string, body string) (string, error) {
for k, v := range args {
cmd += k + ": " + v + "\n"
}
if len(body) != 0 {
cmd += "\n" + body + "\n"
}
return fs.sendCmd(cmd)
}
// Send API command
func (fs *FSock) SendApiCmd(cmdStr string) (string, error) {
return fs.sendCmd("api " + cmdStr + "\n")
}
// Send BGAPI command
func (fs *FSock) SendBgapiCmd(cmdStr string) (out chan string, err error) {
jobUUID := genUUID()
out = make(chan string)
fs.fsMutex.Lock()
fs.backgroundChans[jobUUID] = out
fs.fsMutex.Unlock()
_, err = fs.sendCmd("bgapi " + cmdStr + "\nJob-UUID:" + jobUUID + "\n")
if err != nil {
return nil, err
}
return
}
// SendMsgCmdWithBody command
func (fs *FSock) SendMsgCmdWithBody(uuid string, cmdargs map[string]string, body string) (err error) {
if len(cmdargs) == 0 {
return errors.New("Need command arguments")
}
_, err = fs.SendCmdWithArgs("sendmsg "+uuid+"\n", cmdargs, body)
return
}
// SendMsgCmd command
func (fs *FSock) SendMsgCmd(uuid string, cmdargs map[string]string) error {
return fs.SendMsgCmdWithBody(uuid, cmdargs, "")
}
// SendEventWithBody command
func (fs *FSock) SendEventWithBody(eventSubclass string, eventParams map[string]string, body string) (string, error) {
// Event-Name is overrided to CUSTOM by FreeSWITCH,
// so we use Event-Subclass instead
eventParams["Event-Subclass"] = eventSubclass
return fs.SendCmdWithArgs("sendevent "+eventSubclass+"\n", eventParams, body)
}
// SendEvent command
func (fs *FSock) SendEvent(eventSubclass string, eventParams map[string]string) (string, error) {
return fs.SendEventWithBody(eventSubclass, eventParams, "")
}
// ReadEvents reads events from socket, attempt reconnect if disconnected
func (fs *FSock) ReadEvents() (err error) {
for {
if err = <-fs.errReadEvents; err == io.EOF { // Disconnected, try reconnect
if err = fs.ReconnectIfNeeded(); err != nil {
return
}
}
}
}
func (fs *FSock) LocalAddr() net.Addr {
if !fs.Connected() {
return nil
}
return fs.conn.LocalAddr()
}
// Reads headers until delimiter reached
func (fs *FSock) readHeaders() (header string, err error) {
bytesRead := make([]byte, 0)
var readLine []byte
for {
readLine, err = fs.buffer.ReadBytes('\n')
if err != nil {
fs.logger.Err(fmt.Sprintf("<FSock> Error reading headers: <%s>", err.Error()))
fs.Disconnect()
return
}
// No Error, add received to localread buffer
if len(bytes.TrimSpace(readLine)) == 0 {
break
}
bytesRead = append(bytesRead, readLine...)
}
return string(bytesRead), nil
}
// Reads the body from buffer, ln is given by content-length of headers
func (fs *FSock) readBody(noBytes int) (body string, err error) {
bytesRead := make([]byte, noBytes)
var readByte byte
for i := 0; i < noBytes; i++ {
if readByte, err = fs.buffer.ReadByte(); err != nil {
fs.logger.Err(fmt.Sprintf("<FSock> Error reading message body: <%s>", err.Error()))
fs.Disconnect()
return
}
// No Error, add received to local read buffer
bytesRead[i] = readByte
}
return string(bytesRead), nil
}
// Event is made out of headers and body (if present)
func (fs *FSock) readEvent() (header string, body string, err error) {
if header, err = fs.readHeaders(); err != nil {
return
}
if !strings.Contains(header, "Content-Length") { //No body
return
}
var cl int
if cl, err = strconv.Atoi(headerVal(header, "Content-Length")); err != nil {
err = fmt.Errorf("Cannot extract content length because<%s>", err)
return
}
body, err = fs.readBody(cl)
return
}
// Read events from network buffer, stop when exitChan is closed, report on errReadEvents on error and exit
// Receive exitChan and errReadEvents as parameters so we avoid concurrency on using fs.
func (fs *FSock) readEvents() {
for {
select {
case <-fs.stopReadEvents:
return
default: // Unlock waiting here
}
hdr, body, err := fs.readEvent()
if err != nil {
fs.errReadEvents <- err
return
}
if strings.Contains(hdr, "api/response") {
fs.cmdChan <- body
} else if strings.Contains(hdr, "command/reply") {
fs.cmdChan <- headerVal(hdr, "Reply-Text")
} else if body != "" { // We got a body, could be event, try dispatching it
fs.dispatchEvent(body)
}
}
}
// Subscribe to events
func (fs *FSock) eventsPlain(events []string, bgapiSup bool) (err error) {
eventsCmd := "event plain"
customEvents := ""
for _, ev := range events {
if ev == "ALL" {
eventsCmd = "event plain all"
break
}
if strings.HasPrefix(ev, "CUSTOM") {
customEvents += ev[6:] // will capture here also space between CUSTOM and event
continue
}
eventsCmd += " " + ev
}
if eventsCmd != "event plain all" {
if bgapiSup {
eventsCmd += " BACKGROUND_JOB" // For bgapi
}
if len(customEvents) != 0 { // Add CUSTOM events subscribing in the end otherwise unexpected events are received
eventsCmd += " " + "CUSTOM" + customEvents
}
}
if err = fs.send(eventsCmd + "\n\n"); err != nil {
fs.Disconnect()
return
}
var rply string
if rply, err = fs.readHeaders(); err != nil {
return
}
if !strings.Contains(rply, "Reply-Text: +OK") {
fs.Disconnect()
return fmt.Errorf("Unexpected events-subscribe reply received: <%s>", rply)
}
return
}
// Enable filters
func (fs *FSock) filterEvents(filters map[string][]string, bgapiSup bool) (err error) {
if len(filters) == 0 {
return nil
}
if bgapiSup {
filters["Event-Name"] = append(filters["Event-Name"], "BACKGROUND_JOB") // for bgapi
}
for hdr, vals := range filters {
for _, val := range vals {
if err = fs.send("filter " + hdr + " " + val + "\n\n"); err != nil {
fs.Disconnect()
return
}
var rply string
if rply, err = fs.readHeaders(); err != nil {
return
}
if !strings.Contains(rply, "Reply-Text: +OK") {
fs.Disconnect()
return fmt.Errorf("Unexpected filter-events reply received: <%s>", rply)
}
}
}
return nil
}
// Dispatch events to handlers in async mode
func (fs *FSock) dispatchEvent(event string) {
eventName := headerVal(event, "Event-Name")
if eventName == "BACKGROUND_JOB" { // for bgapi BACKGROUND_JOB
go fs.doBackgroundJob(event)
return
}
if eventName == "CUSTOM" {
eventSubclass := headerVal(event, "Event-Subclass")
if len(eventSubclass) != 0 {
eventName += " " + urlDecode(eventSubclass)
}
}
for _, handleName := range []string{eventName, "ALL"} {
if _, hasHandlers := fs.eventHandlers[handleName]; hasHandlers {
// We have handlers, dispatch to all of them
for _, handlerFunc := range fs.eventHandlers[handleName] {
go handlerFunc(event, fs.connIdx)
}
return
}
}
fs.logger.Warning(fmt.Sprintf("<FSock> No dispatcher for event: <%+v> with event name: %s", event, eventName))
}
// bgapi event lisen fuction
func (fs *FSock) doBackgroundJob(event string) { // add mutex protection
evMap := EventToMap(event)
jobUUID, has := evMap["Job-UUID"]
if !has {
fs.logger.Err("<FSock> BACKGROUND_JOB with no Job-UUID")
return
}
var out chan string
fs.fsMutex.RLock()
out, has = fs.backgroundChans[jobUUID]
fs.fsMutex.RUnlock()
if !has {
fs.logger.Err(fmt.Sprintf("<FSock> BACKGROUND_JOB with UUID %s lost!", jobUUID))
return // not a requested bgapi
}
fs.fsMutex.Lock()
delete(fs.backgroundChans, jobUUID)
fs.fsMutex.Unlock()
out <- evMap[EventBodyTag]
}
// Instantiates a new FSockPool
func NewFSockPool(maxFSocks int, fsaddr, fspasswd string, reconnects int, maxWaitConn time.Duration,
maxReconnectInterval time.Duration, delayFuncConstructor func(time.Duration, time.Duration) func() time.Duration,
eventHandlers map[string][]func(string, int), eventFilters map[string][]string,
l logger, connIdx int, bgapiSup bool) *FSockPool {
if l == nil {
l = nopLogger{}
}
pool := &FSockPool{
connIdx: connIdx,
fsAddr: fsaddr,
fsPasswd: fspasswd,
reconnects: reconnects,
maxReconnectInterval: maxReconnectInterval,
delayFuncConstructor: delayFuncConstructor,
maxWaitConn: maxWaitConn,
eventHandlers: eventHandlers,
eventFilters: eventFilters,
logger: l,
allowedConns: make(chan struct{}, maxFSocks),
fSocks: make(chan *FSock, maxFSocks),
bgapiSup: bgapiSup,
}
for i := 0; i < maxFSocks; i++ {
pool.allowedConns <- struct{}{} // Empty initiate so we do not need to wait later when we pop
}
return pool
}
// Connection handler for commands sent to FreeSWITCH
type FSockPool struct {
connIdx int
fsAddr string
fsPasswd string
reconnects int
maxReconnectInterval time.Duration
delayFuncConstructor func(time.Duration, time.Duration) func() time.Duration
eventHandlers map[string][]func(string, int)
eventFilters map[string][]string
logger logger
allowedConns chan struct{} // Will be populated with members allowed
fSocks chan *FSock // Keep here reference towards the list of opened sockets
maxWaitConn time.Duration // Maximum duration to wait for a connection to be returned by Pop
bgapiSup bool
}
func (fs *FSockPool) PopFSock() (fsock *FSock, err error) {
if fs == nil {
return nil, errors.New("Unconfigured ConnectionPool")
}
if len(fs.fSocks) != 0 { // Select directly if available, so we avoid randomness of selection
fsock = <-fs.fSocks
return
}
tm := time.NewTimer(fs.maxWaitConn)
select { // No fsock available in the pool, wait for first one showing up
case fsock = <-fs.fSocks:
tm.Stop()
return
case <-fs.allowedConns:
tm.Stop()
return NewFSock(fs.fsAddr, fs.fsPasswd, fs.reconnects, fs.maxReconnectInterval, fs.delayFuncConstructor,
fs.eventHandlers, fs.eventFilters, fs.logger, fs.connIdx, fs.bgapiSup)
case <-tm.C:
return nil, ErrConnectionPoolTimeout
}
}
func (fs *FSockPool) PushFSock(fsk *FSock) {
if fs == nil { // Did not initialize the pool
return
}
if fsk == nil || !fsk.Connected() {
fs.allowedConns <- struct{}{}
return
}
fs.fSocks <- fsk
}