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.

150 lines
4.5 KiB

8 years ago
  1. package abcicli
  2. import (
  3. "fmt"
  4. "sync"
  5. "github.com/tendermint/tendermint/abci/types"
  6. "github.com/tendermint/tendermint/libs/service"
  7. tmsync "github.com/tendermint/tendermint/libs/sync"
  8. )
  9. const (
  10. dialRetryIntervalSeconds = 3
  11. echoRetryIntervalSeconds = 1
  12. )
  13. // Client defines an interface for an ABCI client.
  14. // All `Async` methods return a `ReqRes` object.
  15. // All `Sync` methods return the appropriate protobuf ResponseXxx struct and an error.
  16. // Note these are client errors, eg. ABCI socket connectivity issues.
  17. // Application-related errors are reflected in response via ABCI error codes and logs.
  18. type Client interface {
  19. service.Service
  20. SetResponseCallback(Callback)
  21. Error() error
  22. FlushAsync() *ReqRes
  23. EchoAsync(msg string) *ReqRes
  24. InfoAsync(types.RequestInfo) *ReqRes
  25. SetOptionAsync(types.RequestSetOption) *ReqRes
  26. DeliverTxAsync(types.RequestDeliverTx) *ReqRes
  27. CheckTxAsync(types.RequestCheckTx) *ReqRes
  28. QueryAsync(types.RequestQuery) *ReqRes
  29. CommitAsync() *ReqRes
  30. InitChainAsync(types.RequestInitChain) *ReqRes
  31. BeginBlockAsync(types.RequestBeginBlock) *ReqRes
  32. EndBlockAsync(types.RequestEndBlock) *ReqRes
  33. ListSnapshotsAsync(types.RequestListSnapshots) *ReqRes
  34. OfferSnapshotAsync(types.RequestOfferSnapshot) *ReqRes
  35. LoadSnapshotChunkAsync(types.RequestLoadSnapshotChunk) *ReqRes
  36. ApplySnapshotChunkAsync(types.RequestApplySnapshotChunk) *ReqRes
  37. FlushSync() error
  38. EchoSync(msg string) (*types.ResponseEcho, error)
  39. InfoSync(types.RequestInfo) (*types.ResponseInfo, error)
  40. SetOptionSync(types.RequestSetOption) (*types.ResponseSetOption, error)
  41. DeliverTxSync(types.RequestDeliverTx) (*types.ResponseDeliverTx, error)
  42. CheckTxSync(types.RequestCheckTx) (*types.ResponseCheckTx, error)
  43. QuerySync(types.RequestQuery) (*types.ResponseQuery, error)
  44. CommitSync() (*types.ResponseCommit, error)
  45. InitChainSync(types.RequestInitChain) (*types.ResponseInitChain, error)
  46. BeginBlockSync(types.RequestBeginBlock) (*types.ResponseBeginBlock, error)
  47. EndBlockSync(types.RequestEndBlock) (*types.ResponseEndBlock, error)
  48. ListSnapshotsSync(types.RequestListSnapshots) (*types.ResponseListSnapshots, error)
  49. OfferSnapshotSync(types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error)
  50. LoadSnapshotChunkSync(types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error)
  51. ApplySnapshotChunkSync(types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error)
  52. }
  53. //----------------------------------------
  54. // NewClient returns a new ABCI client of the specified transport type.
  55. // It returns an error if the transport is not "socket" or "grpc"
  56. func NewClient(addr, transport string, mustConnect bool) (client Client, err error) {
  57. switch transport {
  58. case "socket":
  59. client = NewSocketClient(addr, mustConnect)
  60. case "grpc":
  61. client = NewGRPCClient(addr, mustConnect)
  62. default:
  63. err = fmt.Errorf("unknown abci transport %s", transport)
  64. }
  65. return
  66. }
  67. type Callback func(*types.Request, *types.Response)
  68. type ReqRes struct {
  69. *types.Request
  70. *sync.WaitGroup
  71. *types.Response // Not set atomically, so be sure to use WaitGroup.
  72. mtx tmsync.RWMutex
  73. done bool // Gets set to true once *after* WaitGroup.Done().
  74. cb func(*types.Response) // A single callback that may be set.
  75. }
  76. func NewReqRes(req *types.Request) *ReqRes {
  77. return &ReqRes{
  78. Request: req,
  79. WaitGroup: waitGroup1(),
  80. Response: nil,
  81. done: false,
  82. cb: nil,
  83. }
  84. }
  85. // Sets sets the callback. If reqRes is already done, it will call the cb
  86. // immediately. Note, reqRes.cb should not change if reqRes.done and only one
  87. // callback is supported.
  88. func (r *ReqRes) SetCallback(cb func(res *types.Response)) {
  89. r.mtx.Lock()
  90. if r.done {
  91. r.mtx.Unlock()
  92. cb(r.Response)
  93. return
  94. }
  95. r.cb = cb
  96. r.mtx.Unlock()
  97. }
  98. // InvokeCallback invokes a thread-safe execution of the configured callback
  99. // if non-nil.
  100. func (r *ReqRes) InvokeCallback() {
  101. r.mtx.Lock()
  102. defer r.mtx.Unlock()
  103. if r.cb != nil {
  104. r.cb(r.Response)
  105. }
  106. }
  107. // GetCallback returns the configured callback of the ReqRes object which may be
  108. // nil. Note, it is not safe to concurrently call this in cases where it is
  109. // marked done and SetCallback is called before calling GetCallback as that
  110. // will invoke the callback twice and create a potential race condition.
  111. //
  112. // ref: https://github.com/tendermint/tendermint/issues/5439
  113. func (r *ReqRes) GetCallback() func(*types.Response) {
  114. r.mtx.RLock()
  115. defer r.mtx.RUnlock()
  116. return r.cb
  117. }
  118. // SetDone marks the ReqRes object as done.
  119. func (r *ReqRes) SetDone() {
  120. r.mtx.Lock()
  121. defer r.mtx.Unlock()
  122. r.done = true
  123. }
  124. func waitGroup1() (wg *sync.WaitGroup) {
  125. wg = &sync.WaitGroup{}
  126. wg.Add(1)
  127. return
  128. }