forked from DeWarner/kalbi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalbi.go
155 lines (128 loc) · 4.25 KB
/
kalbi.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
package kalbi
import (
"github.com/CanisLupusHUN/kalbi/interfaces"
"github.com/CanisLupusHUN/kalbi/log"
"github.com/CanisLupusHUN/kalbi/sip/dialog"
"github.com/CanisLupusHUN/kalbi/sip/message"
"github.com/CanisLupusHUN/kalbi/sip/transaction"
"github.com/CanisLupusHUN/kalbi/transport"
)
// NewSipStack creates new sip stack
func NewSipStack(Name string) *SipStack {
stack := new(SipStack)
stack.Name = Name
stack.TransManager = transaction.NewTransactionManager()
stack.TransportChannel = make(chan interfaces.SipEventObject)
stack.funcMap = make(map[string]func(interfaces.SipEventObject))
return stack
}
// SipStack has multiple protocol listning points
type SipStack struct {
Name string
ListeningPoints []interfaces.ListeningPoint
OutputPoint chan message.SipMsg
InputPoint chan message.SipMsg
Alive bool
TransManager *transaction.TransactionManager
Dialogs []dialog.Dialog
TransportChannel chan interfaces.SipEventObject
sipListener interfaces.SipListener
funcMap map[string]func(interfaces.SipEventObject)
}
// GetTransactionManager returns TransactionManager
func (ed *SipStack) GetTransactionManager() *transaction.TransactionManager {
return ed.TransManager
}
// CreateListenPoint creates listening point to the event dispatcher
func (ed *SipStack) CreateListenPoint(protocol string, host string, port int) interfaces.ListeningPoint {
listenpoint := transport.NewTransportListenPoint(protocol, host, port)
listenpoint.SetTransportChannel(ed.TransportChannel)
ed.ListeningPoints = append(ed.ListeningPoints, listenpoint)
return listenpoint
}
// SetSipListener sets a struct that follows the SipListener interface
func (ed *SipStack) SetSipListener(listener interfaces.SipListener) {
ed.sipListener = listener
}
func (ed *SipStack) INVITE(handler func(interfaces.SipEventObject)) {
ed.funcMap["INVITE"] = handler
}
func (ed *SipStack) ACK(handler func(interfaces.SipEventObject)) {
ed.funcMap["ACK"] = handler
}
func (ed *SipStack) BYE(handler func(interfaces.SipEventObject)) {
ed.funcMap["BYE"] = handler
}
func (ed *SipStack) CANCEL(handler func(interfaces.SipEventObject)) {
ed.funcMap["CANCEL"] = handler
}
func (ed *SipStack) REGISTER(handler func(interfaces.SipEventObject)) {
ed.funcMap["REGISTER"] = handler
}
func (ed *SipStack) INFO(handler func(interfaces.SipEventObject)) {
ed.funcMap["INFO"] = handler
}
func (ed *SipStack) OPTIONS(handler func(interfaces.SipEventObject)) {
ed.funcMap["OPTIONS"] = handler
}
func (ed *SipStack) PRACK(handler func(interfaces.SipEventObject)) {
ed.funcMap["PRACK"] = handler
}
func (ed *SipStack) SUBSCRIBE(handler func(interfaces.SipEventObject)) {
ed.funcMap["SUBSCRIBE"] = handler
}
func (ed *SipStack) NOTIFY(handler func(interfaces.SipEventObject)) {
ed.funcMap["NOTIFY"] = handler
}
func (ed *SipStack) PUBLISH(handler func(interfaces.SipEventObject)) {
ed.funcMap["PUBLISH"] = handler
}
func (ed *SipStack) REFER(handler func(interfaces.SipEventObject)) {
ed.funcMap["REFER"] = handler
}
func (ed *SipStack) MESSAGE(handler func(interfaces.SipEventObject)) {
ed.funcMap["MESSAGE"] = handler
}
func (ed *SipStack) UPDATE(handler func(interfaces.SipEventObject)) {
ed.funcMap["UPDATE"] = handler
}
// IsAlive check if SipStack is alive
func (ed *SipStack) IsAlive() bool {
return ed.Alive
}
// Stop stops SipStack execution
func (ed *SipStack) Stop() {
log.Log.Info("Stopping SIP Server...")
ed.Alive = false
}
// Start starts the sip stack
func (ed *SipStack) Start() {
log.Log.Info("Starting SIP Server...")
ed.TransManager.ListeningPoint = ed.ListeningPoints[0]
ed.Alive = true
for _, listeningPoint := range ed.ListeningPoints {
go listeningPoint.Start()
}
for ed.Alive {
msg := <-ed.TransportChannel
event := ed.TransManager.Handle(msg)
message := event.GetSipMessage()
if message.Req.StatusCode != nil {
go ed.sipListener.HandleResponses(event)
} else if message.Req.Method != nil {
switch string(message.Req.Method) {
case "INVITE":
ed.funcMap["INVITE"](event)
case "ACK":
ed.funcMap["ACK"](event)
case "BYE":
ed.funcMap["BYE"](event)
case "CANCEL":
ed.funcMap["CANCEL"](event)
case "REGISTER":
ed.funcMap["REGISTER"](event)
}
}
//TODO: Handle failed SIP parse send 400 Bad Request
}
}