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.

58 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. "strings"
  5. "google.golang.org/grpc"
  6. "github.com/tendermint/abci/types"
  7. cmn "github.com/tendermint/tmlibs/common"
  8. )
  9. type GRPCServer struct {
  10. cmn.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) cmn.Service {
  19. parts := strings.SplitN(protoAddr, "://", 2)
  20. proto, addr := parts[0], parts[1]
  21. s := &GRPCServer{
  22. proto: proto,
  23. addr: addr,
  24. listener: nil,
  25. app: app,
  26. }
  27. s.BaseService = *cmn.NewBaseService(nil, "ABCIServer", s)
  28. return s
  29. }
  30. // OnStart starts the gRPC service
  31. func (s *GRPCServer) OnStart() error {
  32. if err := s.BaseService.OnStart(); err != nil {
  33. return err
  34. }
  35. ln, err := net.Listen(s.proto, s.addr)
  36. if err != nil {
  37. return err
  38. }
  39. s.listener = ln
  40. s.server = grpc.NewServer()
  41. types.RegisterABCIApplicationServer(s.server, s.app)
  42. go s.server.Serve(s.listener)
  43. return nil
  44. }
  45. // OnStop stops the gRPC server
  46. func (s *GRPCServer) OnStop() {
  47. s.BaseService.OnStop()
  48. s.server.Stop()
  49. }