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.

81 lines
2.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
  1. package proxy
  2. import (
  3. "sync"
  4. "github.com/pkg/errors"
  5. abcicli "github.com/tendermint/tendermint/abci/client"
  6. "github.com/tendermint/tendermint/abci/example/kvstore"
  7. "github.com/tendermint/tendermint/abci/types"
  8. )
  9. // NewABCIClient returns newly connected client
  10. type ClientCreator interface {
  11. NewABCIClient() (abcicli.Client, error)
  12. }
  13. //----------------------------------------------------
  14. // local proxy uses a mutex on an in-proc app
  15. type localClientCreator struct {
  16. mtx *sync.Mutex
  17. app types.Application
  18. }
  19. func NewLocalClientCreator(app types.Application) ClientCreator {
  20. return &localClientCreator{
  21. mtx: new(sync.Mutex),
  22. app: app,
  23. }
  24. }
  25. func (l *localClientCreator) NewABCIClient() (abcicli.Client, error) {
  26. return abcicli.NewLocalClient(l.mtx, l.app), nil
  27. }
  28. //---------------------------------------------------------------
  29. // remote proxy opens new connections to an external app process
  30. type remoteClientCreator struct {
  31. addr string
  32. transport string
  33. mustConnect bool
  34. }
  35. func NewRemoteClientCreator(addr, transport string, mustConnect bool) ClientCreator {
  36. return &remoteClientCreator{
  37. addr: addr,
  38. transport: transport,
  39. mustConnect: mustConnect,
  40. }
  41. }
  42. func (r *remoteClientCreator) NewABCIClient() (abcicli.Client, error) {
  43. remoteApp, err := abcicli.NewClient(r.addr, r.transport, r.mustConnect)
  44. if err != nil {
  45. return nil, errors.Wrap(err, "Failed to connect to proxy")
  46. }
  47. return remoteApp, nil
  48. }
  49. //-----------------------------------------------------------------
  50. // default
  51. func DefaultClientCreator(addr, transport, dbDir string) ClientCreator {
  52. switch addr {
  53. case "kvstore":
  54. fallthrough
  55. case "dummy":
  56. return NewLocalClientCreator(kvstore.NewKVStoreApplication())
  57. case "persistent_kvstore":
  58. fallthrough
  59. case "persistent_dummy":
  60. return NewLocalClientCreator(kvstore.NewPersistentKVStoreApplication(dbDir))
  61. case "nilapp":
  62. return NewLocalClientCreator(types.NewBaseApplication())
  63. default:
  64. mustConnect := false // loop retrying
  65. return NewRemoteClientCreator(addr, transport, mustConnect)
  66. }
  67. }