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.
60 lines
1.5 KiB
60 lines
1.5 KiB
package downloader
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net"
|
|
|
|
pb "github.com/harmony-one/harmony/api/service/syncing/downloader/proto"
|
|
"github.com/harmony-one/harmony/internal/ctxerror"
|
|
"github.com/harmony-one/harmony/internal/utils"
|
|
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
// Constants for downloader server.
|
|
const (
|
|
DefaultDownloadPort = "6666"
|
|
)
|
|
|
|
// Server is the Server struct for downloader package.
|
|
type Server struct {
|
|
downloadInterface DownloadInterface
|
|
GrpcServer *grpc.Server
|
|
}
|
|
|
|
// Query returns the feature at the given point.
|
|
func (s *Server) Query(ctx context.Context, request *pb.DownloaderRequest) (*pb.DownloaderResponse, error) {
|
|
response, err := s.downloadInterface.CalculateResponse(request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
// Start starts the Server on given ip and port.
|
|
func (s *Server) Start(ip, port string) (*grpc.Server, error) {
|
|
addr := net.JoinHostPort("", port)
|
|
lis, err := net.Listen("tcp", addr)
|
|
if err != nil {
|
|
log.Fatalf("[SYNC] failed to listen: %v", err)
|
|
}
|
|
var opts []grpc.ServerOption
|
|
grpcServer := grpc.NewServer(opts...)
|
|
pb.RegisterDownloaderServer(grpcServer, s)
|
|
go func() {
|
|
if err := grpcServer.Serve(lis); err != nil {
|
|
ctxerror.Warn(utils.GetLogger(), err, "[SYNC] (*grpc.Server).Serve failed")
|
|
}
|
|
}()
|
|
|
|
s.GrpcServer = grpcServer
|
|
|
|
return grpcServer, nil
|
|
}
|
|
|
|
// NewServer creates new Server which implements DownloadInterface.
|
|
func NewServer(dlInterface DownloadInterface) *Server {
|
|
s := &Server{downloadInterface: dlInterface}
|
|
return s
|
|
}
|
|
|