diff --git a/cmd/cgr-sessionmanager/cgr-sessionmanager.go b/cmd/cgr-sessionmanager/cgr-sessionmanager.go index 3ba405f47..13499475e 100644 --- a/cmd/cgr-sessionmanager/cgr-sessionmanager.go +++ b/cmd/cgr-sessionmanager/cgr-sessionmanager.go @@ -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 } diff --git a/sessionmanager/fssessionmanager.go b/sessionmanager/fssessionmanager.go index 573790797..fb2ae547f 100644 --- a/sessionmanager/fssessionmanager.go +++ b/sessionmanager/fssessionmanager.go @@ -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 { diff --git a/sessionmanager/session.go b/sessionmanager/session.go index f1a545a97..5304ce761 100644 --- a/sessionmanager/session.go +++ b/sessionmanager/session.go @@ -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) +} diff --git a/sessionmanager/sessiondelegate.go b/sessionmanager/sessiondelegate.go index ad2146adb..41b9599b7 100644 --- a/sessionmanager/sessiondelegate.go +++ b/sessionmanager/sessiondelegate.go @@ -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 } }