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.

57 lines
1.2 KiB

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/tendermint/abci/types"
  6. cmn "github.com/tendermint/tendermint/libs/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.Logger.Info("Listening", "proto", s.proto, "addr", s.addr)
  38. s.listener = ln
  39. s.server = grpc.NewServer()
  40. types.RegisterABCIApplicationServer(s.server, s.app)
  41. go s.server.Serve(s.listener)
  42. return nil
  43. }
  44. // OnStop stops the gRPC server
  45. func (s *GRPCServer) OnStop() {
  46. s.BaseService.OnStop()
  47. s.server.Stop()
  48. }