2018-07-19 23:57:22 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/dht"
|
2018-08-23 16:20:11 +01:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-07-19 15:48:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Server implements the grpc Node Server
|
|
|
|
type Server struct {
|
2018-08-23 16:20:11 +01:00
|
|
|
dht dht.DHT
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query is a node to node communication query
|
2018-09-18 05:39:06 +01:00
|
|
|
func (s *Server) Query(ctx context.Context, req pb.QueryRequest) (pb.QueryResponse, error) {
|
2018-08-23 16:20:11 +01:00
|
|
|
rt, err := s.dht.GetRoutingTable(ctx)
|
|
|
|
if err != nil {
|
2018-09-18 05:39:06 +01:00
|
|
|
return pb.QueryResponse{}, NodeClientErr.New("could not get routing table %s", err)
|
2018-08-23 16:20:11 +01:00
|
|
|
}
|
|
|
|
_, err = s.dht.Ping(ctx, *req.Sender)
|
|
|
|
if err != nil {
|
|
|
|
err = rt.ConnectionFailed(req.Sender)
|
|
|
|
if err != nil {
|
2018-09-18 05:39:06 +01:00
|
|
|
return pb.QueryResponse{}, NodeClientErr.New("could not respond to connection failed %s", err)
|
2018-08-23 16:20:11 +01:00
|
|
|
}
|
2018-09-18 05:39:06 +01:00
|
|
|
return pb.QueryResponse{}, NodeClientErr.New("connection to node %s failed", req.Sender.Id)
|
2018-08-23 16:20:11 +01:00
|
|
|
}
|
|
|
|
err = rt.ConnectionSuccess(req.Sender)
|
|
|
|
if err != nil {
|
2018-09-18 05:39:06 +01:00
|
|
|
return pb.QueryResponse{}, NodeClientErr.New("could not respond to connection success %s", err)
|
2018-08-23 16:20:11 +01:00
|
|
|
}
|
|
|
|
id := kademlia.StringToNodeID(req.Target.Id)
|
|
|
|
nodes, err := rt.FindNear(id, int(req.Limit))
|
|
|
|
if err != nil {
|
2018-09-18 05:39:06 +01:00
|
|
|
return pb.QueryResponse{}, NodeClientErr.New("could not find near %s", err)
|
2018-08-23 16:20:11 +01:00
|
|
|
}
|
2018-09-18 05:39:06 +01:00
|
|
|
return pb.QueryResponse{Sender: req.Sender, Response: nodes}, nil
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|