2018-07-19 15:48:08 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2018-09-18 05:39:06 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
2018-07-19 15:48:08 +01:00
|
|
|
"storj.io/storj/pkg/pool"
|
|
|
|
"storj.io/storj/pkg/transport"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Node is the storj definition for a node in the network
|
|
|
|
type Node struct {
|
2018-09-18 05:39:06 +01:00
|
|
|
self pb.Node
|
2018-07-19 15:48:08 +01:00
|
|
|
tc transport.Client
|
|
|
|
cache pool.Pool
|
|
|
|
}
|
2018-08-23 16:20:11 +01:00
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
// Lookup queries nodes looking for a particular node in the network
|
2018-09-18 05:39:06 +01:00
|
|
|
func (n *Node) Lookup(ctx context.Context, to pb.Node, find pb.Node) ([]*pb.Node, error) {
|
2018-07-19 15:48:08 +01:00
|
|
|
v, err := n.cache.Get(ctx, to.GetId())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var conn *grpc.ClientConn
|
|
|
|
if c, ok := v.(*grpc.ClientConn); ok {
|
|
|
|
conn = c
|
|
|
|
} else {
|
|
|
|
c, err := n.tc.DialNode(ctx, &to)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conn = c
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
c := pb.NewNodesClient(conn)
|
|
|
|
resp, err := c.Query(ctx, &pb.QueryRequest{Sender: &n.self, Target: &find})
|
2018-07-19 15:48:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Response, nil
|
|
|
|
}
|