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.

64 lines
1.4 KiB

8 years ago
8 years ago
  1. package server
  2. import (
  3. "context"
  4. "net"
  5. "google.golang.org/grpc"
  6. "github.com/tendermint/tendermint/abci/types"
  7. "github.com/tendermint/tendermint/libs/log"
  8. tmnet "github.com/tendermint/tendermint/libs/net"
  9. "github.com/tendermint/tendermint/libs/service"
  10. )
  11. type GRPCServer struct {
  12. service.BaseService
  13. logger log.Logger
  14. proto string
  15. addr string
  16. server *grpc.Server
  17. app types.ABCIApplicationServer
  18. }
  19. // NewGRPCServer returns a new gRPC ABCI server
  20. func NewGRPCServer(logger log.Logger, protoAddr string, app types.ABCIApplicationServer) service.Service {
  21. proto, addr := tmnet.ProtocolAndAddress(protoAddr)
  22. s := &GRPCServer{
  23. logger: logger,
  24. proto: proto,
  25. addr: addr,
  26. app: app,
  27. }
  28. s.BaseService = *service.NewBaseService(logger, "ABCIServer", s)
  29. return s
  30. }
  31. // OnStart starts the gRPC service.
  32. func (s *GRPCServer) OnStart(ctx context.Context) error {
  33. ln, err := net.Listen(s.proto, s.addr)
  34. if err != nil {
  35. return err
  36. }
  37. s.server = grpc.NewServer()
  38. types.RegisterABCIApplicationServer(s.server, s.app)
  39. s.logger.Info("Listening", "proto", s.proto, "addr", s.addr)
  40. go func() {
  41. go func() {
  42. <-ctx.Done()
  43. s.server.GracefulStop()
  44. }()
  45. if err := s.server.Serve(ln); err != nil {
  46. s.logger.Error("error serving gRPC server", "err", err)
  47. }
  48. }()
  49. return nil
  50. }
  51. // OnStop stops the gRPC server.
  52. func (s *GRPCServer) OnStop() { s.server.Stop() }