2016-01-12 13:50:06 -08:00
|
|
|
package rpctypes
|
|
|
|
|
|
|
|
import (
|
2016-01-13 15:37:35 -08:00
|
|
|
"encoding/json"
|
2016-02-18 13:07:49 -08:00
|
|
|
"strings"
|
2016-01-13 15:37:35 -08:00
|
|
|
|
2017-03-07 06:34:54 -08:00
|
|
|
events "github.com/tendermint/go-events"
|
|
|
|
wire "github.com/tendermint/go-wire"
|
2016-01-12 13:50:06 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type RPCRequest struct {
|
2017-03-07 06:34:54 -08:00
|
|
|
JSONRPC string `json:"jsonrpc"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
Params map[string]interface{} `json:"params"`
|
2016-01-12 13:50:06 -08:00
|
|
|
}
|
|
|
|
|
2017-03-07 06:34:54 -08:00
|
|
|
func NewRPCRequest(id string, method string, params map[string]interface{}) RPCRequest {
|
2016-01-12 13:50:06 -08:00
|
|
|
return RPCRequest{
|
|
|
|
JSONRPC: "2.0",
|
|
|
|
ID: id,
|
|
|
|
Method: method,
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
Result is a generic interface.
|
|
|
|
Applications should register type-bytes like so:
|
|
|
|
|
|
|
|
var _ = wire.RegisterInterface(
|
|
|
|
struct{ Result }{},
|
|
|
|
wire.ConcreteType{&ResultGenesis{}, ResultTypeGenesis},
|
|
|
|
wire.ConcreteType{&ResultBlockchainInfo{}, ResultTypeBlockchainInfo},
|
|
|
|
...
|
|
|
|
)
|
|
|
|
*/
|
|
|
|
type Result interface {
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
type RPCResponse struct {
|
2016-01-13 15:37:35 -08:00
|
|
|
JSONRPC string `json:"jsonrpc"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
Result *json.RawMessage `json:"result"`
|
|
|
|
Error string `json:"error"`
|
2016-01-12 13:50:06 -08:00
|
|
|
}
|
|
|
|
|
2016-01-13 15:37:35 -08:00
|
|
|
func NewRPCResponse(id string, res interface{}, err string) RPCResponse {
|
2016-01-13 19:16:56 -08:00
|
|
|
var raw *json.RawMessage
|
|
|
|
if res != nil {
|
|
|
|
rawMsg := json.RawMessage(wire.JSONBytes(res))
|
|
|
|
raw = &rawMsg
|
|
|
|
}
|
2016-01-12 13:50:06 -08:00
|
|
|
return RPCResponse{
|
|
|
|
JSONRPC: "2.0",
|
|
|
|
ID: id,
|
2016-01-13 19:16:56 -08:00
|
|
|
Result: raw,
|
2016-01-12 13:50:06 -08:00
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
// *wsConnection implements this interface.
|
|
|
|
type WSRPCConnection interface {
|
|
|
|
GetRemoteAddr() string
|
2016-10-10 00:22:34 -07:00
|
|
|
GetEventSwitch() events.EventSwitch
|
2016-01-12 13:50:06 -08:00
|
|
|
WriteRPCResponse(resp RPCResponse)
|
|
|
|
TryWriteRPCResponse(resp RPCResponse) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// websocket-only RPCFuncs take this as the first parameter.
|
|
|
|
type WSRPCContext struct {
|
|
|
|
Request RPCRequest
|
|
|
|
WSRPCConnection
|
|
|
|
}
|
2016-02-18 13:07:49 -08:00
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
// sockets
|
|
|
|
//
|
|
|
|
// Determine if its a unix or tcp socket.
|
|
|
|
// If tcp, must specify the port; `0.0.0.0` will return incorrectly as "unix" since there's no port
|
|
|
|
func SocketType(listenAddr string) string {
|
|
|
|
socketType := "unix"
|
2016-02-18 18:05:24 -08:00
|
|
|
if len(strings.Split(listenAddr, ":")) >= 2 {
|
2016-02-18 13:07:49 -08:00
|
|
|
socketType = "tcp"
|
|
|
|
}
|
|
|
|
return socketType
|
|
|
|
}
|