-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
133 lines (101 loc) · 2.75 KB
/
main.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
package main
import (
"context"
"net/http"
"sync"
"github.com/CyCoreSystems/ari-proxy/v5/client"
"github.com/CyCoreSystems/ari/v5"
"github.com/CyCoreSystems/ari/v5/client/native"
"github.com/inconshreveable/log15"
)
var ariApp = "test"
var log = log15.New()
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// connect
native.Logger = log
log.Info("Connecting to ARI")
cl, err := client.New(ctx, client.WithApplication(ariApp))
if err != nil {
log.Error("Failed to build ARI client", "error", err)
return
}
// setup app
log.Info("Starting listener app")
listenApp(ctx, cl, channelHandler)
// start call start listener
log.Info("Starting HTTP Handler")
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// make call
log.Info("Make sample call")
h, err := createCall(cl)
if err != nil {
log.Error("Failed to create call", "error", err)
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("Failed to create call: " + err.Error()))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(h.ID()))
}))
log.Info("Listening for requests on port 9990")
http.ListenAndServe(":9990", nil)
return
}
func listenApp(ctx context.Context, cl ari.Client, handler func(*ari.ChannelHandle, *ari.StasisStart)) {
err := client.Listen(ctx, cl, func(ch *ari.ChannelHandle, v *ari.StasisStart) {
log.Info("Got stasis start", "channel", v.Channel.ID)
go handler(ch, v)
})
if err != nil {
log.Crit("failed to listen for new calls", "error", err)
}
return
}
func createCall(cl ari.Client) (h *ari.ChannelHandle, err error) {
h, err = cl.Channel().Create(nil, ari.ChannelCreateRequest{
Endpoint: "Local/1000",
App: ariApp,
})
return
}
func channelHandler(h *ari.ChannelHandle, startEvent *ari.StasisStart) {
log.Info("Running channel handler")
// Subscribe to channel state changes
stateChange := h.Subscribe(ari.Events.ChannelStateChange)
defer stateChange.Cancel()
// Subscribe to StasisEnd events (channel leaving ARI app)
end := h.Subscribe(ari.Events.StasisEnd)
defer end.Cancel()
// Pull the current channel data
data, err := h.Data()
if err != nil {
log.Error("Error getting data", "error", err)
return
}
log.Info("Channel State", "state", data.State)
var wg sync.WaitGroup
wg.Add(1)
go func() {
log.Info("Waiting for channel events")
defer wg.Done()
for {
select {
case <-end.Events():
log.Info("Got stasis end")
return
case e := <-stateChange.Events():
v, ok := e.(*ari.ChannelStateChange)
if !ok {
log.Error("failed to interpret event as ChannelStateChange", "error", err)
return
}
log.Info("New Channel State", "state", v.Channel.State)
}
}
}()
h.Answer()
wg.Wait()
h.Hangup()
}