You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.6 KiB
69 lines
1.6 KiB
6 years ago
|
package downloader
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
6 years ago
|
"log"
|
||
6 years ago
|
"net"
|
||
|
|
||
4 years ago
|
pb "github.com/harmony-one/harmony/api/service/legacysync/downloader/proto"
|
||
6 years ago
|
"github.com/harmony-one/harmony/internal/utils"
|
||
|
|
||
6 years ago
|
"google.golang.org/grpc"
|
||
5 years ago
|
"google.golang.org/grpc/peer"
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
// Constants for downloader server.
|
||
6 years ago
|
const (
|
||
6 years ago
|
DefaultDownloadPort = "6666"
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
// Server is the Server struct for downloader package.
|
||
6 years ago
|
type Server struct {
|
||
6 years ago
|
downloadInterface DownloadInterface
|
||
6 years ago
|
GrpcServer *grpc.Server
|
||
6 years ago
|
}
|
||
|
|
||
|
// Query returns the feature at the given point.
|
||
|
func (s *Server) Query(ctx context.Context, request *pb.DownloaderRequest) (*pb.DownloaderResponse, error) {
|
||
5 years ago
|
var pinfo string
|
||
|
// retrieve ip/port information; used for debug only
|
||
|
p, ok := peer.FromContext(ctx)
|
||
|
if !ok {
|
||
|
pinfo = ""
|
||
|
} else {
|
||
|
pinfo = p.Addr.String()
|
||
|
}
|
||
|
response, err := s.downloadInterface.CalculateResponse(request, pinfo)
|
||
6 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return response, nil
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// Start starts the Server on given ip and port.
|
||
6 years ago
|
func (s *Server) Start(ip, port string) (*grpc.Server, error) {
|
||
6 years ago
|
addr := net.JoinHostPort("", port)
|
||
5 years ago
|
lis, err := net.Listen("tcp4", addr)
|
||
6 years ago
|
if err != nil {
|
||
6 years ago
|
log.Fatalf("[SYNC] failed to listen: %v", err)
|
||
6 years ago
|
}
|
||
|
var opts []grpc.ServerOption
|
||
|
grpcServer := grpc.NewServer(opts...)
|
||
|
pb.RegisterDownloaderServer(grpcServer, s)
|
||
6 years ago
|
go func() {
|
||
|
if err := grpcServer.Serve(lis); err != nil {
|
||
6 years ago
|
utils.Logger().Warn().Err(err).Msg("[SYNC] (*grpc.Server).Serve failed")
|
||
6 years ago
|
}
|
||
|
}()
|
||
|
|
||
6 years ago
|
s.GrpcServer = grpcServer
|
||
|
|
||
6 years ago
|
return grpcServer, nil
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// NewServer creates new Server which implements DownloadInterface.
|
||
6 years ago
|
func NewServer(dlInterface DownloadInterface) *Server {
|
||
|
s := &Server{downloadInterface: dlInterface}
|
||
6 years ago
|
return s
|
||
|
}
|