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.

82 lines
2.1 KiB

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