added postpaid/prepaid flag

This commit is contained in:
Radu Ioan Fericean
2012-05-16 16:06:32 +03:00
parent 02e73e3680
commit 0d5e2ede13
7 changed files with 40 additions and 19 deletions

Binary file not shown.

View File

@@ -30,7 +30,7 @@
}
]
},
{"TOR": "0","CstmId":"1","Subject":"1000","DestinationPrefix":"service", "ActivationPeriods": [
{"TOR": "0","CstmId":"1","Subject":"1000","DestinationPrefix":"0723", "ActivationPeriods": [
{"ActivationTime": "2012-01-01T00:00:00Z", "Intervals": [
{"BillingUnit":60,"ConnectFee":0,"Month":0,"MonthDay":0,"Ponder":0,"Price":1,"StartTime":"","EndTime":""}
]

View File

@@ -2,6 +2,5 @@
{"Id":"nationale", "Prefixes":["0256","0257","0723","0740"]},
{"Id":"retea", "Prefixes":["0723","0724"]},
{"Id":"mobil", "Prefixes":["0723","0740"]},
{"Id":"radu", "Prefixes":["0723045326"]},
{"Id":"freeswitch", "Prefixes":["service"]}
{"Id":"radu", "Prefixes":["0723045326"]}
]

View File

@@ -1,11 +1,11 @@
[
{"Id":"minutosu","Credit":21,"SmsCredit":0,"Traffic":0,"VolumeDiscountSeconds":0,"ReceivedCallSeconds":0,"ResetDayOfTheMonth":10,"TariffPlanId":"seara","MinuteBuckets":
{"Id":"minutosu","Type":"prepaid","Credit":21,"SmsCredit":0,"Traffic":0,"VolumeDiscountSeconds":0,"ReceivedCallSeconds":0,"ResetDayOfTheMonth":10,"TariffPlanId":"seara","MinuteBuckets":
[{"Seconds":10,"Priority":10,"Price":0.01,"DestinationId":"nationale"},
{"Seconds":100,"Priority":20,"Price":0,"DestinationId":"retea"}]},
{"Id":"broker","Credit":0,"SmsCredit":0,"Traffic":0,"VolumeDiscountSeconds":0,"ReceivedCallSeconds":0,"ResetDayOfTheMonth":10,"TariffPlanId":"seara","MinuteBuckets":
{"Id":"broker","Type":"prepaid","Credit":0,"SmsCredit":0,"Traffic":0,"VolumeDiscountSeconds":0,"ReceivedCallSeconds":0,"ResetDayOfTheMonth":10,"TariffPlanId":"seara","MinuteBuckets":
[{"Seconds":10,"Priority":10,"Price":0.01,"DestinationId":"nationale"},
{"Seconds":100,"Priority":20,"Price":0,"DestinationId":"retea"}]},
{"Id":"1000","Credit":0,"SmsCredit":0,"Traffic":0,"VolumeDiscountSeconds":0,"ReceivedCallSeconds":0,"ResetDayOfTheMonth":10,"TariffPlanId":"seara","MinuteBuckets":
{"Id":"1000","Type":"postpaid","Credit":0,"SmsCredit":0,"Traffic":0,"VolumeDiscountSeconds":0,"ReceivedCallSeconds":0,"ResetDayOfTheMonth":10,"TariffPlanId":"seara","MinuteBuckets":
[{"Seconds":10,"Priority":10,"Price":0.01,"DestinationId":"nationale"},
{"Seconds":100,"Priority":20,"Price":0,"DestinationId":"retea"}]}
]

View File

@@ -58,11 +58,17 @@ func (dsd *DirectSessionDelegate) OnChannelAnswer(ev *Event, s *Session) {
s.callDescriptor.Amount = DEBIT_PERIOD.Seconds()
s.callDescriptor.SetStorageGetter(storageGetter)
remainingSeconds, err := s.callDescriptor.GetMaxSessionTime()
if remainingSeconds == -1 || err == nil {
log.Print("Postpaying client: happy talking!")
return
}
if remainingSeconds == 0 || err != nil {
log.Print("No credit left: Disconnect!")
return
}
if remainingSeconds < DEBIT_PERIOD.Seconds() || err != nil {
log.Print("Not enough money for a debit period!")
return
}
}
@@ -79,6 +85,10 @@ func (dsd *DirectSessionDelegate) LoopAction(s *Session, cd *timespans.CallDescr
s.CallCosts = append(s.CallCosts, cc)
cd.Amount = DEBIT_PERIOD.Seconds()
remainingSeconds, err := cd.GetMaxSessionTime()
if remainingSeconds == -1 || err == nil {
log.Print("Postpaying client: happy talking!")
return
}
if remainingSeconds == 0 || err != nil {
log.Print("No credit left: Disconnect!")
}

View File

@@ -260,10 +260,14 @@ func (cd *CallDescriptor) GetMaxSessionTime() (seconds float64, err error) {
now := time.Now()
availableCredit, availableSeconds := 0.0, 0.0
if userBudget, err := cd.getUserBudget(); err == nil && userBudget != nil {
userBudget.mux.RLock()
availableCredit = userBudget.Credit
availableSeconds, _ = userBudget.getSecondsForPrefix(cd.storageGetter, cd.DestinationPrefix)
userBudget.mux.RUnlock()
if userBudget.Type == UB_TYPE_POSTPAID {
return -1, nil
} else {
userBudget.mux.RLock()
availableCredit = userBudget.Credit
availableSeconds, _ = userBudget.getSecondsForPrefix(cd.storageGetter, cd.DestinationPrefix)
userBudget.mux.RUnlock()
}
} else {
return cd.Amount, err
}

View File

@@ -26,11 +26,17 @@ import (
"sync"
)
const (
UB_TYPE_POSTPAID = "postpaid"
UB_TYPE_PREPAID = "prepaid"
)
/*
Structure conatining information about user's credit (minutes, cents, sms...).'
*/
type UserBudget struct {
Id string
Type string // prepaid/postpaid
Credit float64
SmsCredit float64
Traffic float64
@@ -75,6 +81,7 @@ func (bs bucketsorter) Less(j, i int) bool {
Serializes the user budget for the storage. Used for key-value storages.
*/
func (ub *UserBudget) store() (result string) {
result += ub.Type + ";"
result += strconv.FormatFloat(ub.Credit, 'f', -1, 64) + ";"
result += strconv.FormatFloat(ub.SmsCredit, 'f', -1, 64) + ";"
result += strconv.FormatFloat(ub.Traffic, 'f', -1, 64) + ";"
@@ -99,15 +106,16 @@ De-serializes the user budget for the storage. Used for key-value storages.
*/
func (ub *UserBudget) restore(input string) {
elements := strings.Split(input, ";")
ub.Credit, _ = strconv.ParseFloat(elements[0], 64)
ub.SmsCredit, _ = strconv.ParseFloat(elements[1], 64)
ub.Traffic, _ = strconv.ParseFloat(elements[2], 64)
ub.VolumeDiscountSeconds, _ = strconv.ParseFloat(elements[3], 64)
ub.ReceivedCallSeconds, _ = strconv.ParseFloat(elements[4], 64)
ub.ResetDayOfTheMonth, _ = strconv.Atoi(elements[5])
ub.TariffPlanId = elements[6]
if len(elements) > 7 {
for _, mbs := range strings.Split(elements[7], ",") {
ub.Type = elements[0]
ub.Credit, _ = strconv.ParseFloat(elements[1], 64)
ub.SmsCredit, _ = strconv.ParseFloat(elements[2], 64)
ub.Traffic, _ = strconv.ParseFloat(elements[3], 64)
ub.VolumeDiscountSeconds, _ = strconv.ParseFloat(elements[4], 64)
ub.ReceivedCallSeconds, _ = strconv.ParseFloat(elements[5], 64)
ub.ResetDayOfTheMonth, _ = strconv.Atoi(elements[6])
ub.TariffPlanId = elements[7]
if len(elements) > 8 {
for _, mbs := range strings.Split(elements[8], ",") {
mb := &MinuteBucket{}
mb.restore(mbs)
ub.MinuteBuckets = append(ub.MinuteBuckets, mb)