2018-07-19 15:48:08 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-08 16:09:37 +01:00
|
|
|
"log"
|
2018-07-19 15:48:08 +01:00
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2018-09-18 05:39:06 +01:00
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
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-10-08 16:09:37 +01:00
|
|
|
dht dht.DHT
|
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
|
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
|
|
|
if err := n.cache.Add(ctx, to.GetId(), c); err != nil {
|
|
|
|
log.Printf("Error %s occurred adding %s to cache", err, to.GetId())
|
|
|
|
}
|
2018-07-19 15:48:08 +01:00
|
|
|
conn = c
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
c := pb.NewNodesClient(conn)
|
2018-10-08 16:09:37 +01:00
|
|
|
resp, err := c.Query(ctx, &pb.QueryRequest{Limit: 20, Sender: &n.self, Target: &find, Pingback: true})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rt, err := n.dht.GetRoutingTable(ctx)
|
2018-07-19 15:48:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
if err := rt.ConnectionSuccess(&to); err != nil {
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
return resp.Response, nil
|
|
|
|
}
|