Skip to content

Commit

Permalink
🔇 silent changes: rename functions #14
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jan 13, 2024
1 parent 59caf9a commit e9b4cfa
Show file tree
Hide file tree
Showing 32 changed files with 259 additions and 263 deletions.
20 changes: 10 additions & 10 deletions pkg/ami/ami_ confbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ func ConfbridgeList(ctx context.Context, s AMISocket, conference string) ([]AmiR
c := NewCommand().SetId(s.UUID).SetAction(config.AmiActionConfbridgeList).SetVCmd(map[string]interface{}{
config.AmiFieldConference: conference,
})
callback := NewAMICallbackService(ctx, s, c,
callback := NewAmiCallbackService(ctx, s, c,
[]string{config.AmiListenerEventConfbridgeList}, []string{config.AmiListenerEventConfbridgeListComplete})
return callback.SendSuperLevel()
}

// ConfbridgeListRooms lists data about all active conferences.
func ConfbridgeListRooms(ctx context.Context, s AMISocket) ([]AmiReply, error) {
c := NewCommand().SetId(s.UUID).SetAction(config.AmiActionConfbridgeListRooms)
callback := NewAMICallbackService(ctx, s, c,
callback := NewAmiCallbackService(ctx, s, c,
[]string{config.AmiListenerEventConfbridgeListRooms}, []string{config.AmiListenerEventConfbridgeListRoomsComplete})
return callback.SendSuperLevel()
}
Expand All @@ -30,7 +30,7 @@ func ConfbridgeMute(ctx context.Context, s AMISocket, conference string, channel
config.AmiFieldConference: conference,
config.AmiFieldChannel: channel,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -40,7 +40,7 @@ func ConfbridgeUnmute(ctx context.Context, s AMISocket, conference string, chann
config.AmiFieldConference: conference,
config.AmiFieldChannel: channel,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -50,7 +50,7 @@ func ConfbridgeKick(ctx context.Context, s AMISocket, conference string, channel
config.AmiFieldConference: conference,
config.AmiFieldChannel: channel,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -60,7 +60,7 @@ func ConfbridgeLock(ctx context.Context, s AMISocket, conference string, channel
config.AmiFieldConference: conference,
config.AmiFieldChannel: channel,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -70,7 +70,7 @@ func ConfbridgeUnlock(ctx context.Context, s AMISocket, conference string, chann
config.AmiFieldConference: conference,
config.AmiFieldChannel: channel,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -80,7 +80,7 @@ func ConfbridgeSetSingleVideoSrc(ctx context.Context, s AMISocket, conference st
config.AmiFieldConference: conference,
config.AmiFieldChannel: channel,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -93,7 +93,7 @@ func ConfbridgeStartRecord(ctx context.Context, s AMISocket, conference string,
params[config.AmiFieldRecordFile] = recordFile
}
c := NewCommand().SetId(s.UUID).SetAction(config.AmiActionConfbridgeStartRecord).SetVCmd(params)
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -102,6 +102,6 @@ func ConfbridgeStopRecord(ctx context.Context, s AMISocket, conference string) (
c := NewCommand().SetId(s.UUID).SetAction(config.AmiActionConfbridgeStopRecord).SetVCmd(map[string]interface{}{
config.AmiFieldConference: conference,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}
4 changes: 2 additions & 2 deletions pkg/ami/ami_ presence.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ func PresenceState(ctx context.Context, s AMISocket, provider string) (AmiReply,
c.SetV(map[string]interface{}{
config.AmiFieldProvider: provider,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

// PresenceStateList list the current known presence states.
func PresenceStateList(ctx context.Context, s AMISocket) ([]AmiReply, error) {
c := NewCommand().SetId(s.UUID).SetAction(config.AmiActionPresenceStateList)
callback := NewAMICallbackService(ctx, s, c,
callback := NewAmiCallbackService(ctx, s, c,
[]string{config.AmiListenerEventAgents}, []string{config.AmiListenerEventPresenceStateListComplete})
return callback.SendSuperLevel()
}
4 changes: 2 additions & 2 deletions pkg/ami/ami_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// Agents lists agents and their status.
func Agents(ctx context.Context, s AMISocket) ([]AmiReply, error) {
c := NewCommand().SetId(s.UUID).SetAction(config.AmiActionAgents)
callback := NewAMICallbackService(ctx, s, c, []string{config.AmiListenerEventAgents},
callback := NewAmiCallbackService(ctx, s, c, []string{config.AmiListenerEventAgents},
[]string{config.AmiListenerEventAgentsComplete})
return callback.SendSuperLevel()
}
Expand All @@ -21,6 +21,6 @@ func AgentLogoff(ctx context.Context, s AMISocket, agent string, soft bool) (Ami
config.AmiFieldAgent: agent,
config.AmiFieldSoft: soft,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}
4 changes: 2 additions & 2 deletions pkg/ami/ami_agi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func AGI(ctx context.Context, s AMISocket, channel, agiCommand, agiCommandID str
config.AmiFieldCommand: agiCommand,
config.AmiFieldCommandID: agiCommandID,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -25,6 +25,6 @@ func ControlPlayback(ctx context.Context, s AMISocket, channel string, control c
config.AmiFieldChannel: channel,
config.AmiFieldControl: control,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}
4 changes: 2 additions & 2 deletions pkg/ami/ami_atxfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Atxfer(ctx context.Context, s AMISocket, channel, extension, context string
config.AmiFieldExtension: extension,
config.AmiFieldContext: context,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -24,6 +24,6 @@ func CancelAtxfer(ctx context.Context, s AMISocket, channel string) (AmiReply, e
c.SetV(map[string]interface{}{
config.AmiFieldChannel: channel,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}
18 changes: 9 additions & 9 deletions pkg/ami/ami_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Bridge(ctx context.Context, s AMISocket, channel1, channel2 string, tone st
config.AmiFieldChannel2: channel2,
config.AmiFieldTone: tone,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -26,7 +26,7 @@ func BlindTransfer(ctx context.Context, s AMISocket, channel, context, extension
config.AmiFieldContext: context,
config.AmiFieldExtension: extension,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -36,7 +36,7 @@ func BridgeDestroy(ctx context.Context, s AMISocket, bridgeUniqueId string) (Ami
c.SetV(map[string]interface{}{
config.AmiFieldBridgeUniqueId: bridgeUniqueId,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -46,7 +46,7 @@ func BridgeInfo(ctx context.Context, s AMISocket, bridgeUniqueId string) (AmiRep
c.SetV(map[string]interface{}{
config.AmiFieldBridgeUniqueId: bridgeUniqueId,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -60,7 +60,7 @@ func BridgeKick(ctx context.Context, s AMISocket, bridgeUniqueId, channel string
}
c := NewCommand().SetId(s.UUID).SetAction(config.AmiActionBridgeKick)
c.SetV(params)
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -70,14 +70,14 @@ func BridgeList(ctx context.Context, s AMISocket, bridgeType string) (AmiReply,
c.SetV(map[string]interface{}{
config.AmiFieldBridgeType: bridgeType,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

// BridgeTechnologyList list available bridging technologies and their statuses.
func BridgeTechnologyList(ctx context.Context, s AMISocket) ([]AmiReply, error) {
c := NewCommand().SetId(s.UUID).SetAction(config.AmiActionBridgeTechnologyList)
callback := NewAMICallbackService(ctx, s, c,
callback := NewAmiCallbackService(ctx, s, c,
[]string{config.AmiListenerEventBridgeTechnologyListItem}, []string{config.AmiListenerEventBridgeTechnologyListComplete})
return callback.SendSuperLevel()
}
Expand All @@ -88,7 +88,7 @@ func BridgeTechnologySuspend(ctx context.Context, s AMISocket, bridgeTechnology
c.SetV(map[string]interface{}{
config.AmiFieldBridgeTechnology: bridgeTechnology,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}

Expand All @@ -98,6 +98,6 @@ func BridgeTechnologyUnsuspend(ctx context.Context, s AMISocket, bridgeTechnolog
c.SetV(map[string]interface{}{
config.AmiFieldBridgeTechnology: bridgeTechnology,
})
callback := NewAMICallbackService(ctx, s, c, []string{}, []string{})
callback := NewAmiCallbackService(ctx, s, c, []string{}, []string{})
return callback.Send()
}
45 changes: 14 additions & 31 deletions pkg/ami/ami_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ package ami

import (
"context"
"log"
"strings"
"time"

"github.com/pnguyen215/voipkit/pkg/ami/config"
)

type AMICallbackService interface {
type AmiCallbackService interface {
Send() (AmiReply, error)
SendLevel() (AmiReplies, error)
SendSuperLevel() ([]AmiReply, error)
}

func NewAMICallbackService(ctx context.Context, socket AMISocket, command *AMICommand,
acceptedEvents []string, ignoreEvents []string) AMICallbackService {
a := NewAMICallbackHandler()
func NewAmiCallbackService(ctx context.Context, socket AMISocket, command *AMICommand,
acceptedEvents []string, ignoreEvents []string) AmiCallbackService {
a := NewAmiCallbackHandler()
a.SetContext(ctx)
a.SetSocket(socket)
a.SetCommand(command)
Expand All @@ -26,11 +25,11 @@ func NewAMICallbackService(ctx context.Context, socket AMISocket, command *AMICo
return a
}

func NewAMICallbackServiceWith(handler *AMICallbackHandler) AMICallbackService {
func NewAmiCallbackHandlerService(handler *AMICallbackHandler) AmiCallbackService {
return handler
}

func NewAMICallbackHandler() *AMICallbackHandler {
func NewAmiCallbackHandler() *AMICallbackHandler {
a := &AMICallbackHandler{}
return a
}
Expand Down Expand Up @@ -88,28 +87,23 @@ func (h *AMICallbackHandler) Send() (AmiReply, error) {
var total time.Duration = 0

for i := 1; i <= h.Socket.MaxRetries; i++ {
// _start := time.Now().UnixMilli()
_start := time.Now()
response, err = h.Command.Send(h.Ctx, h.Socket, h.Command)
// _end := time.Now().UnixMilli() - _start
_end := time.Since(_start)
total += _end
if _end == 0 || strings.EqualFold(response.GetVal(config.AmiJsonFieldStatus), config.AmiFullyBootedKey) {
if _end == 0 || strings.EqualFold(response.Get(config.AmiJsonFieldStatus), config.AmiFullyBootedKey) {
continue
}

if len(response) > 0 && err == nil {
if h.Socket.DebugMode {
log.Printf("Send(). callback return for the %v time(s) and waste time = %v", i, _end)
D().Info("Send(). callback return for the %v time(s) and waste time: %v", i, _end)
}
break
}
}

if h.Socket.DebugMode {
log.Printf("Send(). callback total waste time = %v", total)
D().Info("Send(). callback total waste time: %v", total)
}

return response, err
}

Expand All @@ -127,29 +121,23 @@ func (h *AMICallbackHandler) SendLevel() (AmiReplies, error) {
var total time.Duration = 0

for i := 1; i <= h.Socket.MaxRetries; i++ {
// _start := time.Now().UnixMilli()
_start := time.Now()
response, err = h.Command.SendLevel(h.Ctx, h.Socket, h.Command)
// _end := time.Now().UnixMilli() - _start
_end := time.Since(_start)
// total += _end
total += _end
if _end == 0 || strings.EqualFold(response.GetVal(config.AmiJsonFieldStatus), config.AmiFullyBootedKey) {
if _end == 0 || strings.EqualFold(response.Get(config.AmiJsonFieldStatus), config.AmiFullyBootedKey) {
continue
}

if len(response) > 0 && err == nil {
if h.Socket.DebugMode {
log.Printf("SendLevel(). callback return for the %v time(s) and waste time = %v", i, _end)
D().Info("SendLevel(). callback return for the %v time(s) and waste time: %v", i, _end)
}
break
}
}

if h.Socket.DebugMode {
log.Printf("SendLevel(). callback total waste time = %v", total)
D().Info("SendLevel(). callback total waste time: %v", total)
}

return response, err
}

Expand All @@ -167,27 +155,22 @@ func (h *AMICallbackHandler) SendSuperLevel() ([]AmiReply, error) {
var total time.Duration = 0

for i := 1; i <= h.Socket.MaxRetries; i++ {
// _start := time.Now().UnixMilli()
_start := time.Now()
response, err = h.Command.DoGetResult(h.Ctx, h.Socket, h.Command, h.AcceptedEvents, h.IgnoreEvents)
// _end := time.Now().UnixMilli() - _start
_end := time.Since(_start)
total += _end
if _end == 0 {
continue
}

if len(response) > 0 && err == nil {
if h.Socket.DebugMode {
log.Printf("SendSuperLevel(). callback return for the %v time(s) and waste time = %v", i, _end)
D().Info("SendSuperLevel(). callback return for the %v time(s) and waste time: %v", i, _end)
}
break
}
}

if h.Socket.DebugMode {
log.Printf("SendSuperLevel(). callback total waste time = %v", total)
D().Info("SendSuperLevel(). callback total waste time: %v", total)
}

return response, err
}
Loading

0 comments on commit e9b4cfa

Please sign in to comment.