forked from axyjo/dialstar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (111 loc) · 4.44 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
package main
import (
"callerhandler"
"container/list"
"fmt"
"net/http"
"net/url"
"twiml"
"utils"
"webui"
)
var user_queue *list.List
//This is just all the handlers and shit
func main() {
//Create a new channel of size 10 (shouldn't get much larger than this)
callers_waiting := make(chan twiml.Thingy, 10)
push := make([]chan webui.PushData, 0)
//Create a new CallerHandler with a CallerWrapper/HangupWrapper with the shared channel callers_waiting
Conf_waiters := callerhandler.CallerWrapper{Callerid: callers_waiting}
Conf_dequeue := callerhandler.HangUpWrapper{Callerid: callers_waiting, Push: &push}
Conf_newUser := callerhandler.WelcomeWrapper{Push: &push}
Conf_push := webui.WebSocketWrapper{Push: &push}
ad_count := make([]int, 3)
Conf_adremove := callerhandler.AdWrapper{Push: &push, AdsPlayed: ad_count}
//Have a function that polls users and queues and dequeues users as necessary
go PollWaiters(callers_waiting, &push)
//Register the Handle functions for the given patters and appropriate handlers
http.HandleFunc("/caller/", Conf_waiters.CallerHandler)
http.HandleFunc("/conference/", callerhandler.ConferenceHandler)
http.HandleFunc("/hangup/", Conf_dequeue.HangUpHandler)
http.HandleFunc("/welcome/", Conf_newUser.WelcomeHandler)
http.HandleFunc("/ad/", Conf_adremove.AdHandler)
http.HandleFunc("/webui/websocket", Conf_push.WebSocketHandler)
http.HandleFunc("/", webui.WebHandler)
//Starts the HTTP server at the address Localhost:3000
port := 3000
fmt.Printf("HTTP server listening on port %d\n", port)
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
func PollWaiters(c chan twiml.Thingy, p *[]chan webui.PushData) {
//Creates an empty queue to put users in
user_queue = list.New()
//Iterates over each element in the channel
for element := range c {
//If the user is to be added to the queue..
if element.Add {
//Push the user onto the queue
_ = user_queue.PushBack(element)
fmt.Printf("%.6s was added into the queue\n", element.CallSid)
len := user_queue.Len()
fmt.Printf("There are %d users in the queue\n", len)
//If there are 2 or more users in the queue
if len >= 3 {
//Get a pointer to the first element of the queue
first := user_queue.Front()
f := first.Value.(twiml.Thingy).CallSid
//Get a pointer to the second user who's in the third position in the queue to prevent call repetition
second := user_queue.Front().Next().Next()
s := second.Value.(twiml.Thingy).CallSid
//remove the first and second user from the queue
user_queue.Remove(first)
user_queue.Remove(second)
fmt.Printf("Created a conference for %.6s and %.6s\n", f, s)
fmt.Printf("There are %d users in the queue\n", user_queue.Len())
//Concatenate the first and second user's CallSid to be used in the ConfURL
ConferenceId := f + s
ConfURLBase := "http://twilio.axyjo.com/conference/?ConferenceId=" + ConferenceId + "&OtherCity="
//Authentication stuff
authToken := "79ed2712d0cf06c87aa2783eee6aaa7a"
accountId := "AC6f0fa1837933462d780f6fc1daf57d44"
//Map of strings to arry of strings
values := make(url.Values)
//Set the URLs for both the first and second user and put them into a conference together
values.Set("Url", ConfURLBase+url.QueryEscape(second.Value.(twiml.Thingy).City))
_, err := http.PostForm("https://"+accountId+":"+authToken+"@api.twilio.com/2010-04-01/Accounts/"+accountId+"/Calls/"+f, values)
if err != nil {
panic(err)
}
values.Set("Url", ConfURLBase+url.QueryEscape(first.Value.(twiml.Thingy).City))
_, err = http.PostForm("https://"+accountId+":"+authToken+"@api.twilio.com/2010-04-01/Accounts/"+accountId+"/Calls/"+s, values)
if err != nil {
panic(err)
}
pData := webui.PushData{UserCount: utils.GetUserCount()}
if webui.UseNumbers {
pData.Call1Id = first.Value.(twiml.Thingy).Number
pData.Call2Id = second.Value.(twiml.Thingy).Number
} else {
pData.Call1Id = f
pData.Call2Id = s
}
for _, j := range *p {
j <- pData
}
}
} else {
//Otherwise the user is to be dequeued.
//Iterate through the list and find a matching CallSid, remove the user from the queue and break out of the loop
for i := user_queue.Front(); i != nil; i = i.Next() {
if i.Value.(twiml.Thingy).CallSid == element.CallSid {
user_queue.Remove(i)
pData := webui.PushData{UserCount: utils.GetUserCount()}
for _, j := range *p {
j <- pData
}
break
}
}
}
}
}