-
Notifications
You must be signed in to change notification settings - Fork 43
/
connection.go
331 lines (297 loc) · 9.51 KB
/
connection.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
/*
* Copyright (c) 2020 Percipia
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Contributor(s):
* Andrew Querol <[email protected]>
*/
package eslgo
import (
"bufio"
"context"
"errors"
"fmt"
"net"
"net/textproto"
"sync"
"time"
"github.com/percipia/eslgo/command"
)
type Conn struct {
conn net.Conn
reader *bufio.Reader
header *textproto.Reader
writeLock sync.Mutex
runningContext context.Context
stopFunc func()
responseChannels map[string]chan *RawResponse
responseChanMutex sync.RWMutex
eventListenerLock sync.RWMutex
eventListeners map[string]map[string]EventListener
eventListenerCounter int
outbound bool
logger Logger
exitTimeout time.Duration
closeOnce sync.Once
closeDelay time.Duration
}
// Options - Generic options for an ESL connection, either inbound or outbound
type Options struct {
Context context.Context // This specifies the base running context for the connection. If this context expires all connections will be terminated.
Logger Logger // This specifies the logger to be used for any library internal messages. Can be set to nil to suppress everything.
ExitTimeout time.Duration // How long should we wait for FreeSWITCH to respond to our "exit" command. 5 seconds is a sane default.
}
// DefaultOptions - The default options used for creating the connection
var DefaultOptions = Options{
Context: context.Background(),
Logger: NormalLogger{},
ExitTimeout: 5 * time.Second,
}
const EndOfMessage = "\r\n\r\n"
func newConnection(c net.Conn, outbound bool, opts Options) *Conn {
reader := bufio.NewReader(c)
header := textproto.NewReader(reader)
// If logger is nil, do not actually output anything
if opts.Logger == nil {
opts.Logger = NilLogger{}
}
runningContext, stop := context.WithCancel(opts.Context)
instance := &Conn{
conn: c,
reader: reader,
header: header,
responseChannels: map[string]chan *RawResponse{
TypeReply: make(chan *RawResponse),
TypeAPIResponse: make(chan *RawResponse),
TypeEventPlain: make(chan *RawResponse),
TypeEventXML: make(chan *RawResponse),
TypeEventJSON: make(chan *RawResponse),
TypeAuthRequest: make(chan *RawResponse, 1), // Buffered to ensure we do not lose the initial auth request before we are setup to respond
TypeDisconnect: make(chan *RawResponse),
},
runningContext: runningContext,
stopFunc: stop,
eventListeners: make(map[string]map[string]EventListener),
outbound: outbound,
logger: opts.Logger,
exitTimeout: opts.ExitTimeout,
}
go instance.receiveLoop()
go instance.eventLoop()
return instance
}
// RegisterEventListener - Registers a new event listener for the specified channel UUID(or EventListenAll). Returns the registered listener ID used to remove it.
func (c *Conn) RegisterEventListener(channelUUID string, listener EventListener) string {
c.eventListenerLock.Lock()
defer c.eventListenerLock.Unlock()
c.eventListenerCounter++
id := fmt.Sprintf("%d", c.eventListenerCounter)
if _, ok := c.eventListeners[channelUUID]; ok {
c.eventListeners[channelUUID][id] = listener
} else {
c.eventListeners[channelUUID] = map[string]EventListener{id: listener}
}
return id
}
// RemoveEventListener - Removes the listener for the specified channel UUID with the listener ID returned from RegisterEventListener
func (c *Conn) RemoveEventListener(channelUUID string, id string) {
c.eventListenerLock.Lock()
defer c.eventListenerLock.Unlock()
if listeners, ok := c.eventListeners[channelUUID]; ok {
delete(listeners, id)
}
}
// SendCommand - Sends the specified ESL command to FreeSWITCH with the provided context. Returns the response data and any errors encountered.
func (c *Conn) SendCommand(ctx context.Context, cmd command.Command) (*RawResponse, error) {
if linger, ok := cmd.(command.Linger); ok {
c.writeLock.Lock()
if linger.Enabled {
if linger.Seconds > 0 {
c.closeDelay = linger.Seconds
} else {
c.closeDelay = -1
}
} else {
c.closeDelay = 0
}
c.writeLock.Unlock()
}
deadline, ok := ctx.Deadline()
c.writeLock.Lock()
if ok {
_ = c.conn.SetWriteDeadline(deadline)
}
_, err := c.conn.Write([]byte(cmd.BuildMessage() + EndOfMessage))
if err != nil {
c.writeLock.Unlock()
return nil, err
}
if ok {
_ = c.conn.SetWriteDeadline(time.Time{})
}
c.writeLock.Unlock()
// Get response
c.responseChanMutex.RLock()
defer c.responseChanMutex.RUnlock()
select {
case response := <-c.responseChannels[TypeReply]:
if response == nil {
// We only get nil here if the channel is closed
return nil, errors.New("connection closed")
}
return response, nil
case response := <-c.responseChannels[TypeAPIResponse]:
if response == nil {
// We only get nil here if the channel is closed
return nil, errors.New("connection closed")
}
return response, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
// ExitAndClose - Attempt to gracefully send FreeSWITCH "exit" over the ESL connection before closing our connection and stopping. Protected by a sync.Once
func (c *Conn) ExitAndClose() {
c.closeOnce.Do(func() {
// Attempt a graceful closing of the connection with FreeSWITCH
ctx, cancel := context.WithTimeout(c.runningContext, c.exitTimeout)
_, _ = c.SendCommand(ctx, command.Exit{})
cancel()
c.close()
})
}
// Close - Close our connection to FreeSWITCH without sending "exit". Protected by a sync.Once
func (c *Conn) Close() {
c.closeOnce.Do(c.close)
}
func (c *Conn) close() {
// Allow users to do anything they need to do before we tear everything down
c.stopFunc()
c.responseChanMutex.Lock()
defer c.responseChanMutex.Unlock()
for key, responseChan := range c.responseChannels {
close(responseChan)
delete(c.responseChannels, key)
}
// Close the connection only after we have the response channel lock and we have deleted all response channels to ensure we don't receive on a closed channel
_ = c.conn.Close()
}
func (c *Conn) callEventListener(event *Event) {
c.eventListenerLock.RLock()
defer c.eventListenerLock.RUnlock()
// First check if there are any general event listener
if listeners, ok := c.eventListeners[EventListenAll]; ok {
for _, listener := range listeners {
go listener(event)
}
}
// Next call any listeners for a particular channel
if event.HasHeader("Unique-Id") {
channelUUID := event.GetHeader("Unique-Id")
if listeners, ok := c.eventListeners[channelUUID]; ok {
for _, listener := range listeners {
go listener(event)
}
}
}
// Next call any listeners for a particular application
if event.HasHeader("Application-UUID") {
appUUID := event.GetHeader("Application-UUID")
if listeners, ok := c.eventListeners[appUUID]; ok {
for _, listener := range listeners {
go listener(event)
}
}
}
// Next call any listeners for a particular job
if event.HasHeader("Job-UUID") {
jobUUID := event.GetHeader("Job-UUID")
if listeners, ok := c.eventListeners[jobUUID]; ok {
for _, listener := range listeners {
go listener(event)
}
}
}
}
func (c *Conn) eventLoop() {
for {
var event *Event
var err error
c.responseChanMutex.RLock()
select {
case raw := <-c.responseChannels[TypeEventPlain]:
if raw == nil {
// We only get nil here if the channel is closed
c.responseChanMutex.RUnlock()
return
}
event, err = readPlainEvent(raw.Body)
case raw := <-c.responseChannels[TypeEventXML]:
if raw == nil {
// We only get nil here if the channel is closed
c.responseChanMutex.RUnlock()
return
}
event, err = readXMLEvent(raw.Body)
case raw := <-c.responseChannels[TypeEventJSON]:
if raw == nil {
// We only get nil here if the channel is closed
c.responseChanMutex.RUnlock()
return
}
event, err = readJSONEvent(raw.Body)
case <-c.runningContext.Done():
c.responseChanMutex.RUnlock()
return
}
c.responseChanMutex.RUnlock()
if err != nil {
c.logger.Warn("Error parsing event\n%s\n", err.Error())
continue
}
c.callEventListener(event)
}
}
func (c *Conn) receiveLoop() {
for c.runningContext.Err() == nil {
err := c.doMessage()
if err != nil {
c.logger.Warn("Error receiving message: %s\n", err.Error())
break
}
}
}
func (c *Conn) doMessage() error {
response, err := c.readResponse()
if err != nil {
return err
}
c.responseChanMutex.RLock()
defer c.responseChanMutex.RUnlock()
responseChan, ok := c.responseChannels[response.GetHeader("Content-Type")]
if !ok && len(c.responseChannels) <= 0 {
// We must have shutdown!
return errors.New("no response channels")
}
// We have a handler
if ok {
// Only allow 5 seconds to allow the handler to receive hte message on the channel
ctx, cancel := context.WithTimeout(c.runningContext, 5*time.Second)
defer cancel()
select {
case responseChan <- response:
case <-c.runningContext.Done():
// Parent connection context has stopped we most likely shutdown in the middle of waiting for a handler to handle the message
return c.runningContext.Err()
case <-ctx.Done():
// Do not return an error since this is not fatal but log since it could be a indication of problems
c.logger.Warn("No one to handle response\nIs the connection overloaded or stopping?\n%v\n\n", response)
}
} else {
return errors.New("no response channel for Content-Type: " + response.GetHeader("Content-Type"))
}
return nil
}