diff --git a/sessionmanager/session.go b/sessionmanager/session.go index 05f726220..4dd3704c8 100644 --- a/sessionmanager/session.go +++ b/sessionmanager/session.go @@ -25,27 +25,42 @@ import ( "time" ) +const ( + DEBIT_PERIOD = 10 * time.Second +) + type Session struct { uuid, cstmId, subject, destination string startTime time.Time // destination: startTime + stopDebit chan byte } -func NewSession(ev *Event) *Session { +func NewSession(ev *Event) (s *Session) { startTime, err := time.Parse(time.RFC1123, ev.Fields[START_TIME]) if err != nil { log.Print("Error parsing answer event start time, using time.Now!") startTime = time.Now() } - return &Session{uuid: ev.Fields[UUID], + s = &Session{uuid: ev.Fields[UUID], cstmId: ev.Fields[CSTMID], subject: ev.Fields[SUBJECT], destination: ev.Fields[DESTINATION], - startTime: startTime} + startTime: startTime, + stopDebit: make(chan byte)} + go s.StartDebitLoop() + return } func (s *Session) StartDebitLoop() { for { - + select { + case <-s.stopDebit: + log.Print("Put back!") + return + default: + } + log.Print("Debit") + time.Sleep(DEBIT_PERIOD) } } @@ -77,5 +92,5 @@ func (s *Session) GetSessionCost() (callCosts *timespans.CallCost, err error) { } func (s *Session) Close() { - + s.stopDebit <- 1 } diff --git a/sessionmanager/session_test.go b/sessionmanager/session_test.go index c89c5773c..61399ca5b 100644 --- a/sessionmanager/session_test.go +++ b/sessionmanager/session_test.go @@ -53,6 +53,7 @@ var ( func TestSessionDurationSingle(t *testing.T) { s := NewSession(newEvent) + defer s.Close() twoSeconds, _ := time.ParseDuration("2s") if d := s.GetSessionDurationFrom(s.startTime.Add(twoSeconds)); d.Seconds() < 2 || d.Seconds() > 3 { t.Errorf("Wrong session duration %v", d) @@ -61,6 +62,7 @@ func TestSessionDurationSingle(t *testing.T) { func TestSessionCostSingle(t *testing.T) { s := NewSession(newEvent) + defer s.Close() twoSeconds, _ := time.ParseDuration("60s") if cc, err := s.GetSessionCostFrom(s.startTime.Add(twoSeconds)); err != nil { t.Errorf("Get cost returned error %v", err)