tendermint/proxy/client.go

82 lines
2.0 KiB
Go
Raw Normal View History

package proxy
import (
"sync"
2017-05-16 10:06:35 -07:00
"github.com/pkg/errors"
2018-06-21 21:59:02 -07:00
abcicli "github.com/tendermint/tendermint/abci/client"
"github.com/tendermint/tendermint/abci/example/kvstore"
"github.com/tendermint/tendermint/abci/types"
)
2017-01-12 12:53:32 -08:00
// NewABCIClient returns newly connected client
type ClientCreator interface {
2017-01-12 12:53:32 -08:00
NewABCIClient() (abcicli.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,
}
}
2017-01-12 12:53:32 -08:00
func (l *localClientCreator) NewABCIClient() (abcicli.Client, error) {
return abcicli.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,
}
}
2017-01-12 12:53:32 -08:00
func (r *remoteClientCreator) NewABCIClient() (abcicli.Client, error) {
remoteApp, err := abcicli.NewClient(r.addr, r.transport, r.mustConnect)
if err != nil {
2017-05-16 10:06:35 -07:00
return nil, errors.Wrap(err, "Failed to connect to proxy")
}
return remoteApp, nil
}
//-----------------------------------------------------------------
// default
2017-04-28 21:04:01 -07:00
func DefaultClientCreator(addr, transport, dbDir string) ClientCreator {
switch addr {
case "kvstore":
fallthrough
case "dummy":
return NewLocalClientCreator(kvstore.NewKVStoreApplication())
case "persistent_kvstore":
fallthrough
case "persistent_dummy":
return NewLocalClientCreator(kvstore.NewPersistentKVStoreApplication(dbDir))
2016-09-10 17:46:12 -07:00
case "nilapp":
2017-02-13 18:07:26 -08:00
return NewLocalClientCreator(types.NewBaseApplication())
default:
mustConnect := false // loop retrying
return NewRemoteClientCreator(addr, transport, mustConnect)
}
}