tendermint/proxy/local_app_conn.go

101 lines
2.0 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
}
func (app *localAppConn) EchoAsync(msg string) {
app.Callback(
tmsp.RequestEcho{msg},
2016-01-25 14:34:08 -08:00
tmsp.ResponseEcho{msg},
)
}
func (app *localAppConn) FlushAsync() {
// Do nothing
}
func (app *localAppConn) SetOptionAsync(key string, value string) {
app.mtx.Lock()
2016-01-25 14:34:08 -08:00
log := app.Application.SetOption(key, value)
app.mtx.Unlock()
app.Callback(
tmsp.RequestSetOption{key, value},
2016-01-25 14:34:08 -08:00
tmsp.ResponseSetOption{log},
)
}
func (app *localAppConn) AppendTxAsync(tx []byte) {
app.mtx.Lock()
2016-01-25 14:34:08 -08:00
code, result, log := app.Application.AppendTx(tx)
app.mtx.Unlock()
app.Callback(
tmsp.RequestAppendTx{tx},
2016-01-25 14:34:08 -08:00
tmsp.ResponseAppendTx{code, result, log},
)
}
func (app *localAppConn) CheckTxAsync(tx []byte) {
app.mtx.Lock()
2016-01-25 14:34:08 -08:00
code, result, log := app.Application.CheckTx(tx)
app.mtx.Unlock()
app.Callback(
tmsp.RequestCheckTx{tx},
2016-01-25 14:34:08 -08:00
tmsp.ResponseCheckTx{code, result, log},
)
}
func (app *localAppConn) GetHashAsync() {
app.mtx.Lock()
2016-01-25 14:34:08 -08:00
hash, log := app.Application.GetHash()
app.mtx.Unlock()
app.Callback(
tmsp.RequestGetHash{},
2016-01-25 14:34:08 -08:00
tmsp.ResponseGetHash{hash, log},
)
}
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-01-25 14:34:08 -08:00
func (app *localAppConn) GetHashSync() (hash []byte, log string, err error) {
app.mtx.Lock()
2016-01-25 14:34:08 -08:00
hash, log = app.Application.GetHash()
app.mtx.Unlock()
2016-01-25 14:34:08 -08:00
return hash, log, nil
}