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.

115 lines
2.5 KiB

9 years ago
9 years ago
  1. package proxy
  2. import (
  3. tmspcli "github.com/tendermint/tmsp/client"
  4. tmsp "github.com/tendermint/tmsp/types"
  5. "sync"
  6. )
  7. type localAppConn struct {
  8. mtx *sync.Mutex
  9. tmsp.Application
  10. tmspcli.Callback
  11. }
  12. func NewLocalAppConn(mtx *sync.Mutex, app tmsp.Application) *localAppConn {
  13. return &localAppConn{
  14. mtx: mtx,
  15. Application: app,
  16. }
  17. }
  18. func (app *localAppConn) SetResponseCallback(cb tmspcli.Callback) {
  19. app.mtx.Lock()
  20. defer app.mtx.Unlock()
  21. app.Callback = cb
  22. }
  23. // TODO: change tmsp.Application to include Error()?
  24. func (app *localAppConn) Error() error {
  25. return nil
  26. }
  27. func (app *localAppConn) EchoAsync(msg string) *tmspcli.ReqRes {
  28. return app.callback(
  29. tmsp.RequestEcho(msg),
  30. tmsp.ResponseEcho(msg),
  31. )
  32. }
  33. func (app *localAppConn) FlushAsync() *tmspcli.ReqRes {
  34. // Do nothing
  35. return NewReqRes(tmsp.RequestFlush(), nil)
  36. }
  37. func (app *localAppConn) SetOptionAsync(key string, value string) *tmspcli.ReqRes {
  38. app.mtx.Lock()
  39. log := app.Application.SetOption(key, value)
  40. app.mtx.Unlock()
  41. return app.callback(
  42. tmsp.RequestSetOption(key, value),
  43. tmsp.ResponseSetOption(log),
  44. )
  45. }
  46. func (app *localAppConn) AppendTxAsync(tx []byte) *tmspcli.ReqRes {
  47. app.mtx.Lock()
  48. code, result, log := app.Application.AppendTx(tx)
  49. app.mtx.Unlock()
  50. return app.callback(
  51. tmsp.RequestAppendTx(tx),
  52. tmsp.ResponseAppendTx(code, result, log),
  53. )
  54. }
  55. func (app *localAppConn) CheckTxAsync(tx []byte) *tmspcli.ReqRes {
  56. app.mtx.Lock()
  57. code, result, log := app.Application.CheckTx(tx)
  58. app.mtx.Unlock()
  59. return app.callback(
  60. tmsp.RequestCheckTx(tx),
  61. tmsp.ResponseCheckTx(code, result, log),
  62. )
  63. }
  64. func (app *localAppConn) CommitAsync() *tmspcli.ReqRes {
  65. app.mtx.Lock()
  66. hash, log := app.Application.Commit()
  67. app.mtx.Unlock()
  68. return app.callback(
  69. tmsp.RequestCommit(),
  70. tmsp.ResponseCommit(hash, log),
  71. )
  72. }
  73. func (app *localAppConn) InfoSync() (info string, err error) {
  74. app.mtx.Lock()
  75. info = app.Application.Info()
  76. app.mtx.Unlock()
  77. return info, nil
  78. }
  79. func (app *localAppConn) FlushSync() error {
  80. return nil
  81. }
  82. func (app *localAppConn) CommitSync() (hash []byte, log string, err error) {
  83. app.mtx.Lock()
  84. hash, log = app.Application.Commit()
  85. app.mtx.Unlock()
  86. return hash, log, nil
  87. }
  88. //-------------------------------------------------------
  89. func (app *localAppConn) callback(req *tmsp.Request, res *tmsp.Response) *tmspcli.ReqRes {
  90. app.Callback(req, res)
  91. return NewReqRes(req, res)
  92. }
  93. func NewReqRes(req *tmsp.Request, res *tmsp.Response) *tmspcli.ReqRes {
  94. reqRes := tmspcli.NewReqRes(req)
  95. reqRes.Response = res
  96. reqRes.SetDone()
  97. return reqRes
  98. }