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.

100 lines
2.0 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) {
  28. app.Callback(
  29. tmsp.RequestEcho{msg},
  30. tmsp.ResponseEcho{msg},
  31. )
  32. }
  33. func (app *localAppConn) FlushAsync() {
  34. // Do nothing
  35. }
  36. func (app *localAppConn) SetOptionAsync(key string, value string) {
  37. app.mtx.Lock()
  38. log := app.Application.SetOption(key, value)
  39. app.mtx.Unlock()
  40. app.Callback(
  41. tmsp.RequestSetOption{key, value},
  42. tmsp.ResponseSetOption{log},
  43. )
  44. }
  45. func (app *localAppConn) AppendTxAsync(tx []byte) {
  46. app.mtx.Lock()
  47. code, result, log := app.Application.AppendTx(tx)
  48. app.mtx.Unlock()
  49. app.Callback(
  50. tmsp.RequestAppendTx{tx},
  51. tmsp.ResponseAppendTx{code, result, log},
  52. )
  53. }
  54. func (app *localAppConn) CheckTxAsync(tx []byte) {
  55. app.mtx.Lock()
  56. code, result, log := app.Application.CheckTx(tx)
  57. app.mtx.Unlock()
  58. app.Callback(
  59. tmsp.RequestCheckTx{tx},
  60. tmsp.ResponseCheckTx{code, result, log},
  61. )
  62. }
  63. func (app *localAppConn) GetHashAsync() {
  64. app.mtx.Lock()
  65. hash, log := app.Application.GetHash()
  66. app.mtx.Unlock()
  67. app.Callback(
  68. tmsp.RequestGetHash{},
  69. tmsp.ResponseGetHash{hash, log},
  70. )
  71. }
  72. func (app *localAppConn) InfoSync() (info string, err error) {
  73. app.mtx.Lock()
  74. info = app.Application.Info()
  75. app.mtx.Unlock()
  76. return info, nil
  77. }
  78. func (app *localAppConn) FlushSync() error {
  79. return nil
  80. }
  81. func (app *localAppConn) GetHashSync() (hash []byte, log string, err error) {
  82. app.mtx.Lock()
  83. hash, log = app.Application.GetHash()
  84. app.mtx.Unlock()
  85. return hash, log, nil
  86. }