forked from DeWarner/kalbi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalbi.go
86 lines (73 loc) · 2.43 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
package kalbi
import (
"github.com/lmendes86/Kalbi/interfaces"
"github.com/lmendes86/Kalbi/log"
"github.com/lmendes86/Kalbi/sip/dialog"
"github.com/lmendes86/Kalbi/sip/message"
"github.com/lmendes86/Kalbi/sip/transaction"
"github.com/lmendes86/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)
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
}
//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
}
//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 SIPStack...")
ed.Alive = false
}
//Start starts the sip stack
func (ed *SipStack) Start() {
log.Log.Info("Starting SIPStack...")
ed.TransManager.ListeningPoint = ed.ListeningPoints[0]
ed.Alive = true
for _, listeningPoint := range ed.ListeningPoints {
go listeningPoint.Start()
}
for ed.Alive == true {
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 {
go ed.sipListener.HandleRequests(event)
}
//TODO: Handle failed SIP parse send 400 Bad Request
}
}