Skip to content

Commit

Permalink
Move SIP client and server init to the service package.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Nov 30, 2023
1 parent 9842f96 commit 3c2bc60
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
35 changes: 2 additions & 33 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}

}
}()

Expand Down
41 changes: 35 additions & 6 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 3c2bc60

Please sign in to comment.