hangup if no creadit working

This commit is contained in:
Radu Ioan Fericean
2012-05-16 20:09:05 +03:00
parent 232693b73a
commit 5ee6aaacf8
4 changed files with 15 additions and 22 deletions

View File

@@ -35,6 +35,6 @@ func main() {
sm := &sessionmanager.FSSessionManager{}
sm.Connect(new(sessionmanager.DirectSessionDelegate), *freeswitchsrv, *freeswitchpass)
waitChan := make(<-chan byte)
log.Print("Session manager!")
log.Print("CGRateS is listening!")
<-waitChan
}

View File

@@ -88,8 +88,9 @@ func (sm *FSSessionManager) GetSession(uuid string) *Session {
return nil
}
// Disconnects a session by sending hangup command to freeswitch
func (sm *FSSessionManager) DisconnectSession(s *Session) {
fmt.Fprint(sm.conn, fmt.Sprintf("SendMsg %s\ncall-command: hangup\nhangup-cause: \n\n", s.uuid, "MANAGER_REQUEST"))
fmt.Fprint(sm.conn, fmt.Sprintf("SendMsg %s\ncall-command: hangup\nhangup-cause: MANAGER_REQUEST\n\n", s.uuid))
s.Close()
}
@@ -129,7 +130,7 @@ func (sm *FSSessionManager) OnChannelHangupComplete(ev Event) {
// Called on freeswitch's events not processed by the session manger,
// for logging purposes (maybe).
func (sm *FSSessionManager) OnOther(ev Event) {
//log.Printf("Other event: %s", ev.Fields["Event-Name"])
//log.Printf("Other event: %s", ev.GetName())
}
func (sm *FSSessionManager) GetSessionDelegate() SessionDelegate {

View File

@@ -49,7 +49,7 @@ func NewSession(ev Event, sm SessionManager) (s *Session) {
TimeStart: startTime}
s = &Session{uuid: ev.GetUUID(),
callDescriptor: cd,
stopDebit: make(chan byte)}
stopDebit: make(chan byte, 2)} //buffer it for multiple close signals
s.sessionManager = sm
go s.startDebitLoop()
return
@@ -95,6 +95,12 @@ func (s *Session) Close() {
s.callDescriptor.TimeEnd = time.Now()
}
// Disconects a session using session manager
func (s *Session) Disconnect() {
s.sessionManager.DisconnectSession(s)
}
// Nice print for session
func (s *Session) String() string {
return fmt.Sprintf("%v: %s -> %s", s.callDescriptor.TimeStart, s.callDescriptor.Subject, s.callDescriptor.DestinationPrefix)
}

View File

@@ -55,23 +55,7 @@ func (dsd *DirectSessionDelegate) OnHeartBeat(ev Event) {
}
func (dsd *DirectSessionDelegate) OnChannelAnswer(ev Event, s *Session) {
s.callDescriptor.Amount = DEBIT_PERIOD.Seconds()
s.callDescriptor.SetStorageGetter(storageGetter)
remainingSeconds, err := s.callDescriptor.GetMaxSessionTime()
if remainingSeconds == -1 || err == nil {
log.Print("Postpaying client: happy talking!")
return
}
if remainingSeconds == 0 || err != nil {
log.Printf("No credit left: Disconnect %v", s)
s.Disconnect()
return
}
if remainingSeconds < DEBIT_PERIOD.Seconds() || err != nil {
log.Printf("Not enough money for another debit period %v", s)
s.Disconnect()
return
}
log.Print("direct answer")
}
func (dsd *DirectSessionDelegate) OnChannelHangupComplete(ev Event, s *Session) {
@@ -87,17 +71,19 @@ func (dsd *DirectSessionDelegate) LoopAction(s *Session, cd *timespans.CallDescr
s.CallCosts = append(s.CallCosts, cc)
cd.Amount = DEBIT_PERIOD.Seconds()
remainingSeconds, err := cd.GetMaxSessionTime()
if remainingSeconds == -1 || err == nil {
if remainingSeconds == -1 && err == nil {
log.Print("Postpaying client: happy talking!")
return
}
if remainingSeconds == 0 || err != nil {
log.Printf("No credit left: Disconnect %v", s)
s.Disconnect()
return
}
if remainingSeconds < DEBIT_PERIOD.Seconds() || err != nil {
log.Printf("Not enough money for another debit period %v", s)
s.Disconnect()
return
}
}