|
@ -13,9 +13,8 @@ import ( |
|
|
"github.com/tendermint/tmsp/types" |
|
|
"github.com/tendermint/tmsp/types" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// This is goroutine-safe, but users should beware that
|
|
|
|
|
|
// the application in general is not meant to be interfaced
|
|
|
|
|
|
// with concurrent callers.
|
|
|
|
|
|
|
|
|
// A stripped copy of the remoteClient that makes
|
|
|
|
|
|
// synchronous calls using grpc
|
|
|
type grpcClient struct { |
|
|
type grpcClient struct { |
|
|
QuitService |
|
|
QuitService |
|
|
mustConnect bool |
|
|
mustConnect bool |
|
@ -42,15 +41,14 @@ func dialerFunc(addr string, timeout time.Duration) (net.Conn, error) { |
|
|
return Connect(addr) |
|
|
return Connect(addr) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (cli *grpcClient) OnStart() (err error) { |
|
|
|
|
|
|
|
|
func (cli *grpcClient) OnStart() error { |
|
|
cli.QuitService.OnStart() |
|
|
cli.QuitService.OnStart() |
|
|
RETRY_LOOP: |
|
|
RETRY_LOOP: |
|
|
for { |
|
|
for { |
|
|
conn, err_ := grpc.Dial(cli.addr, grpc.WithInsecure(), grpc.WithDialer(dialerFunc)) |
|
|
|
|
|
if err_ != nil { |
|
|
|
|
|
|
|
|
conn, err := grpc.Dial(cli.addr, grpc.WithInsecure(), grpc.WithDialer(dialerFunc)) |
|
|
|
|
|
if err != nil { |
|
|
if cli.mustConnect { |
|
|
if cli.mustConnect { |
|
|
err = err_ // OnStart() will return this.
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
return err |
|
|
} else { |
|
|
} else { |
|
|
fmt.Printf("tmsp.grpcClient failed to connect to %v. Retrying...\n", cli.addr) |
|
|
fmt.Printf("tmsp.grpcClient failed to connect to %v. Retrying...\n", cli.addr) |
|
|
time.Sleep(time.Second * 3) |
|
|
time.Sleep(time.Second * 3) |
|
@ -58,13 +56,13 @@ RETRY_LOOP: |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
cli.client = types.NewTMSPApplicationClient(conn) |
|
|
cli.client = types.NewTMSPApplicationClient(conn) |
|
|
return |
|
|
|
|
|
|
|
|
return nil |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (cli *grpcClient) OnStop() { |
|
|
func (cli *grpcClient) OnStop() { |
|
|
cli.QuitService.OnStop() |
|
|
cli.QuitService.OnStop() |
|
|
// TODO: close client (?)
|
|
|
|
|
|
|
|
|
// TODO: how to close when TMSPApplicationClient interface doesn't expose Close ?
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Set listener for all responses
|
|
|
// Set listener for all responses
|
|
@ -96,23 +94,6 @@ func (cli *grpcClient) Error() error { |
|
|
// maybe one day, if people really want it, we use grpc streams,
|
|
|
// maybe one day, if people really want it, we use grpc streams,
|
|
|
// but hopefully not :D
|
|
|
// but hopefully not :D
|
|
|
|
|
|
|
|
|
func (cli *grpcClient) finishAsyncCall(req *types.Request, res *types.Response) *ReqRes { |
|
|
|
|
|
reqres := NewReqRes(req) |
|
|
|
|
|
reqres.Response = res // Set response
|
|
|
|
|
|
reqres.Done() // Release waiters
|
|
|
|
|
|
|
|
|
|
|
|
// Notify reqRes listener if set
|
|
|
|
|
|
if cb := reqres.GetCallback(); cb != nil { |
|
|
|
|
|
cb(res) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Notify client listener if set
|
|
|
|
|
|
if cli.resCb != nil { |
|
|
|
|
|
cli.resCb(reqres.Request, res) |
|
|
|
|
|
} |
|
|
|
|
|
return reqres |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (cli *grpcClient) EchoAsync(msg string) *ReqRes { |
|
|
func (cli *grpcClient) EchoAsync(msg string) *ReqRes { |
|
|
req := types.ToRequestEcho(msg) |
|
|
req := types.ToRequestEcho(msg) |
|
|
res, err := cli.client.Echo(context.Background(), req.GetEcho()) |
|
|
res, err := cli.client.Echo(context.Background(), req.GetEcho()) |
|
@ -212,6 +193,23 @@ func (cli *grpcClient) EndBlockAsync(height uint64) *ReqRes { |
|
|
return cli.finishAsyncCall(req, &types.Response{&types.Response_EndBlock{res}}) |
|
|
return cli.finishAsyncCall(req, &types.Response{&types.Response_EndBlock{res}}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (cli *grpcClient) finishAsyncCall(req *types.Request, res *types.Response) *ReqRes { |
|
|
|
|
|
reqres := NewReqRes(req) |
|
|
|
|
|
reqres.Response = res // Set response
|
|
|
|
|
|
reqres.Done() // Release waiters
|
|
|
|
|
|
|
|
|
|
|
|
// Notify reqRes listener if set
|
|
|
|
|
|
if cb := reqres.GetCallback(); cb != nil { |
|
|
|
|
|
cb(res) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Notify client listener if set
|
|
|
|
|
|
if cli.resCb != nil { |
|
|
|
|
|
cli.resCb(reqres.Request, res) |
|
|
|
|
|
} |
|
|
|
|
|
return reqres |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
|
func (cli *grpcClient) EchoSync(msg string) (res types.Result) { |
|
|
func (cli *grpcClient) EchoSync(msg string) (res types.Result) { |
|
|