better parser

This commit is contained in:
Radu Ioan Fericean
2012-05-03 22:42:09 +03:00
parent 0c521b180c
commit 216f5cf92b
3 changed files with 25 additions and 21 deletions

View File

@@ -13,6 +13,10 @@ type Session struct {
startTimes []time.Time
}
func NewSession() *Session {
return &Session{}
}
func (s *Session) AddCallToSession(destination string, startTime time.Time) {
s.destinations = append(s.destinations, destination)
s.startTimes = append(s.startTimes, startTime)

View File

@@ -25,29 +25,32 @@ import (
func TestSessionDurationSingle(t *testing.T) {
s := &Session{}
s.AddCallToSession("", time.Now())
start := time.Date(2012, 5, 3, 14, 30, 0, 0, time.UTC)
s.AddCallToSession("", start)
twoSeconds, _ := time.ParseDuration("2s")
if d := s.GetSessionDurationFrom(time.Now().Add(twoSeconds)); d.Seconds() < 2 || d.Seconds() > 3 {
if d := s.GetSessionDurationFrom(start.Add(twoSeconds)); d.Seconds() < 2 || d.Seconds() > 3 {
t.Errorf("Wrong duration %v", d)
}
}
func TestSessionDurationMultiple(t *testing.T) {
s := &Session{}
s.AddCallToSession("", time.Now())
s.AddCallToSession("", time.Now())
s.AddCallToSession("", time.Now())
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(time.Now().Add(twoSeconds)); d.Seconds() < 6 || d.Seconds() > 7 {
if d := s.GetSessionDurationFrom(start.Add(twoSeconds)); d.Seconds() < 6 || d.Seconds() > 7 {
t.Errorf("Wrong duration %v", d)
}
}
func TestSessionCostSingle(t *testing.T) {
s := &Session{customer: "vdf", subject: "rif"}
s.AddCallToSession("0723", time.Now())
start := time.Date(2012, 5, 3, 14, 30, 0, 0, time.UTC)
s.AddCallToSession("0723", start)
twoSeconds, _ := time.ParseDuration("60s")
if ccs, err := s.GetSessionCostFrom(time.Now().Add(twoSeconds)); err != nil {
if ccs, err := s.GetSessionCostFrom(start.Add(twoSeconds)); err != nil {
t.Errorf("Get cost returned error %v", err)
} else {
if len(ccs) != 1 || ccs[0].Cost < 1 || ccs[0].Cost > 1.1 {
@@ -58,11 +61,12 @@ func TestSessionCostSingle(t *testing.T) {
func TestSessionCostMultiple(t *testing.T) {
s := &Session{customer: "vdf", subject: "rif"}
s.AddCallToSession("0723", time.Now())
s.AddCallToSession("0257", time.Now())
s.AddCallToSession("0256", time.Now())
start := time.Date(2012, 5, 3, 14, 30, 0, 0, time.UTC)
s.AddCallToSession("0723", start)
s.AddCallToSession("0257", start)
s.AddCallToSession("0256", start)
twoSeconds, _ := time.ParseDuration("60s")
if ccs, err := s.GetSessionCostFrom(time.Now().Add(twoSeconds)); err != nil {
if ccs, err := s.GetSessionCostFrom(start.Add(twoSeconds)); err != nil {
t.Errorf("Get cost returned error %v", err)
} else {
sum := 0.0

View File

@@ -48,7 +48,7 @@ func (ev *Event) String() (result string) {
}
type SessionManager struct {
conn net.Conn
buf *bufio.Reader
eventBodyRE *regexp.Regexp
sessions []*Session
}
@@ -58,18 +58,14 @@ func (sm *SessionManager) Connect(address, pass string) {
if err != nil {
log.Fatal("Could not connect to freeswitch server!")
}
sm.conn = conn
sm.buf = bufio.NewReaderSize(conn, 8192)
sm.eventBodyRE, _ = regexp.Compile(`"(.*?)":\s+"(.*?)"`)
fmt.Fprint(sm.conn, fmt.Sprintf("auth %s\n\n", pass))
fmt.Fprint(sm.conn, "event json all\n\n")
}
func (sm *SessionManager) Close() {
sm.conn.Close()
fmt.Fprint(conn, fmt.Sprintf("auth %s\n\n", pass))
fmt.Fprint(conn, "event json all\n\n")
}
func (sm *SessionManager) ReadNextEvent() (ev *Event) {
body, err := bufio.NewReader(sm.conn).ReadString('}')
body, err := sm.buf.ReadString('}')
if err != nil {
log.Print("Could not read from freeswitch connection!")
}