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.

93 lines
2.1 KiB

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