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.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. if err := s.BaseService.OnStart(); err != nil {
  32. return err
  33. }
  34. ln, err := net.Listen(s.proto, s.addr)
  35. if err != nil {
  36. return err
  37. }
  38. s.Logger.Info("Listening", "proto", s.proto, "addr", s.addr)
  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. }