code for unparking the call

This commit is contained in:
Radu Ioan Fericean
2012-09-14 16:04:15 +03:00
parent 83d6cb792c
commit 1c5fa996bf
5 changed files with 49 additions and 0 deletions

View File

@@ -49,6 +49,7 @@ const (
HEARTBEAT = "HEARTBEAT"
ANSWER = "CHANNEL_ANSWER"
HANGUP = "CHANNEL_HANGUP_COMPLETE"
PARK = "CHANNEL_PARK"
)
// Nice printing for the event object.

View File

@@ -103,6 +103,8 @@ func (sm *FSSessionManager) readNextEvent(exitChan chan bool) (ev Event) {
switch ev.GetName() {
case HEARTBEAT:
sm.OnHeartBeat(ev)
case PARK:
sm.OnChannelPark(ev)
case ANSWER:
sm.OnChannelAnswer(ev)
case HANGUP:
@@ -129,6 +131,11 @@ func (sm *FSSessionManager) DisconnectSession(s *Session) {
s.Close()
}
// Sends the transfer command to unpark the call to freeswitch
func (sm *FSSessionManager) UnparkCall(uuid, dest string) {
fmt.Fprint(sm.conn, fmt.Sprintf("uuid_transfer %s %s XML rou_cgrates\n\n", uuid, dest))
}
// Called on freeswitch's hearbeat event
func (sm *FSSessionManager) OnHeartBeat(ev Event) {
if sm.sessionDelegate != nil {
@@ -138,6 +145,15 @@ func (sm *FSSessionManager) OnHeartBeat(ev Event) {
}
}
// Called on freeswitch's answer event
func (sm *FSSessionManager) OnChannelPark(ev Event) {
if sm.sessionDelegate != nil {
sm.sessionDelegate.OnChannelPark(ev, sm)
} else {
timespans.Logger.Info("park")
}
}
// Called on freeswitch's answer event
func (sm *FSSessionManager) OnChannelAnswer(ev Event) {
if sm.sessionDelegate != nil {

View File

@@ -21,6 +21,7 @@ package sessionmanager
import (
"fmt"
"github.com/cgrates/cgrates/timespans"
"strings"
"time"
)
@@ -34,6 +35,35 @@ func (rsd *SessionDelegate) OnHeartBeat(ev Event) {
timespans.Logger.Info("freeswitch ♥")
}
func (rsd *SessionDelegate) OnChannelPark(ev Event, sm SessionManager) {
startTime, err := ev.GetStartTime()
if err != nil {
timespans.Logger.Err("Error parsing answer event start time, using time.Now!")
startTime = time.Now()
}
// if there is no account configured leave the call alone
if strings.TrimSpace(ev.GetAccount()) == "" {
sm.UnparkCall(ev.GetUUID(), ev.GetDestination())
}
cd := timespans.CallDescriptor{
Direction: ev.GetDirection(),
Tenant: ev.GetTenant(),
TOR: ev.GetTOR(),
Subject: ev.GetSubject(),
Account: ev.GetAccount(),
Destination: ev.GetDestination(),
TimeStart: startTime}
var remainingTime float64
err = rsd.Connector.GetMaxSessionTime(cd, &remainingTime)
if err != nil {
timespans.Logger.Err(fmt.Sprintf("Could not get max session time for %v: %v", ev.GetUUID(), err))
}
if remainingTime == 0 {
timespans.Logger.Info(fmt.Sprintf("Not enough credit for trasferring the call %v.", ev.GetUUID()))
}
sm.UnparkCall(ev.GetUUID(), ev.GetDestination())
}
func (rsd *SessionDelegate) OnChannelAnswer(ev Event, s *Session) {
timespans.Logger.Info("freeswitch answer")
}

View File

@@ -24,6 +24,7 @@ import (
type SessionManager interface {
DisconnectSession(*Session)
UnparkCall(string, string)
GetSessionDelegate() *SessionDelegate
GetDbLogger() timespans.DataStorage
}

View File

@@ -286,6 +286,7 @@ func (cd *CallDescriptor) GetCost() (*CallCost, error) {
Returns the approximate max allowed session for user balance. It will try the max amount received in the call descriptor
and will decrease it by 10% for nine times. So if the user has little credit it will still allow 10% of the initial amount.
If the user has no credit then it will return 0.
If the user has postpayied plan it returns -1.
*/
func (cd *CallDescriptor) GetMaxSessionTime() (seconds float64, err error) {
_, err = cd.LoadActivationPeriods()