-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgami.go
executable file
·295 lines (253 loc) · 7.03 KB
/
gami.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
package gami
import (
"bytes"
"fmt"
"log"
"net"
"os"
"sort"
"sync"
"strings"
)
const (
_LINE_TERM = "\r\n" // packet line separator
_KEY_VAL_TERM = ":" // header value separator
_READ_BUF = 1024 // buffer size for socket reader
_CMD_END = "--END COMMAND--" // Asterisk command data end
_HOST = "gami" // default host value
ORIG_TMOUT = 30000 // Originate timeout
)
var (
_PT_BYTES = []byte(_LINE_TERM + _LINE_TERM) // packet separator
)
// basic Asterisk message
type Message map[string]string
// Nice output with sorted keys
func (m Message) String() string {
res := []string{"\n"}
keys := []string{}
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := m[k]
res = append(res, fmt.Sprintf("\t%s:%s%s", k, strings.Repeat(" ", 20 - len(k)), v))
}
return strings.Join(res, "\n")
}
// action id generator
type Aid struct {
host string
id int
mu *sync.RWMutex
}
// NewAid, Aid factory
func NewAid() *Aid {
var err error
a := &Aid{}
a.mu = &sync.RWMutex{}
if a.host, err = os.Hostname(); err != nil {
a.host = _HOST
}
return a
}
// GenerateId, generate new action id
func (a *Aid) Generate() string {
a.mu.Lock()
defer a.mu.Unlock()
a.id++
return a.host + "-" + fmt.Sprint(a.id)
}
// callback function storage
type cbList struct {
mu *sync.RWMutex
f map[string]*func(Message)
sd map[string]bool // callback will self delete (used for multi-message responses)
}
// set, setting handle function for specific action id|event (will overwrite current if present)
func (cbl *cbList) set(key string, f *func(Message), sd bool) {
cbl.mu.Lock()
defer cbl.mu.Unlock()
cbl.f[key] = f
cbl.sd[key] = sd
}
// del, deleting callback for specific action id|event
func (cbl *cbList) del(key string) {
cbl.mu.Lock()
defer cbl.mu.Unlock()
delete(cbl.f, key)
delete(cbl.sd, key)
}
// get, returns function for specific action id/event
func (cbl *cbList) get(key string) (*func(Message), bool) {
cbl.mu.RLock()
defer cbl.mu.RUnlock()
return cbl.f[key], cbl.sd[key]
}
// Originate, struct used in Originate command
// if pointed Context and Application, Context has higher priority
type Originate struct {
Channel string // channel to which originate
Context string // context to move after originate success
Exten string // exten to move after originate success
Priority string // priority to move after originate success
Timeout int // originate timeout ms
CallerID string // caller identification string
Account string // used for CDR
Application string // application to execute after successful originate
Data string // data passed to application
Async bool // asynchronous call
}
// NewOriginate, Originate default values constructor (to context)
func NewOriginate(channel, context, exten, priority string) *Originate {
return &Originate{
Channel: channel,
Context: context,
Exten: exten,
Priority: priority,
Timeout: ORIG_TMOUT,
Async: false,
}
}
// NewOriginateApp, constructor for originate to application
func NewOriginateApp(channel, app, data string) *Originate {
return &Originate{
Channel: channel,
Timeout: ORIG_TMOUT,
Application: app,
Data: data,
Async: false,
}
}
// main working entity
type Asterisk struct {
address string // string adress to host
login string // login for AMI
password string // password for AMI
conn *net.TCPConn // network connection to Asterisk
actionHandlers *cbList // action response handle functions
eventHandlers *cbList // event handle functions
defaultHandler *func(Message) // default handler for all Asterisk messages, useful for debugging
netErrHandler *func(error) // network error handle function
aid *Aid // action id
authorized bool // is successful logined to AMI
}
// NewAsterisk, Asterisk factory
func NewAsterisk(address, login, password string) *Asterisk {
return &Asterisk{
address: address,
login: login,
password: password,
actionHandlers: &cbList{
&sync.RWMutex{},
make(map[string]*func(Message)),
make(map[string]bool),
},
eventHandlers: &cbList{
&sync.RWMutex{},
make(map[string]*func(Message)),
make(map[string]bool),
},
aid: NewAid(),
}
}
// send, send Message to socket
func (a *Asterisk) send(m Message) error {
buf := bytes.NewBufferString("")
for k, v := range m {
buf.Write([]byte(k))
buf.Write([]byte(_KEY_VAL_TERM))
buf.Write([]byte(v))
buf.Write([]byte(_LINE_TERM))
}
buf.Write([]byte(_LINE_TERM))
if wrb, err := (*a.conn).Write(buf.Bytes()); wrb != buf.Len() || err != nil {
if err != nil {
return err
}
return fmt.Errorf("Not fully writed packet to output stream\n")
}
return nil
}
func (a *Asterisk) read(pbuf *bytes.Buffer, buf *[]byte) error {
rc, err := (*a.conn).Read(*buf)
if err != nil { // network error
return err
}
wb, err := pbuf.Write((*buf)[:rc])
if err != nil || wb != rc { // can't write to data buffer, just skip
return nil
}
// while has end of packet symbols in buffer
initial := bytes.Index(pbuf.Bytes(), _PT_BYTES)
for pos := initial; pos != -1; pos = bytes.Index(pbuf.Bytes(), _PT_BYTES) {
bp := make([]byte, pos+len(_PT_BYTES))
r, err := pbuf.Read(bp) // reading packet to separate puffer
if err != nil || r != pos+len(_PT_BYTES) { // reading problems, just skip
continue
}
m := make(Message)
// splitting packet by line separator
for _, line := range bytes.Split(bp, []byte(_LINE_TERM)) {
// empty line
if len(line) == 0 {
continue
}
kvl := bytes.SplitN(line, []byte(_KEY_VAL_TERM), 2)
// not standard header
if len(kvl) == 1 {
if string(line) != _CMD_END {
m["CmdData"] += string(line)
}
continue
}
k := bytes.TrimSpace(kvl[0])
v := bytes.TrimSpace(kvl[1])
m[string(k)] = string(v)
}
// if has ActionID and has callback run it and delete
if v, vok := m["ActionID"]; vok {
if f, sd := a.actionHandlers.get(v); f != nil {
go (*f)(m)
if !sd { // will never remove "self-delete" callbacks
a.actionHandlers.del(v)
}
}
}
// if Event and has callback run it
if v, vok := m["Event"]; vok {
if f, _ := a.eventHandlers.get(v); f != nil {
go (*f)(m)
}
}
// run default handler if not nil
if a.defaultHandler != nil {
go (*a.defaultHandler)(m)
}
}
buf = nil
return nil
}
// readDispatcher, reads data from socket and builds messages
func (a *Asterisk) readDispatcher(finishChann <-chan struct{}) {
pbuf := bytes.NewBufferString("") // data buffer
buf := make([]byte, _READ_BUF) // read buffer
for {
select {
case <-finishChann:
log.Println("Finalizing ami read events")
a.conn.Close()
return
default:
if err := a.read(pbuf, &buf); err != nil {
log.Println("Error reading from socket:", err)
a.authorized = false // unauth
if a.netErrHandler != nil { // run network error callback
(*a.netErrHandler)(err)
}
return
}
}
}
}