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.

63 lines
1.3 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. proto string
  14. addr string
  15. listener net.Listener
  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. proto: proto,
  24. addr: addr,
  25. listener: nil,
  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.listener = ln
  38. s.server = grpc.NewServer()
  39. types.RegisterABCIApplicationServer(s.server, s.app)
  40. s.Logger.Info("Listening", "proto", s.proto, "addr", s.addr)
  41. go func() {
  42. if err := s.server.Serve(s.listener); err != nil {
  43. s.Logger.Error("Error serving gRPC server", "err", err)
  44. }
  45. }()
  46. return nil
  47. }
  48. // OnStop stops the gRPC server.
  49. func (s *GRPCServer) OnStop() {
  50. s.server.Stop()
  51. }