From 0d5e2ede135b3508fb40d28d4e272ffbac667fd3 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 16 May 2012 16:06:32 +0300 Subject: [PATCH] added postpaid/prepaid flag --- data/test.kch | Bin 6298952 -> 6298904 bytes data/test_activation_periods.json | 2 +- data/test_destinations.json | 3 +-- data/test_userbudgets.json | 6 +++--- sessionmanager/sessiondelegate.go | 10 ++++++++++ timespans/calldesc.go | 12 ++++++++---- timespans/userbudget.go | 26 +++++++++++++++++--------- 7 files changed, 40 insertions(+), 19 deletions(-) diff --git a/data/test.kch b/data/test.kch index b6fe7ab2cf638a2cd8bb13c9571284c97c3ede03..44ca14e5fb13d30d4000c5af11127ed1b468d536 100644 GIT binary patch delta 505 zcmX@HtATk25KmC&g7jI9V|8y)&LF!L}cAViQgB8(uG1+;4Q8K8kP8x6M_GG<=|G6P{| zPqhQ`a$vk9pu;dt-RSW5F;GLRJoOC8r3`=oEy}Fcry~?dA(ZfH)M0!+FeFtPg1Y5!vk(5(yU>MZxh1Be+!!tqcte45qJg7jNJ%j8y)&LF!L}cB1DiiB8(uG1+;4Q8K8ml8x6M_GG<=|GJ{}d zPqhQ`ieS9Wvh4?txd5>%5W4}fI}m#SvFG-K$Gn27fNJYtYLkGTAi)JkU4WXS5t@OX zG~I4lWdG(4P&^kQPQ0No%Qrf>=>yG3#WW*Xv)z1Q2oQ$?aTpMX191cpM*?xwcJqbN zQYRRvBI^SN)e+h46%q*-8D&|)@d+jPR1K{R4Gavdic^cqGLuuM%Sa_?P4AFOxT(gD dtd5I4ttd6MxID8YIRmQn9aw&QymZ1&MgYOqxxD}Y diff --git a/data/test_activation_periods.json b/data/test_activation_periods.json index cb1a8d2ee..8b4d86bbb 100644 --- a/data/test_activation_periods.json +++ b/data/test_activation_periods.json @@ -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":""} ] diff --git a/data/test_destinations.json b/data/test_destinations.json index b2fdda58a..6d18e55cb 100644 --- a/data/test_destinations.json +++ b/data/test_destinations.json @@ -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"]} ] diff --git a/data/test_userbudgets.json b/data/test_userbudgets.json index b5b2dc5be..00917a13c 100644 --- a/data/test_userbudgets.json +++ b/data/test_userbudgets.json @@ -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"}]} ] diff --git a/sessionmanager/sessiondelegate.go b/sessionmanager/sessiondelegate.go index c9905b757..94830a84f 100644 --- a/sessionmanager/sessiondelegate.go +++ b/sessionmanager/sessiondelegate.go @@ -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!") } diff --git a/timespans/calldesc.go b/timespans/calldesc.go index 92d215c57..8882127ca 100644 --- a/timespans/calldesc.go +++ b/timespans/calldesc.go @@ -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 } diff --git a/timespans/userbudget.go b/timespans/userbudget.go index 92eb72510..d47e43406 100644 --- a/timespans/userbudget.go +++ b/timespans/userbudget.go @@ -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)