forked from cgrates/fsock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
195 lines (179 loc) · 5.09 KB
/
utils.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
/*
utils.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 (
"crypto/rand"
"encoding/json"
"fmt"
"io"
"net/url"
"sort"
"strings"
"time"
)
const EventBodyTag = "EvBody"
type logger interface {
Alert(string) error
Close() error
Crit(string) error
Debug(string) error
Emerg(string) error
Err(string) error
Info(string) error
Notice(string) error
Warning(string) error
}
type nopLogger struct{}
func (nopLogger) Alert(string) error { return nil }
func (nopLogger) Close() error { return nil }
func (nopLogger) Crit(string) error { return nil }
func (nopLogger) Debug(string) error { return nil }
func (nopLogger) Emerg(string) error { return nil }
func (nopLogger) Err(string) error { return nil }
func (nopLogger) Info(string) error { return nil }
func (nopLogger) Notice(string) error { return nil }
func (nopLogger) Warning(string) error { return nil }
// Convert fseventStr into fseventMap
func FSEventStrToMap(fsevstr string, headers []string) map[string]string {
fsevent := make(map[string]string)
filtered := (len(headers) != 0)
for _, strLn := range strings.Split(fsevstr, "\n") {
if hdrVal := strings.SplitN(strLn, ": ", 2); len(hdrVal) == 2 {
if filtered && isSliceMember(headers, hdrVal[0]) {
continue // Loop again since we only work on filtered fields
}
fsevent[hdrVal[0]] = urlDecode(strings.TrimSpace(strings.TrimRight(hdrVal[1], "\n")))
}
}
return fsevent
}
// Converts string received from fsock into a list of channel info, each represented in a map
func MapChanData(chanInfoStr string) (chansInfoMap []map[string]string) {
chansInfoMap = make([]map[string]string, 0)
spltChanInfo := strings.Split(chanInfoStr, "\n")
if len(spltChanInfo) <= 4 {
return
}
hdrs := strings.Split(spltChanInfo[0], ",")
for _, chanInfoLn := range spltChanInfo[1 : len(spltChanInfo)-3] {
chanInfo := splitIgnoreGroups(chanInfoLn, ",")
if len(hdrs) != len(chanInfo) {
continue
}
chnMp := make(map[string]string)
for iHdr, hdr := range hdrs {
chnMp[hdr] = chanInfo[iHdr]
}
chansInfoMap = append(chansInfoMap, chnMp)
}
return
}
func EventToMap(event string) (result map[string]string) {
result = make(map[string]string)
body := false
spltevent := strings.Split(event, "\n")
for i := 0; i < len(spltevent); i++ {
if len(spltevent[i]) == 0 {
body = true
continue
}
if body {
result[EventBodyTag] = strings.Join(spltevent[i:], "\n")
return
}
if val := strings.SplitN(spltevent[i], ": ", 2); len(val) == 2 {
result[val[0]] = urlDecode(strings.TrimSpace(val[1]))
}
}
return
}
// helper function for uuid generation
func genUUID() string {
b := make([]byte, 16)
io.ReadFull(rand.Reader, b)
b[6] = (b[6] & 0x0F) | 0x40
b[8] = (b[8] &^ 0x40) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[:4], b[4:6], b[6:8], b[8:10],
b[10:])
}
func toJSON(v interface{}) string {
b, _ := json.Marshal(v)
return string(b)
}
// splitIgnoreGroups splits input string by specified separator while ignoring elements grouped using "{}" or "[]"
func splitIgnoreGroups(s string, sep string) (sl []string) {
if s == "" {
return []string{}
}
if sep == "" {
return []string{s}
}
var idx, sqBrackets, crlBrackets int
for i, ch := range s {
if s[i] == sep[0] && sqBrackets == 0 && crlBrackets == 0 {
sl = append(sl, s[idx:i])
idx = i + 1
} else if ch == '[' {
sqBrackets++
} else if ch == ']' && sqBrackets > 0 {
sqBrackets--
} else if ch == '{' {
crlBrackets++
} else if ch == '}' && crlBrackets > 0 {
crlBrackets--
}
}
sl = append(sl, s[idx:])
return sl
}
// Extracts value of a header from anywhere in content string
func headerVal(hdrs, hdr string) string {
var hdrSIdx, hdrEIdx int
if hdrSIdx = strings.Index(hdrs, hdr); hdrSIdx == -1 {
return ""
} else if hdrEIdx = strings.Index(hdrs[hdrSIdx:], "\n"); hdrEIdx == -1 {
hdrEIdx = len(hdrs[hdrSIdx:])
}
splt := strings.SplitN(hdrs[hdrSIdx:hdrSIdx+hdrEIdx], ": ", 2)
if len(splt) != 2 {
return ""
}
return strings.TrimSpace(strings.TrimRight(splt[1], "\n"))
}
// FS event header values are urlencoded. Use this to decode them. On error, use original value
func urlDecode(hdrVal string) string {
if valUnescaped, errUnescaping := url.QueryUnescape(hdrVal); errUnescaping == nil {
hdrVal = valUnescaped
}
return hdrVal
}
func getMapKeys(m map[string][]func(string, int)) (keys []string) {
keys = make([]string, len(m))
indx := 0
for key := range m {
keys[indx] = key
indx++
}
return
}
// Binary string search in slice
func isSliceMember(ss []string, s string) bool {
sort.Strings(ss)
i := sort.SearchStrings(ss, s)
return (i < len(ss) && ss[i] == s)
}
// fibDuration returns successive Fibonacci numbers converted to time.Duration.
func fibDuration(durationUnit, maxDuration time.Duration) func() time.Duration {
a, b := 0, 1
return func() time.Duration {
a, b = b, a+b
fibNrAsDuration := time.Duration(a) * durationUnit
if maxDuration > 0 && maxDuration < fibNrAsDuration {
return maxDuration
}
return fibNrAsDuration
}
}