[#109] object/put: Implement remote object sender

Define RemoteSender structure with PutObject method that puts object to a
remote node locally.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-10-21 14:46:40 +03:00 committed by Alex Vanin
parent 0c3e6c7a04
commit 53efa18e14

View file

@ -28,6 +28,19 @@ type remoteTarget struct {
obj *object.Object
}
// RemoteSender represents utility for
// sending an object to a remote host.
type RemoteSender struct {
keyStorage *util.KeyStorage
}
// RemotePutPrm groups remote put operation parameters.
type RemotePutPrm struct {
node *network.Address
obj *object.Object
}
func (t *remoteTarget) WriteHeader(obj *object.RawObject) error {
t.obj = obj.Object()
@ -66,3 +79,45 @@ func (t *remoteTarget) Close() (*transformer.AccessIdentifiers, error) {
return new(transformer.AccessIdentifiers).
WithSelfID(id), nil
}
// NewRemoteSender creates, initializes and returns new RemoteSender instance.
func NewRemoteSender(keyStorage *util.KeyStorage) *RemoteSender {
return &RemoteSender{
keyStorage: keyStorage,
}
}
// WithNodeAddress sets network address of the remote node.
func (p *RemotePutPrm) WithNodeAddress(v *network.Address) *RemotePutPrm {
if p != nil {
p.node = v
}
return p
}
// WithObject sets transferred object.
func (p *RemotePutPrm) WithObject(v *object.Object) *RemotePutPrm {
if p != nil {
p.obj = v
}
return p
}
// PutObject sends object to remote node.
func (s *RemoteSender) PutObject(ctx context.Context, p *RemotePutPrm) error {
t := &remoteTarget{
ctx: ctx,
keyStorage: s.keyStorage,
addr: p.node,
}
if err := t.WriteHeader(object.NewRawFromObject(p.obj)); err != nil {
return errors.Wrapf(err, "(%T) could not send object header", s)
} else if _, err := t.Close(); err != nil {
return errors.Wrapf(err, "(%T) could not send object", s)
}
return nil
}