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-07-19 15:48:08 +01:00
|
|
|
proto "storj.io/storj/protos/overlay"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
func (s *Server) Query(ctx context.Context, req proto.QueryRequest) (proto.QueryResponse, error) {
|
2018-08-23 16:20:11 +01:00
|
|
|
rt, err := s.dht.GetRoutingTable(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return proto.QueryResponse{}, NodeClientErr.New("could not get routing table %s", err)
|
|
|
|
}
|
|
|
|
_, err = s.dht.Ping(ctx, *req.Sender)
|
|
|
|
if err != nil {
|
|
|
|
err = rt.ConnectionFailed(req.Sender)
|
|
|
|
if err != nil {
|
|
|
|
return proto.QueryResponse{}, NodeClientErr.New("could not respond to connection failed %s", err)
|
|
|
|
}
|
|
|
|
return proto.QueryResponse{}, NodeClientErr.New("connection to node %s failed", req.Sender.Id)
|
|
|
|
}
|
|
|
|
err = rt.ConnectionSuccess(req.Sender)
|
|
|
|
if err != nil {
|
|
|
|
return proto.QueryResponse{}, NodeClientErr.New("could not respond to connection success %s", err)
|
|
|
|
}
|
|
|
|
id := kademlia.StringToNodeID(req.Target.Id)
|
|
|
|
nodes, err := rt.FindNear(id, int(req.Limit))
|
|
|
|
if err != nil {
|
|
|
|
return proto.QueryResponse{}, NodeClientErr.New("could not find near %s", err)
|
|
|
|
}
|
|
|
|
return proto.QueryResponse{Sender: req.Sender, Response: nodes}, nil
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|