2019-07-03 17:39:07 +00:00
|
|
|
package jsonrpc
|
2019-06-28 13:49:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-06-28 14:53:01 +00:00
|
|
|
"context"
|
2019-06-28 13:49:34 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-06-28 14:05:10 +00:00
|
|
|
"fmt"
|
2019-06-28 13:49:34 +00:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
2019-07-03 14:32:31 +00:00
|
|
|
"sync/atomic"
|
2019-06-28 13:49:34 +00:00
|
|
|
)
|
|
|
|
|
2019-06-28 14:53:01 +00:00
|
|
|
var (
|
2019-07-01 20:00:22 +00:00
|
|
|
errorType = reflect.TypeOf(new(error)).Elem()
|
2019-06-28 14:53:01 +00:00
|
|
|
contextType = reflect.TypeOf(new(context.Context)).Elem()
|
|
|
|
)
|
|
|
|
|
2019-07-02 19:08:30 +00:00
|
|
|
// ErrClient is an error which occurred on the client side the library
|
2019-06-28 14:05:10 +00:00
|
|
|
type ErrClient struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrClient) Error() string {
|
|
|
|
return fmt.Sprintf("RPC client error: %s", e.err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 17:45:03 +00:00
|
|
|
// Unwrap unwraps the actual error
|
2019-06-28 14:05:10 +00:00
|
|
|
func (e *ErrClient) Unwrap(err error) error {
|
|
|
|
return e.err
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:49:34 +00:00
|
|
|
type result reflect.Value
|
|
|
|
|
|
|
|
func (r *result) UnmarshalJSON(raw []byte) error {
|
|
|
|
return json.Unmarshal(raw, reflect.Value(*r).Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientResponse struct {
|
|
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
|
|
Result result `json:"result"`
|
2019-07-02 19:08:30 +00:00
|
|
|
ID int64 `json:"id"`
|
2019-06-28 13:49:34 +00:00
|
|
|
Error *respError `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-07-03 12:30:21 +00:00
|
|
|
// ClientCloser is used to close Client from further use
|
|
|
|
type ClientCloser func()
|
|
|
|
|
2019-07-02 17:45:03 +00:00
|
|
|
// NewClient creates new josnrpc 2.0 client
|
|
|
|
//
|
|
|
|
// handler must be pointer to a struct with function fields
|
2019-07-03 14:32:31 +00:00
|
|
|
// Returned value closes the client connection
|
2019-07-02 17:45:03 +00:00
|
|
|
// TODO: Example
|
2019-07-03 12:30:21 +00:00
|
|
|
func NewClient(addr string, namespace string, handler interface{}) ClientCloser {
|
2019-06-28 13:49:34 +00:00
|
|
|
htyp := reflect.TypeOf(handler)
|
|
|
|
if htyp.Kind() != reflect.Ptr {
|
|
|
|
panic("expected handler to be a pointer")
|
|
|
|
}
|
|
|
|
typ := htyp.Elem()
|
|
|
|
if typ.Kind() != reflect.Struct {
|
|
|
|
panic("handler should be a struct")
|
|
|
|
}
|
|
|
|
|
|
|
|
val := reflect.ValueOf(handler)
|
|
|
|
|
2019-07-03 14:32:31 +00:00
|
|
|
var idCtr int64
|
2019-06-28 13:49:34 +00:00
|
|
|
|
|
|
|
for i := 0; i < typ.NumField(); i++ {
|
|
|
|
f := typ.Field(i)
|
|
|
|
ftyp := f.Type
|
|
|
|
if ftyp.Kind() != reflect.Func {
|
|
|
|
panic("handler field not a func")
|
|
|
|
}
|
|
|
|
|
|
|
|
valOut, errOut, nout := processFuncOut(ftyp)
|
|
|
|
|
2019-07-02 13:49:10 +00:00
|
|
|
processResponse := func(resp clientResponse, code int) []reflect.Value {
|
2019-06-28 13:49:34 +00:00
|
|
|
out := make([]reflect.Value, nout)
|
|
|
|
|
|
|
|
if valOut != -1 {
|
|
|
|
out[valOut] = reflect.Value(resp.Result).Elem()
|
|
|
|
}
|
|
|
|
if errOut != -1 {
|
2019-06-28 14:53:01 +00:00
|
|
|
out[errOut] = reflect.New(errorType).Elem()
|
2019-06-28 13:49:34 +00:00
|
|
|
if resp.Error != nil {
|
2019-07-02 13:49:10 +00:00
|
|
|
out[errOut].Set(reflect.ValueOf(resp.Error))
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-07-01 20:00:22 +00:00
|
|
|
processError := func(err error) []reflect.Value {
|
2019-06-28 14:05:10 +00:00
|
|
|
out := make([]reflect.Value, nout)
|
|
|
|
|
|
|
|
if valOut != -1 {
|
|
|
|
out[valOut] = reflect.New(ftyp.Out(valOut)).Elem()
|
|
|
|
}
|
|
|
|
if errOut != -1 {
|
2019-06-28 14:53:01 +00:00
|
|
|
out[errOut] = reflect.New(errorType).Elem()
|
2019-06-28 14:05:10 +00:00
|
|
|
out[errOut].Set(reflect.ValueOf(&ErrClient{err}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:53:01 +00:00
|
|
|
hasCtx := 0
|
|
|
|
if ftyp.NumIn() > 0 && ftyp.In(0) == contextType {
|
|
|
|
hasCtx = 1
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:49:34 +00:00
|
|
|
fn := reflect.MakeFunc(ftyp, func(args []reflect.Value) (results []reflect.Value) {
|
2019-07-03 14:32:31 +00:00
|
|
|
id := atomic.AddInt64(&idCtr, 1)
|
2019-07-01 20:00:22 +00:00
|
|
|
params := make([]param, len(args)-hasCtx)
|
2019-06-28 14:53:01 +00:00
|
|
|
for i, arg := range args[hasCtx:] {
|
2019-06-28 13:49:34 +00:00
|
|
|
params[i] = param{
|
|
|
|
v: arg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req := request{
|
|
|
|
Jsonrpc: "2.0",
|
2019-07-02 19:08:30 +00:00
|
|
|
ID: &id,
|
2019-06-28 13:49:34 +00:00
|
|
|
Method: namespace + "." + f.Name,
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(&req)
|
|
|
|
if err != nil {
|
2019-06-28 14:05:10 +00:00
|
|
|
return processError(err)
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:53:01 +00:00
|
|
|
// prepare / execute http request
|
|
|
|
|
|
|
|
hreq, err := http.NewRequest("POST", addr, bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
return processError(err)
|
|
|
|
}
|
|
|
|
if hasCtx == 1 {
|
|
|
|
hreq = hreq.WithContext(args[0].Interface().(context.Context))
|
|
|
|
}
|
|
|
|
hreq.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
httpResp, err := http.DefaultClient.Do(hreq)
|
2019-06-28 13:49:34 +00:00
|
|
|
if err != nil {
|
2019-06-28 14:05:10 +00:00
|
|
|
return processError(err)
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:53:01 +00:00
|
|
|
// process response
|
|
|
|
|
2019-06-28 13:49:34 +00:00
|
|
|
var resp clientResponse
|
|
|
|
if valOut != -1 {
|
|
|
|
resp.Result = result(reflect.New(ftyp.Out(valOut)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
|
2019-06-28 14:05:10 +00:00
|
|
|
return processError(err)
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:08:30 +00:00
|
|
|
if err := httpResp.Body.Close(); err != nil {
|
|
|
|
return processError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.ID != *req.ID {
|
2019-06-28 14:05:10 +00:00
|
|
|
return processError(errors.New("request and response id didn't match"))
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 13:49:10 +00:00
|
|
|
return processResponse(resp, httpResp.StatusCode)
|
2019-06-28 13:49:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
val.Elem().Field(i).Set(fn)
|
|
|
|
}
|
2019-07-03 14:32:31 +00:00
|
|
|
|
|
|
|
// TODO: if this is still unused as of 2020, remove the closer stuff
|
|
|
|
return func() {} // noop for now, not for long though
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|