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.

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