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.

128 lines
2.5 KiB

  1. package proxy
  2. import (
  3. tmsp "github.com/tendermint/tmsp/types"
  4. "sync"
  5. )
  6. type localAppConn struct {
  7. mtx *sync.Mutex
  8. tmsp.Application
  9. Callback
  10. }
  11. func NewLocalAppConn(mtx *sync.Mutex, app tmsp.Application) *localAppConn {
  12. return &localAppConn{
  13. mtx: mtx,
  14. Application: app,
  15. }
  16. }
  17. func (app *localAppConn) SetResponseCallback(cb Callback) {
  18. app.mtx.Lock()
  19. defer app.mtx.Unlock()
  20. app.Callback = cb
  21. }
  22. // TODO: change tmsp.Application to include Error()?
  23. func (app *localAppConn) Error() error {
  24. return nil
  25. }
  26. func (app *localAppConn) EchoAsync(msg string) {
  27. app.mtx.Lock()
  28. msg2 := app.Application.Echo(msg)
  29. app.mtx.Unlock()
  30. app.Callback(
  31. tmsp.RequestEcho{msg},
  32. tmsp.ResponseEcho{msg2},
  33. )
  34. }
  35. func (app *localAppConn) FlushAsync() {
  36. // Do nothing
  37. }
  38. func (app *localAppConn) SetOptionAsync(key string, value string) {
  39. app.mtx.Lock()
  40. retCode := app.Application.SetOption(key, value)
  41. app.mtx.Unlock()
  42. app.Callback(
  43. tmsp.RequestSetOption{key, value},
  44. tmsp.ResponseSetOption{retCode},
  45. )
  46. }
  47. func (app *localAppConn) AppendTxAsync(tx []byte) {
  48. app.mtx.Lock()
  49. events, retCode := app.Application.AppendTx(tx)
  50. app.mtx.Unlock()
  51. app.Callback(
  52. tmsp.RequestAppendTx{tx},
  53. tmsp.ResponseAppendTx{retCode},
  54. )
  55. for _, event := range events {
  56. app.Callback(
  57. nil,
  58. tmsp.ResponseEvent{event},
  59. )
  60. }
  61. }
  62. func (app *localAppConn) CheckTxAsync(tx []byte) {
  63. app.mtx.Lock()
  64. retCode := app.Application.CheckTx(tx)
  65. app.mtx.Unlock()
  66. app.Callback(
  67. tmsp.RequestCheckTx{tx},
  68. tmsp.ResponseCheckTx{retCode},
  69. )
  70. }
  71. func (app *localAppConn) GetHashAsync() {
  72. app.mtx.Lock()
  73. hash, retCode := app.Application.GetHash()
  74. app.mtx.Unlock()
  75. app.Callback(
  76. tmsp.RequestGetHash{},
  77. tmsp.ResponseGetHash{retCode, hash},
  78. )
  79. }
  80. func (app *localAppConn) AddListenerAsync(key string) {
  81. app.mtx.Lock()
  82. retCode := app.Application.AddListener(key)
  83. app.mtx.Unlock()
  84. app.Callback(
  85. tmsp.RequestAddListener{key},
  86. tmsp.ResponseAddListener{retCode},
  87. )
  88. }
  89. func (app *localAppConn) RemListenerAsync(key string) {
  90. app.mtx.Lock()
  91. retCode := app.Application.RemListener(key)
  92. app.mtx.Unlock()
  93. app.Callback(
  94. tmsp.RequestRemListener{key},
  95. tmsp.ResponseRemListener{retCode},
  96. )
  97. }
  98. func (app *localAppConn) InfoSync() (info []string, err error) {
  99. app.mtx.Lock()
  100. info = app.Application.Info()
  101. app.mtx.Unlock()
  102. return info, nil
  103. }
  104. func (app *localAppConn) FlushSync() error {
  105. return nil
  106. }
  107. func (app *localAppConn) GetHashSync() (hash []byte, err error) {
  108. app.mtx.Lock()
  109. hash, retCode := app.Application.GetHash()
  110. app.mtx.Unlock()
  111. return hash, retCode.Error()
  112. }