Adding default fallbackSubject to fsevent

This commit is contained in:
DanB
2013-04-07 13:28:59 +02:00
parent f70cecaa17
commit 4e6a14412d
6 changed files with 44 additions and 35 deletions

View File

@@ -63,9 +63,9 @@ type CGRConfig struct {
SMRater string // address where to access rater. Can be internal, direct rater address or the address of a balancer
SMDebitPeriod int // the period to be debited in advanced during a call (in seconds)
SMRPCEncoding string // use JSON for RPC encoding
SMDefaultTOR string // set default type of record label to 0
SMDefaultTenant string // set default tenant to 0
SMDefaultSubject string // set default rating subject to 0
SMDefaultTOR string // set default type of record
SMDefaultTenant string // set default tenant
SMDefaultSubject string // set default rating subject
MediatorEnabled bool
MediatorCDRPath string // Freeswitch Master CSV CDR path.
MediatorCDROutPath string // Freeswitch Master CSV CDR output path.

View File

@@ -30,23 +30,23 @@ type Event interface {
GetSubject() string
GetAccount() string
GetDestination() string
GetCallDestNr() string
GetTOR() string
GetUUID() string
GetTenant() string
GetReqType() string
GetCallDestNr() string
GetStartTime(string) (time.Time, error)
GetEndTime() (time.Time, error)
GetFallbackSubj() string
MissingParameter() bool
}
// Returns first non empty string out of vals. Useful to extract defaults
func firstNonEmpty( vals []string ) string {
for _,val := range vals {
if len(val) != 0 {
return val
}
}
return ""
func firstNonEmpty(vals []string) string {
for _, val := range vals {
if len(val) != 0 {
return val
}
}
return ""
}

View File

@@ -27,7 +27,7 @@ func TestFirstNonEmpty(t *testing.T) {
sampleMap := make(map[string]string)
sampleMap["Third"] = "third"
fourthElmnt := "fourth"
winnerElmnt := firstNonEmpty( []string{firstElmnt, sampleMap["second"], sampleMap["Third"], fourthElmnt} )
winnerElmnt := firstNonEmpty([]string{firstElmnt, sampleMap["second"], sampleMap["Third"], fourthElmnt})
if winnerElmnt != sampleMap["Third"] {
t.Error("Wrong elemnt returned: ", winnerElmnt)
}

View File

@@ -60,8 +60,6 @@ const (
SYSTEM_ERROR = "-SYSTEM_ERROR"
MANAGER_REQUEST = "+MANAGER_REQUEST"
USERNAME = "Caller-Username"
TOR_DEFAULT = "0"
CSTMID_DEFAULT = "0"
)
// Nice printing for the event object.
@@ -92,25 +90,29 @@ func (fsev *FSEvent) GetOrigId() string {
return fsev.fields[ORIG_ID]
}
func (fsev *FSEvent) GetSubject() string {
return firstNonEmpty( []string{fsev.fields[SUBJECT], fsev.fields[USERNAME]} )
return firstNonEmpty([]string{fsev.fields[SUBJECT], fsev.fields[USERNAME]})
}
func (fsev *FSEvent) GetAccount() string {
return firstNonEmpty( []string{fsev.fields[ACCOUNT], fsev.fields[USERNAME]} )
return firstNonEmpty([]string{fsev.fields[ACCOUNT], fsev.fields[USERNAME]})
}
// Charging destination number
func (fsev *FSEvent) GetDestination() string {
return firstNonEmpty( []string{fsev.fields[DESTINATION], fsev.fields[CALL_DEST_NR]} )
return firstNonEmpty([]string{fsev.fields[DESTINATION], fsev.fields[CALL_DEST_NR]})
}
// Original dialed destination number, useful in case of unpark
func (fsev *FSEvent) GetCallDestNr() string {
return fsev.fields[CALL_DEST_NR]
}
func (fsev *FSEvent) GetTOR() string {
return firstNonEmpty( []string{fsev.fields[TOR], TOR_DEFAULT} )
return firstNonEmpty([]string{fsev.fields[TOR], cfg.SMDefaultTOR})
}
func (fsev *FSEvent) GetUUID() string {
return fsev.fields[UUID]
}
func (fsev *FSEvent) GetTenant() string {
return firstNonEmpty( []string{fsev.fields[CSTMID], CSTMID_DEFAULT} )
}
func (fsev *FSEvent) GetCallDestNr() string {
return fsev.fields[CALL_DEST_NR]
return firstNonEmpty([]string{fsev.fields[CSTMID], cfg.SMDefaultTenant})
}
func (fsev *FSEvent) GetReqType() string {
return fsev.fields[REQTYPE]
@@ -126,6 +128,9 @@ func (fsev *FSEvent) MissingParameter() bool {
strings.TrimSpace(fsev.GetTenant()) == "" ||
strings.TrimSpace(fsev.GetCallDestNr()) == ""
}
func (fsev *FSEvent) GetFallbackSubj() string {
return cfg.SMDefaultSubject
}
func (fsev *FSEvent) GetStartTime(field string) (t time.Time, err error) {
st, err := strconv.ParseInt(fsev.fields[field], 0, 64)
t = time.Unix(0, st*1000)

View File

@@ -22,8 +22,8 @@ import (
"bufio"
"errors"
"fmt"
"github.com/cgrates/cgrates/rater"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/rater"
"github.com/cgrates/fsock"
"log/syslog"
"net"
@@ -31,6 +31,8 @@ import (
"time"
)
var cfg *config.CGRConfig
// The freeswitch session manager type holding a buffer for the network connection
// and the active sessions
type FSSessionManager struct {
@@ -48,7 +50,8 @@ func NewFSSessionManager(storage rater.DataStorage, connector rater.Connector, d
// Connects to the freeswitch mod_event_socket server and starts
// listening for events.
func (sm *FSSessionManager) Connect(cfg *config.CGRConfig) (err error) {
func (sm *FSSessionManager) Connect(cgrCfg *config.CGRConfig) (err error) {
cfg = cgrCfg // make config global
eventFilters := map[string]string{"Call-Direction": "inbound"}
if fsock.FS, err = fsock.NewFSock(cfg.FreeswitchServer, cfg.FreeswitchPass, cfg.FreeswitchReconnects, sm.createHandlers(), eventFilters, rater.Logger.(*syslog.Writer)); err != nil {
return err
@@ -152,14 +155,15 @@ func (sm *FSSessionManager) OnChannelPark(ev Event) {
return
}
cd := rater.CallDescriptor{
Direction: ev.GetDirection(),
Tenant: ev.GetTenant(),
TOR: ev.GetTOR(),
Subject: ev.GetSubject(),
Account: ev.GetAccount(),
Destination: ev.GetDestination(),
Amount: sm.debitPeriod.Seconds(),
TimeStart: startTime}
Direction: ev.GetDirection(),
Tenant: ev.GetTenant(),
TOR: ev.GetTOR(),
Subject: ev.GetSubject(),
Account: ev.GetAccount(),
Destination: ev.GetDestination(),
Amount: sm.debitPeriod.Seconds(),
TimeStart: startTime,
FallbackSubject: ev.GetFallbackSubj()}
var remainingSeconds float64
err = sm.connector.GetMaxSessionTime(cd, &remainingSeconds)
if err != nil {
@@ -325,7 +329,7 @@ func (sm *FSSessionManager) GetDbLogger() rater.DataStorage {
}
func (sm *FSSessionManager) Shutdown() (err error) {
if fsock.FS==nil || !fsock.FS.Connected() {
if fsock.FS == nil || !fsock.FS.Connected() {
return errors.New("Cannot shutdown sessions, fsock not connected")
}
rater.Logger.Info("Shutting down all sessions...")

View File

@@ -19,8 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package sessionmanager
import (
"github.com/cgrates/cgrates/rater"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/rater"
"time"
)