diff --git a/cmd/inquirer/http_responder.go b/cmd/inquirer/http_responder.go index f2d0c73da..92dd3cebb 100644 --- a/cmd/inquirer/http_responder.go +++ b/cmd/inquirer/http_responder.go @@ -43,7 +43,7 @@ func getCostHandler(w http.ResponseWriter, r *http.Request) { } /* -curl "http://127.0.0.1:8000/getcost?cstmid=vdf&subj=rif&dest=0257@amount=100" +curl "http://127.0.0.1:8000/debitbalance?cstmid=vdf&subj=rif&dest=0257@amount=100" */ func debitBalanceHandler(w http.ResponseWriter, r *http.Request) { enc := json.NewEncoder(w) @@ -58,12 +58,12 @@ func debitBalanceHandler(w http.ResponseWriter, r *http.Request) { return } arg := ×pans.CallDescriptor{CstmId: cstmid[0], Subject: subj[0], DestinationPrefix: dest[0], Amount: amount} - result := Debit(arg, "Storage.DebitCents") + result := CallMethod(arg, "Storage.DebitCents") enc.Encode(result) } /* -curl "http://127.0.0.1:8000/getcost?cstmid=vdf&subj=rif&dest=0257@amount=100" +curl "http://127.0.0.1:8000/debitsms?cstmid=vdf&subj=rif&dest=0257@amount=100" */ func debitSMSHandler(w http.ResponseWriter, r *http.Request) { enc := json.NewEncoder(w) @@ -78,12 +78,12 @@ func debitSMSHandler(w http.ResponseWriter, r *http.Request) { return } arg := ×pans.CallDescriptor{CstmId: cstmid[0], Subject: subj[0], DestinationPrefix: dest[0], Amount: amount} - result := Debit(arg, "Storage.DebitSMS") + result := CallMethod(arg, "Storage.DebitSMS") enc.Encode(result) } /* -curl "http://127.0.0.1:8000/getcost?cstmid=vdf&subj=rif&dest=0257@amount=100" +curl "http://127.0.0.1:8000/debitseconds?cstmid=vdf&subj=rif&dest=0257@amount=100" */ func debitSecondsHandler(w http.ResponseWriter, r *http.Request) { enc := json.NewEncoder(w) @@ -98,7 +98,27 @@ func debitSecondsHandler(w http.ResponseWriter, r *http.Request) { return } arg := ×pans.CallDescriptor{CstmId: cstmid[0], Subject: subj[0], DestinationPrefix: dest[0], Amount: amount} - result := Debit(arg, "Storage.DebitSeconds") + result := CallMethod(arg, "Storage.DebitSeconds") + enc.Encode(result) +} + +/* +curl "http://127.0.0.1:8000/getmaxsessiontime?cstmid=vdf&subj=rif&dest=0257@amount=100" +*/ +func getMaxSessionTimeHandler(w http.ResponseWriter, r *http.Request) { + enc := json.NewEncoder(w) + r.ParseForm() + cstmid, ok1 := r.Form["cstmid"] + subj, ok2 := r.Form["subj"] + dest, ok3 := r.Form["dest"] + amount_s, ok4 := r.Form["amount"] + amount, err := strconv.ParseFloat(amount_s[0], 64) + if !ok1 || !ok2 || !ok3 || !ok4 || err != nil { + enc.Encode(IncorrectParameters{"Incorrect parameters"}) + return + } + arg := ×pans.CallDescriptor{CstmId: cstmid[0], Subject: subj[0], DestinationPrefix: dest[0], Amount: amount} + result := CallMethod(arg, "Storage.GetMaxSessionTime") enc.Encode(result) } @@ -108,6 +128,7 @@ func listenToHttpRequests() { http.HandleFunc("/debitbalance", debitBalanceHandler) http.HandleFunc("/debitsms", debitSMSHandler) http.HandleFunc("/debitseconds", debitSecondsHandler) + http.HandleFunc("/getmaxsessiontime", debitSecondsHandler) log.Print("The server is listening on ", *httpApiAddress) http.ListenAndServe(*httpApiAddress, nil) } diff --git a/cmd/inquirer/inquirer.go b/cmd/inquirer/inquirer.go index 5dfcb6d16..f11296899 100644 --- a/cmd/inquirer/inquirer.go +++ b/cmd/inquirer/inquirer.go @@ -40,7 +40,7 @@ func GetCost(key *timespans.CallDescriptor) (reply *timespans.CallCost) { /* The function that gets the information from the raters using balancer. */ -func Debit(key *timespans.CallDescriptor, method string) (reply float64) { +func CallMethod(key *timespans.CallDescriptor, method string) (reply float64) { err := errors.New("") //not nil value for err != nil { client := raterList.Balance() diff --git a/cmd/inquirer/jsonrpc_responder.go b/cmd/inquirer/jsonrpc_responder.go index 8c86037fd..545e8eb0d 100644 --- a/cmd/inquirer/jsonrpc_responder.go +++ b/cmd/inquirer/jsonrpc_responder.go @@ -19,17 +19,22 @@ func (r *Responder) GetGost(arg timespans.CallDescriptor, replay *timespans.Call } func (r *Responder) DebitBalance(arg timespans.CallDescriptor, replay *float64) (err error) { - *replay = Debit(&arg, "Storage.DebitCents") + *replay = CallMethod(&arg, "Storage.DebitCents") return } func (r *Responder) DebitSMS(arg timespans.CallDescriptor, replay *float64) (err error) { - *replay = Debit(&arg, "Storage.DebitSMS") + *replay = CallMethod(&arg, "Storage.DebitSMS") return } func (r *Responder) DebitSeconds(arg timespans.CallDescriptor, replay *float64) (err error) { - *replay = Debit(&arg, "Storage.DebitSeconds") + *replay = CallMethod(&arg, "Storage.DebitSeconds") + return +} + +func (r *Responder) GetMaxSessionTime(arg timespans.CallDescriptor, replay *float64) (err error) { + *replay = CallMethod(&arg, "Storage.GetMaxSessionTime") return } diff --git a/cmd/rater/rater.go b/cmd/rater/rater.go index 568f71891..28e6cb06a 100644 --- a/cmd/rater/rater.go +++ b/cmd/rater/rater.go @@ -58,6 +58,14 @@ func (s *Storage) DebitSeconds(cd timespans.CallDescriptor, reply *float64) (err return err } +func (s *Storage) GetMaxSessionTime(cd timespans.CallDescriptor, reply *float64) (err error) { + descriptor := &cd + descriptor.StorageGetter = s.sg + r, e := descriptor.GetMaxSessionTime() + *reply, err = r, e + return err +} + /* RPC method that trigers rater shutdown in case of server exit. */ diff --git a/timespans/calldesc.go b/timespans/calldesc.go index 9197e9022..31fb5bb24 100644 --- a/timespans/calldesc.go +++ b/timespans/calldesc.go @@ -207,16 +207,16 @@ func (cd *CallDescriptor) getPresentSecondCost() (cost float64, err error) { /* Returns the cost of a second in the present time conditions. */ -func (cd *CallDescriptor) GetMaxSessionTime(maxSessionSeconds int) (seconds int, err error) { +func (cd *CallDescriptor) GetMaxSessionTime() (seconds float64, err error) { _, err = cd.RestoreFromStorage() now := time.Now() availableCredit := 0.0 if userBudget, err := cd.StorageGetter.GetUserBudget(cd.Subject); err == nil && userBudget != nil { availableCredit = userBudget.Credit } else { - return maxSessionSeconds, err + return cd.Amount, err } - orig_maxSessionSeconds := maxSessionSeconds + maxSessionSeconds := cd.Amount for i := 0; i < 10; i++ { maxDuration, _ := time.ParseDuration(fmt.Sprintf("%vs", maxSessionSeconds)) ts := &TimeSpan{TimeStart: now, TimeEnd: now.Add(maxDuration)} @@ -232,7 +232,7 @@ func (cd *CallDescriptor) GetMaxSessionTime(maxSessionSeconds int) (seconds int, if cost < availableCredit { return maxSessionSeconds, nil } else { //decrease the period by 10% and try again - maxSessionSeconds -= int(float64(orig_maxSessionSeconds) * 0.1) + maxSessionSeconds -= cd.Amount * 0.1 } } return 0, nil diff --git a/timespans/calldesc_test.go b/timespans/calldesc_test.go index 459b5ecad..ac58bdea8 100644 --- a/timespans/calldesc_test.go +++ b/timespans/calldesc_test.go @@ -331,3 +331,69 @@ func BenchmarkMongoGetCost(b *testing.B) { cd.GetCost() } } + +func BenchmarkKyotoSingleGetSessionTime(b *testing.B) { + b.StopTimer() + getter, _ := NewKyotoStorage("test.kch") + defer getter.Close() + cd := &CallDescriptor{CstmId: "vdf", Subject: "minutosu", DestinationPrefix: "0723", StorageGetter: getter} + b.StartTimer() + for i := 0; i < b.N; i++ { + cd.GetMaxSessionTime(100) + } +} + +func BenchmarkKyotoMultipleGetSessionTime(b *testing.B) { + b.StopTimer() + getter, _ := NewKyotoStorage("test.kch") + defer getter.Close() + cd := &CallDescriptor{CstmId: "vdf", Subject: "minutosu", DestinationPrefix: "0723", StorageGetter: getter} + b.StartTimer() + for i := 0; i < b.N; i++ { + cd.GetMaxSessionTime(5400) + } +} + +func BenchmarkRedisSingleGetSessionTime(b *testing.B) { + b.StopTimer() + getter, _ := NewRedisStorage("", 10) + defer getter.Close() + cd := &CallDescriptor{CstmId: "vdf", Subject: "minutosu", DestinationPrefix: "0723", StorageGetter: getter} + b.StartTimer() + for i := 0; i < b.N; i++ { + cd.GetMaxSessionTime(100) + } +} + +func BenchmarkRedisMultipleGetSessionTime(b *testing.B) { + b.StopTimer() + getter, _ := NewRedisStorage("", 10) + defer getter.Close() + cd := &CallDescriptor{CstmId: "vdf", Subject: "minutosu", DestinationPrefix: "0723", StorageGetter: getter} + b.StartTimer() + for i := 0; i < b.N; i++ { + cd.GetMaxSessionTime(5400) + } +} + +func BenchmarkMongoSingleGetSessionTime(b *testing.B) { + b.StopTimer() + getter, _ := NewMongoStorage("127.0.0.1", "test") + defer getter.Close() + cd := &CallDescriptor{CstmId: "vdf", Subject: "minutosu", DestinationPrefix: "0723", StorageGetter: getter} + b.StartTimer() + for i := 0; i < b.N; i++ { + cd.GetMaxSessionTime(100) + } +} + +func BenchmarkMongoMultipleGetSessionTime(b *testing.B) { + b.StopTimer() + getter, _ := NewMongoStorage("127.0.0.1", "test") + defer getter.Close() + cd := &CallDescriptor{CstmId: "vdf", Subject: "minutosu", DestinationPrefix: "0723", StorageGetter: getter} + b.StartTimer() + for i := 0; i < b.N; i++ { + cd.GetMaxSessionTime(5400) + } +}