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.

120 lines
2.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package abcicli
  2. import (
  3. "fmt"
  4. "sync"
  5. "github.com/tendermint/abci/types"
  6. cmn "github.com/tendermint/tmlibs/common"
  7. )
  8. type Client interface {
  9. cmn.Service
  10. SetResponseCallback(Callback)
  11. Error() error
  12. FlushAsync() *ReqRes
  13. EchoAsync(msg string) *ReqRes
  14. InfoAsync() *ReqRes
  15. SetOptionAsync(key string, value string) *ReqRes
  16. DeliverTxAsync(tx []byte) *ReqRes
  17. CheckTxAsync(tx []byte) *ReqRes
  18. QueryAsync(reqQuery types.RequestQuery) *ReqRes
  19. CommitAsync() *ReqRes
  20. FlushSync() error
  21. EchoSync(msg string) (res types.Result)
  22. InfoSync() (resInfo types.ResponseInfo, err error)
  23. SetOptionSync(key string, value string) (res types.Result)
  24. DeliverTxSync(tx []byte) (res types.Result)
  25. CheckTxSync(tx []byte) (res types.Result)
  26. QuerySync(reqQuery types.RequestQuery) (resQuery types.ResponseQuery, err error)
  27. CommitSync() (res types.Result)
  28. InitChainAsync(validators []*types.Validator) *ReqRes
  29. BeginBlockAsync(hash []byte, header *types.Header) *ReqRes
  30. EndBlockAsync(height uint64) *ReqRes
  31. InitChainSync(validators []*types.Validator) (err error)
  32. BeginBlockSync(hash []byte, header *types.Header) (err error)
  33. EndBlockSync(height uint64) (resEndBlock types.ResponseEndBlock, err error)
  34. }
  35. //----------------------------------------
  36. func NewClient(addr, transport string, mustConnect bool) (client Client, err error) {
  37. switch transport {
  38. case "socket":
  39. client, err = NewSocketClient(addr, mustConnect)
  40. case "grpc":
  41. client, err = NewGRPCClient(addr, mustConnect)
  42. default:
  43. err = fmt.Errorf("Unknown abci transport %s", transport)
  44. }
  45. return
  46. }
  47. //----------------------------------------
  48. type Callback func(*types.Request, *types.Response)
  49. //----------------------------------------
  50. type ReqRes struct {
  51. *types.Request
  52. *sync.WaitGroup
  53. *types.Response // Not set atomically, so be sure to use WaitGroup.
  54. mtx sync.Mutex
  55. done bool // Gets set to true once *after* WaitGroup.Done().
  56. cb func(*types.Response) // A single callback that may be set.
  57. }
  58. func NewReqRes(req *types.Request) *ReqRes {
  59. return &ReqRes{
  60. Request: req,
  61. WaitGroup: waitGroup1(),
  62. Response: nil,
  63. done: false,
  64. cb: nil,
  65. }
  66. }
  67. // Sets the callback for this ReqRes atomically.
  68. // If reqRes is already done, calls cb immediately.
  69. // NOTE: reqRes.cb should not change if reqRes.done.
  70. // NOTE: only one callback is supported.
  71. func (reqRes *ReqRes) SetCallback(cb func(res *types.Response)) {
  72. reqRes.mtx.Lock()
  73. if reqRes.done {
  74. reqRes.mtx.Unlock()
  75. cb(reqRes.Response)
  76. return
  77. }
  78. defer reqRes.mtx.Unlock()
  79. reqRes.cb = cb
  80. }
  81. func (reqRes *ReqRes) GetCallback() func(*types.Response) {
  82. reqRes.mtx.Lock()
  83. defer reqRes.mtx.Unlock()
  84. return reqRes.cb
  85. }
  86. // NOTE: it should be safe to read reqRes.cb without locks after this.
  87. func (reqRes *ReqRes) SetDone() {
  88. reqRes.mtx.Lock()
  89. reqRes.done = true
  90. reqRes.mtx.Unlock()
  91. }
  92. func waitGroup1() (wg *sync.WaitGroup) {
  93. wg = &sync.WaitGroup{}
  94. wg.Add(1)
  95. return
  96. }