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.

118 lines
2.7 KiB

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