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.

56 lines
1.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package server
  2. import (
  3. "net"
  4. "google.golang.org/grpc"
  5. "github.com/tendermint/abci/types"
  6. cmn "github.com/tendermint/tmlibs/common"
  7. )
  8. type GRPCServer struct {
  9. cmn.BaseService
  10. proto string
  11. addr string
  12. listener net.Listener
  13. server *grpc.Server
  14. app types.ABCIApplicationServer
  15. }
  16. // NewGRPCServer returns a new gRPC ABCI server
  17. func NewGRPCServer(protoAddr string, app types.ABCIApplicationServer) cmn.Service {
  18. proto, addr := cmn.ProtocolAndAddress(protoAddr)
  19. s := &GRPCServer{
  20. proto: proto,
  21. addr: addr,
  22. listener: nil,
  23. app: app,
  24. }
  25. s.BaseService = *cmn.NewBaseService(nil, "ABCIServer", s)
  26. return s
  27. }
  28. // OnStart starts the gRPC service
  29. func (s *GRPCServer) OnStart() error {
  30. if err := s.BaseService.OnStart(); err != nil {
  31. return err
  32. }
  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. go s.server.Serve(s.listener)
  41. return nil
  42. }
  43. // OnStop stops the gRPC server
  44. func (s *GRPCServer) OnStop() {
  45. s.BaseService.OnStop()
  46. s.server.Stop()
  47. }