Merge branch 'rpc_jae' into develop

Conflicts:
	node/node.go
	rpc/core/accounts.go
	rpc/core_client/client.go
	rpc/handlers.go
	rpc/http_server.go
	rpc/test/helpers.go
	rpc/test/http_rpc_test.go
	rpc/test/json_rpc_test.go
This commit is contained in:
Jae Kwon
2015-04-08 14:27:03 -07:00
20 changed files with 892 additions and 657 deletions

View File

@ -5,14 +5,51 @@ import (
"fmt"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/rpc"
// NOTE: do not import rpc/core.
// What kind of client imports all of core logic? :P
"io/ioutil"
"net/http"
"net/url"
"reflect"
//"reflect"
// Uncomment to use go:generate
// _ "github.com/tendermint/go-rpc-gen"
)
// maps camel-case function names to lower case rpc version
var reverseFuncMap = map[string]string{
"Status": "status",
"NetInfo": "net_info",
"BlockchainInfo": "blockchain",
"GetBlock": "get_block",
"GetAccount": "get_account",
"GetStorage": "get_storage",
"Call": "call",
"CallCode": "call_code",
"ListValidators": "list_validators",
"DumpStorage": "dump_storage",
"BroadcastTx": "broadcast_tx",
"ListAccounts": "list_accounts",
"GenPrivAccount": "unsafe/gen_priv_account",
"SignTx": "unsafe/sign_tx",
}
/*
// fill the map from camelcase to lowercase
func fillReverseFuncMap() map[string]string {
fMap := make(map[string]string)
for name, f := range core.Routes {
camelName := runtime.FuncForPC(f.f.Pointer()).Name()
spl := strings.Split(camelName, ".")
if len(spl) > 1 {
camelName = spl[len(spl)-1]
}
fMap[camelName] = name
}
return fMap
}
*/
type Response struct {
Status string
Data interface{}
@ -39,67 +76,21 @@ func NewClient(addr, typ string) Client {
return nil
}
func argsToJson(args ...interface{}) ([][]string, error) {
func argsToJson(args ...interface{}) ([]string, error) {
l := len(args)
jsons := make([][]string, l)
jsons := make([]string, l)
n, err := new(int64), new(error)
for i, a := range args {
//if its a slice, we serliaze separately and pack into a slice of strings
// otherwise its a slice of length 1
if v := reflect.ValueOf(a); v.Kind() == reflect.Slice {
ty := v.Type()
rt := ty.Elem()
if rt.Kind() == reflect.Uint8 {
buf := new(bytes.Buffer)
binary.WriteJSON(a, buf, n, err)
if *err != nil {
return nil, *err
}
jsons[i] = []string{string(buf.Bytes())}
} else {
slice := make([]string, v.Len())
for j := 0; j < v.Len(); j++ {
buf := new(bytes.Buffer)
binary.WriteJSON(v.Index(j).Interface(), buf, n, err)
if *err != nil {
return nil, *err
}
slice[j] = string(buf.Bytes())
}
jsons[i] = slice
}
} else {
buf := new(bytes.Buffer)
binary.WriteJSON(a, buf, n, err)
if *err != nil {
return nil, *err
}
jsons[i] = []string{string(buf.Bytes())}
buf := new(bytes.Buffer)
binary.WriteJSON(a, buf, n, err)
if *err != nil {
return nil, *err
}
jsons[i] = string(buf.Bytes())
}
return jsons, nil
}
func (c *ClientHTTP) RequestResponse(method string, values url.Values) (*Response, error) {
resp, err := http.PostForm(c.addr+method, values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
response := new(Response)
fmt.Println(string(body))
binary.ReadJSON(response, body, &err)
if err != nil {
return nil, err
}
fmt.Println(response.Data)
return response, nil
}
func (c *ClientJSON) RequestResponse(s rpc.RPCRequest) (b []byte, err error) {
b = binary.JSONBytes(s)
buf := bytes.NewBuffer(b)
@ -154,10 +145,10 @@ func argsToURLValues(argNames []string, args ...interface{}) (url.Values, error)
}
for i, name := range argNames {
s := slice[i]
values.Set(name, s[0])
for i := 1; i < len(s); i++ {
values.Add(name, s[i])
}
values.Set(name, s) // s[0]
/*for j := 1; j < len(s); j++ {
values.Add(name, s[j])
}*/
}
return values, nil
}
@ -177,7 +168,7 @@ fmt
/*rpc-gen:template:*ClientJSON func (c *ClientJSON) {{name}}({{args.def}}) ({{response}}) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: {{lowername}},
Method: reverseFuncMap["{{name}}"],
Params: []interface{}{ {{args.ident}} },
Id: 0,
}
@ -206,7 +197,7 @@ fmt
if err != nil{
return nil, err
}
resp, err := http.PostForm(c.addr+{{lowername}}, values)
resp, err := http.PostForm(c.addr+reverseFuncMap["{{name}}"], values)
if err != nil {
return nil, err
}