|
|
@ -6,6 +6,7 @@ import ( |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"net" |
|
|
|
"reflect" |
|
|
|
"sync" |
|
|
|
"time" |
|
|
|
|
|
|
@ -13,6 +14,11 @@ import ( |
|
|
|
"github.com/tendermint/tmsp/types" |
|
|
|
) |
|
|
|
|
|
|
|
const ( |
|
|
|
OK = types.CodeType_OK |
|
|
|
LOG = "" |
|
|
|
) |
|
|
|
|
|
|
|
const reqQueueSize = 256 // TODO make configurable
|
|
|
|
const maxResponseSize = 1048576 // 1MB TODO make configurable
|
|
|
|
const flushThrottleMS = 20 // Don't wait longer than...
|
|
|
@ -36,7 +42,7 @@ type remoteClient struct { |
|
|
|
resCb func(*types.Request, *types.Response) // listens to all callbacks
|
|
|
|
} |
|
|
|
|
|
|
|
func NewClient(addr string, mustConnect bool) (*remoteClient, error) { |
|
|
|
func NewSocketClient(addr string, mustConnect bool) (*remoteClient, error) { |
|
|
|
cli := &remoteClient{ |
|
|
|
reqQueue: make(chan *ReqRes, reqQueueSize), |
|
|
|
flushTimer: NewThrottleTimer("remoteClient", flushThrottleMS), |
|
|
@ -48,39 +54,28 @@ func NewClient(addr string, mustConnect bool) (*remoteClient, error) { |
|
|
|
} |
|
|
|
cli.QuitService = *NewQuitService(nil, "remoteClient", cli) |
|
|
|
_, err := cli.Start() // Just start it, it's confusing for callers to remember to start.
|
|
|
|
if mustConnect { |
|
|
|
return nil, err |
|
|
|
} else { |
|
|
|
return cli, nil |
|
|
|
} |
|
|
|
return cli, err |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) OnStart() (err error) { |
|
|
|
func (cli *remoteClient) OnStart() error { |
|
|
|
cli.QuitService.OnStart() |
|
|
|
doneCh := make(chan struct{}) |
|
|
|
go func() { |
|
|
|
RETRY_LOOP: |
|
|
|
for { |
|
|
|
conn, err_ := Connect(cli.addr) |
|
|
|
if err_ != nil { |
|
|
|
if cli.mustConnect { |
|
|
|
err = err_ // OnStart() will return this.
|
|
|
|
close(doneCh) |
|
|
|
return |
|
|
|
} else { |
|
|
|
fmt.Printf("tmsp.remoteClient failed to connect to %v. Retrying...\n", cli.addr) |
|
|
|
time.Sleep(time.Second * 3) |
|
|
|
continue RETRY_LOOP |
|
|
|
} |
|
|
|
RETRY_LOOP: |
|
|
|
for { |
|
|
|
conn, err := Connect(cli.addr) |
|
|
|
if err != nil { |
|
|
|
if cli.mustConnect { |
|
|
|
return err |
|
|
|
} else { |
|
|
|
fmt.Printf("tmsp.remoteClient failed to connect to %v. Retrying...\n", cli.addr) |
|
|
|
time.Sleep(time.Second * 3) |
|
|
|
continue RETRY_LOOP |
|
|
|
} |
|
|
|
go cli.sendRequestsRoutine(conn) |
|
|
|
go cli.recvResponseRoutine(conn) |
|
|
|
close(doneCh) // OnStart() will return no error.
|
|
|
|
return |
|
|
|
} |
|
|
|
}() |
|
|
|
<-doneCh |
|
|
|
return // err
|
|
|
|
go cli.sendRequestsRoutine(conn) |
|
|
|
go cli.recvResponseRoutine(conn) |
|
|
|
return err |
|
|
|
} |
|
|
|
return nil // never happens
|
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) OnStop() { |
|
|
@ -122,7 +117,7 @@ func (cli *remoteClient) sendRequestsRoutine(conn net.Conn) { |
|
|
|
select { |
|
|
|
case <-cli.flushTimer.Ch: |
|
|
|
select { |
|
|
|
case cli.reqQueue <- NewReqRes(types.RequestFlush()): |
|
|
|
case cli.reqQueue <- NewReqRes(types.ToRequestFlush()): // cant this block ?
|
|
|
|
default: |
|
|
|
// Probably will fill the buffer, or retry later.
|
|
|
|
} |
|
|
@ -136,7 +131,7 @@ func (cli *remoteClient) sendRequestsRoutine(conn net.Conn) { |
|
|
|
return |
|
|
|
} |
|
|
|
// log.Debug("Sent request", "requestType", reflect.TypeOf(reqres.Request), "request", reqres.Request)
|
|
|
|
if reqres.Request.Type == types.MessageType_Flush { |
|
|
|
if _, ok := reqres.Request.Value.(*types.Request_Flush); ok { |
|
|
|
err = w.Flush() |
|
|
|
if err != nil { |
|
|
|
cli.StopForError(err) |
|
|
@ -156,10 +151,10 @@ func (cli *remoteClient) recvResponseRoutine(conn net.Conn) { |
|
|
|
cli.StopForError(err) |
|
|
|
return |
|
|
|
} |
|
|
|
switch res.Type { |
|
|
|
case types.MessageType_Exception: |
|
|
|
switch r := res.Value.(type) { |
|
|
|
case *types.Response_Exception: |
|
|
|
// XXX After setting cli.err, release waiters (e.g. reqres.Done())
|
|
|
|
cli.StopForError(errors.New(res.Error)) |
|
|
|
cli.StopForError(errors.New(r.Exception.Error)) |
|
|
|
default: |
|
|
|
// log.Debug("Received response", "responseType", reflect.TypeOf(res), "response", res)
|
|
|
|
err := cli.didRecvResponse(res) |
|
|
@ -183,12 +178,12 @@ func (cli *remoteClient) didRecvResponse(res *types.Response) error { |
|
|
|
// Get the first ReqRes
|
|
|
|
next := cli.reqSent.Front() |
|
|
|
if next == nil { |
|
|
|
return fmt.Errorf("Unexpected result type %v when nothing expected", res.Type) |
|
|
|
return fmt.Errorf("Unexpected result type %v when nothing expected", reflect.TypeOf(res.Value)) |
|
|
|
} |
|
|
|
reqres := next.Value.(*ReqRes) |
|
|
|
if !resMatchesReq(reqres.Request, res) { |
|
|
|
return fmt.Errorf("Unexpected result type %v when response to %v expected", |
|
|
|
res.Type, reqres.Request.Type) |
|
|
|
reflect.TypeOf(res.Value), reflect.TypeOf(reqres.Request.Value)) |
|
|
|
} |
|
|
|
|
|
|
|
reqres.Response = res // Set response
|
|
|
@ -211,128 +206,128 @@ func (cli *remoteClient) didRecvResponse(res *types.Response) error { |
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
func (cli *remoteClient) EchoAsync(msg string) *ReqRes { |
|
|
|
return cli.queueRequest(types.RequestEcho(msg)) |
|
|
|
return cli.queueRequest(types.ToRequestEcho(msg)) |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) FlushAsync() *ReqRes { |
|
|
|
return cli.queueRequest(types.RequestFlush()) |
|
|
|
return cli.queueRequest(types.ToRequestFlush()) |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) InfoAsync() *ReqRes { |
|
|
|
return cli.queueRequest(types.RequestInfo()) |
|
|
|
return cli.queueRequest(types.ToRequestInfo()) |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) SetOptionAsync(key string, value string) *ReqRes { |
|
|
|
return cli.queueRequest(types.RequestSetOption(key, value)) |
|
|
|
return cli.queueRequest(types.ToRequestSetOption(key, value)) |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) AppendTxAsync(tx []byte) *ReqRes { |
|
|
|
return cli.queueRequest(types.RequestAppendTx(tx)) |
|
|
|
return cli.queueRequest(types.ToRequestAppendTx(tx)) |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) CheckTxAsync(tx []byte) *ReqRes { |
|
|
|
return cli.queueRequest(types.RequestCheckTx(tx)) |
|
|
|
return cli.queueRequest(types.ToRequestCheckTx(tx)) |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) QueryAsync(query []byte) *ReqRes { |
|
|
|
return cli.queueRequest(types.RequestQuery(query)) |
|
|
|
return cli.queueRequest(types.ToRequestQuery(query)) |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) CommitAsync() *ReqRes { |
|
|
|
return cli.queueRequest(types.RequestCommit()) |
|
|
|
return cli.queueRequest(types.ToRequestCommit()) |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) InitChainAsync(validators []*types.Validator) *ReqRes { |
|
|
|
return cli.queueRequest(types.RequestInitChain(validators)) |
|
|
|
return cli.queueRequest(types.ToRequestInitChain(validators)) |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) BeginBlockAsync(height uint64) *ReqRes { |
|
|
|
return cli.queueRequest(types.RequestBeginBlock(height)) |
|
|
|
return cli.queueRequest(types.ToRequestBeginBlock(height)) |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) EndBlockAsync(height uint64) *ReqRes { |
|
|
|
return cli.queueRequest(types.RequestEndBlock(height)) |
|
|
|
return cli.queueRequest(types.ToRequestEndBlock(height)) |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
func (cli *remoteClient) EchoSync(msg string) (res types.Result) { |
|
|
|
reqres := cli.queueRequest(types.RequestEcho(msg)) |
|
|
|
reqres := cli.queueRequest(types.ToRequestEcho(msg)) |
|
|
|
cli.FlushSync() |
|
|
|
if cli.err != nil { |
|
|
|
return types.ErrInternalError.SetLog(cli.err.Error()) |
|
|
|
} |
|
|
|
resp := reqres.Response |
|
|
|
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} |
|
|
|
resp := reqres.Response.GetEcho() |
|
|
|
return types.Result{Code: OK, Data: []byte(resp.Message), Log: LOG} |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) FlushSync() error { |
|
|
|
cli.queueRequest(types.RequestFlush()).Wait() |
|
|
|
cli.queueRequest(types.ToRequestFlush()).Wait() |
|
|
|
return cli.err |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) InfoSync() (res types.Result) { |
|
|
|
reqres := cli.queueRequest(types.RequestInfo()) |
|
|
|
reqres := cli.queueRequest(types.ToRequestInfo()) |
|
|
|
cli.FlushSync() |
|
|
|
if cli.err != nil { |
|
|
|
return types.ErrInternalError.SetLog(cli.err.Error()) |
|
|
|
} |
|
|
|
resp := reqres.Response |
|
|
|
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} |
|
|
|
resp := reqres.Response.GetInfo() |
|
|
|
return types.Result{Code: OK, Data: []byte(resp.Info), Log: LOG} |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) SetOptionSync(key string, value string) (res types.Result) { |
|
|
|
reqres := cli.queueRequest(types.RequestSetOption(key, value)) |
|
|
|
reqres := cli.queueRequest(types.ToRequestSetOption(key, value)) |
|
|
|
cli.FlushSync() |
|
|
|
if cli.err != nil { |
|
|
|
return types.ErrInternalError.SetLog(cli.err.Error()) |
|
|
|
} |
|
|
|
resp := reqres.Response |
|
|
|
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} |
|
|
|
resp := reqres.Response.GetSetOption() |
|
|
|
return types.Result{Code: OK, Data: nil, Log: resp.Log} |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) AppendTxSync(tx []byte) (res types.Result) { |
|
|
|
reqres := cli.queueRequest(types.RequestAppendTx(tx)) |
|
|
|
reqres := cli.queueRequest(types.ToRequestAppendTx(tx)) |
|
|
|
cli.FlushSync() |
|
|
|
if cli.err != nil { |
|
|
|
return types.ErrInternalError.SetLog(cli.err.Error()) |
|
|
|
} |
|
|
|
resp := reqres.Response |
|
|
|
resp := reqres.Response.GetAppendTx() |
|
|
|
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) CheckTxSync(tx []byte) (res types.Result) { |
|
|
|
reqres := cli.queueRequest(types.RequestCheckTx(tx)) |
|
|
|
reqres := cli.queueRequest(types.ToRequestCheckTx(tx)) |
|
|
|
cli.FlushSync() |
|
|
|
if cli.err != nil { |
|
|
|
return types.ErrInternalError.SetLog(cli.err.Error()) |
|
|
|
} |
|
|
|
resp := reqres.Response |
|
|
|
resp := reqres.Response.GetCheckTx() |
|
|
|
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) QuerySync(query []byte) (res types.Result) { |
|
|
|
reqres := cli.queueRequest(types.RequestQuery(query)) |
|
|
|
reqres := cli.queueRequest(types.ToRequestQuery(query)) |
|
|
|
cli.FlushSync() |
|
|
|
if cli.err != nil { |
|
|
|
return types.ErrInternalError.SetLog(cli.err.Error()) |
|
|
|
} |
|
|
|
resp := reqres.Response |
|
|
|
resp := reqres.Response.GetQuery() |
|
|
|
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) CommitSync() (res types.Result) { |
|
|
|
reqres := cli.queueRequest(types.RequestCommit()) |
|
|
|
reqres := cli.queueRequest(types.ToRequestCommit()) |
|
|
|
cli.FlushSync() |
|
|
|
if cli.err != nil { |
|
|
|
return types.ErrInternalError.SetLog(cli.err.Error()) |
|
|
|
} |
|
|
|
resp := reqres.Response |
|
|
|
resp := reqres.Response.GetCommit() |
|
|
|
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) InitChainSync(validators []*types.Validator) (err error) { |
|
|
|
cli.queueRequest(types.RequestInitChain(validators)) |
|
|
|
cli.queueRequest(types.ToRequestInitChain(validators)) |
|
|
|
cli.FlushSync() |
|
|
|
if cli.err != nil { |
|
|
|
return cli.err |
|
|
@ -341,7 +336,7 @@ func (cli *remoteClient) InitChainSync(validators []*types.Validator) (err error |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) BeginBlockSync(height uint64) (err error) { |
|
|
|
cli.queueRequest(types.RequestBeginBlock(height)) |
|
|
|
cli.queueRequest(types.ToRequestBeginBlock(height)) |
|
|
|
cli.FlushSync() |
|
|
|
if cli.err != nil { |
|
|
|
return cli.err |
|
|
@ -350,24 +345,25 @@ func (cli *remoteClient) BeginBlockSync(height uint64) (err error) { |
|
|
|
} |
|
|
|
|
|
|
|
func (cli *remoteClient) EndBlockSync(height uint64) (validators []*types.Validator, err error) { |
|
|
|
reqres := cli.queueRequest(types.RequestEndBlock(height)) |
|
|
|
reqres := cli.queueRequest(types.ToRequestEndBlock(height)) |
|
|
|
cli.FlushSync() |
|
|
|
if cli.err != nil { |
|
|
|
return nil, cli.err |
|
|
|
} |
|
|
|
return reqres.Response.Validators, nil |
|
|
|
return reqres.Response.GetEndBlock().Diffs, nil |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
func (cli *remoteClient) queueRequest(req *types.Request) *ReqRes { |
|
|
|
reqres := NewReqRes(req) |
|
|
|
|
|
|
|
// TODO: set cli.err if reqQueue times out
|
|
|
|
cli.reqQueue <- reqres |
|
|
|
|
|
|
|
// Maybe auto-flush, or unset auto-flush
|
|
|
|
switch req.Type { |
|
|
|
case types.MessageType_Flush: |
|
|
|
switch req.Value.(type) { |
|
|
|
case *types.Request_Flush: |
|
|
|
cli.flushTimer.Unset() |
|
|
|
default: |
|
|
|
cli.flushTimer.Set() |
|
|
@ -379,5 +375,27 @@ func (cli *remoteClient) queueRequest(req *types.Request) *ReqRes { |
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
func resMatchesReq(req *types.Request, res *types.Response) (ok bool) { |
|
|
|
return req.Type == res.Type |
|
|
|
switch req.Value.(type) { |
|
|
|
case *types.Request_Echo: |
|
|
|
_, ok = res.Value.(*types.Response_Echo) |
|
|
|
case *types.Request_Flush: |
|
|
|
_, ok = res.Value.(*types.Response_Flush) |
|
|
|
case *types.Request_Info: |
|
|
|
_, ok = res.Value.(*types.Response_Info) |
|
|
|
case *types.Request_SetOption: |
|
|
|
_, ok = res.Value.(*types.Response_SetOption) |
|
|
|
case *types.Request_AppendTx: |
|
|
|
_, ok = res.Value.(*types.Response_AppendTx) |
|
|
|
case *types.Request_CheckTx: |
|
|
|
_, ok = res.Value.(*types.Response_CheckTx) |
|
|
|
case *types.Request_Commit: |
|
|
|
_, ok = res.Value.(*types.Response_Commit) |
|
|
|
case *types.Request_Query: |
|
|
|
_, ok = res.Value.(*types.Response_Query) |
|
|
|
case *types.Request_InitChain: |
|
|
|
_, ok = res.Value.(*types.Response_InitChain) |
|
|
|
case *types.Request_EndBlock: |
|
|
|
_, ok = res.Value.(*types.Response_EndBlock) |
|
|
|
} |
|
|
|
return ok |
|
|
|
} |