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)
|
2017-11-23 09:18:37 +00:00
|
|
|
return nil, fmt.Errorf("This is not Client Notify")
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2017-11-03 07:58:40 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-11-09 08:38:02 +00:00
|
|
|
func (ccn *ClientCodecNotify) Params() ([]string, error) {
|
2017-11-08 06:35:27 +00:00
|
|
|
if ccn.err == nil && ccn.notify.Params != nil {
|
2017-11-09 08:38:02 +00:00
|
|
|
var results []string
|
|
|
|
|
|
|
|
if err := json.Unmarshal(*ccn.notify.Params, &results); err != nil {
|
|
|
|
ccn.err = &Error{
|
|
|
|
Code: E_INVALID_REQ,
|
|
|
|
Message: err.Error(),
|
|
|
|
Data: ccn.notify.Params,
|
|
|
|
}
|
|
|
|
return nil, ccn.err
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
2017-11-08 06:35:27 +00:00
|
|
|
}
|
|
|
|
return nil, ccn.err
|
|
|
|
}
|
|
|
|
|
2017-10-31 09:25:44 +00:00
|
|
|
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)
|
|
|
|
}
|