storj/pkg/overlay/service.go

37 lines
899 B
Go
Raw Normal View History

2018-04-12 14:50:22 +01:00
package overlay
import (
"fmt"
"net"
"github.com/coyle/storj/protos/overlay"
"google.golang.org/grpc"
)
// NewServer creates a new Overlay Service Server and begins serving requests on the provided port
func NewServer(port uint32) (*grpc.Server, error) {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return nil, err
}
grpcServer := grpc.NewServer()
overlay.RegisterOverlayServer(grpcServer, &Overlay{})
if err := grpcServer.Serve(lis); err != nil {
return nil, err
}
return grpcServer, nil
}
// NewClient connects to grpc server at the provided address with the provided options
// returns a new instance of an overlay Client
func NewClient(serverAddr *string, opts ...grpc.DialOption) (overlay.OverlayClient, error) {
conn, err := grpc.Dial(*serverAddr, opts...)
if err != nil {
return nil, err
}
return overlay.NewOverlayClient(conn), nil
}