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
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. )
  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. // Run forever in a loop
  44. remoteApp, err := abcicli.NewClient(r.addr, r.transport, r.mustConnect)
  45. if err != nil {
  46. return nil, fmt.Errorf("Failed to connect to proxy: %v", err)
  47. }
  48. return remoteApp, nil
  49. }
  50. //-----------------------------------------------------------------
  51. // default
  52. func DefaultClientCreator(addr, transport, dbDir string) ClientCreator {
  53. switch addr {
  54. case "dummy":
  55. return NewLocalClientCreator(dummy.NewDummyApplication())
  56. case "persistent_dummy":
  57. return NewLocalClientCreator(dummy.NewPersistentDummyApplication(dbDir))
  58. case "nilapp":
  59. return NewLocalClientCreator(types.NewBaseApplication())
  60. default:
  61. mustConnect := false // loop retrying
  62. return NewRemoteClientCreator(addr, transport, mustConnect)
  63. }
  64. }