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.

97 lines
3.0 KiB

8 years ago
8 years ago
8 years ago
  1. package proxy
  2. import (
  3. "fmt"
  4. "io"
  5. abcicli "github.com/tendermint/tendermint/abci/client"
  6. "github.com/tendermint/tendermint/abci/example/counter"
  7. "github.com/tendermint/tendermint/abci/example/kvstore"
  8. "github.com/tendermint/tendermint/abci/types"
  9. tmsync "github.com/tendermint/tendermint/internal/libs/sync"
  10. )
  11. // ClientCreator creates new ABCI clients.
  12. type ClientCreator interface {
  13. // NewABCIClient returns a new ABCI client.
  14. NewABCIClient() (abcicli.Client, error)
  15. }
  16. //----------------------------------------------------
  17. // local proxy uses a mutex on an in-proc app
  18. type localClientCreator struct {
  19. mtx *tmsync.RWMutex
  20. app types.Application
  21. }
  22. // NewLocalClientCreator returns a ClientCreator for the given app,
  23. // which will be running locally.
  24. func NewLocalClientCreator(app types.Application) ClientCreator {
  25. return &localClientCreator{
  26. mtx: new(tmsync.RWMutex),
  27. app: app,
  28. }
  29. }
  30. func (l *localClientCreator) NewABCIClient() (abcicli.Client, error) {
  31. return abcicli.NewLocalClient(l.mtx, l.app), nil
  32. }
  33. //---------------------------------------------------------------
  34. // remote proxy opens new connections to an external app process
  35. type remoteClientCreator struct {
  36. addr string
  37. transport string
  38. mustConnect bool
  39. }
  40. // NewRemoteClientCreator returns a ClientCreator for the given address (e.g.
  41. // "192.168.0.1") and transport (e.g. "tcp"). Set mustConnect to true if you
  42. // want the client to connect before reporting success.
  43. func NewRemoteClientCreator(addr, transport string, mustConnect bool) ClientCreator {
  44. return &remoteClientCreator{
  45. addr: addr,
  46. transport: transport,
  47. mustConnect: mustConnect,
  48. }
  49. }
  50. func (r *remoteClientCreator) NewABCIClient() (abcicli.Client, error) {
  51. remoteApp, err := abcicli.NewClient(r.addr, r.transport, r.mustConnect)
  52. if err != nil {
  53. return nil, fmt.Errorf("failed to connect to proxy: %w", err)
  54. }
  55. return remoteApp, nil
  56. }
  57. // DefaultClientCreator returns a default ClientCreator, which will create a
  58. // local client if addr is one of: 'counter', 'counter_serial', 'kvstore',
  59. // 'persistent_kvstore' or 'noop', otherwise - a remote client.
  60. //
  61. // The Closer is a noop except for persistent_kvstore applications,
  62. // which will clean up the store.
  63. func DefaultClientCreator(addr, transport, dbDir string) (ClientCreator, io.Closer) {
  64. switch addr {
  65. case "counter":
  66. return NewLocalClientCreator(counter.NewApplication(false)), noopCloser{}
  67. case "counter_serial":
  68. return NewLocalClientCreator(counter.NewApplication(true)), noopCloser{}
  69. case "kvstore":
  70. return NewLocalClientCreator(kvstore.NewApplication()), noopCloser{}
  71. case "persistent_kvstore":
  72. app := kvstore.NewPersistentKVStoreApplication(dbDir)
  73. return NewLocalClientCreator(app), app
  74. case "noop":
  75. return NewLocalClientCreator(types.NewBaseApplication()), noopCloser{}
  76. default:
  77. mustConnect := false // loop retrying
  78. return NewRemoteClientCreator(addr, transport, mustConnect), noopCloser{}
  79. }
  80. }
  81. type noopCloser struct{}
  82. func (noopCloser) Close() error { return nil }