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.

55 lines
1.0 KiB

  1. package server
  2. import (
  3. "net"
  4. "strings"
  5. "google.golang.org/grpc"
  6. . "github.com/tendermint/go-common"
  7. "github.com/tendermint/tmsp/types"
  8. )
  9. // var maxNumberConnections = 2
  10. type GRPCServer struct {
  11. QuitService
  12. proto string
  13. addr string
  14. listener net.Listener
  15. app types.TMSPApplicationServer
  16. }
  17. func NewGRPCServer(protoAddr string, app types.TMSPApplicationServer) (Service, error) {
  18. parts := strings.SplitN(protoAddr, "://", 2)
  19. proto, addr := parts[0], parts[1]
  20. s := &GRPCServer{
  21. proto: proto,
  22. addr: addr,
  23. listener: nil,
  24. app: app,
  25. }
  26. s.QuitService = *NewQuitService(nil, "TMSPServer", s)
  27. _, err := s.Start() // Just start it
  28. return s, err
  29. }
  30. func (s *GRPCServer) OnStart() error {
  31. s.QuitService.OnStart()
  32. ln, err := net.Listen(s.proto, s.addr)
  33. if err != nil {
  34. return err
  35. }
  36. s.listener = ln
  37. grpcServer := grpc.NewServer()
  38. types.RegisterTMSPApplicationServer(grpcServer, s.app)
  39. go grpcServer.Serve(ln)
  40. return nil
  41. }
  42. func (s *GRPCServer) OnStop() {
  43. s.QuitService.OnStop()
  44. s.listener.Close()
  45. }