github.com/evdatsion/aphelion-dpos-bft@v0.32.1/proxy/client.go (about) 1 package proxy 2 3 import ( 4 "sync" 5 6 "github.com/pkg/errors" 7 8 abcicli "github.com/evdatsion/aphelion-dpos-bft/abci/client" 9 "github.com/evdatsion/aphelion-dpos-bft/abci/example/counter" 10 "github.com/evdatsion/aphelion-dpos-bft/abci/example/kvstore" 11 "github.com/evdatsion/aphelion-dpos-bft/abci/types" 12 ) 13 14 // NewABCIClient returns newly connected client 15 type ClientCreator interface { 16 NewABCIClient() (abcicli.Client, error) 17 } 18 19 //---------------------------------------------------- 20 // local proxy uses a mutex on an in-proc app 21 22 type localClientCreator struct { 23 mtx *sync.Mutex 24 app types.Application 25 } 26 27 func NewLocalClientCreator(app types.Application) ClientCreator { 28 return &localClientCreator{ 29 mtx: new(sync.Mutex), 30 app: app, 31 } 32 } 33 34 func (l *localClientCreator) NewABCIClient() (abcicli.Client, error) { 35 return abcicli.NewLocalClient(l.mtx, l.app), nil 36 } 37 38 //--------------------------------------------------------------- 39 // remote proxy opens new connections to an external app process 40 41 type remoteClientCreator struct { 42 addr string 43 transport string 44 mustConnect bool 45 } 46 47 func NewRemoteClientCreator(addr, transport string, mustConnect bool) ClientCreator { 48 return &remoteClientCreator{ 49 addr: addr, 50 transport: transport, 51 mustConnect: mustConnect, 52 } 53 } 54 55 func (r *remoteClientCreator) NewABCIClient() (abcicli.Client, error) { 56 remoteApp, err := abcicli.NewClient(r.addr, r.transport, r.mustConnect) 57 if err != nil { 58 return nil, errors.Wrap(err, "Failed to connect to proxy") 59 } 60 return remoteApp, nil 61 } 62 63 //----------------------------------------------------------------- 64 // default 65 66 func DefaultClientCreator(addr, transport, dbDir string) ClientCreator { 67 switch addr { 68 case "counter": 69 return NewLocalClientCreator(counter.NewCounterApplication(false)) 70 case "counter_serial": 71 return NewLocalClientCreator(counter.NewCounterApplication(true)) 72 case "kvstore": 73 return NewLocalClientCreator(kvstore.NewKVStoreApplication()) 74 case "persistent_kvstore": 75 return NewLocalClientCreator(kvstore.NewPersistentKVStoreApplication(dbDir)) 76 case "noop": 77 return NewLocalClientCreator(types.NewBaseApplication()) 78 default: 79 mustConnect := false // loop retrying 80 return NewRemoteClientCreator(addr, transport, mustConnect) 81 } 82 }