From 4640099a67341f86e7b5b76fdb66fe41f7f1ee71 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 15 Mar 2021 14:04:13 +0300 Subject: [PATCH] [#425] control: Implement service RPCs Signed-off-by: Leonard Lyubich --- pkg/services/control/convert.go | 94 +++++++++++++++++++++++++++++ pkg/services/control/rpc.go | 102 ++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 pkg/services/control/convert.go create mode 100644 pkg/services/control/rpc.go diff --git a/pkg/services/control/convert.go b/pkg/services/control/convert.go new file mode 100644 index 0000000000..dd5d63091e --- /dev/null +++ b/pkg/services/control/convert.go @@ -0,0 +1,94 @@ +package control + +import ( + "github.com/nspcc-dev/neofs-api-go/rpc/grpc" + "github.com/nspcc-dev/neofs-api-go/rpc/message" +) + +type requestWrapper struct { + message.Message + m grpc.Message +} + +func (w *requestWrapper) ToGRPCMessage() grpc.Message { + return w.m +} + +type healthCheckResponseWrapper struct { + m *HealthCheckResponse +} + +func (w *healthCheckResponseWrapper) ToGRPCMessage() grpc.Message { + return w.m +} + +func (w *healthCheckResponseWrapper) FromGRPCMessage(m grpc.Message) error { + var ok bool + + w.m, ok = m.(*HealthCheckResponse) + if !ok { + return message.NewUnexpectedMessageType(m, w.m) + } + + return nil +} + +type netmapSnapshotResponseWrapper struct { + message.Message + m *NetmapSnapshotResponse +} + +func (w *netmapSnapshotResponseWrapper) ToGRPCMessage() grpc.Message { + return w.m +} + +func (w *netmapSnapshotResponseWrapper) FromGRPCMessage(m grpc.Message) error { + var ok bool + + w.m, ok = m.(*NetmapSnapshotResponse) + if !ok { + return message.NewUnexpectedMessageType(m, w.m) + } + + return nil +} + +type setNetmapStatusResponseWrapper struct { + message.Message + m *SetNetmapStatusResponse +} + +func (w *setNetmapStatusResponseWrapper) ToGRPCMessage() grpc.Message { + return w.m +} + +func (w *setNetmapStatusResponseWrapper) FromGRPCMessage(m grpc.Message) error { + var ok bool + + w.m, ok = m.(*SetNetmapStatusResponse) + if !ok { + return message.NewUnexpectedMessageType(m, w.m) + } + + return nil +} + +type dropObjectsResponseWrapper struct { + message.Message + m *DropObjectsResponse +} + +func (w *dropObjectsResponseWrapper) ToGRPCMessage() grpc.Message { + return w.m +} + +func (w *dropObjectsResponseWrapper) FromGRPCMessage(m grpc.Message) error { + var ok bool + + w.m, ok = m.(*DropObjectsResponse) + if !ok { + return message.NewUnexpectedMessageType(m, w.m) + } + + return nil +} diff --git a/pkg/services/control/rpc.go b/pkg/services/control/rpc.go new file mode 100644 index 0000000000..15fdf1c439 --- /dev/null +++ b/pkg/services/control/rpc.go @@ -0,0 +1,102 @@ +package control + +import ( + "github.com/nspcc-dev/neofs-api-go/rpc/client" + "github.com/nspcc-dev/neofs-api-go/rpc/common" +) + +const serviceName = "control.ControlService" + +const ( + rpcHealthCheck = "HealthCheck" + rpcNetmapSnapshot = "NetmapSnapshot" + rpcSetNetmapStatus = "SetNetmapStatus" + rpcDropObjects = "DropObjects" +) + +// HealthCheck executes ControlService.HealthCheck RPC. +func HealthCheck( + cli *client.Client, + req *HealthCheckRequest, + opts ...client.CallOption, +) (*HealthCheckResponse, error) { + wResp := &healthCheckResponseWrapper{ + m: new(HealthCheckResponse), + } + + wReq := &requestWrapper{ + m: req, + } + + err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcHealthCheck), wReq, wResp, opts...) + if err != nil { + return nil, err + } + + return wResp.m, nil +} + +// NetmapSnapshot executes ControlService.NetmapSnapshot RPC. +func NetmapSnapshot( + cli *client.Client, + req *NetmapSnapshotRequest, + opts ...client.CallOption, +) (*NetmapSnapshotResponse, error) { + wResp := &netmapSnapshotResponseWrapper{ + m: new(NetmapSnapshotResponse), + } + + wReq := &requestWrapper{ + m: req, + } + + err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcNetmapSnapshot), wReq, wResp, opts...) + if err != nil { + return nil, err + } + + return wResp.m, nil +} + +// SetNetmapStatus executes ControlService.SetNetmapStatus RPC. +func SetNetmapStatus( + cli *client.Client, + req *SetNetmapStatusRequest, + opts ...client.CallOption, +) (*SetNetmapStatusResponse, error) { + wResp := &setNetmapStatusResponseWrapper{ + m: new(SetNetmapStatusResponse), + } + + wReq := &requestWrapper{ + m: req, + } + + err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcSetNetmapStatus), wReq, wResp, opts...) + if err != nil { + return nil, err + } + + return wResp.m, nil +} + +// DropObjects executes ControlService.DropObjects RPC. +func DropObjects( + cli *client.Client, + req *DropObjectsRequest, + opts ...client.CallOption, +) (*DropObjectsResponse, error) { + wResp := &dropObjectsResponseWrapper{ + m: new(DropObjectsResponse), + } + + wReq := &requestWrapper{ + m: req, + } + err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcDropObjects), wReq, wResp, opts...) + if err != nil { + return nil, err + } + + return wResp.m, nil +}