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