sending disconnect messages to fs

This commit is contained in:
Radu Ioan Fericean
2012-10-09 18:05:27 +03:00
parent 75e5bd3b98
commit 67afda7db2
4 changed files with 44 additions and 14 deletions

View File

@@ -420,6 +420,5 @@ func main() {
rater.Logger.Info("Starting CGRateS mediator.")
go startMediator(responder, loggerDb)
}
<-exitChan
}

View File

@@ -25,6 +25,7 @@ type fSock struct {
eventHandlers map[string]func(string)
eventFilters map[string]string
apiChan chan string
cmdChan chan string
reconnects int
delayFunc func() int
}
@@ -260,28 +261,48 @@ func SendApiCmd(cmdStr string) error {
return nil
}
// SendMessage command
func SendMsgCmd(uuid string, cmdargs map[string]string) error {
if len(cmdargs) == 0 {
return errors.New("Need command arguments")
}
if !Connected() {
return errors.New("Not connected to FS")
}
argStr := ""
for k, v := range cmdargs {
argStr += fmt.Sprintf("%s:%s\n", k, v)
}
fmt.Fprint(fs.conn, fmt.Sprintf("sendmsg %s\n%s\n", uuid, argStr))
replyTxt := <-fs.cmdChan
if strings.HasPrefix(replyTxt, "-ERR") {
return fmt.Errorf("SendMessage: %s", replyTxt)
}
return nil
}
// Reads events from socket
func ReadEvents() {
// Read events from buffer, firing them up further
for {
hdr, body, err := readEvent()
if err != nil {
fmt.Println("WARNING: got error while reading events: ", err.Error())
connErr := Connect(fs.reconnects)
if connErr != nil {
fmt.Println("FSock reader - cannot connect to FS")
return
}
continue // Connection reset
}
if strings.Contains(hdr, "api/response") {
fs.apiChan <- hdr + body
} else if strings.Contains(hdr, "command/reply") {
fs.cmdChan <- headerVal(hdr, "Reply-Text")
}
if body != "" { // We got a body, could be event, try dispatching it
dispatchEvent(body)
}
}
fmt.Println("Exiting ReadEvents")
return
}

View File

@@ -94,16 +94,29 @@ func (sm *FSSessionManager) GetSession(uuid string) *Session {
}
// Disconnects a session by sending hangup command to freeswitch
func (sm *FSSessionManager) DisconnectSession(s *Session, notify string) error {
err := fsock.Disconnect()
func (sm *FSSessionManager) DisconnectSession(s *Session, notify string) {
err := fsock.SendApiCmd(fmt.Sprintf("api 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": notify})
if err != nil {
rater.Logger.Err("could not send disconect msg to freeswitch")
}
s.Close()
return err
return
}
// Sends the transfer command to unpark the call to freeswitch
func (sm *FSSessionManager) unparkCall(uuid, call_dest_nb, notify string) {
fsock.SendApiCmd(fmt.Sprintf("uuid_setvar %s cgr_notify %s\n\n", uuid, notify))
fsock.SendApiCmd(fmt.Sprintf("uuid_transfer %s %s\n\n", uuid, call_dest_nb))
err := fsock.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))
if err != nil {
rater.Logger.Err("could not send unpark api call to freeswitch")
}
}
func (sm *FSSessionManager) OnHeartBeat(ev Event) {
@@ -299,10 +312,7 @@ func (sm *FSSessionManager) GetDbLogger() rater.DataStorage {
func (sm *FSSessionManager) Shutdown() (err error) {
rater.Logger.Info("Shutting down all sessions...")
for _, s := range sm.sessions {
err = sm.DisconnectSession(s, MANAGER_REQUEST)
if err != nil {
return
}
sm.DisconnectSession(s, MANAGER_REQUEST)
}
return
}

View File

@@ -25,7 +25,7 @@ import (
type SessionManager interface {
Connect(address, pass string) error
DisconnectSession(*Session, string) error
DisconnectSession(*Session, string)
LoopAction(*Session, *rater.CallDescriptor)
GetDebitPeriod() time.Duration
GetDbLogger() rater.DataStorage