storj/pkg/node/server.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

44 lines
1.3 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package node
import (
"context"
"storj.io/storj/pkg/dht"
"storj.io/storj/pkg/kademlia"
"storj.io/storj/pkg/pb"
)
// Server implements the grpc Node Server
type Server struct {
dht dht.DHT
}
// Query is a node to node communication query
func (s *Server) Query(ctx context.Context, req pb.QueryRequest) (pb.QueryResponse, error) {
rt, err := s.dht.GetRoutingTable(ctx)
if err != nil {
return pb.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 pb.QueryResponse{}, NodeClientErr.New("could not respond to connection failed %s", err)
}
return pb.QueryResponse{}, NodeClientErr.New("connection to node %s failed", req.Sender.Id)
}
err = rt.ConnectionSuccess(req.Sender)
if err != nil {
return pb.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 pb.QueryResponse{}, NodeClientErr.New("could not find near %s", err)
}
return pb.QueryResponse{Sender: req.Sender, Response: nodes}, nil
}