tendermint/client/local_client.go

231 lines
5.2 KiB
Go
Raw Normal View History

2017-01-12 12:47:55 -08:00
package abcicli
2016-03-24 10:19:48 -07:00
import (
"sync"
2017-01-12 12:47:55 -08:00
types "github.com/tendermint/abci/types"
2017-04-21 15:25:13 -07:00
cmn "github.com/tendermint/tmlibs/common"
2016-03-24 10:19:48 -07:00
)
var _ Client = (*localClient)(nil)
2016-03-24 10:19:48 -07:00
type localClient struct {
2017-01-23 20:26:17 -08:00
cmn.BaseService
2016-03-24 10:19:48 -07:00
mtx *sync.Mutex
types.Application
Callback
}
func NewLocalClient(mtx *sync.Mutex, app types.Application) *localClient {
if mtx == nil {
mtx = new(sync.Mutex)
}
cli := &localClient{
2016-03-24 10:19:48 -07:00
mtx: mtx,
Application: app,
}
2017-04-27 13:37:18 -07:00
cli.BaseService = *cmn.NewBaseService(nil, "localClient", cli)
return cli
2016-03-24 10:19:48 -07:00
}
func (app *localClient) SetResponseCallback(cb Callback) {
app.mtx.Lock()
defer app.mtx.Unlock()
app.Callback = cb
}
// TODO: change types.Application to include Error()?
func (app *localClient) Error() error {
return nil
}
func (app *localClient) FlushAsync() *ReqRes {
// Do nothing
2016-05-13 23:22:32 -07:00
return newLocalReqRes(types.ToRequestFlush(), nil)
2016-03-24 10:19:48 -07:00
}
func (app *localClient) EchoAsync(msg string) *ReqRes {
return app.callback(
2016-05-13 23:22:32 -07:00
types.ToRequestEcho(msg),
types.ToResponseEcho(msg),
2016-03-24 10:19:48 -07:00
)
}
2017-09-22 08:10:39 -07:00
func (app *localClient) InfoAsync(req types.RequestInfo) *ReqRes {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.Info(req)
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return app.callback(
2017-09-22 08:10:39 -07:00
types.ToRequestInfo(req),
types.ToResponseInfo(res),
2016-03-24 10:19:48 -07:00
)
}
func (app *localClient) SetOptionAsync(req types.RequestSetOption) *ReqRes {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.SetOption(req)
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return app.callback(
types.ToRequestSetOption(req),
types.ToResponseSetOption(res),
2016-03-24 10:19:48 -07:00
)
}
2017-01-12 12:27:08 -08:00
func (app *localClient) DeliverTxAsync(tx []byte) *ReqRes {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
2017-01-12 12:27:08 -08:00
res := app.Application.DeliverTx(tx)
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return app.callback(
2017-01-12 12:27:08 -08:00
types.ToRequestDeliverTx(tx),
types.ToResponseDeliverTx(res),
2016-03-24 10:19:48 -07:00
)
}
func (app *localClient) CheckTxAsync(tx []byte) *ReqRes {
app.mtx.Lock()
res := app.Application.CheckTx(tx)
app.mtx.Unlock()
return app.callback(
2016-05-13 23:22:32 -07:00
types.ToRequestCheckTx(tx),
types.ToResponseCheckTx(res),
2016-03-24 10:19:48 -07:00
)
}
func (app *localClient) QueryAsync(req types.RequestQuery) *ReqRes {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.Query(req)
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return app.callback(
types.ToRequestQuery(req),
types.ToResponseQuery(res),
2016-03-24 10:19:48 -07:00
)
}
func (app *localClient) CommitAsync() *ReqRes {
app.mtx.Lock()
res := app.Application.Commit()
app.mtx.Unlock()
return app.callback(
2016-05-13 23:22:32 -07:00
types.ToRequestCommit(),
types.ToResponseCommit(res),
2016-03-24 10:19:48 -07:00
)
}
func (app *localClient) InitChainAsync(req types.RequestInitChain) *ReqRes {
2016-03-26 22:35:23 -07:00
app.mtx.Lock()
res := app.Application.InitChain(req)
2016-03-26 22:35:23 -07:00
reqRes := app.callback(
types.ToRequestInitChain(req),
types.ToResponseInitChain(res),
2016-03-26 22:35:23 -07:00
)
app.mtx.Unlock()
return reqRes
}
func (app *localClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes {
2016-03-26 22:35:23 -07:00
app.mtx.Lock()
res := app.Application.BeginBlock(req)
2016-03-26 22:35:23 -07:00
app.mtx.Unlock()
return app.callback(
types.ToRequestBeginBlock(req),
types.ToResponseBeginBlock(res),
2016-03-26 22:35:23 -07:00
)
}
func (app *localClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes {
2016-03-26 22:35:23 -07:00
app.mtx.Lock()
res := app.Application.EndBlock(req)
2016-03-26 22:35:23 -07:00
app.mtx.Unlock()
return app.callback(
types.ToRequestEndBlock(req),
types.ToResponseEndBlock(res),
2016-03-26 22:35:23 -07:00
)
}
2016-03-24 10:19:48 -07:00
//-------------------------------------------------------
func (app *localClient) FlushSync() error {
return nil
}
func (app *localClient) EchoSync(msg string) (*types.ResponseEcho, error) {
return &types.ResponseEcho{msg}, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.Info(req)
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.SetOption(req)
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.DeliverTx(tx)
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) CheckTxSync(tx []byte) (*types.ResponseCheckTx, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.CheckTx(tx)
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.Query(req)
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) CommitSync() (*types.ResponseCommit, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.Commit()
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.InitChain(req)
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
func (app *localClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) {
2016-03-26 22:35:23 -07:00
app.mtx.Lock()
res := app.Application.BeginBlock(req)
2016-03-26 22:35:23 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-26 22:35:23 -07:00
}
func (app *localClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) {
2016-03-24 10:19:48 -07:00
app.mtx.Lock()
res := app.Application.EndBlock(req)
2016-03-24 10:19:48 -07:00
app.mtx.Unlock()
return &res, nil
2016-03-24 10:19:48 -07:00
}
//-------------------------------------------------------
func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes {
app.Callback(req, res)
return newLocalReqRes(req, res)
}
func newLocalReqRes(req *types.Request, res *types.Response) *ReqRes {
reqRes := NewReqRes(req)
reqRes.Response = res
reqRes.SetDone()
return reqRes
}