-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchannel.go
278 lines (241 loc) · 7.42 KB
/
channel.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
package esl
import (
"errors"
"fmt"
"strings"
"sync"
"sync/atomic"
)
// channel and the protocol wrappers
// when the channel closed will callback this function
type closeFunc = func()
// inner channel
type channel struct {
connection // inner connection body
sync.Mutex // sync mutex when execute the command
reply chan *Event // reply channel
response chan *Event // response channel
Events chan *Event // events channel
signal chan struct{} // signal useful for outbound pattern
close closeFunc // inner connection close callback
err error
running atomic.Value
}
// loop for recv
func (c *channel) loop() {
c.running.Store(true)
for {
running, _ := c.running.Load().(bool)
if !running {
break
}
if c.err != nil {
debug("channel meet a fuck IO error")
break
}
if event, err := c.recv(); err != nil {
debugf("recv error %v", err)
break
} else {
if event.Type == EslEvent {
c.Events <- event
}
if event.Type == EslReply {
c.reply <- event
}
if event.Type == EslResponse {
c.response <- event
} else {
m, _ := event.IntoPlain()
errorf("unsupport content type %v, and the result is %s", event.Type, m)
break
}
}
}
c.shutdown()
}
// clear fucntion only used in the shutdown
func (c *channel) clear() {
if c.signal != nil {
close(c.signal)
}
if c.close != nil {
c.close()
}
}
// shutdown the channel
func (c *channel) shutdown() {
c.connection.Close()
c.clear()
}
// execute command sync without reply
func (channel *channel) noreplycmd(cmd string) (err error) {
_, err = channel.replycmd(cmd)
return
}
// execute command sync with reply
func (channel *channel) replycmd(cmd string) (reply *Event, err error) {
cmd = fmt.Sprintf("%s\n\n", cmd)
channel.Lock()
err = channel.send(cmd)
if err != nil {
//FIXME: in FS this situation will cause the socket close FUCK!
errorf("channel execute command failed %v", err)
channel.err = err
return
}
reply = <-channel.reply
channel.Unlock()
if strings.Contains(reply.Body, "-ERR") {
//FIXME: in FS this situation will cause the socket close FUCK!
debugf("channel send command failed %v", err)
return nil, errors.New(reply.Body)
}
return
}
// execute command sendmsg
// FIXME: https://freeswitch.org/confluence/display/FREESWITCH/mod_event_socket#mod_event_socket-3.9sendmsg
// FIX to the suitable pattern of this command
func (channel *channel) sendmsg(body, uuid string) (response *Event, err error) {
builder := strings.Builder{}
builder.WriteString("sendmsg")
if uuid != "" {
builder.WriteString(fmt.Sprintf("%s\n", uuid))
} else {
builder.WriteString("\n")
}
builder.WriteString(fmt.Sprintf("%s\n", body)) //FIXME: maybe the \n is useless
channel.Lock()
err = channel.send(builder.String())
if err != nil {
//FIXME: in FS this situation will cause the socket close FUCK!
errorf("channel execute command failed %v", err)
channel.err = err
return
}
response = <-channel.reply // TODO: check if really use the reply channel ?
channel.Unlock()
if strings.Contains(response.Body, "-ERR") {
//FIXME: in FS this situation will cause the socket close FUCK!
debugf("channel send command failed %v", err)
return nil, errors.New(response.Body)
}
return
}
// execute the sendmsg logic with application
func (channel *channel) execute(application, arg, uuid string) (response *Event, err error) {
builder := strings.Builder{}
builder.WriteString("call-command: execute\n")
builder.WriteString(fmt.Sprintf("execute-app-name: %s\n", application))
if arg != "" {
builder.WriteString(fmt.Sprintf("execute-app-arg: %s\n", arg))
}
//FIXME: arg need be urlencoded
builder.WriteString("event-lock: true\n") // the sendmsg call will fill the final \n
response, err = channel.sendmsg(builder.String(), uuid)
return
}
// execute unload a module mod_event_socket
func (channel *channel) unload() (err error) {
return channel.noreplycmd("api bgapi unload mod_event_socket")
}
// execute reload a module mod_event_socket
func (channel *channel) reload() (err error) {
return channel.noreplycmd("api bgapi reload mod_event_socket")
}
// execute the filter command
func (channel *channel) filter(action string, events ...string) (err error) {
// filter delete commands...
// filter add commands...
es := strings.Join(events, " ")
return channel.noreplycmd(fmt.Sprintf("filter %s %s", action, es))
}
// execute the resume command
// TODO: should know when should recover the session in the listener strtuct
// should review the FS source code to figure out this fuck thing
func (channel *channel) resume() (err error) {
// in FS could set this session as LFLAG_RESUME
// maybe useful for Inbound mode
return channel.noreplycmd("resume")
}
// TODO: except these methods other methods should check the auth logic
// execute auth command
func (channel *channel) auth(password string) (err error) {
return channel.noreplycmd(fmt.Sprintf("auth %s", password))
}
// execute userauth command
func (channel *channel) userauth(username, password string) (err error) {
return channel.noreplycmd(fmt.Sprintf("userauth %s %s", username, password))
}
// bellow methods should be authorizated or outbound method
// execute connect command
func (channel *channel) connect() (response *Event, err error) {
return channel.replycmd("connect")
}
// execute linger command
func (channel *channel) linger(second int) (err error) {
return channel.noreplycmd(fmt.Sprintf("linger %d", second))
}
// execute nolinger command
func (channel *channel) nolinger() (err error) {
return channel.noreplycmd("nolinger")
}
// execute the getvar command to get the variable of current channel
func (channel *channel) getvar(key string) (result string, err error) {
r, err := channel.replycmd(fmt.Sprintf("getvar %s", key))
if err != nil {
return
}
result = r.Body // this command result will be emplaced in the Body
return
}
// execute sendevent command
func (channel *channel) sendevent(event *Event) (err error) {
return
}
// execute api command sync
func (channel *channel) api() (err error) {
return
}
// execute bgapi command async
// FIXME: return response ? or Job-UUID ?
func (channel *channel) bgapi() (err error) {
return
}
// execute event command to subcribe the events specificed
func (channel *channel) event(format string, events ...string) (err error) {
es := strings.Join(events, " ")
return channel.noreplycmd(fmt.Sprintf("event %s %s", format, es))
}
// execute nixevent cancel events subcribe
func (channel *channel) nixevent(events ...string) (err error) {
es := strings.Join(events, " ")
return channel.noreplycmd(fmt.Sprintf("nixevent %s", es))
}
// execute noevents command
func (channel *channel) noevents() (err error) {
return channel.noreplycmd("noevents")
}
// execute divert_events command
func (channel *channel) divertEvents(open bool) (err error) {
if open {
return channel.noreplycmd("divert_events on")
}
return channel.noreplycmd("divert_events off")
}
// execute myevents command
func (channel *channel) myevents(uuid, format string) (err error) {
if format != "" {
channel.noreplycmd(fmt.Sprintf("myevents %s %s", uuid, format))
}
return channel.noreplycmd("myevents")
}
// execute the exit command
func (channel *channel) exit() {
channel.noreplycmd("exit")
// TODO: should change the channel state?
// which could make sync closed ?
// but in some concurrent situation this thing could make memory leak
// see more about the SC
channel.running.Store(false)
}