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.

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