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.

61 lines
1.2 KiB

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