Completing GetBalance command, fixup reflect code to use Interface instead of String method

This commit is contained in:
DanB
2012-09-13 20:49:07 +02:00
parent e0fe75a2b0
commit 98a0a91763
5 changed files with 50 additions and 14 deletions

View File

@@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package main
import (
"console"
"flag"
"fmt"
"log"
@@ -27,7 +26,8 @@ import (
"net/rpc/jsonrpc"
"os"
"reflect"
"timespans"
"github.com/cgrates/cgrates/timespans"
"github.com/cgrates/cgrates/console"
)
var (
@@ -64,7 +64,6 @@ func main() {
if rpcErr := client.Call(cmd.RpcMethod(), cmd.RpcParams(), res); rpcErr != nil {
log.Fatal(rpcErr)
}
fmt.Println(reflect.ValueOf(res).Elem().String())
fmt.Println(reflect.ValueOf(res).Elem().Interface())
}

View File

@@ -7,30 +7,42 @@ import (
"path/filepath"
)
type PrmsGetBalance struct {
// Data being sent to the rpc responder
type ArgsGetBalance struct {
Tenant string
User string
Direction string
BalanceTag string
BalanceId string
}
// Received back from query
type ReplyGetBalance struct {
Tenant string
User string
Direction string
BalanceId string
Balance float64
}
// Commander implementation
type CmdGetBalance struct {
rpcMethod string
rpcParams PrmsGetBalance
rpcResult string
rpcParams ArgsGetBalance
rpcResult ReplyGetBalance
idxArgsToRpcPrms map[int]string
}
// name should be exec's name
func (self *CmdGetBalance) Usage(name string) string {
return fmt.Sprintf("\n\tUsage: %s get_balance <tenant> <user> [<balance_tag> [<direction>]]", name)
return fmt.Sprintf("\n\tUsage: %s [cfg_opts...{-h}] get_balance <tenant> <user> [<balanceid> [<direction>]]", name)
}
// set param defaults
func (self *CmdGetBalance) defaults() error {
self.idxArgsToRpcPrms = map[int]string{2: "Tenant", 3: "User", 4: "BalanceTag", 5:"Direction" }
self.idxArgsToRpcPrms = map[int]string{2: "Tenant", 3: "User", 4: "BalanceId", 5:"Direction" }
self.rpcMethod = "Responder.GetBalance"
self.rpcParams.BalanceTag = "MONETARY"
self.rpcParams.BalanceId = "MONETARY"
self.rpcParams.Direction = "OUT"
return nil
}

View File

@@ -35,7 +35,7 @@ func CmdRpcPrmsFromArgs(rpcPrms interface{}, args []string, idxArgsToRpcPrms map
// Process args and return right command Value or error
func GetCommandValue(args []string) (Commander, error) {
if len(args) < 2 {
return nil, fmt.Errorf("\n\tUsage: %s <command>\n", filepath.Base(args[0]))
return nil, fmt.Errorf("\n\tUsage: %s [cfg_opts...{-h}] <command>\n", filepath.Base(args[0]))
}
cmd := args[1]
var cmdVal Commander
@@ -45,7 +45,7 @@ func GetCommandValue(args []string) (Commander, error) {
case "get_balance":
cmdVal = &CmdGetBalance{}
default:
return nil, fmt.Errorf("\n\tUsage: %s <status|get_balance>\n", filepath.Base(args[0]))
return nil, fmt.Errorf("\n\tUsage: %s [cfg_opts...{-h}] <status|get_balance>\n", filepath.Base(args[0]))
}
if err := cmdVal.FromArgs(args); err != nil {
return nil, err

View File

@@ -11,7 +11,7 @@ type CmdStatus struct {
}
func (self *CmdStatus) Usage(name string) string {
return fmt.Sprintf("\n\tUsage: %s status", name)
return fmt.Sprintf("\n\tUsage: %s [cfg_opts...{-h}] status", name)
}
func (self *CmdStatus) defaults() error {

View File

@@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"github.com/rif/balancer2go"
"github.com/cgrates/cgrates/console"
"net/rpc"
"reflect"
"runtime"
@@ -175,6 +176,29 @@ func (rs *Responder) Shutdown(arg string, reply *string) (err error) {
return
}
// Get balance
func (rs *Responder) GetBalance(arg console.ArgsGetBalance, reply *console.ReplyGetBalance) (err error) {
if rs.Bal != nil {
return fmt.Errorf("No balancer supported for this command right now")
}
ubKey := arg.Direction+":"+arg.Tenant+":"+arg.User
userBalance, err := storageGetter.GetUserBalance(ubKey)
if err != nil {
return err
}
if balance,balExists := userBalance.BalanceMap[arg.BalanceId]; !balExists {
// No match, balanceId not found
return fmt.Errorf("-BALANCE_NOT_FOUND")
} else {
reply.Tenant = arg.Tenant
reply.User = arg.User
reply.Direction = arg.Direction
reply.BalanceId = arg.BalanceId
reply.Balance = balance
}
return nil
}
/*
The function that gets the information from the raters using balancer.
*/
@@ -322,3 +346,4 @@ func (rcc *RPCClientConnector) DebitSeconds(cd CallDescriptor, resp *float64) er
func (rcc *RPCClientConnector) GetMaxSessionTime(cd CallDescriptor, resp *float64) error {
return rcc.Client.Call("Responder.GetMaxSessionTime", cd, resp)
}