[#259] netmap: Add NetworkInfo call to Client

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-02-17 20:39:29 +03:00 committed by Alex Vanin
parent 8cb1f960f4
commit 88d6857a3c

View file

@ -12,6 +12,8 @@ import (
// Client represents universal netmap transport client.
type Client struct {
cLocalNodeInfo *localNodeInfoClient
cNetworkInfo *networkInfoClient
}
// Option represents Client option.
@ -43,6 +45,14 @@ type localNodeInfoClient struct {
responseConverter func(interface{}) *LocalNodeInfoResponse
}
type networkInfoClient struct {
requestConverter func(*NetworkInfoRequest) interface{}
caller func(context.Context, interface{}) (interface{}, error)
responseConverter func(interface{}) *NetworkInfoResponse
}
// LocalNodeInfo sends LocalNodeInfoRequest over the network.
func (c *Client) LocalNodeInfo(ctx context.Context, req *LocalNodeInfoRequest) (*LocalNodeInfoResponse, error) {
resp, err := c.cLocalNodeInfo.caller(ctx, c.cLocalNodeInfo.requestConverter(req))
@ -53,6 +63,16 @@ func (c *Client) LocalNodeInfo(ctx context.Context, req *LocalNodeInfoRequest) (
return c.cLocalNodeInfo.responseConverter(resp), nil
}
// NetworkInfo sends NetworkInfoRequest over the network.
func (c *Client) NetworkInfo(ctx context.Context, req *NetworkInfoRequest) (*NetworkInfoResponse, error) {
resp, err := c.cNetworkInfo.caller(ctx, c.cNetworkInfo.requestConverter(req))
if err != nil {
return nil, errors.Wrap(err, "could not send network info request")
}
return c.cNetworkInfo.responseConverter(resp), nil
}
func defaultCfg() *cfg {
return &cfg{
proto: client.ProtoGRPC,
@ -88,6 +108,17 @@ func NewClient(opts ...Option) (*Client, error) {
return LocalNodeInfoResponseFromGRPCMessage(resp.(*netmap.LocalNodeInfoResponse))
},
},
cNetworkInfo: &networkInfoClient{
requestConverter: func(req *NetworkInfoRequest) interface{} {
return NetworkInfoRequestToGRPCMessage(req)
},
caller: func(ctx context.Context, req interface{}) (interface{}, error) {
return c.NetworkInfo(ctx, req.(*netmap.NetworkInfoRequest))
},
responseConverter: func(resp interface{}) *NetworkInfoResponse {
return NetworkInfoResponseFromGRPCMessage(resp.(*netmap.NetworkInfoResponse))
},
},
}, nil
default:
err = client.ErrProtoUnsupported