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.

77 lines
1.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package proxy
  2. import (
  3. "sync"
  4. "github.com/pkg/errors"
  5. abcicli "github.com/tendermint/abci/client"
  6. "github.com/tendermint/abci/example/dummy"
  7. "github.com/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 "dummy":
  54. return NewLocalClientCreator(dummy.NewDummyApplication())
  55. case "persistent_dummy":
  56. return NewLocalClientCreator(dummy.NewPersistentDummyApplication(dbDir))
  57. case "nilapp":
  58. return NewLocalClientCreator(types.NewBaseApplication())
  59. default:
  60. mustConnect := false // loop retrying
  61. return NewRemoteClientCreator(addr, transport, mustConnect)
  62. }
  63. }