2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-07-19 15:48:08 +01:00
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// Node is the storj definition for a node in the network
|
|
|
|
type Node struct {
|
2018-10-26 17:38:22 +01:00
|
|
|
dht dht.DHT
|
|
|
|
self pb.Node
|
|
|
|
pool *ConnectionPool
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
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-12-12 15:40:33 +00:00
|
|
|
func (node *Node) Lookup(ctx context.Context, to pb.Node, find pb.Node) ([]*pb.Node, error) {
|
2019-01-02 18:47:34 +00:00
|
|
|
to.Type.DPanicOnInvalid("node Lookup")
|
2018-12-12 15:40:33 +00:00
|
|
|
conn, err := node.pool.Dial(ctx, &to)
|
2019-01-02 18:47:34 +00:00
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
if err != nil {
|
2018-10-26 17:38:22 +01:00
|
|
|
return nil, NodeClientErr.Wrap(err)
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
2018-12-12 15:40:33 +00:00
|
|
|
resp, err := conn.Query(ctx, &pb.QueryRequest{
|
|
|
|
Limit: 20,
|
|
|
|
Sender: &node.self,
|
|
|
|
Target: &find,
|
|
|
|
Pingback: true,
|
|
|
|
})
|
2018-10-08 16:09:37 +01:00
|
|
|
if err != nil {
|
2018-10-26 17:38:22 +01:00
|
|
|
return nil, NodeClientErr.Wrap(err)
|
2018-10-08 16:09:37 +01:00
|
|
|
}
|
2018-12-12 15:40:33 +00:00
|
|
|
rt, err := node.dht.GetRoutingTable(ctx)
|
2018-07-19 15:48:08 +01:00
|
|
|
if err != nil {
|
2018-10-26 17:38:22 +01:00
|
|
|
return nil, NodeClientErr.Wrap(err)
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
if err := rt.ConnectionSuccess(&to); err != nil {
|
2018-10-26 17:38:22 +01:00
|
|
|
return nil, NodeClientErr.Wrap(err)
|
2018-10-08 16:09:37 +01:00
|
|
|
}
|
2018-07-19 15:48:08 +01:00
|
|
|
return resp.Response, nil
|
|
|
|
}
|
2018-10-26 15:07:02 +01:00
|
|
|
|
2018-10-26 17:38:22 +01:00
|
|
|
// Ping attempts to establish a connection with a node to verify it is alive
|
2018-12-12 15:40:33 +00:00
|
|
|
func (node *Node) Ping(ctx context.Context, to pb.Node) (bool, error) {
|
2019-01-02 18:47:34 +00:00
|
|
|
to.Type.DPanicOnInvalid("node ping")
|
2018-12-12 15:40:33 +00:00
|
|
|
conn, err := node.pool.Dial(ctx, &to)
|
2018-10-26 17:38:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, NodeClientErr.Wrap(err)
|
|
|
|
}
|
2018-12-12 15:40:33 +00:00
|
|
|
_, err = conn.Ping(ctx, &pb.PingRequest{})
|
2018-10-30 18:04:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2018-10-26 17:38:22 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect closes all connections within the pool
|
2018-12-12 15:40:33 +00:00
|
|
|
func (node *Node) Disconnect() error {
|
|
|
|
return node.pool.DisconnectAll()
|
2018-10-26 15:07:02 +01:00
|
|
|
}
|