mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
session_manager/event interface with field filter, preparing for multiple sessions
This commit is contained in:
@@ -25,15 +25,15 @@ import (
|
||||
type Event interface {
|
||||
New(string) Event
|
||||
GetName() string
|
||||
GetDirection() string
|
||||
GetSubject() string
|
||||
GetAccount() string
|
||||
GetDestination() string
|
||||
GetCallDestNr() string
|
||||
GetTOR() string
|
||||
GetUUID() string
|
||||
GetTenant() string
|
||||
GetReqType() string
|
||||
GetDirection(string) string
|
||||
GetSubject(string) string
|
||||
GetAccount(string) string
|
||||
GetDestination(string) string
|
||||
GetCallDestNr(string) string
|
||||
GetTOR(string) string
|
||||
GetTenant(string) string
|
||||
GetReqType(string) string
|
||||
GetStartTime(string) (time.Time, error)
|
||||
GetEndTime() (time.Time, error)
|
||||
MissingParameter() bool
|
||||
|
||||
@@ -77,48 +77,48 @@ func (fsev FSEvent) New(body string) Event {
|
||||
func (fsev FSEvent) GetName() string {
|
||||
return fsev[NAME]
|
||||
}
|
||||
func (fsev FSEvent) GetDirection() string {
|
||||
func (fsev FSEvent) GetDirection(fieldName string) string {
|
||||
//TODO: implement direction
|
||||
return "*out"
|
||||
//return fsev[DIRECTION]
|
||||
}
|
||||
func (fsev FSEvent) GetSubject() string {
|
||||
return utils.FirstNonEmpty(fsev[SUBJECT], fsev[USERNAME])
|
||||
func (fsev FSEvent) GetSubject(fieldName string) string {
|
||||
return utils.FirstNonEmpty(fsev[fieldName], fsev[SUBJECT], fsev[USERNAME])
|
||||
}
|
||||
func (fsev FSEvent) GetAccount() string {
|
||||
return utils.FirstNonEmpty(fsev[ACCOUNT], fsev[USERNAME])
|
||||
func (fsev FSEvent) GetAccount(fieldName string) string {
|
||||
return utils.FirstNonEmpty(fsev[fieldName], fsev[ACCOUNT], fsev[USERNAME])
|
||||
}
|
||||
|
||||
// Charging destination number
|
||||
func (fsev FSEvent) GetDestination() string {
|
||||
return utils.FirstNonEmpty(fsev[DESTINATION], fsev[CALL_DEST_NR])
|
||||
func (fsev FSEvent) GetDestination(fieldName string) string {
|
||||
return utils.FirstNonEmpty(fsev[fieldName], fsev[DESTINATION], fsev[CALL_DEST_NR])
|
||||
}
|
||||
|
||||
// Original dialed destination number, useful in case of unpark
|
||||
func (fsev FSEvent) GetCallDestNr() string {
|
||||
return fsev[CALL_DEST_NR]
|
||||
func (fsev FSEvent) GetCallDestNr(fieldName string) string {
|
||||
return utils.FirstNonEmpty(fsev[fieldName], fsev[CALL_DEST_NR])
|
||||
}
|
||||
func (fsev FSEvent) GetTOR() string {
|
||||
return utils.FirstNonEmpty(fsev[TOR], cfg.DefaultTOR)
|
||||
func (fsev FSEvent) GetTOR(fieldName string) string {
|
||||
return utils.FirstNonEmpty(fsev[fieldName], fsev[TOR], cfg.DefaultTOR)
|
||||
}
|
||||
func (fsev FSEvent) GetUUID() string {
|
||||
return fsev[UUID]
|
||||
}
|
||||
func (fsev FSEvent) GetTenant() string {
|
||||
return utils.FirstNonEmpty(fsev[CSTMID], cfg.DefaultTenant)
|
||||
func (fsev FSEvent) GetTenant(fieldName string) string {
|
||||
return utils.FirstNonEmpty(fsev[fieldName], fsev[CSTMID], cfg.DefaultTenant)
|
||||
}
|
||||
func (fsev FSEvent) GetReqType() string {
|
||||
return utils.FirstNonEmpty(fsev[REQTYPE], cfg.DefaultReqType)
|
||||
func (fsev FSEvent) GetReqType(fieldName string) string {
|
||||
return utils.FirstNonEmpty(fsev[fieldName], fsev[REQTYPE], cfg.DefaultReqType)
|
||||
}
|
||||
func (fsev FSEvent) MissingParameter() bool {
|
||||
return strings.TrimSpace(fsev.GetDirection()) == "" ||
|
||||
strings.TrimSpace(fsev.GetSubject()) == "" ||
|
||||
strings.TrimSpace(fsev.GetAccount()) == "" ||
|
||||
strings.TrimSpace(fsev.GetDestination()) == "" ||
|
||||
strings.TrimSpace(fsev.GetTOR()) == "" ||
|
||||
return strings.TrimSpace(fsev.GetDirection("")) == "" ||
|
||||
strings.TrimSpace(fsev.GetSubject("")) == "" ||
|
||||
strings.TrimSpace(fsev.GetAccount("")) == "" ||
|
||||
strings.TrimSpace(fsev.GetDestination("")) == "" ||
|
||||
strings.TrimSpace(fsev.GetTOR("")) == "" ||
|
||||
strings.TrimSpace(fsev.GetUUID()) == "" ||
|
||||
strings.TrimSpace(fsev.GetTenant()) == "" ||
|
||||
strings.TrimSpace(fsev.GetCallDestNr()) == ""
|
||||
strings.TrimSpace(fsev.GetTenant("")) == "" ||
|
||||
strings.TrimSpace(fsev.GetCallDestNr("")) == ""
|
||||
}
|
||||
func (fsev FSEvent) GetStartTime(field string) (t time.Time, err error) {
|
||||
st, err := strconv.ParseInt(fsev[field], 0, 64)
|
||||
|
||||
@@ -157,21 +157,21 @@ func (sm *FSSessionManager) OnChannelPark(ev Event) {
|
||||
startTime = time.Now()
|
||||
}
|
||||
// if there is no account configured leave the call alone
|
||||
if !utils.IsSliceMember([]string{utils.PREPAID, utils.PSEUDOPREPAID}, strings.TrimSpace(ev.GetReqType())) {
|
||||
if !utils.IsSliceMember([]string{utils.PREPAID, utils.PSEUDOPREPAID}, strings.TrimSpace(ev.GetReqType(""))) {
|
||||
return // we unpark only prepaid and pseudoprepaid calls
|
||||
}
|
||||
if ev.MissingParameter() {
|
||||
sm.unparkCall(ev.GetUUID(), ev.GetCallDestNr(), MISSING_PARAMETER)
|
||||
sm.unparkCall(ev.GetUUID(), ev.GetCallDestNr(""), MISSING_PARAMETER)
|
||||
engine.Logger.Err(fmt.Sprintf("Missing parameter for %s", ev.GetUUID()))
|
||||
return
|
||||
}
|
||||
cd := engine.CallDescriptor{
|
||||
Direction: ev.GetDirection(),
|
||||
Tenant: ev.GetTenant(),
|
||||
TOR: ev.GetTOR(),
|
||||
Subject: ev.GetSubject(),
|
||||
Account: ev.GetAccount(),
|
||||
Destination: ev.GetDestination(),
|
||||
Direction: ev.GetDirection(""),
|
||||
Tenant: ev.GetTenant(""),
|
||||
TOR: ev.GetTOR(""),
|
||||
Subject: ev.GetSubject(""),
|
||||
Account: ev.GetAccount(""),
|
||||
Destination: ev.GetDestination(""),
|
||||
TimeStart: startTime,
|
||||
TimeEnd: startTime.Add(cfg.SMMaxCallDuration),
|
||||
}
|
||||
@@ -179,24 +179,24 @@ func (sm *FSSessionManager) OnChannelPark(ev Event) {
|
||||
err = sm.connector.GetMaxSessionTime(cd, &remainingDurationFloat)
|
||||
if err != nil {
|
||||
engine.Logger.Err(fmt.Sprintf("Could not get max session time for %s: %v", ev.GetUUID(), err))
|
||||
sm.unparkCall(ev.GetUUID(), ev.GetCallDestNr(), SYSTEM_ERROR)
|
||||
sm.unparkCall(ev.GetUUID(), ev.GetCallDestNr(""), SYSTEM_ERROR)
|
||||
return
|
||||
}
|
||||
remainingDuration := time.Duration(remainingDurationFloat)
|
||||
//engine.Logger.Info(fmt.Sprintf("Remaining duration: %v", remainingDuration))
|
||||
if remainingDuration == 0 {
|
||||
//engine.Logger.Info(fmt.Sprintf("Not enough credit for trasferring the call %s for %s.", ev.GetUUID(), cd.GetKey(cd.Subject)))
|
||||
sm.unparkCall(ev.GetUUID(), ev.GetCallDestNr(), INSUFFICIENT_FUNDS)
|
||||
sm.unparkCall(ev.GetUUID(), ev.GetCallDestNr(""), INSUFFICIENT_FUNDS)
|
||||
return
|
||||
}
|
||||
sm.setMaxCallDuration(ev.GetUUID(), remainingDuration)
|
||||
sm.unparkCall(ev.GetUUID(), ev.GetCallDestNr(), AUTH_OK)
|
||||
sm.unparkCall(ev.GetUUID(), ev.GetCallDestNr(""), AUTH_OK)
|
||||
}
|
||||
|
||||
func (sm *FSSessionManager) OnChannelAnswer(ev Event) {
|
||||
//engine.Logger.Info("<SessionManager> FreeSWITCH answer.")
|
||||
// Make sure cgr_type is enforced even if not set by FreeSWITCH
|
||||
if _, err := fsock.FS.SendApiCmd(fmt.Sprintf("uuid_setvar %s cgr_reqtype %s\n\n", ev.GetUUID(), ev.GetReqType())); err != nil {
|
||||
if _, err := fsock.FS.SendApiCmd(fmt.Sprintf("uuid_setvar %s cgr_reqtype %s\n\n", ev.GetUUID(), ev.GetReqType(""))); err != nil {
|
||||
engine.Logger.Err(fmt.Sprintf("Error on attempting to overwrite cgr_type in chan variables: %v", err))
|
||||
}
|
||||
s := NewSession(ev, sm)
|
||||
@@ -212,7 +212,7 @@ func (sm *FSSessionManager) OnChannelHangupComplete(ev Event) {
|
||||
return
|
||||
}
|
||||
defer s.Close(ev) // Stop loop and save the costs deducted so far to database
|
||||
if ev.GetReqType() == utils.POSTPAID {
|
||||
if ev.GetReqType("") == utils.POSTPAID {
|
||||
startTime, err := ev.GetStartTime(START_TIME)
|
||||
if err != nil {
|
||||
engine.Logger.Crit("Error parsing postpaid call start time from event")
|
||||
@@ -224,14 +224,14 @@ func (sm *FSSessionManager) OnChannelHangupComplete(ev Event) {
|
||||
return
|
||||
}
|
||||
cd := engine.CallDescriptor{
|
||||
Direction: ev.GetDirection(),
|
||||
Tenant: ev.GetTenant(),
|
||||
TOR: ev.GetTOR(),
|
||||
Subject: ev.GetSubject(),
|
||||
Account: ev.GetAccount(),
|
||||
Direction: ev.GetDirection(""),
|
||||
Tenant: ev.GetTenant(""),
|
||||
TOR: ev.GetTOR(""),
|
||||
Subject: ev.GetSubject(""),
|
||||
Account: ev.GetAccount(""),
|
||||
LoopIndex: 0,
|
||||
CallDuration: endTime.Sub(startTime),
|
||||
Destination: ev.GetDestination(),
|
||||
Destination: ev.GetDestination(""),
|
||||
TimeStart: startTime,
|
||||
TimeEnd: endTime,
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ type Session struct {
|
||||
// Creates a new session and starts the debit loop
|
||||
func NewSession(ev Event, sm SessionManager) (s *Session) {
|
||||
// SesionManager only handles prepaid and postpaid calls
|
||||
if ev.GetReqType() != utils.PREPAID && ev.GetReqType() != utils.POSTPAID {
|
||||
if ev.GetReqType("") != utils.PREPAID && ev.GetReqType("") != utils.POSTPAID {
|
||||
return
|
||||
}
|
||||
startTime, err := ev.GetStartTime(START_TIME)
|
||||
@@ -49,12 +49,12 @@ func NewSession(ev Event, sm SessionManager) (s *Session) {
|
||||
}
|
||||
|
||||
cd := &engine.CallDescriptor{
|
||||
Direction: ev.GetDirection(),
|
||||
Tenant: ev.GetTenant(),
|
||||
TOR: ev.GetTOR(),
|
||||
Subject: ev.GetSubject(),
|
||||
Account: ev.GetAccount(),
|
||||
Destination: ev.GetDestination(),
|
||||
Direction: ev.GetDirection(""),
|
||||
Tenant: ev.GetTenant(""),
|
||||
TOR: ev.GetTOR(""),
|
||||
Subject: ev.GetSubject(""),
|
||||
Account: ev.GetAccount(""),
|
||||
Destination: ev.GetDestination(""),
|
||||
TimeStart: startTime}
|
||||
s = &Session{uuid: ev.GetUUID(),
|
||||
callDescriptor: cd,
|
||||
@@ -63,7 +63,7 @@ func NewSession(ev Event, sm SessionManager) (s *Session) {
|
||||
if ev.MissingParameter() {
|
||||
sm.DisconnectSession(s, MISSING_PARAMETER)
|
||||
} else {
|
||||
switch ev.GetReqType() {
|
||||
switch ev.GetReqType("") {
|
||||
case utils.PREPAID:
|
||||
go s.startDebitLoop()
|
||||
case utils.POSTPAID:
|
||||
|
||||
Reference in New Issue
Block a user