2020-10-22 11:06:25 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/util"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/util/response"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
type ResponseService struct {
|
2020-10-22 11:06:25 +00:00
|
|
|
respSvc *response.Service
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
svc ServiceServer
|
2020-10-22 11:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type searchStreamResponser struct {
|
2020-12-10 12:26:40 +00:00
|
|
|
util.ServerStream
|
|
|
|
|
|
|
|
respWriter util.ResponseMessageWriter
|
2020-10-22 11:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type getStreamResponser struct {
|
2020-12-02 23:45:25 +00:00
|
|
|
util.ServerStream
|
|
|
|
|
|
|
|
respWriter util.ResponseMessageWriter
|
2020-10-22 11:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type getRangeStreamResponser struct {
|
2020-12-07 17:49:47 +00:00
|
|
|
util.ServerStream
|
|
|
|
|
|
|
|
respWriter util.ResponseMessageWriter
|
2020-10-22 11:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type putStreamResponser struct {
|
|
|
|
stream *response.ClientMessageStreamer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewResponseService returns object service instance that passes internal service
|
|
|
|
// call to response service.
|
2020-12-02 23:45:25 +00:00
|
|
|
func NewResponseService(objSvc ServiceServer, respSvc *response.Service) *ResponseService {
|
|
|
|
return &ResponseService{
|
2020-10-22 11:06:25 +00:00
|
|
|
respSvc: respSvc,
|
|
|
|
svc: objSvc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *getStreamResponser) Send(resp *object.GetResponse) error {
|
|
|
|
return s.respWriter(resp)
|
2020-10-22 11:06:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *ResponseService) Get(req *object.GetRequest, stream GetObjectStream) error {
|
|
|
|
return s.svc.Get(req, &getStreamResponser{
|
|
|
|
ServerStream: stream,
|
2021-01-11 15:17:06 +00:00
|
|
|
respWriter: s.respSvc.HandleServerStreamRequest(func(resp util.ResponseMessage) error {
|
2020-12-02 23:45:25 +00:00
|
|
|
return stream.Send(resp.(*object.GetResponse))
|
|
|
|
}),
|
|
|
|
})
|
2020-10-22 11:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *putStreamResponser) Send(req *object.PutRequest) error {
|
|
|
|
return s.stream.Send(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *putStreamResponser) CloseAndRecv() (*object.PutResponse, error) {
|
|
|
|
r, err := s.stream.CloseAndRecv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "(%T) could not receive response", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.(*object.PutResponse), nil
|
|
|
|
}
|
|
|
|
|
2021-03-15 10:53:08 +00:00
|
|
|
func (s *ResponseService) Put(ctx context.Context) (PutObjectStream, error) {
|
2020-10-22 11:06:25 +00:00
|
|
|
stream, err := s.svc.Put(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not create Put object streamer")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &putStreamResponser{
|
|
|
|
stream: s.respSvc.CreateRequestStreamer(
|
|
|
|
func(req interface{}) error {
|
|
|
|
return stream.Send(req.(*object.PutRequest))
|
|
|
|
},
|
|
|
|
func() (util.ResponseMessage, error) {
|
|
|
|
return stream.CloseAndRecv()
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *ResponseService) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) {
|
2020-10-22 11:06:25 +00:00
|
|
|
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
|
|
|
|
func(ctx context.Context, req interface{}) (util.ResponseMessage, error) {
|
|
|
|
return s.svc.Head(ctx, req.(*object.HeadRequest))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.(*object.HeadResponse), nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
func (s *searchStreamResponser) Send(resp *object.SearchResponse) error {
|
|
|
|
return s.respWriter(resp)
|
2020-10-22 11:06:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
func (s *ResponseService) Search(req *object.SearchRequest, stream SearchStream) error {
|
|
|
|
return s.svc.Search(req, &searchStreamResponser{
|
|
|
|
ServerStream: stream,
|
2021-01-11 15:17:06 +00:00
|
|
|
respWriter: s.respSvc.HandleServerStreamRequest(func(resp util.ResponseMessage) error {
|
2020-12-10 12:26:40 +00:00
|
|
|
return stream.Send(resp.(*object.SearchResponse))
|
|
|
|
}),
|
|
|
|
})
|
2020-10-22 11:06:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *ResponseService) Delete(ctx context.Context, req *object.DeleteRequest) (*object.DeleteResponse, error) {
|
2020-10-22 11:06:25 +00:00
|
|
|
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
|
|
|
|
func(ctx context.Context, req interface{}) (util.ResponseMessage, error) {
|
|
|
|
return s.svc.Delete(ctx, req.(*object.DeleteRequest))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.(*object.DeleteResponse), nil
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (s *getRangeStreamResponser) Send(resp *object.GetRangeResponse) error {
|
|
|
|
return s.respWriter(resp)
|
2020-10-22 11:06:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (s *ResponseService) GetRange(req *object.GetRangeRequest, stream GetObjectRangeStream) error {
|
|
|
|
return s.svc.GetRange(req, &getRangeStreamResponser{
|
|
|
|
ServerStream: stream,
|
2021-01-11 15:17:06 +00:00
|
|
|
respWriter: s.respSvc.HandleServerStreamRequest(func(resp util.ResponseMessage) error {
|
2020-12-07 17:49:47 +00:00
|
|
|
return stream.Send(resp.(*object.GetRangeResponse))
|
|
|
|
}),
|
|
|
|
})
|
2020-10-22 11:06:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *ResponseService) GetRangeHash(ctx context.Context, req *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
|
2020-10-22 11:06:25 +00:00
|
|
|
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
|
|
|
|
func(ctx context.Context, req interface{}) (util.ResponseMessage, error) {
|
|
|
|
return s.svc.GetRangeHash(ctx, req.(*object.GetRangeHashRequest))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.(*object.GetRangeHashResponse), nil
|
|
|
|
}
|