storj/pkg/node/node.go
Egon Elbre b6b6111173
Flatten proto definitions into a single package (#360)
* protos: move streams to pb
* protos: move overlay to pb
* protos: move pointerdb to pb
* protos: move piecestore to pb
* fix statdb import naming
2018-09-18 07:39:06 +03:00

49 lines
958 B
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information
package node
import (
"context"
"google.golang.org/grpc"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/pool"
"storj.io/storj/pkg/transport"
)
// Node is the storj definition for a node in the network
type Node struct {
self pb.Node
tc transport.Client
cache pool.Pool
}
// Lookup queries nodes looking for a particular node in the network
func (n *Node) Lookup(ctx context.Context, to pb.Node, find pb.Node) ([]*pb.Node, error) {
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
}
c := pb.NewNodesClient(conn)
resp, err := c.Query(ctx, &pb.QueryRequest{Sender: &n.self, Target: &find})
if err != nil {
return nil, err
}
return resp.Response, nil
}