2018-06-19 15:00:15 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2018-07-09 18:43:13 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
2018-08-24 05:01:03 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-06-19 15:00:15 +01:00
|
|
|
proto "storj.io/storj/protos/overlay"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client is the interface that defines an overlay client.
|
|
|
|
//
|
|
|
|
// Choose returns a list of storage NodeID's that fit the provided criteria.
|
|
|
|
// limit is the maximum number of nodes to be returned.
|
|
|
|
// space is the storage and bandwidth requested consumption in bytes.
|
|
|
|
//
|
|
|
|
// Lookup finds a Node with the provided identifier.
|
|
|
|
type Client interface {
|
2018-07-23 21:05:02 +01:00
|
|
|
Choose(ctx context.Context, limit int, space int64) ([]*proto.Node, error)
|
|
|
|
Lookup(ctx context.Context, nodeID dht.NodeID) (*proto.Node, error)
|
2018-06-19 15:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overlay is the overlay concrete implementation of the client interface
|
|
|
|
type Overlay struct {
|
|
|
|
client proto.OverlayClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewOverlayClient returns a new intialized Overlay Client
|
2018-08-24 05:01:03 +01:00
|
|
|
func NewOverlayClient(identity *provider.FullIdentity, address string) (*Overlay, error) {
|
|
|
|
dialOpt, err := identity.DialOption()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c, err := NewClient(address, dialOpt)
|
2018-06-19 15:00:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-03 14:15:52 +01:00
|
|
|
|
2018-06-19 15:00:15 +01:00
|
|
|
return &Overlay{
|
|
|
|
client: c,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-07-23 21:05:02 +01:00
|
|
|
// a compiler trick to make sure *Overlay implements Client
|
|
|
|
var _ Client = (*Overlay)(nil)
|
|
|
|
|
2018-06-19 15:00:15 +01:00
|
|
|
// Choose implements the client.Choose interface
|
2018-08-03 14:15:52 +01:00
|
|
|
func (o *Overlay) Choose(ctx context.Context, amount int, space int64) ([]*proto.Node, error) {
|
2018-06-19 15:00:15 +01:00
|
|
|
// TODO(coyle): We will also need to communicate with the reputation service here
|
2018-08-01 15:15:38 +01:00
|
|
|
resp, err := o.client.FindStorageNodes(ctx, &proto.FindStorageNodesRequest{
|
2018-08-03 14:15:52 +01:00
|
|
|
Opts: &proto.OverlayOptions{Amount: int64(amount), Restrictions: &proto.NodeRestrictions{
|
2018-08-01 15:15:38 +01:00
|
|
|
FreeDisk: space,
|
|
|
|
}},
|
|
|
|
})
|
2018-06-19 15:00:15 +01:00
|
|
|
if err != nil {
|
2018-08-03 14:15:52 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2018-06-19 15:00:15 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
return resp.GetNodes(), nil
|
2018-06-19 15:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup provides a Node with the given address
|
2018-07-09 18:43:13 +01:00
|
|
|
func (o *Overlay) Lookup(ctx context.Context, nodeID dht.NodeID) (*proto.Node, error) {
|
2018-06-19 15:00:15 +01:00
|
|
|
resp, err := o.client.Lookup(ctx, &proto.LookupRequest{NodeID: nodeID.String()})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.GetNode(), nil
|
|
|
|
}
|