From 503a366ef423c059ef29dba54892d16c454bab56 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 21 Nov 2023 11:37:27 +0800 Subject: [PATCH] replace DiameterMessage.AVP() with DiameterMessage.add() --- README.md | 37 ++++++++++++++++++------------------- cmd/dict_generator/main.go | 1 - diameter.go | 5 +++++ example/example.js | 37 ++++++++++++++++++------------------- 4 files changed, 41 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 437a88f..c79ea0e 100644 --- a/README.md +++ b/README.md @@ -22,29 +22,28 @@ import { cmd, app, code, flag, vendor } from './diam/const.js' import { check } from 'k6' let client = diam.Client() -let dataType = diam.DataType() +let data = diam.DataType() export default function () { client.connect("localhost:3868") - let msg = diam.newMessage(cmd.CreditControl, app.ChargingControl); - - msg.AVP(code.OriginHost, 0, 0, dataType.DiameterIdentity("origin.host")) - msg.AVP(code.OriginRealm, 0, 0, dataType.DiameterIdentity("origin.realm")) - msg.AVP(code.DestinationHost, 0, 0, dataType.DiameterIdentity("dest.host")) - msg.AVP(code.DestinationRealm, 0, 0, dataType.DiameterIdentity("dest.realm")) - msg.AVP(code.SessionId, 0, flag.M, dataType.UTF8String("Session-8888")) - msg.AVP(code.CCRequestType, 0, flag.M, dataType.Enumerated(1)) - msg.AVP(code.CCRequestNumber, 0, flag.M, dataType.Unsigned32(1000)) - msg.AVP(code.SubscriptionId, 0, flag.M, dataType.Grouped([ - avp.New(code.SubscriptionIdData, 0, flag.M, dataType.UTF8String("subs-data")), - avp.New(code.SubscriptionIdType, 0, flag.M, dataType.Enumerated(1)) - ])) - - const response = client.send(msg) - console.log("Response: ", response.dump()) - - const resultCode = response.findAVP(code.ResultCode, 0) + let ccr = diam.newMessage(cmd.CreditControl, app.ChargingControl); + ccr.add(avp.New(code.OriginHost, 0, 0, data.DiameterIdentity("origin.host"))) + ccr.add(avp.New(code.OriginRealm, 0, 0, data.DiameterIdentity("origin.realm"))) + ccr.add(avp.New(code.DestinationHost, 0, 0, data.DiameterIdentity("dest.host"))) + ccr.add(avp.New(code.DestinationRealm, 0, 0, data.DiameterIdentity("dest.realm"))) + ccr.add(avp.New(code.SessionId, 0, flag.M, data.UTF8String("Session-8888"))) + ccr.add(avp.New(code.CCRequestType, 0, flag.M, data.Enumerated(1))) + ccr.add(avp.New(code.CCRequestNumber, 0, flag.M, data.Unsigned32(1000))) + ccr.add(avp.New(code.SubscriptionId, 0, flag.M, data.Grouped([ + avp.New(code.SubscriptionIdData, 0, flag.M, data.UTF8String("subs-data")), + avp.New(code.SubscriptionIdType, 0, flag.M, data.Enumerated(1)) + ]))) + + const cca = client.send(msg) + console.log("cca: ", cca.dump()) + + const resultCode = cca.findAVP(code.ResultCode, 0) check(resultCode, {'Result-Code == 2001': r => r == 2001,}) } ``` diff --git a/cmd/dict_generator/main.go b/cmd/dict_generator/main.go index ff986cd..1df4ed4 100644 --- a/cmd/dict_generator/main.go +++ b/cmd/dict_generator/main.go @@ -107,7 +107,6 @@ func PrintVendorId(w io.Writer, parser *dict.Parser) { vendorIds[vendor.ID] = exists fmt.Fprintf(w, " %-20s %d,\n", vendor.Name+":", vendor.ID) - // fmt.Fprintf(w, " %s: %d,\n", vendor.Name, vendor.ID) } } fmt.Fprintf(w, "}\n") diff --git a/diameter.go b/diameter.go index 22ae8d8..f2d14e6 100644 --- a/diameter.go +++ b/diameter.go @@ -147,10 +147,15 @@ func (*Diameter) NewMessage(cmd uint32, appid uint32) *DiameterMessage { } } +// deprecated func (m *DiameterMessage) XAVP(code uint32, vendor uint32, flags uint8, data datatype.Type) { m.diamMsg.NewAVP(code, flags, vendor, data) } +func (m *DiameterMessage) Add(a *diam.AVP) { + m.diamMsg.AddAVP(a) +} + func (m *DiameterMessage) Dump() string { return m.diamMsg.PrettyDump() } diff --git a/example/example.js b/example/example.js index 0b876d3..ae8481b 100644 --- a/example/example.js +++ b/example/example.js @@ -17,28 +17,27 @@ dict.load("dict/extra.xml") let client = diam.Client({ requestTimeout: "50ms", }) -let dataType = diam.DataType() +let data = diam.DataType() export default function () { client.connect("localhost:3868") - let msg = diam.newMessage(cmd.CreditControl, app.ChargingControl); - - msg.AVP(code.OriginHost, 0, 0, dataType.DiameterIdentity("origin.host")) - msg.AVP(code.OriginRealm, 0, 0, dataType.DiameterIdentity("origin.realm")) - msg.AVP(code.DestinationHost, 0, 0, dataType.DiameterIdentity("dest.host")) - msg.AVP(code.DestinationRealm, 0, 0, dataType.DiameterIdentity("dest.realm")) - msg.AVP(code.SessionId, 0, flag.M, dataType.UTF8String("Session-8888")) - msg.AVP(code.CCRequestType, 0, flag.M, dataType.Enumerated(1)) - msg.AVP(code.CCRequestNumber, 0, flag.M, dataType.Unsigned32(1000)) - msg.AVP(code.SubscriptionId, 0, flag.M, dataType.Grouped([ - avp.New(code.SubscriptionIdData, 0, flag.M, dataType.UTF8String("subs-data")), - avp.New(code.SubscriptionIdType, 0, flag.M, dataType.Enumerated(1)) - ])) - - const response = client.send(msg) - console.log("Response: ", response.dump()) - - const resultCode = response.findAVP(code.ResultCode, 0) + let ccr = diam.newMessage(cmd.CreditControl, app.ChargingControl); + ccr.add(avp.New(code.OriginHost, 0, 0, data.DiameterIdentity("origin.host"))) + ccr.add(avp.New(code.OriginRealm, 0, 0, data.DiameterIdentity("origin.realm"))) + ccr.add(avp.New(code.DestinationHost, 0, 0, data.DiameterIdentity("dest.host"))) + ccr.add(avp.New(code.DestinationRealm, 0, 0, data.DiameterIdentity("dest.realm"))) + ccr.add(avp.New(code.SessionId, 0, flag.M, data.UTF8String("Session-8888"))) + ccr.add(avp.New(code.CCRequestType, 0, flag.M, data.Enumerated(1))) + ccr.add(avp.New(code.CCRequestNumber, 0, flag.M, data.Unsigned32(1000))) + ccr.add(avp.New(code.SubscriptionId, 0, flag.M, data.Grouped([ + avp.New(code.SubscriptionIdData, 0, flag.M, data.UTF8String("subs-data")), + avp.New(code.SubscriptionIdType, 0, flag.M, data.Enumerated(1)) + ]))) + + const cca = client.send(ccr) + console.log("CCA: ", cca.dump()) + + const resultCode = cca.findAVP(code.ResultCode, 0) check(resultCode, {'Result-Code == 2001': r => r == 2001,}) }