From f5eedc4498bcc16e3577de67fefe5bf286a968b2 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 3 May 2012 23:14:23 +0300 Subject: [PATCH] event handlers --- sessionmanager/event.go | 39 +++++++++++++++++++++++++++ sessionmanager/session.go | 36 ++++++++++++++++++------- sessionmanager/session_test.go | 14 +++++----- sessionmanager/sessionmanager.go | 30 +++++++++------------ sessionmanager/sessionmanager_test.go | 8 +++--- 5 files changed, 89 insertions(+), 38 deletions(-) create mode 100644 sessionmanager/event.go diff --git a/sessionmanager/event.go b/sessionmanager/event.go new file mode 100644 index 000000000..fdeed6e25 --- /dev/null +++ b/sessionmanager/event.go @@ -0,0 +1,39 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012 Radu Ioan Fericean + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package sessionmanager + +import ( + "fmt" +) + +type Event struct { + Fields map[string]string +} + +func NewEvent() (ev *Event) { + return &Event{Fields: make(map[string]string)} +} + +func (ev *Event) String() (result string) { + for k, v := range ev.Fields { + result += fmt.Sprintf("%s = %s\n", k, v) + } + result += "==============================================================" + return +} diff --git a/sessionmanager/session.go b/sessionmanager/session.go index a77074336..b7cdd9df0 100644 --- a/sessionmanager/session.go +++ b/sessionmanager/session.go @@ -1,3 +1,21 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012 Radu Ioan Fericean + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + package sessionmanager import ( @@ -8,24 +26,22 @@ import ( ) type Session struct { - customer, subject string - destinations []string - startTimes []time.Time + id, cstmId, subject string + callsMap map[string]time.Time } -func NewSession() *Session { - return &Session{} +func NewSession(cstmId, subject string) *Session { + return &Session{cstmId: cstmId, subject: subject, callsMap: make(map[string]time.Time)} } func (s *Session) AddCallToSession(destination string, startTime time.Time) { - s.destinations = append(s.destinations, destination) - s.startTimes = append(s.startTimes, startTime) + s.callsMap[destination] = startTime } func (s *Session) GetSessionDurationFrom(now time.Time) (d time.Duration) { seconds := 0.0 - for _, st := range s.startTimes { + for _, st := range s.callsMap { seconds += now.Sub(st).Seconds() } d, err := time.ParseDuration(fmt.Sprintf("%ds", int(seconds))) @@ -40,8 +56,8 @@ func (s *Session) GetSessionDuration() time.Duration { } func (s *Session) GetSessionCostFrom(now time.Time) (callCosts []*timespans.CallCost, err error) { - for i, st := range s.startTimes { - cd := ×pans.CallDescriptor{TOR: 1, CstmId: s.customer, Subject: s.subject, DestinationPrefix: s.destinations[i], TimeStart: st, TimeEnd: now} + for dest, st := range s.callsMap { + cd := ×pans.CallDescriptor{TOR: 1, CstmId: s.cstmId, Subject: s.subject, DestinationPrefix: dest, TimeStart: st, TimeEnd: now} cd.SetStorageGetter(storageGetter) if cc, err := cd.GetCost(); err == nil { callCosts = append(callCosts, cc) diff --git a/sessionmanager/session_test.go b/sessionmanager/session_test.go index 5f47a3884..744725345 100644 --- a/sessionmanager/session_test.go +++ b/sessionmanager/session_test.go @@ -24,29 +24,29 @@ import ( ) func TestSessionDurationSingle(t *testing.T) { - s := &Session{} + s := NewSession("", "") start := time.Date(2012, 5, 3, 14, 30, 0, 0, time.UTC) s.AddCallToSession("", start) twoSeconds, _ := time.ParseDuration("2s") if d := s.GetSessionDurationFrom(start.Add(twoSeconds)); d.Seconds() < 2 || d.Seconds() > 3 { - t.Errorf("Wrong duration %v", d) + t.Errorf("Wrong session duration %v", d) } } func TestSessionDurationMultiple(t *testing.T) { - s := &Session{} + s := NewSession("", "") start := time.Date(2012, 5, 3, 14, 30, 0, 0, time.UTC) s.AddCallToSession("", start) s.AddCallToSession("", start) s.AddCallToSession("", start) twoSeconds, _ := time.ParseDuration("2s") - if d := s.GetSessionDurationFrom(start.Add(twoSeconds)); d.Seconds() < 6 || d.Seconds() > 7 { - t.Errorf("Wrong duration %v", d) + if d := s.GetSessionDurationFrom(start.Add(twoSeconds)); d.Seconds() < 2 || d.Seconds() > 3 { + t.Errorf("Wrong session duration %v", d) } } func TestSessionCostSingle(t *testing.T) { - s := &Session{customer: "vdf", subject: "rif"} + s := NewSession("vdf", "rif") start := time.Date(2012, 5, 3, 14, 30, 0, 0, time.UTC) s.AddCallToSession("0723", start) twoSeconds, _ := time.ParseDuration("60s") @@ -60,7 +60,7 @@ func TestSessionCostSingle(t *testing.T) { } func TestSessionCostMultiple(t *testing.T) { - s := &Session{customer: "vdf", subject: "rif"} + s := NewSession("vdf", "rif") start := time.Date(2012, 5, 3, 14, 30, 0, 0, time.UTC) s.AddCallToSession("0723", start) s.AddCallToSession("0257", start) diff --git a/sessionmanager/sessionmanager.go b/sessionmanager/sessionmanager.go index 4ff03e4a0..d05d673df 100644 --- a/sessionmanager/sessionmanager.go +++ b/sessionmanager/sessionmanager.go @@ -27,26 +27,10 @@ import ( "regexp" ) -type Event struct { - Fields map[string]string -} - -func NewEvent() (ev *Event) { - return &Event{Fields: make(map[string]string)} -} - var ( storageGetter, _ = timespans.NewRedisStorage("tcp:127.0.0.1:6379", 10) ) -func (ev *Event) String() (result string) { - for k, v := range ev.Fields { - result += fmt.Sprintf("%s = %s\n", k, v) - } - result += "==============================================================" - return -} - type SessionManager struct { buf *bufio.Reader eventBodyRE *regexp.Regexp @@ -77,9 +61,19 @@ func (sm *SessionManager) ReadNextEvent() (ev *Event) { log.Printf("malformed event field: %v", fields) } } + switch ev.Fields["Event-Name"] { + case "HEARTBEAT": + sm.OnHeartBeat(ev) + case "RE_SCHEDULE": + sm.OnReSchedule(ev) + } return } -func (ssm *SessionManager) onHeartBeat(event string) { - log.Printf("hear beat event: %s", event) +func (sm *SessionManager) OnHeartBeat(ev *Event) { + log.Print("heartbeat") +} + +func (sm *SessionManager) OnReSchedule(ev *Event) { + log.Print("re_schedule") } diff --git a/sessionmanager/sessionmanager_test.go b/sessionmanager/sessionmanager_test.go index 1bc004436..d5cee8da2 100644 --- a/sessionmanager/sessionmanager_test.go +++ b/sessionmanager/sessionmanager_test.go @@ -19,7 +19,7 @@ along with this program. If not, see package sessionmanager import ( - "log" + //"log" "testing" ) @@ -28,7 +28,9 @@ func TestConnect(t *testing.T) { sm.Connect("localhost:8021", "ClueCon") for { ev := sm.ReadNextEvent() - log.Printf("%s : %s", ev.Fields["Event-Name"], ev.Fields["Event-Subclass"]) - log.Print(ev) + if ev == nil { + t.Error("Got nil event!") + } + //log.Print(ev) } }