You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.7 KiB

9 years ago
9 years ago
  1. package rpctypes
  2. import (
  3. "encoding/json"
  4. "github.com/tendermint/go-events"
  5. "github.com/tendermint/go-wire"
  6. )
  7. type RPCRequest struct {
  8. JSONRPC string `json:"jsonrpc"`
  9. ID string `json:"id"`
  10. Method string `json:"method"`
  11. Params []interface{} `json:"params"`
  12. }
  13. func NewRPCRequest(id string, method string, params []interface{}) RPCRequest {
  14. return RPCRequest{
  15. JSONRPC: "2.0",
  16. ID: id,
  17. Method: method,
  18. Params: params,
  19. }
  20. }
  21. //----------------------------------------
  22. /*
  23. Result is a generic interface.
  24. Applications should register type-bytes like so:
  25. var _ = wire.RegisterInterface(
  26. struct{ Result }{},
  27. wire.ConcreteType{&ResultGenesis{}, ResultTypeGenesis},
  28. wire.ConcreteType{&ResultBlockchainInfo{}, ResultTypeBlockchainInfo},
  29. ...
  30. )
  31. */
  32. type Result interface {
  33. }
  34. //----------------------------------------
  35. type RPCResponse struct {
  36. JSONRPC string `json:"jsonrpc"`
  37. ID string `json:"id"`
  38. Result *json.RawMessage `json:"result"`
  39. Error string `json:"error"`
  40. }
  41. func NewRPCResponse(id string, res interface{}, err string) RPCResponse {
  42. var raw *json.RawMessage
  43. if res != nil {
  44. rawMsg := json.RawMessage(wire.JSONBytes(res))
  45. raw = &rawMsg
  46. }
  47. return RPCResponse{
  48. JSONRPC: "2.0",
  49. ID: id,
  50. Result: raw,
  51. Error: err,
  52. }
  53. }
  54. //----------------------------------------
  55. // *wsConnection implements this interface.
  56. type WSRPCConnection interface {
  57. GetRemoteAddr() string
  58. GetEventSwitch() *events.EventSwitch
  59. WriteRPCResponse(resp RPCResponse)
  60. TryWriteRPCResponse(resp RPCResponse) bool
  61. }
  62. // websocket-only RPCFuncs take this as the first parameter.
  63. type WSRPCContext struct {
  64. Request RPCRequest
  65. WSRPCConnection
  66. }