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.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. server *grpc.Server
  16. app types.TMSPApplicationServer
  17. }
  18. func NewGRPCServer(protoAddr string, app types.TMSPApplicationServer) (Service, error) {
  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.QuitService = *NewQuitService(nil, "TMSPServer", s)
  28. _, err := s.Start() // Just start it
  29. return s, err
  30. }
  31. func (s *GRPCServer) OnStart() error {
  32. s.QuitService.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.RegisterTMSPApplicationServer(s.server, s.app)
  40. go s.server.Serve(s.listener)
  41. return nil
  42. }
  43. func (s *GRPCServer) OnStop() {
  44. s.QuitService.OnStop()
  45. s.server.Stop()
  46. }