diff --git a/sessionmanager/fsevent.go b/sessionmanager/fsevent.go index c551035f3..be01356ec 100644 --- a/sessionmanager/fsevent.go +++ b/sessionmanager/fsevent.go @@ -1,6 +1,6 @@ /* Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +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 @@ -20,7 +20,7 @@ package sessionmanager import ( "fmt" - "github.com/cgrates/cgrates/fsock" + "github.com/cgrates/fsock" "strconv" "strings" "time" @@ -59,9 +59,9 @@ const ( MISSING_PARAMETER = "-MISSING_PARAMETER" SYSTEM_ERROR = "-SYSTEM_ERROR" MANAGER_REQUEST = "+MANAGER_REQUEST" - USERNAME = "username" - REQ_USER = "sip_req_user" - TOR_DEFAULT = "0" + USERNAME = "username" + REQ_USER = "sip_req_user" + TOR_DEFAULT = "0" ) // Nice printing for the event object. diff --git a/sessionmanager/fssessionmanager.go b/sessionmanager/fssessionmanager.go index 7645d5bd2..4c6f105f9 100644 --- a/sessionmanager/fssessionmanager.go +++ b/sessionmanager/fssessionmanager.go @@ -1,6 +1,6 @@ /* Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +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 @@ -20,9 +20,10 @@ package sessionmanager import ( "bufio" + "errors" "fmt" - "github.com/cgrates/cgrates/fsock" "github.com/cgrates/cgrates/rater" + "github.com/cgrates/fsock" "log/syslog" "net" "strings" @@ -45,20 +46,20 @@ func NewFSSessionManager(storage rater.DataStorage, connector rater.Connector, d } // Connects to the freeswitch mod_event_socket server and starts -// listening for events in json format. +// listening for events. func (sm *FSSessionManager) Connect(address, pass string) (err error) { eventFilters := map[string]string{"Call-Direction": "inbound"} - if err = fsock.New(address, pass, 10, sm.createHandlers(), eventFilters, rater.Logger.(*syslog.Writer)); err != nil { + if fsock.FS, err = fsock.NewFSock(address, pass, 10, sm.createHandlers(), eventFilters, rater.Logger.(*syslog.Writer)); err != nil { rater.Logger.Crit(fmt.Sprintf("FreeSWITCH error:", err)) return - } else if fsock.Connected() { - rater.Logger.Info("Successfully connected to FreeSWITCH") + } else if !fsock.FS.Connected() { + return errors.New("Cannot connect to FreeSWITCH") } - fsock.ReadEvents() + fsock.FS.ReadEvents() return nil } -func (sm *FSSessionManager) createHandlers() (handlers map[string]func(string)) { +func (sm *FSSessionManager) createHandlers() (handlers map[string][]func(string)) { hb := func(body string) { ev := new(FSEvent).New(body) sm.OnHeartBeat(ev) @@ -75,11 +76,11 @@ func (sm *FSSessionManager) createHandlers() (handlers map[string]func(string)) ev := new(FSEvent).New(body) sm.OnChannelHangupComplete(ev) } - return map[string]func(string){ - "HEARTBEAT": hb, - "CHANNEL_PARK": cp, - "CHANNEL_ANSWER": ca, - "CHANNEL_HANGUP_COMPLETE": ch, + return map[string][]func(string){ + "HEARTBEAT": []func(string){hb}, + "CHANNEL_PARK": []func(string){cp}, + "CHANNEL_ANSWER": []func(string){ca}, + "CHANNEL_HANGUP_COMPLETE": []func(string){ch}, } } @@ -96,11 +97,11 @@ func (sm *FSSessionManager) GetSession(uuid string) *Session { // Disconnects a session by sending hangup command to freeswitch func (sm *FSSessionManager) DisconnectSession(s *Session, notify string) { rater.Logger.Debug(fmt.Sprintf("Session: %+v", s.uuid)) - err := fsock.SendApiCmd(fmt.Sprintf("uuid_setvar %s cgr_notify %s\n\n", s.uuid, notify)) + err := fsock.FS.SendApiCmd(fmt.Sprintf("uuid_setvar %s cgr_notify %s\n\n", s.uuid, notify)) if err != nil { rater.Logger.Err("could not send disconect api notification to freeswitch") } - err = fsock.SendMsgCmd(s.uuid, map[string]string{"call-command": "hangup", "hangup-cause": "MANAGER_REQUEST"}) // without + sign + err = fsock.FS.SendMsgCmd(s.uuid, map[string]string{"call-command": "hangup", "hangup-cause": "MANAGER_REQUEST"}) // without + sign if err != nil { rater.Logger.Err("could not send disconect msg to freeswitch") } @@ -120,11 +121,11 @@ func (sm *FSSessionManager) RemoveSession(s *Session) { // Sends the transfer command to unpark the call to freeswitch func (sm *FSSessionManager) unparkCall(uuid, call_dest_nb, notify string) { - err := fsock.SendApiCmd(fmt.Sprintf("uuid_setvar %s cgr_notify %s\n\n", uuid, notify)) + err := fsock.FS.SendApiCmd(fmt.Sprintf("uuid_setvar %s cgr_notify %s\n\n", uuid, notify)) if err != nil { rater.Logger.Err("could not send unpark api notification to freeswitch") } - err = fsock.SendApiCmd(fmt.Sprintf("uuid_transfer %s %s\n\n", uuid, call_dest_nb)) + err = fsock.FS.SendApiCmd(fmt.Sprintf("uuid_transfer %s %s\n\n", uuid, call_dest_nb)) if err != nil { rater.Logger.Err("could not send unpark api call to freeswitch") } @@ -325,8 +326,8 @@ func (sm *FSSessionManager) GetDbLogger() rater.DataStorage { func (sm *FSSessionManager) Shutdown() (err error) { rater.Logger.Info("Shutting down all sessions...") - fsock.SendApiCmd("hupall MANAGER_REQUEST cgr_reqtype prepaid") - fsock.SendApiCmd("hupall MANAGER_REQUEST cgr_reqtype postpaid") + fsock.FS.SendApiCmd("hupall MANAGER_REQUEST cgr_reqtype prepaid") + fsock.FS.SendApiCmd("hupall MANAGER_REQUEST cgr_reqtype postpaid") for guard := 0; len(sm.sessions) > 0 && guard < 20; guard++ { time.Sleep(100 * time.Millisecond) // wait for the hungup event to be fired rater.Logger.Info(fmt.Sprintf("sessions: %s", sm.sessions)) diff --git a/test.sh b/test.sh index 7d513d80e..55ab0fc25 100755 --- a/test.sh +++ b/test.sh @@ -10,7 +10,7 @@ go test github.com/cgrates/cgrates/inotify it=$? go test github.com/cgrates/cgrates/mediator md=$? -go test github.com/cgrates/cgrates/fsock +go test github.com/cgrates/fsock fs=$? exit $ts && $sm && $bl && $cr && $it && $md && $fs