forked from CyCoreSystems/ari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (113 loc) · 2.96 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"context"
"net/http"
"sync"
"github.com/inconshreveable/log15"
"github.com/CyCoreSystems/ari/v6"
"github.com/CyCoreSystems/ari/v6/client/native"
)
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 := native.Connect(&native.Options{
Application: "test",
Username: "admin",
Password: "admin",
URL: "http://localhost:8088/ari",
WebsocketURL: "ws://localhost:8088/ari/events",
})
if err != nil {
log.Error("Failed to build ARI client", "error", err)
return
}
// setup app
log.Info("Starting listener app")
go 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(cl ari.Client, h *ari.ChannelHandle)) {
sub := cl.Bus().Subscribe(nil, "StasisStart")
end := cl.Bus().Subscribe(nil, "StasisEnd")
for {
select {
case e := <-sub.Events():
v := e.(*ari.StasisStart)
log.Info("Got stasis start", "channel", v.Channel.ID)
go handler(cl, cl.Channel().Get(v.Key(ari.ChannelKey, v.Channel.ID)))
case <-end.Events():
log.Info("Got stasis end")
case <-ctx.Done():
return
}
}
}
func createCall(cl ari.Client) (h *ari.ChannelHandle, err error) {
h, err = cl.Channel().Create(nil, ari.ChannelCreateRequest{
Endpoint: "Local/1000",
App: "example",
})
return
}
func connect(ctx context.Context) (cl ari.Client, err error) {
log.Info("Connecting")
cl, err = native.Connect(&native.Options{})
return
}
func channelHandler(cl ari.Client, h *ari.ChannelHandle) {
log.Info("Running channel handler")
stateChange := h.Subscribe(ari.Events.ChannelStateChange)
defer stateChange.Cancel()
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 <-stateChange.Events():
log.Info("Got state change request")
data, err = h.Data()
if err != nil {
log.Error("Error getting data", "error", err)
continue
}
log.Info("New Channel State", "state", data.State)
if data.State == "Up" {
stateChange.Cancel() // stop subscription to state change events
return
}
}
}
}()
h.Answer()
wg.Wait()
h.Hangup()
}