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.

230 lines
5.2 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package abcicli
  2. import (
  3. "sync"
  4. types "github.com/tendermint/abci/types"
  5. cmn "github.com/tendermint/tmlibs/common"
  6. )
  7. var _ Client = (*localClient)(nil)
  8. type localClient struct {
  9. cmn.BaseService
  10. mtx *sync.Mutex
  11. types.Application
  12. Callback
  13. }
  14. func NewLocalClient(mtx *sync.Mutex, app types.Application) *localClient {
  15. if mtx == nil {
  16. mtx = new(sync.Mutex)
  17. }
  18. cli := &localClient{
  19. mtx: mtx,
  20. Application: app,
  21. }
  22. cli.BaseService = *cmn.NewBaseService(nil, "localClient", cli)
  23. return cli
  24. }
  25. func (app *localClient) SetResponseCallback(cb Callback) {
  26. app.mtx.Lock()
  27. defer app.mtx.Unlock()
  28. app.Callback = cb
  29. }
  30. // TODO: change types.Application to include Error()?
  31. func (app *localClient) Error() error {
  32. return nil
  33. }
  34. func (app *localClient) FlushAsync() *ReqRes {
  35. // Do nothing
  36. return newLocalReqRes(types.ToRequestFlush(), nil)
  37. }
  38. func (app *localClient) EchoAsync(msg string) *ReqRes {
  39. return app.callback(
  40. types.ToRequestEcho(msg),
  41. types.ToResponseEcho(msg),
  42. )
  43. }
  44. func (app *localClient) InfoAsync(req types.RequestInfo) *ReqRes {
  45. app.mtx.Lock()
  46. res := app.Application.Info(req)
  47. app.mtx.Unlock()
  48. return app.callback(
  49. types.ToRequestInfo(req),
  50. types.ToResponseInfo(res),
  51. )
  52. }
  53. func (app *localClient) SetOptionAsync(req types.RequestSetOption) *ReqRes {
  54. app.mtx.Lock()
  55. res := app.Application.SetOption(req)
  56. app.mtx.Unlock()
  57. return app.callback(
  58. types.ToRequestSetOption(req),
  59. types.ToResponseSetOption(res),
  60. )
  61. }
  62. func (app *localClient) DeliverTxAsync(tx []byte) *ReqRes {
  63. app.mtx.Lock()
  64. res := app.Application.DeliverTx(tx)
  65. app.mtx.Unlock()
  66. return app.callback(
  67. types.ToRequestDeliverTx(tx),
  68. types.ToResponseDeliverTx(res),
  69. )
  70. }
  71. func (app *localClient) CheckTxAsync(tx []byte) *ReqRes {
  72. app.mtx.Lock()
  73. res := app.Application.CheckTx(tx)
  74. app.mtx.Unlock()
  75. return app.callback(
  76. types.ToRequestCheckTx(tx),
  77. types.ToResponseCheckTx(res),
  78. )
  79. }
  80. func (app *localClient) QueryAsync(req types.RequestQuery) *ReqRes {
  81. app.mtx.Lock()
  82. res := app.Application.Query(req)
  83. app.mtx.Unlock()
  84. return app.callback(
  85. types.ToRequestQuery(req),
  86. types.ToResponseQuery(res),
  87. )
  88. }
  89. func (app *localClient) CommitAsync() *ReqRes {
  90. app.mtx.Lock()
  91. res := app.Application.Commit()
  92. app.mtx.Unlock()
  93. return app.callback(
  94. types.ToRequestCommit(),
  95. types.ToResponseCommit(res),
  96. )
  97. }
  98. func (app *localClient) InitChainAsync(req types.RequestInitChain) *ReqRes {
  99. app.mtx.Lock()
  100. res := app.Application.InitChain(req)
  101. reqRes := app.callback(
  102. types.ToRequestInitChain(req),
  103. types.ToResponseInitChain(res),
  104. )
  105. app.mtx.Unlock()
  106. return reqRes
  107. }
  108. func (app *localClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes {
  109. app.mtx.Lock()
  110. res := app.Application.BeginBlock(req)
  111. app.mtx.Unlock()
  112. return app.callback(
  113. types.ToRequestBeginBlock(req),
  114. types.ToResponseBeginBlock(res),
  115. )
  116. }
  117. func (app *localClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes {
  118. app.mtx.Lock()
  119. res := app.Application.EndBlock(req)
  120. app.mtx.Unlock()
  121. return app.callback(
  122. types.ToRequestEndBlock(req),
  123. types.ToResponseEndBlock(res),
  124. )
  125. }
  126. //-------------------------------------------------------
  127. func (app *localClient) FlushSync() error {
  128. return nil
  129. }
  130. func (app *localClient) EchoSync(msg string) (*types.ResponseEcho, error) {
  131. return &types.ResponseEcho{msg}, nil
  132. }
  133. func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
  134. app.mtx.Lock()
  135. res := app.Application.Info(req)
  136. app.mtx.Unlock()
  137. return &res, nil
  138. }
  139. func (app *localClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) {
  140. app.mtx.Lock()
  141. res := app.Application.SetOption(req)
  142. app.mtx.Unlock()
  143. return &res, nil
  144. }
  145. func (app *localClient) DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error) {
  146. app.mtx.Lock()
  147. res := app.Application.DeliverTx(tx)
  148. app.mtx.Unlock()
  149. return &res, nil
  150. }
  151. func (app *localClient) CheckTxSync(tx []byte) (*types.ResponseCheckTx, error) {
  152. app.mtx.Lock()
  153. res := app.Application.CheckTx(tx)
  154. app.mtx.Unlock()
  155. return &res, nil
  156. }
  157. func (app *localClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) {
  158. app.mtx.Lock()
  159. res := app.Application.Query(req)
  160. app.mtx.Unlock()
  161. return &res, nil
  162. }
  163. func (app *localClient) CommitSync() (*types.ResponseCommit, error) {
  164. app.mtx.Lock()
  165. res := app.Application.Commit()
  166. app.mtx.Unlock()
  167. return &res, nil
  168. }
  169. func (app *localClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) {
  170. app.mtx.Lock()
  171. res := app.Application.InitChain(req)
  172. app.mtx.Unlock()
  173. return &res, nil
  174. }
  175. func (app *localClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) {
  176. app.mtx.Lock()
  177. res := app.Application.BeginBlock(req)
  178. app.mtx.Unlock()
  179. return &res, nil
  180. }
  181. func (app *localClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) {
  182. app.mtx.Lock()
  183. res := app.Application.EndBlock(req)
  184. app.mtx.Unlock()
  185. return &res, nil
  186. }
  187. //-------------------------------------------------------
  188. func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes {
  189. app.Callback(req, res)
  190. return newLocalReqRes(req, res)
  191. }
  192. func newLocalReqRes(req *types.Request, res *types.Response) *ReqRes {
  193. reqRes := NewReqRes(req)
  194. reqRes.Response = res
  195. reqRes.SetDone()
  196. return reqRes
  197. }