started rating unit implementation

This commit is contained in:
Radu Ioan Fericean
2013-09-09 22:16:07 +03:00
parent 372fbe0e10
commit 1ee31ca004
4 changed files with 14 additions and 11 deletions

View File

@@ -66,3 +66,10 @@ func (cc *CallCost) GetStartTime() time.Time {
}
return cc.Timespans[0].TimeStart
}
func (cc *CallCost) GetTotalDuration() (td time.Duration) {
for _, ts := range cc.Timespans {
td += ts.GetDuration()
}
return
}

View File

@@ -315,8 +315,8 @@ func (sm *FSSessionManager) OnChannelHangupComplete(ev Event) {
}
func (sm *FSSessionManager) LoopAction(s *Session, cd *engine.CallDescriptor, index float64) {
cc := &engine.CallCost{}
func (sm *FSSessionManager) LoopAction(s *Session, cd *engine.CallDescriptor, index float64) (cc *engine.CallCost) {
cc = &engine.CallCost{}
cd.LoopIndex = index
cd.Amount = sm.debitPeriod.Seconds()
cd.CallDuration += time.Duration(cd.Amount) * time.Second
@@ -326,18 +326,14 @@ func (sm *FSSessionManager) LoopAction(s *Session, cd *engine.CallDescriptor, in
// disconnect session
s.sessionManager.DisconnectSession(s, SYSTEM_ERROR)
}
nbts := len(cc.Timespans)
remainingSeconds := 0.0
engine.Logger.Debug(fmt.Sprintf("Result of MaxDebit call: %v", cc))
if nbts > 0 {
remainingSeconds = cc.Timespans[nbts-1].TimeEnd.Sub(cc.Timespans[0].TimeStart).Seconds()
}
if remainingSeconds == 0 || err != nil {
if cc.GetTotalDuration() == 0 || err != nil {
engine.Logger.Info(fmt.Sprintf("No credit left: Disconnect %v", s))
sm.DisconnectSession(s, INSUFFICIENT_FUNDS)
return
}
s.CallCosts = append(s.CallCosts, cc)
return
}
func (sm *FSSessionManager) GetDebitPeriod() time.Duration {

View File

@@ -86,8 +86,8 @@ func (s *Session) startDebitLoop() {
nextCd.TimeStart = nextCd.TimeEnd
}
nextCd.TimeEnd = nextCd.TimeStart.Add(s.sessionManager.GetDebitPeriod())
s.sessionManager.LoopAction(s, &nextCd, index)
time.Sleep(s.sessionManager.GetDebitPeriod())
cc := s.sessionManager.LoopAction(s, &nextCd, index)
time.Sleep(cc.GetTotalDuration())
index++
}
}

View File

@@ -28,7 +28,7 @@ type SessionManager interface {
Connect(*config.CGRConfig) error
DisconnectSession(*Session, string)
RemoveSession(*Session)
LoopAction(*Session, *engine.CallDescriptor, float64)
LoopAction(*Session, *engine.CallDescriptor, float64) *engine.CallCost
GetDebitPeriod() time.Duration
GetDbLogger() engine.LogStorage
Shutdown() error