rpc-go/protocol/json/server_request.go

174 lines
4.8 KiB
Go
Raw Permalink Normal View History

2018-04-03 08:58:26 +00:00
package json
import (
"encoding/json"
2018-04-06 10:43:55 +00:00
"strings"
2018-04-03 08:58:26 +00:00
2018-06-30 16:37:26 +00:00
"git.loafle.net/commons/rpc-go/codec"
2018-04-03 08:58:26 +00:00
"git.loafle.net/commons/rpc-go/protocol"
cuej "git.loafle.net/commons/util-go/encoding/json"
)
// ----------------------------------------------------------------------------
// Request
// ----------------------------------------------------------------------------
// serverRequest represents a JSON-RPC request received by the server.
type serverRequest struct {
// JSON-RPC protocol.
Version string `json:"jsonrpc"`
// A String containing the name of the method to be invoked.
Method string `json:"method"`
// A Structured value to pass as arguments to the method.
Params *json.RawMessage `json:"params,omitempty"`
// The request id. MUST be a string, number or null.
// Our implementation will not do type checking for id.
// It will be copied as it is.
ID *json.RawMessage `json:"id,omitempty"`
}
// ----------------------------------------------------------------------------
// ServerRequestCodec
// ----------------------------------------------------------------------------
// newRequestCodec returns a new ServerRequestCodec.
2018-06-30 16:37:26 +00:00
func newServerRequestCodec(codecSelector codec.CodecSelector, buf []byte) (protocol.ServerRequestCodec, error) {
2018-04-03 08:58:26 +00:00
req := &serverRequest{}
err := json.Unmarshal(buf, req)
if err != nil {
err = &protocol.Error{
Code: protocol.E_PARSE,
Message: err.Error(),
Data: req,
}
}
if req.Version != Version {
err = &protocol.Error{
Code: protocol.E_INVALID_REQ,
Message: "jsonrpc must be " + Version,
Data: req,
}
}
2018-06-30 16:37:26 +00:00
return &ServerRequestCodec{codecSelector: codecSelector, req: req, err: err}, nil
2018-04-03 08:58:26 +00:00
}
// ServerRequestCodec decodes and encodes a single request.
type ServerRequestCodec struct {
2018-06-30 16:37:26 +00:00
codecSelector codec.CodecSelector
req *serverRequest
err error
2018-04-03 08:58:26 +00:00
}
func (src *ServerRequestCodec) HasResponse() bool {
return src.req.ID != nil
}
// Method returns the RPC method for the current request.
//
// The method uses a dotted notation as in "Service.Method".
func (src *ServerRequestCodec) Method() string {
return src.req.Method
}
// ReadRequest fills the request object for the RPC method.
//
// ReadRequest parses request parameters in two supported forms in
// accordance with http://www.jsonrpc.org/specification#parameter_structures
//
// by-position: params MUST be an Array, containing the
// values in the Server expected order.
//
// by-name: params MUST be an Object, with member names
// that match the Server expected parameter names. The
// absence of expected names MAY result in an error being
// generated. The names MUST match exactly, including
// case, to the method's expected parameters.
func (src *ServerRequestCodec) ReadParams(args []interface{}) error {
if src.err == nil && src.req.Params != nil {
// Note: if scr.request.Params is nil it's not an error, it's an optional member.
// JSON params structured object. Unmarshal to the args object.
2018-04-10 09:02:35 +00:00
if err := cuej.SetValueWithJSONStringArrayBytes(*src.req.Params, args); nil != err {
2018-04-03 08:58:26 +00:00
src.err = &protocol.Error{
2018-04-05 01:12:33 +00:00
Code: protocol.E_BAD_PARAMS,
2018-04-03 08:58:26 +00:00
Message: err.Error(),
Data: src.req.Params,
}
return src.err
}
return nil
}
return src.err
}
func (src *ServerRequestCodec) Params() ([]string, error) {
if src.err == nil && src.req.Params != nil {
var values []string
if err := json.Unmarshal(*src.req.Params, &values); err != nil {
src.err = &protocol.Error{
2018-04-05 01:12:33 +00:00
Code: protocol.E_BAD_PARAMS,
2018-04-03 08:58:26 +00:00
Message: err.Error(),
Data: src.req.Params,
}
return nil, src.err
}
return values, nil
}
return nil, src.err
}
2018-06-30 16:37:26 +00:00
func (src *ServerRequestCodec) encodeResponse(res *serverResponse) (messageType int, message []byte, err error) {
buf, err := json.Marshal(res)
if nil != err {
return 0, nil, err
}
return src.codecSelector.Encode(buf)
}
func (src *ServerRequestCodec) NewResponse(reply interface{}, replyErr error) (messageType int, message []byte, err error) {
res := src.newServerResponse(reply, replyErr)
return src.encodeResponse(res)
2018-04-06 10:04:59 +00:00
}
2018-06-30 16:37:26 +00:00
func (src *ServerRequestCodec) NewResponseWithString(reply string, replyErr error) (messageType int, message []byte, err error) {
res := src.newServerResponse(nil, replyErr)
2018-04-06 10:04:59 +00:00
2018-04-06 10:43:55 +00:00
r := strings.TrimSpace(reply)
2018-04-06 10:44:51 +00:00
if nil == err && "" != r {
2018-04-06 10:42:32 +00:00
var i interface{}
2018-04-06 10:43:55 +00:00
mErr := json.Unmarshal([]byte(r), &i)
2018-04-06 10:42:32 +00:00
if nil != mErr {
2018-06-30 16:37:26 +00:00
return 0, nil, mErr
2018-04-06 10:42:32 +00:00
}
2018-04-06 10:04:59 +00:00
2018-04-06 10:42:32 +00:00
res.Result = i
}
2018-06-30 16:37:26 +00:00
return src.encodeResponse(res)
2018-04-06 10:04:59 +00:00
}
func (src *ServerRequestCodec) newServerResponse(reply interface{}, err error) *serverResponse {
2018-04-03 08:58:26 +00:00
res := &serverResponse{Version: Version, Result: reply, ID: src.req.ID}
2018-04-05 01:12:33 +00:00
if nil != err {
jsonErr, ok := err.(*protocol.Error)
if !ok {
jsonErr = &protocol.Error{
Code: protocol.E_SERVER,
Message: err.Error(),
}
2018-04-03 08:58:26 +00:00
}
2018-04-05 01:12:33 +00:00
res.Error = jsonErr
2018-04-03 08:58:26 +00:00
}
2018-04-05 01:12:33 +00:00
2018-04-06 10:04:59 +00:00
return res
2018-04-03 08:58:26 +00:00
}