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.

70 lines
1.5 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. Package server is used to start a new ABCI server.
  3. It defines the struct for gRPC server settings, and functions for:
  4. * Starting a new gRPC server
  5. * Stopping a gRPC server
  6. */
  7. package server
  8. import (
  9. "net"
  10. "strings"
  11. "google.golang.org/grpc"
  12. "github.com/tendermint/abci/types"
  13. cmn "github.com/tendermint/tmlibs/common"
  14. )
  15. // var maxNumberConnections = 2
  16. //GRPCServer is used to set the protocol and address for gRPC.
  17. type GRPCServer struct {
  18. cmn.BaseService
  19. proto string
  20. addr string
  21. listener net.Listener
  22. server *grpc.Server
  23. app types.ABCIApplicationServer
  24. }
  25. //NewGRPCServer allows setting up a new gRPC ABCI server.
  26. func NewGRPCServer(protoAddr string, app types.ABCIApplicationServer) (cmn.Service, error) {
  27. parts := strings.SplitN(protoAddr, "://", 2)
  28. proto, addr := parts[0], parts[1]
  29. s := &GRPCServer{
  30. proto: proto,
  31. addr: addr,
  32. listener: nil,
  33. app: app,
  34. }
  35. s.BaseService = *cmn.NewBaseService(nil, "ABCIServer", s)
  36. _, err := s.Start() // Just start it
  37. return s, err
  38. }
  39. //Onstart registers a new gRPC service and tells that service to listen on the port that is set in NewGRPCServer.
  40. func (s *GRPCServer) OnStart() error {
  41. s.BaseService.OnStart()
  42. ln, err := net.Listen(s.proto, s.addr)
  43. if err != nil {
  44. return err
  45. }
  46. s.listener = ln
  47. s.server = grpc.NewServer()
  48. types.RegisterABCIApplicationServer(s.server, s.app)
  49. go s.server.Serve(s.listener)
  50. return nil
  51. }
  52. //OnStop is called when a gRPC server is stopped.
  53. func (s *GRPCServer) OnStop() {
  54. s.BaseService.OnStop()
  55. s.server.Stop()
  56. }