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.

80 lines
2.0 KiB

  1. package proxy
  2. import (
  3. "fmt"
  4. "sync"
  5. cfg "github.com/tendermint/go-config"
  6. tmspcli "github.com/tendermint/tmsp/client"
  7. "github.com/tendermint/tmsp/example/dummy"
  8. nilapp "github.com/tendermint/tmsp/example/nil"
  9. "github.com/tendermint/tmsp/types"
  10. )
  11. // NewTMSPClient returns newly connected client
  12. type ClientCreator interface {
  13. NewTMSPClient() (tmspcli.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) NewTMSPClient() (tmspcli.Client, error) {
  28. return tmspcli.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) NewTMSPClient() (tmspcli.Client, error) {
  45. // Run forever in a loop
  46. remoteApp, err := tmspcli.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("tmsp")
  57. switch addr {
  58. case "dummy":
  59. return NewLocalClientCreator(dummy.NewDummyApplication())
  60. case "nilapp":
  61. return NewLocalClientCreator(nilapp.NewNilApplication())
  62. default:
  63. mustConnect := false // loop retrying
  64. return NewRemoteClientCreator(addr, transport, mustConnect)
  65. }
  66. }