Skip to content

Commit

Permalink
add config.go
Browse files Browse the repository at this point in the history
  • Loading branch information
lwlee2608 committed Nov 19, 2023
1 parent 3bc8108 commit e1b1082
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 29 deletions.
91 changes: 91 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package diameter

import (
"encoding/json"
"time"

log "github.com/sirupsen/logrus"
)

type CapacityExchangeConfig struct {
VendorID *uint32 `json:"vendorID"`
ProductName *string `json:"productName,omitempty"`
OriginHost *string `json:"originHost,omitempty"`
OriginRealm *string `json:"originRealm,omitempty"`
}

type DiameterConfig struct {
MaxRetransmits *uint `json:"maxRetransmits,omitempty"`
RetransmitInterval *time.Duration `json:"retransmitInterval,omitempty"`
EnableWatchdog *bool `json:"enableWatchdog,omitempty"`
WatchdogInterval *time.Duration `json:"watchdogInterval,omitempty"`
WatchdogStream *uint `json:"watchdogStream,omitempty"`
CapacityExchange *CapacityExchangeConfig `json:"capacityExchange,omitempty"`
}

func processConfig(arg map[string]interface{}) (*DiameterConfig, error) {

var config DiameterConfig
if b, err := json.Marshal(arg); err != nil {
return nil, err
} else {
if err = json.Unmarshal(b, &config); err != nil {
return nil, err
}
}

setDiameterConfigDefaults(&config)

log.Infof("Config %+v\n", config)
log.Infof("CE Config %+v\n", config.CapacityExchange)

return &config, nil
}

func setDiameterConfigDefaults(config *DiameterConfig) {
// Default values
var defaultMaxRetransmits uint = 1
var defaultRetransmitInterval = 1 * time.Second
var defaultEnableWatchdog = true
var defaultWatchdogInterval = 5 * time.Second
var defaultWatchdogStream uint = 0

var defaultVendorID uint32 = 13
var defaultProductName = "xk6-diameter"
var defaultOriginHost = "origin.host"
var defaultOriginRealm = "origin.realm"

// Set defaults for DiameterConfig
if config.MaxRetransmits == nil {
config.MaxRetransmits = &defaultMaxRetransmits
}
if config.RetransmitInterval == nil {
config.RetransmitInterval = &defaultRetransmitInterval
}
if config.EnableWatchdog == nil {
config.EnableWatchdog = &defaultEnableWatchdog
}
if config.WatchdogInterval == nil {
config.WatchdogInterval = &defaultWatchdogInterval
}
if config.WatchdogStream == nil {
config.WatchdogStream = &defaultWatchdogStream
}

// Set defaults for CapacityExchangeConfig
if config.CapacityExchange == nil {
config.CapacityExchange = &CapacityExchangeConfig{}
}
if config.CapacityExchange.VendorID == nil {
config.CapacityExchange.VendorID = &defaultVendorID
}
if config.CapacityExchange.ProductName == nil {
config.CapacityExchange.ProductName = &defaultProductName
}
if config.CapacityExchange.OriginHost == nil {
config.CapacityExchange.OriginHost = &defaultOriginHost
}
if config.CapacityExchange.OriginRealm == nil {
config.CapacityExchange.OriginRealm = &defaultOriginRealm
}
}
42 changes: 14 additions & 28 deletions diameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,18 @@ type AVP struct{}

type Dict struct{}

type DiameterConfig struct {
// Settings
VendorID datatype.Unsigned32 `json:"vendorID"`
ProductName datatype.UTF8String `json:"productName"`

// Client Config
MaxRetransmits uint `json:"maxRetransmits"`
RetransmitInterval time.Duration `json:"retransmitInterval"`
EnableWatchdog bool `json:"enableWatchdog"`
WatchdogInterval time.Duration `json:"watchdogInterval"`
WatchdogStream uint `json:"watchdogStream"`

// SupportedVendorID []*diam.AVP // Supported vendor ID
// AcctApplicationID []*diam.AVP // Acct applications
// AuthApplicationID []*diam.AVP // Auth applications
// VendorSpecificApplicationID []*diam.AVP // Vendor specific applications
}
func (*Diameter) XClient(arg map[string]interface{}) (*DiameterClient, error) {

func (*Diameter) XClient() (*DiameterClient, error) {
config, err := processConfig(arg)
if err != nil {
return nil, err
}

// TODO make all this configurable later
cfg := &sm.Settings{
OriginHost: datatype.DiameterIdentity("diam.host"),
OriginRealm: datatype.DiameterIdentity("diam.realm"),
VendorID: 13,
ProductName: "xk6-diameter",
OriginHost: datatype.DiameterIdentity(*config.CapacityExchange.OriginHost),
OriginRealm: datatype.DiameterIdentity(*config.CapacityExchange.OriginRealm),
VendorID: datatype.Unsigned32(*config.CapacityExchange.VendorID),
ProductName: datatype.UTF8String(*config.CapacityExchange.ProductName),
OriginStateID: datatype.Unsigned32(time.Now().Unix()),
FirmwareRevision: 1,
HostIPAddresses: []datatype.Address{
Expand All @@ -67,15 +53,15 @@ func (*Diameter) XClient() (*DiameterClient, error) {

hopIds := make(map[uint32]chan *diam.Message)
mux.Handle("CCA", handleCCA(hopIds))
// TODO need to support other diameter CMD

client := &sm.Client{
Dict: dict.Default,
Handler: mux,
MaxRetransmits: 1,
RetransmitInterval: time.Second,
EnableWatchdog: true,
WatchdogInterval: 5 * time.Second,
MaxRetransmits: *config.MaxRetransmits,
RetransmitInterval: *config.RetransmitInterval,
EnableWatchdog: *config.EnableWatchdog,
WatchdogInterval: *config.WatchdogInterval,
WatchdogStream: *config.WatchdogStream,
AuthApplicationID: []*diam.AVP{
diam.NewAVP(avp.AuthApplicationID, avp.Mbit, 0, datatype.Unsigned32(4)),
},
Expand Down
6 changes: 5 additions & 1 deletion example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export let options = {
dict.load("dict/extra.xml")

// Init Client
let client = diam.Client()
let client = diam.Client({
MaxRetransmits: 1,
EnableWatchdog: false,
})

let dataType = diam.DataType()

export default function () {
Expand Down

0 comments on commit e1b1082

Please sign in to comment.