Skip to content

Commit

Permalink
Use function pointers instead of interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Nov 30, 2023
1 parent ca0f9c3 commit 7f53a99
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
3 changes: 2 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func runService(c *cli.Context) error {
}

svc := service.NewService(conf, sipsrv.InternalServerImpl(), psrpcClient, bus)
sipsrv.SetServerHandler(svc)
sipsrv.SetAuthHandler(svc.HandleTrunkAuthentication)
sipsrv.SetDispatchRuleHandlerFunc(svc.HandleDispatchRules)

if err = sipsrv.Start(); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions pkg/sip/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (s *Server) onInvite(req *sip.Request, tx sip.ServerTransaction) {
}
src := req.Source()

username, password, err := s.handler.HandleTrunkAuthentication(from.Address.User, to.Address.User, src)
username, password, err := s.authHandler(from.Address.User, to.Address.User, src)
if err != nil {
sipErrorResponse(tx, req)
return
Expand Down Expand Up @@ -153,7 +153,7 @@ func (s *Server) newInboundCall(tag string, from *sip.FromHeader, to *sip.ToHead
func (c *inboundCall) handleInvite(req *sip.Request, tx sip.ServerTransaction) {
// Send initial request. In the best case scenario, we will immediately get a room name to join.
// Otherwise, we could even learn that this number is not allowed and reject the call, or ask for pin if required.
roomName, identity, requirePin, rejectInvite := c.s.handler.HandleDispatchRules(c.from.Address.User, c.to.Address.User, c.src, "", false)
roomName, identity, requirePin, rejectInvite := c.s.dispatchRuleHandler(c.from.Address.User, c.to.Address.User, c.src, "", false)
if rejectInvite {
sipErrorResponse(tx, req)
return
Expand Down Expand Up @@ -226,7 +226,7 @@ func (c *inboundCall) pinPrompt() {
noPin = pin == ""

log.Printf("Checking Pin for SIP call %q -> %q = %q (noPin = %v)\n", c.from.Address.User, c.to.Address.User, pin, noPin)
roomName, identity, requirePin, reject := c.s.handler.HandleDispatchRules(c.from.Address.User, c.to.Address.User, c.src, pin, noPin)
roomName, identity, requirePin, reject := c.s.dispatchRuleHandler(c.from.Address.User, c.to.Address.User, c.src, pin, noPin)
if reject || requirePin || roomName == "" {
c.playAudio(c.s.res.wrongPin)
c.Close()
Expand Down
30 changes: 12 additions & 18 deletions pkg/sip/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ var (
)

type (
Server struct {
AuthHandlerFunc func(from, to, srcAddress string) (username, password string, err error)
DispatchRuleHandlerFunc func(callingNumber, calledNumber, srcAddress string, pin string, noPin bool) (joinRoom, identity string, requestPin, rejectInvite bool)
Server struct {
sipSrv *sipgo.Server
publicIp string

Expand All @@ -47,8 +49,9 @@ type (
cmu sync.RWMutex
activeCalls map[string]*inboundCall

handler ServerHandler
conf *config.Config
authHandler AuthHandlerFunc
dispatchRuleHandler DispatchRuleHandlerFunc
conf *config.Config

res mediaRes
}
Expand All @@ -59,19 +62,6 @@ type (
}
)

type AuthHandler interface {
HandleTrunkAuthentication(from, to, srcAddress string) (username, password string, err error)
}

type DispatchRuleHandler interface {
HandleDispatchRules(callingNumber, calledNumber, srcAddress string, pin string, noPin bool) (joinRoom, identity string, requestPin, rejectInvite bool)
}

type ServerHandler interface {
AuthHandler
DispatchRuleHandler
}

func NewServer(conf *config.Config) *Server {
s := &Server{
conf: conf,
Expand All @@ -83,8 +73,12 @@ func NewServer(conf *config.Config) *Server {
return s
}

func (s *Server) SetHandler(handler ServerHandler) {
s.handler = handler
func (s *Server) SetAuthHandler(handler AuthHandlerFunc) {
s.authHandler = handler
}

func (s *Server) SetDispatchRuleHandlerFunc(handler DispatchRuleHandlerFunc) {
s.dispatchRuleHandler = handler
}

func getTagValue(req *sip.Request) (string, error) {
Expand Down
9 changes: 7 additions & 2 deletions pkg/sip/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/emiago/sipgo"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/rpc"

"github.com/livekit/sip/pkg/config"
"github.com/livekit/sip/version"
)
Expand Down Expand Up @@ -49,8 +50,12 @@ func (s *Service) Stop(kill bool) {
}
}

func (s *Service) SetServerHandler(handler ServerHandler) {
s.srv.SetHandler(handler)
func (s *Service) SetAuthHandler(handler AuthHandlerFunc) {
s.srv.SetAuthHandler(handler)
}

func (s *Service) SetDispatchRuleHandlerFunc(handler DispatchRuleHandlerFunc) {
s.srv.SetDispatchRuleHandlerFunc(handler)
}

func (s *Service) InternalServerImpl() rpc.SIPInternalServerImpl {
Expand Down

0 comments on commit 7f53a99

Please sign in to comment.