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. "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. s.BaseService.OnStart()
  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. }