event handlers

This commit is contained in:
Radu Ioan Fericean
2012-05-03 23:14:23 +03:00
parent 216f5cf92b
commit f5eedc4498
5 changed files with 89 additions and 38 deletions

39
sessionmanager/event.go Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>
*/
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
}

View File

@@ -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 <http://www.gnu.org/licenses/>
*/
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 := &timespans.CallDescriptor{TOR: 1, CstmId: s.customer, Subject: s.subject, DestinationPrefix: s.destinations[i], TimeStart: st, TimeEnd: now}
for dest, st := range s.callsMap {
cd := &timespans.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)

View File

@@ -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)

View File

@@ -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")
}

View File

@@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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)
}
}