diff --git a/cmd/server/main.go b/cmd/server/main.go index 14cfdf5a..1933c005 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -16,23 +16,19 @@ package main import ( "fmt" - "log" "os" "os/signal" "syscall" - "github.com/emiago/sipgo" "github.com/urfave/cli/v2" "github.com/livekit/protocol/logger" "github.com/livekit/protocol/redis" - "github.com/livekit/protocol/rpc" "github.com/livekit/psrpc" "github.com/livekit/sip/pkg/config" "github.com/livekit/sip/pkg/errors" "github.com/livekit/sip/pkg/service" - "github.com/livekit/sip/pkg/sip" "github.com/livekit/sip/version" ) @@ -72,9 +68,9 @@ func runService(c *cli.Context) error { if err != nil { return err } - bus := psrpc.NewRedisMessageBus(rc) - psrpcClient, err := rpc.NewIOInfoClient(bus) + + svc, err := service.NewService(conf, bus) if err != nil { return err } @@ -85,41 +81,14 @@ func runService(c *cli.Context) error { killChan := make(chan os.Signal, 1) signal.Notify(killChan, syscall.SIGINT) - ua, err := sipgo.NewUA( - sipgo.WithUserAgent(sip.UserAgent), - ) - if err != nil { - return err - } - - sipCli := sip.NewClient(conf) - if err = sipCli.Start(ua); err != nil { - return err - } - - svc := service.NewService(conf, sipCli, psrpcClient, bus) - - sipSrv := sip.NewServer(conf, svc.HandleTrunkAuthentication, svc.HandleDispatchRules) - if err = sipSrv.Start(ua); err != nil { - return err - } - go func() { select { case sig := <-stopChan: logger.Infow("exit requested, finishing all SIP then shutting down", "signal", sig) svc.Stop(false) - case sig := <-killChan: logger.Infow("exit requested, stopping all SIP and shutting down", "signal", sig) svc.Stop(true) - if err = sipCli.Stop(); err != nil { - log.Println(err) - } - if err = sipSrv.Stop(); err != nil { - log.Println(err) - } - } }() diff --git a/pkg/service/service.go b/pkg/service/service.go index 094fee5b..96cd7445 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -18,44 +18,73 @@ import ( "context" "log" + "github.com/emiago/sipgo" "github.com/frostbyte73/core" "github.com/livekit/protocol/logger" "github.com/livekit/protocol/rpc" "github.com/livekit/psrpc" "github.com/livekit/sip/pkg/config" + "github.com/livekit/sip/pkg/sip" "github.com/livekit/sip/version" ) type Service struct { conf *config.Config + cli *sip.Client + srv *sip.Server - psrpcServer rpc.SIPInternalServerImpl psrpcClient rpc.IOInfoClient bus psrpc.MessageBus shutdown core.Fuse } -func NewService(conf *config.Config, srv rpc.SIPInternalServerImpl, cli rpc.IOInfoClient, bus psrpc.MessageBus) *Service { +func NewService(conf *config.Config, bus psrpc.MessageBus) (*Service, error) { + psrpcClient, err := rpc.NewIOInfoClient(bus) + if err != nil { + return nil, err + } + cli := sip.NewClient(conf) s := &Service{ conf: conf, - psrpcServer: srv, - psrpcClient: cli, + cli: cli, + psrpcClient: psrpcClient, bus: bus, shutdown: core.NewFuse(), } - return s + s.srv = sip.NewServer(conf, s.HandleTrunkAuthentication, s.HandleDispatchRules) + return s, nil } func (s *Service) Stop(kill bool) { s.shutdown.Break() + if kill { + if err := s.cli.Stop(); err != nil { + log.Println(err) + } + if err := s.srv.Stop(); err != nil { + log.Println(err) + } + } } func (s *Service) Run() error { logger.Debugw("starting service", "version", version.Version) + ua, err := sipgo.NewUA( + sipgo.WithUserAgent(sip.UserAgent), + ) + if err != nil { + return err + } + if err = s.cli.Start(ua); err != nil { + return err + } + if err = s.srv.Start(ua); err != nil { + return err + } - srv, err := rpc.NewSIPInternalServer(s.psrpcServer, s.bus) + srv, err := rpc.NewSIPInternalServer(s.cli, s.bus) if err != nil { return err }