2017-10-31 09:25:44 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// ClientCodecNotify
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// newCodecRequest returns a new ClientCodecNotify.
|
2017-11-01 08:13:23 +00:00
|
|
|
func newClientCodecNotify(raw json.RawMessage) (protocol.ClientCodecNotify, error) {
|
2017-10-31 09:25:44 +00:00
|
|
|
// Decode the request body and check if RPC method is valid.
|
|
|
|
ccn := retainClientCodecNotify()
|
2017-11-01 08:13:23 +00:00
|
|
|
err := json.Unmarshal(raw, &ccn.notify)
|
2017-10-31 09:25:44 +00:00
|
|
|
if err != nil {
|
|
|
|
releaseClientCodecNotify(ccn)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if "" == ccn.notify.Method {
|
|
|
|
releaseClientCodecNotify(ccn)
|
|
|
|
return nil, fmt.Errorf("This is not ClientNotify")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ccn.notify.Version != Version {
|
|
|
|
ccn.err = &Error{
|
|
|
|
Code: E_INVALID_REQ,
|
|
|
|
Message: "jsonrpc must be " + Version,
|
|
|
|
Data: ccn.notify,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ccn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientCodecNotify decodes and encodes a single notification.
|
|
|
|
type ClientCodecNotify struct {
|
|
|
|
notify clientNotify
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ccn *ClientCodecNotify) Method() string {
|
|
|
|
return ccn.notify.Method
|
|
|
|
}
|
|
|
|
|
2017-11-03 06:35:43 +00:00
|
|
|
func (ccn *ClientCodecNotify) ReadParams(args []interface{}) error {
|
2017-10-31 09:25:44 +00:00
|
|
|
if ccn.err == nil && ccn.notify.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.
|
|
|
|
if err := json.Unmarshal(*ccn.notify.Params, args); err != nil {
|
2017-11-02 06:39:30 +00:00
|
|
|
ccn.err = &Error{
|
|
|
|
Code: E_INVALID_REQ,
|
|
|
|
Message: err.Error(),
|
|
|
|
Data: ccn.notify.Params,
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ccn.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ccn *ClientCodecNotify) Complete() {
|
|
|
|
releaseClientCodecNotify(ccn)
|
|
|
|
}
|
|
|
|
|
|
|
|
var clientCodecNotifyPool sync.Pool
|
|
|
|
|
|
|
|
func retainClientCodecNotify() *ClientCodecNotify {
|
|
|
|
v := clientCodecNotifyPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &ClientCodecNotify{}
|
|
|
|
}
|
|
|
|
return v.(*ClientCodecNotify)
|
|
|
|
}
|
|
|
|
|
|
|
|
func releaseClientCodecNotify(ccn *ClientCodecNotify) {
|
|
|
|
ccn.notify.Version = ""
|
|
|
|
ccn.notify.Method = ""
|
|
|
|
ccn.notify.Params = nil
|
|
|
|
|
|
|
|
clientCodecNotifyPool.Put(ccn)
|
|
|
|
}
|