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"
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2018-07-19 15:48:08 +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
|
|
|
)
|
|
|
|
|
|
|
|
// Server implements the grpc Node Server
|
|
|
|
type Server struct {
|
2018-10-08 16:09:37 +01:00
|
|
|
dht dht.DHT
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns a newly instantiated Node Server
|
|
|
|
func NewServer(dht dht.DHT) *Server {
|
|
|
|
return &Server{
|
|
|
|
dht: dht,
|
|
|
|
logger: zap.L(),
|
|
|
|
}
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query is a node to node communication query
|
2018-10-08 16:09:37 +01:00
|
|
|
func (s *Server) Query(ctx context.Context, req *pb.QueryRequest) (*pb.QueryResponse, error) {
|
|
|
|
if s.logger == nil {
|
|
|
|
s.logger = zap.L()
|
|
|
|
}
|
2018-11-20 16:54:52 +00:00
|
|
|
|
2018-08-23 16:20:11 +01:00
|
|
|
rt, err := s.dht.GetRoutingTable(ctx)
|
|
|
|
if err != nil {
|
2018-10-08 16:09:37 +01:00
|
|
|
return &pb.QueryResponse{}, NodeClientErr.New("could not get routing table %s", err)
|
2018-08-23 16:20:11 +01:00
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
|
|
|
if req.GetPingback() {
|
|
|
|
_, err = s.dht.Ping(ctx, *req.Sender)
|
2018-08-23 16:20:11 +01:00
|
|
|
if err != nil {
|
2018-10-08 16:09:37 +01:00
|
|
|
err = rt.ConnectionFailed(req.Sender)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("could not respond to connection failed", zap.Error(err))
|
|
|
|
}
|
|
|
|
s.logger.Error("connection to node failed", zap.Error(err), zap.String("nodeID", req.Sender.Id))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rt.ConnectionSuccess(req.Sender)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("could not respond to connection success", zap.Error(err))
|
2018-08-23 16:20:11 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
|
|
|
id := IDFromString(req.Target.Id)
|
2018-08-23 16:20:11 +01:00
|
|
|
nodes, err := rt.FindNear(id, int(req.Limit))
|
|
|
|
if err != nil {
|
2018-10-08 16:09:37 +01:00
|
|
|
return &pb.QueryResponse{}, NodeClientErr.New("could not find near %s", err)
|
2018-08-23 16:20:11 +01:00
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
|
|
|
return &pb.QueryResponse{Sender: req.Sender, Response: nodes}, nil
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
2018-10-30 18:04:29 +00:00
|
|
|
|
|
|
|
// Ping provides an easy way to verify a node is online and accepting requests
|
|
|
|
func (s *Server) Ping(ctx context.Context, req *pb.PingRequest) (*pb.PingResponse, error) {
|
2018-11-08 16:18:28 +00:00
|
|
|
//TODO
|
2018-10-30 18:04:29 +00:00
|
|
|
return &pb.PingResponse{}, nil
|
|
|
|
}
|