grpc server and client

This commit is contained in:
Dennis Coyle 2018-04-12 08:50:22 -05:00 committed by JT Olds
parent 9e31cfc7a0
commit 1975b98eba
5 changed files with 66 additions and 0 deletions

View File

@ -10,3 +10,8 @@ lint:
--enable=gosimple \
--exclude=.*\.pb\.go \
./...
proto:
@echo "Running ${@}"
./scripts/build-protos.sh

22
pkg/overlay/overlay.go Normal file
View File

@ -0,0 +1,22 @@
package overlay
import (
"context"
"github.com/coyle/storj/protos/overlay"
)
// Overlay implements our overlay RPC service
type Overlay struct{}
// Lookup finds the address of a node in our overlay network
func (o *Overlay) Lookup(ctx context.Context, req *overlay.LookupRequest) (*overlay.LookupResponse, error) {
// TODO: fill this in with logic to communicate with kademlia
return nil, nil
}
// FindStorageNodes searches the overlay network for nodes that meet the provided requirements
func (o *Overlay) FindStorageNodes(ctx context.Context, req *overlay.FindStorageNodesRequest) (*overlay.FindStorageNodesResponse, error) {
// TODO: fill this in with logic to communicate with kademlia
return nil, nil
}

36
pkg/overlay/service.go Normal file
View File

@ -0,0 +1,36 @@
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
}

View File

3
scripts/build-protos.sh Executable file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
protoc -Iprotos/overlay -Iprotos/google protos/overlay/overlay.proto --go_out=plugins=grpc:protos/overlay/