tendermint/proxy/local_app_conn.go

107 lines
2.3 KiB
Go
Raw Normal View History

package proxy
import (
2016-01-25 14:34:08 -08:00
tmspcli "github.com/tendermint/tmsp/client"
tmsp "github.com/tendermint/tmsp/types"
"sync"
)
type localAppConn struct {
mtx *sync.Mutex
tmsp.Application
2016-01-22 15:48:13 -08:00
tmspcli.Callback
}
func NewLocalAppConn(mtx *sync.Mutex, app tmsp.Application) *localAppConn {
return &localAppConn{
mtx: mtx,
Application: app,
}
}
2016-01-22 15:48:13 -08:00
func (app *localAppConn) SetResponseCallback(cb tmspcli.Callback) {
app.mtx.Lock()
defer app.mtx.Unlock()
app.Callback = cb
}
// TODO: change tmsp.Application to include Error()?
func (app *localAppConn) Error() error {
return nil
}
2016-02-08 00:48:58 -08:00
func (app *localAppConn) EchoAsync(msg string) *tmspcli.ReqRes {
app.Callback(
2016-01-31 08:11:50 -08:00
tmsp.RequestEcho(msg),
tmsp.ResponseEcho(msg),
)
2016-02-08 00:48:58 -08:00
return nil // TODO maybe create a ReqRes
}
2016-02-08 00:48:58 -08:00
func (app *localAppConn) FlushAsync() *tmspcli.ReqRes {
// Do nothing
2016-02-08 00:48:58 -08:00
return nil // TODO maybe create a ReqRes
}
2016-02-08 00:48:58 -08:00
func (app *localAppConn) SetOptionAsync(key string, value string) *tmspcli.ReqRes {
app.mtx.Lock()
2016-01-25 14:34:08 -08:00
log := app.Application.SetOption(key, value)
app.mtx.Unlock()
app.Callback(
2016-01-31 08:11:50 -08:00
tmsp.RequestSetOption(key, value),
tmsp.ResponseSetOption(log),
)
2016-02-08 00:48:58 -08:00
return nil // TODO maybe create a ReqRes
}
2016-02-08 00:48:58 -08:00
func (app *localAppConn) AppendTxAsync(tx []byte) *tmspcli.ReqRes {
app.mtx.Lock()
2016-01-25 14:34:08 -08:00
code, result, log := app.Application.AppendTx(tx)
app.mtx.Unlock()
app.Callback(
2016-01-31 08:11:50 -08:00
tmsp.RequestAppendTx(tx),
tmsp.ResponseAppendTx(code, result, log),
)
2016-02-08 00:48:58 -08:00
return nil // TODO maybe create a ReqRes
}
2016-02-08 00:48:58 -08:00
func (app *localAppConn) CheckTxAsync(tx []byte) *tmspcli.ReqRes {
app.mtx.Lock()
2016-01-25 14:34:08 -08:00
code, result, log := app.Application.CheckTx(tx)
app.mtx.Unlock()
app.Callback(
2016-01-31 08:11:50 -08:00
tmsp.RequestCheckTx(tx),
tmsp.ResponseCheckTx(code, result, log),
)
2016-02-08 00:48:58 -08:00
return nil // TODO maybe create a ReqRes
}
2016-02-14 13:11:01 -08:00
func (app *localAppConn) CommitAsync() *tmspcli.ReqRes {
app.mtx.Lock()
2016-02-14 13:11:01 -08:00
hash, log := app.Application.Commit()
app.mtx.Unlock()
app.Callback(
2016-02-14 13:11:01 -08:00
tmsp.RequestCommit(),
tmsp.ResponseCommit(hash, log),
)
2016-02-08 00:48:58 -08:00
return nil // TODO maybe create a ReqRes
}
2016-01-25 14:34:08 -08:00
func (app *localAppConn) InfoSync() (info string, err error) {
app.mtx.Lock()
info = app.Application.Info()
app.mtx.Unlock()
return info, nil
}
func (app *localAppConn) FlushSync() error {
return nil
}
2016-02-14 13:11:01 -08:00
func (app *localAppConn) CommitSync() (hash []byte, log string, err error) {
app.mtx.Lock()
2016-02-14 13:11:01 -08:00
hash, log = app.Application.Commit()
app.mtx.Unlock()
2016-01-25 14:34:08 -08:00
return hash, log, nil
}