From 216f5cf92b5ab3772d15ce6e145860e58e9a1c95 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 3 May 2012 22:42:09 +0300 Subject: [PATCH] better parser --- sessionmanager/session.go | 4 ++++ sessionmanager/session_test.go | 28 ++++++++++++++++------------ sessionmanager/sessionmanager.go | 14 +++++--------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/sessionmanager/session.go b/sessionmanager/session.go index 015f71132..a77074336 100644 --- a/sessionmanager/session.go +++ b/sessionmanager/session.go @@ -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) diff --git a/sessionmanager/session_test.go b/sessionmanager/session_test.go index 79f1d8d3a..5f47a3884 100644 --- a/sessionmanager/session_test.go +++ b/sessionmanager/session_test.go @@ -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 diff --git a/sessionmanager/sessionmanager.go b/sessionmanager/sessionmanager.go index 6fc5a7fa5..4ff03e4a0 100644 --- a/sessionmanager/sessionmanager.go +++ b/sessionmanager/sessionmanager.go @@ -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!") }