|
|
@ -4,40 +4,77 @@ import ( |
|
|
|
"fmt" |
|
|
|
"sync" |
|
|
|
|
|
|
|
cfg "github.com/tendermint/go-config" |
|
|
|
tmspcli "github.com/tendermint/tmsp/client" |
|
|
|
"github.com/tendermint/tmsp/example/dummy" |
|
|
|
nilapp "github.com/tendermint/tmsp/example/nil" |
|
|
|
"github.com/tendermint/tmsp/types" |
|
|
|
) |
|
|
|
|
|
|
|
// Function type to get a connected tmsp client
|
|
|
|
// Allows consumers to provide their own in-proc apps,
|
|
|
|
// or to implement alternate address schemes and transports
|
|
|
|
type NewTMSPClient func(addr, transport string) (tmspcli.Client, error) |
|
|
|
// NewTMSPClient returns newly connected client
|
|
|
|
type ClientCreator interface { |
|
|
|
NewTMSPClient() (tmspcli.Client, error) |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------------------------------------
|
|
|
|
// local proxy uses a mutex on an in-proc app
|
|
|
|
|
|
|
|
type localClientCreator struct { |
|
|
|
mtx *sync.Mutex |
|
|
|
app types.Application |
|
|
|
} |
|
|
|
|
|
|
|
func NewLocalClientCreator(app types.Application) ClientCreator { |
|
|
|
return &localClientCreator{ |
|
|
|
mtx: new(sync.Mutex), |
|
|
|
app: app, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (l *localClientCreator) NewTMSPClient() (tmspcli.Client, error) { |
|
|
|
return tmspcli.NewLocalClient(l.mtx, l.app), nil |
|
|
|
} |
|
|
|
|
|
|
|
//---------------------------------------------------------------
|
|
|
|
// remote proxy opens new connections to an external app process
|
|
|
|
|
|
|
|
type remoteClientCreator struct { |
|
|
|
addr string |
|
|
|
transport string |
|
|
|
mustConnect bool |
|
|
|
} |
|
|
|
|
|
|
|
func NewRemoteClientCreator(addr, transport string, mustConnect bool) ClientCreator { |
|
|
|
return &remoteClientCreator{ |
|
|
|
addr: addr, |
|
|
|
transport: transport, |
|
|
|
mustConnect: mustConnect, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (r *remoteClientCreator) NewTMSPClient() (tmspcli.Client, error) { |
|
|
|
// Run forever in a loop
|
|
|
|
remoteApp, err := tmspcli.NewClient(r.addr, r.transport, r.mustConnect) |
|
|
|
if err != nil { |
|
|
|
return nil, fmt.Errorf("Failed to connect to proxy: %v", err) |
|
|
|
} |
|
|
|
return remoteApp, nil |
|
|
|
} |
|
|
|
|
|
|
|
//-----------------------------------------------------------------
|
|
|
|
// default
|
|
|
|
|
|
|
|
// Get a connected tmsp client.
|
|
|
|
// Offers some default in-proc apps, else socket/grpc.
|
|
|
|
func NewTMSPClientDefault(addr, transport string) (tmspcli.Client, error) { |
|
|
|
var client tmspcli.Client |
|
|
|
func DefaultClientCreator(config cfg.Config) ClientCreator { |
|
|
|
addr := config.GetString("proxy_app") |
|
|
|
transport := config.GetString("tmsp") |
|
|
|
|
|
|
|
// use local app (for testing)
|
|
|
|
// TODO: local proxy app conn
|
|
|
|
switch addr { |
|
|
|
case "nilapp": |
|
|
|
app := nilapp.NewNilApplication() |
|
|
|
mtx := new(sync.Mutex) // TODO
|
|
|
|
client = tmspcli.NewLocalClient(mtx, app) |
|
|
|
case "dummy": |
|
|
|
app := dummy.NewDummyApplication() |
|
|
|
mtx := new(sync.Mutex) // TODO
|
|
|
|
client = tmspcli.NewLocalClient(mtx, app) |
|
|
|
return NewLocalClientCreator(dummy.NewDummyApplication()) |
|
|
|
case "nil": |
|
|
|
return NewLocalClientCreator(nilapp.NewNilApplication()) |
|
|
|
default: |
|
|
|
// Run forever in a loop
|
|
|
|
mustConnect := false |
|
|
|
remoteApp, err := tmspcli.NewClient(addr, transport, mustConnect) |
|
|
|
if err != nil { |
|
|
|
return nil, fmt.Errorf("Failed to connect to proxy for mempool: %v", err) |
|
|
|
} |
|
|
|
client = remoteApp |
|
|
|
mustConnect := true |
|
|
|
return NewRemoteClientCreator(addr, transport, mustConnect) |
|
|
|
} |
|
|
|
return client, nil |
|
|
|
} |