本文整理匯總了Golang中github.com/fiorix/go-diameter/diam.NewRequest函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewRequest函數的具體用法?Golang NewRequest怎麽用?Golang NewRequest使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewRequest函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestHandleDWR_Fail
func TestHandleDWR_Fail(t *testing.T) {
sm := New(serverSettings)
srv := diamtest.NewServer(sm, dict.Default)
defer srv.Close()
mc := make(chan *diam.Message, 1)
mux := diam.NewServeMux()
mux.HandleFunc("CEA", func(c diam.Conn, m *diam.Message) {
mc <- m
})
mux.HandleFunc("DWA", func(c diam.Conn, m *diam.Message) {
mc <- m
})
cli, err := diam.Dial(srv.Addr, mux, dict.Default)
if err != nil {
t.Fatal(err)
}
defer cli.Close()
// Send CER first.
m := diam.NewRequest(diam.CapabilitiesExchange, 1001, dict.Default)
m.NewAVP(avp.OriginHost, avp.Mbit, 0, clientSettings.OriginHost)
m.NewAVP(avp.OriginRealm, avp.Mbit, 0, clientSettings.OriginRealm)
m.NewAVP(avp.HostIPAddress, avp.Mbit, 0, localhostAddress)
m.NewAVP(avp.VendorID, avp.Mbit, 0, clientSettings.VendorID)
m.NewAVP(avp.ProductName, 0, 0, clientSettings.ProductName)
m.NewAVP(avp.OriginStateID, avp.Mbit, 0, datatype.Unsigned32(1))
m.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(1001))
m.NewAVP(avp.FirmwareRevision, avp.Mbit, 0, clientSettings.FirmwareRevision)
_, err = m.WriteTo(cli)
if err != nil {
t.Fatal(err)
}
select {
case resp := <-mc:
if !testResultCode(resp, diam.Success) {
t.Fatalf("Unexpected result code for CEA.\n%s", resp)
}
case err := <-mux.ErrorReports():
t.Fatal(err)
case <-time.After(time.Second):
t.Fatal("No CEA received")
}
// Send broken DWR (missing Origin-Host, etc).
m = diam.NewRequest(diam.DeviceWatchdog, 0, dict.Default)
_, err = m.WriteTo(cli)
if err != nil {
t.Fatal(err)
}
select {
case err := <-sm.ErrorReports():
if err.Error != smparser.ErrMissingOriginHost {
t.Fatalf("Unexpected error. Want ErrMissingOriginHost, have %#v", err.Error)
}
case err := <-mux.ErrorReports():
t.Fatal(err)
case <-time.After(time.Second):
t.Fatal("No DWA received")
}
}
示例2: TestDmtAgentDryRun1
func TestDmtAgentDryRun1(t *testing.T) {
if !*testIntegration {
return
}
ccr := diam.NewRequest(diam.CreditControl, 4, nil)
ccr.NewAVP(avp.SessionID, avp.Mbit, 0, datatype.UTF8String("cgrates;1451911932;00082"))
ccr.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.DiameterIdentity("CGR-DA"))
ccr.NewAVP(avp.OriginRealm, avp.Mbit, 0, datatype.DiameterIdentity("cgrates.org"))
ccr.NewAVP(avp.AuthApplicationID, avp.Mbit, 0, datatype.Unsigned32(4))
ccr.NewAVP(avp.ServiceContextID, avp.Mbit, 0, datatype.UTF8String("pubsub1")) // Match specific DryRun profile
ccr.NewAVP(avp.CCRequestType, avp.Mbit, 0, datatype.Enumerated(4))
ccr.NewAVP(avp.CCRequestNumber, avp.Mbit, 0, datatype.Unsigned32(0))
ccr.NewAVP(avp.EventTimestamp, avp.Mbit, 0, datatype.Time(time.Date(2016, 1, 5, 11, 30, 10, 0, time.UTC)))
if _, err := ccr.NewAVP("Framed-IP-Address", avp.Mbit, 0, datatype.UTF8String("10.228.16.4")); err != nil {
t.Error(err)
}
if err := dmtClient.SendMessage(ccr); err != nil {
t.Error(err)
}
time.Sleep(time.Duration(100) * time.Millisecond)
msg := dmtClient.ReceivedMessage()
if msg == nil {
t.Fatal("No message returned")
}
if avps, err := msg.FindAVPsWithPath([]interface{}{"Result-Code"}, dict.UndefinedVendorID); err != nil {
t.Error(err)
} else if len(avps) == 0 {
t.Error("Result-Code")
} else if strResult := avpValAsString(avps[0]); strResult != "300" { // Result-Code set in the template
t.Errorf("Expecting 300, received: %s", strResult)
}
}
示例3: makeCER
func (cli *Client) makeCER(ip net.IP) *diam.Message {
m := diam.NewRequest(diam.CapabilitiesExchange, 0, cli.Dict)
m.NewAVP(avp.OriginHost, avp.Mbit, 0, cli.Handler.cfg.OriginHost)
m.NewAVP(avp.OriginRealm, avp.Mbit, 0, cli.Handler.cfg.OriginRealm)
m.NewAVP(avp.HostIPAddress, avp.Mbit, 0, datatype.Address(ip))
m.NewAVP(avp.VendorID, avp.Mbit, 0, cli.Handler.cfg.VendorID)
m.NewAVP(avp.ProductName, 0, 0, cli.Handler.cfg.ProductName)
stateid := datatype.Unsigned32(uint32(time.Now().Unix()))
m.NewAVP(avp.OriginStateID, avp.Mbit, 0, stateid)
if cli.SupportedVendorID != nil {
for _, a := range cli.SupportedVendorID {
m.AddAVP(a)
}
}
if cli.AuthApplicationID != nil {
for _, a := range cli.AuthApplicationID {
m.AddAVP(a)
}
}
m.NewAVP(avp.InbandSecurityID, avp.Mbit, 0, datatype.Unsigned32(0))
if cli.AcctApplicationID != nil {
for _, a := range cli.AcctApplicationID {
m.AddAVP(a)
}
}
if cli.VendorSpecificApplicationID != nil {
for _, a := range cli.VendorSpecificApplicationID {
m.AddAVP(a)
}
}
m.NewAVP(avp.FirmwareRevision, avp.Mbit, 0, cli.Handler.cfg.FirmwareRevision)
return m
}
示例4: TestMetaValueExponent
func TestMetaValueExponent(t *testing.T) {
m := diam.NewRequest(diam.CreditControl, 4, nil)
m.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String("simuhuawei;1449573472;00002"))
m.NewAVP(avp.RequestedServiceUnit, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.CCMoney, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.UnitValue, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.ValueDigits, avp.Mbit, 0, datatype.Integer64(10000)),
diam.NewAVP(avp.Exponent, avp.Mbit, 0, datatype.Integer32(-5)),
},
}),
diam.NewAVP(avp.CurrencyCode, avp.Mbit, 0, datatype.Unsigned32(33)),
},
}),
},
})
if val, err := metaValueExponent(m, utils.ParseRSRFieldsMustCompile("Requested-Service-Unit>CC-Money>Unit-Value>Value-Digits;^|;Requested-Service-Unit>CC-Money>Unit-Value>Exponent", utils.INFIELD_SEP), 10); err != nil {
t.Error(err)
} else if val != "0.1" {
t.Error("Received: ", val)
}
if _, err = metaValueExponent(m, utils.ParseRSRFieldsMustCompile("Requested-Service-Unit>CC-Money>Unit-Value>Value-Digits;Requested-Service-Unit>CC-Money>Unit-Value>Exponent", utils.INFIELD_SEP), 10); err == nil {
t.Error("Should have received error") // Insufficient number arguments
}
}
示例5: sendACR
func sendACR(c diam.Conn, cfg *sm.Settings, n int) {
// Get this client's metadata from the connection object,
// which is set by the state machine after the handshake.
// It contains the peer's Origin-Host and Realm from the
// CER/CEA handshake. We use it to populate the AVPs below.
meta, ok := smpeer.FromContext(c.Context())
if !ok {
log.Fatal("Client connection does not contain metadata")
}
var err error
var m *diam.Message
for i := 0; i < n; i++ {
m = diam.NewRequest(diam.Accounting, 0, c.Dictionary())
m.NewAVP(avp.SessionID, avp.Mbit, 0,
datatype.UTF8String(strconv.Itoa(i)))
m.NewAVP(avp.OriginHost, avp.Mbit, 0, cfg.OriginHost)
m.NewAVP(avp.OriginRealm, avp.Mbit, 0, cfg.OriginRealm)
m.NewAVP(avp.DestinationRealm, avp.Mbit, 0, meta.OriginRealm)
m.NewAVP(avp.AccountingRecordType, avp.Mbit, 0, eventRecord)
m.NewAVP(avp.AccountingRecordNumber, avp.Mbit, 0,
datatype.Unsigned32(i))
m.NewAVP(avp.DestinationHost, avp.Mbit, 0, meta.OriginHost)
if _, err = m.WriteTo(c); err != nil {
log.Fatal(err)
}
}
}
示例6: makeDWR
func (cli *Client) makeDWR(osid uint32) *diam.Message {
m := diam.NewRequest(diam.DeviceWatchdog, 0, cli.Dict)
m.NewAVP(avp.OriginHost, avp.Mbit, 0, cli.Handler.cfg.OriginHost)
m.NewAVP(avp.OriginRealm, avp.Mbit, 0, cli.Handler.cfg.OriginRealm)
m.NewAVP(avp.OriginStateID, avp.Mbit, 0, datatype.Unsigned32(osid))
return m
}
示例7: TestDWR_MissingOriginHost
func TestDWR_MissingOriginHost(t *testing.T) {
m := diam.NewRequest(diam.DeviceWatchdog, 0, dict.Default)
dwr := new(DWR)
err := dwr.Parse(m)
if err != nil && err != ErrMissingOriginHost {
t.Fatal("Unexpected error:", err)
}
}
示例8: TestDWR_MissingOriginRealm
func TestDWR_MissingOriginRealm(t *testing.T) {
m := diam.NewRequest(diam.CapabilitiesExchange, 0, dict.Default)
m.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.DiameterIdentity("foobar"))
dwr := new(DWR)
err := dwr.Parse(m)
if err != nil && err != ErrMissingOriginRealm {
t.Fatal("Unexpected error:", err)
}
}
示例9: AsBareDiameterMessage
// AsBareDiameterMessage converts CCR into a bare DiameterMessage
// Compatible with the required fields of CCA
func (self *CCR) AsBareDiameterMessage() *diam.Message {
m := diam.NewRequest(diam.CreditControl, 4, nil)
m.NewAVP(avp.SessionID, avp.Mbit, 0, datatype.UTF8String(self.SessionId))
m.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.DiameterIdentity(self.OriginHost))
m.NewAVP(avp.OriginRealm, avp.Mbit, 0, datatype.DiameterIdentity(self.OriginRealm))
m.NewAVP(avp.AuthApplicationID, avp.Mbit, 0, datatype.Unsigned32(self.AuthApplicationId))
m.NewAVP(avp.CCRequestType, avp.Mbit, 0, datatype.Enumerated(self.CCRequestType))
m.NewAVP(avp.CCRequestNumber, avp.Mbit, 0, datatype.Unsigned32(self.CCRequestNumber))
return m
}
示例10: TestDWR_OK
func TestDWR_OK(t *testing.T) {
m := diam.NewRequest(diam.CapabilitiesExchange, 0, dict.Default)
m.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.DiameterIdentity("foobar"))
m.NewAVP(avp.OriginRealm, avp.Mbit, 0, datatype.DiameterIdentity("test"))
m.NewAVP(avp.OriginStateID, avp.Mbit, 0, datatype.Unsigned32(1))
dwr := new(DWR)
err := dwr.Parse(m)
if err != nil {
t.Fatal(err)
}
}
示例11: sendCER
func sendCER(w io.Writer) (n int64, err error) {
m := diam.NewRequest(diam.CapabilitiesExchange, 0, nil)
m.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.OctetString("cli"))
m.NewAVP(avp.OriginRealm, avp.Mbit, 0, datatype.OctetString("localhost"))
m.NewAVP(avp.HostIPAddress, avp.Mbit, 0, datatype.Address(net.ParseIP("127.0.0.1")))
m.NewAVP(avp.VendorID, avp.Mbit, 0, datatype.Unsigned32(99))
m.NewAVP(avp.ProductName, avp.Mbit, 0, datatype.UTF8String("go-diameter"))
m.NewAVP(avp.OriginStateID, avp.Mbit, 0, datatype.Unsigned32(1234))
m.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(1))
return m.WriteTo(w)
}
示例12: TestCER_MissingOriginHost
func TestCER_MissingOriginHost(t *testing.T) {
m := diam.NewRequest(diam.CapabilitiesExchange, 0, dict.Default)
cer := new(CER)
_, err := cer.Parse(m)
if err == nil {
t.Fatal("Broken CER was parsed with no errors")
}
if err != ErrMissingOriginHost {
t.Fatal("Unexpected error:", err)
}
}
示例13: TestCER_MissingOriginRealm
func TestCER_MissingOriginRealm(t *testing.T) {
m := diam.NewRequest(diam.CapabilitiesExchange, 0, dict.Default)
m.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.DiameterIdentity("foobar"))
cer := new(CER)
_, err := cer.Parse(m)
if err == nil {
t.Fatal("Broken CER was parsed with no errors")
}
if err != ErrMissingOriginRealm {
t.Fatal("Unexpected error:", err)
}
}
示例14: TestFieldOutVal
func TestFieldOutVal(t *testing.T) {
m := diam.NewRequest(diam.CreditControl, 4, nil)
m.NewAVP("Session-Id", avp.Mbit, 0, datatype.UTF8String("simuhuawei;1449573472;00002"))
m.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(450, avp.Mbit, 0, datatype.Enumerated(0)), // Subscription-Id-Type
diam.NewAVP(444, avp.Mbit, 0, datatype.UTF8String("33708000003")), // Subscription-Id-Data
}})
m.NewAVP("Subscription-Id", avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(450, avp.Mbit, 0, datatype.Enumerated(1)), // Subscription-Id-Type
diam.NewAVP(444, avp.Mbit, 0, datatype.UTF8String("208708000003")), // Subscription-Id-Data
}})
m.NewAVP("Service-Identifier", avp.Mbit, 0, datatype.Unsigned32(0))
m.NewAVP("Requested-Service-Unit", avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(420, avp.Mbit, 0, datatype.Unsigned32(360))}}) // CC-Time
cfgFld := &config.CfgCdrField{Tag: "StaticTest", Type: utils.META_COMPOSED, FieldId: utils.TOR,
Value: utils.ParseRSRFieldsMustCompile("^*voice", utils.INFIELD_SEP), Mandatory: true}
eOut := "*voice"
if fldOut, err := fieldOutVal(m, cfgFld, time.Duration(0)); err != nil {
t.Error(err)
} else if fldOut != eOut {
t.Errorf("Expecting: %s, received: %s", eOut, fldOut)
}
cfgFld = &config.CfgCdrField{Tag: "ComposedTest", Type: utils.META_COMPOSED, FieldId: utils.DESTINATION,
Value: utils.ParseRSRFieldsMustCompile("Requested-Service-Unit>CC-Time", utils.INFIELD_SEP), Mandatory: true}
eOut = "360"
if fldOut, err := fieldOutVal(m, cfgFld, time.Duration(0)); err != nil {
t.Error(err)
} else if fldOut != eOut {
t.Errorf("Expecting: %s, received: %s", eOut, fldOut)
}
// Without filter, we shoud get always the first subscriptionId
cfgFld = &config.CfgCdrField{Tag: "Grouped1", Type: utils.MetaGrouped, FieldId: "Account",
Value: utils.ParseRSRFieldsMustCompile("Subscription-Id>Subscription-Id-Data", utils.INFIELD_SEP), Mandatory: true}
eOut = "33708000003"
if fldOut, err := fieldOutVal(m, cfgFld, time.Duration(0)); err != nil {
t.Error(err)
} else if fldOut != eOut {
t.Errorf("Expecting: %s, received: %s", eOut, fldOut)
}
// Without groupedAVP, we shoud get the first subscriptionId
cfgFld = &config.CfgCdrField{Tag: "Grouped2", Type: utils.MetaGrouped, FieldId: "Account",
FieldFilter: utils.ParseRSRFieldsMustCompile("Subscription-Id>Subscription-Id-Type(1)", utils.INFIELD_SEP),
Value: utils.ParseRSRFieldsMustCompile("Subscription-Id>Subscription-Id-Data", utils.INFIELD_SEP), Mandatory: true}
eOut = "208708000003"
if fldOut, err := fieldOutVal(m, cfgFld, time.Duration(0)); err != nil {
t.Error(err)
} else if fldOut != eOut {
t.Errorf("Expecting: %s, received: %s", eOut, fldOut)
}
}
示例15: storedCdrToCCR
func storedCdrToCCR(cdr *engine.StoredCdr, originHost, originRealm string, vendorId int, productName string, firmwareRev int, debitInterval time.Duration, callEnded bool) *diam.Message {
sid := "session;" + strconv.Itoa(int(rand.Uint32()))
reqType, reqNr, ccTime := disectUsageForCCR(cdr.Usage, debitInterval, callEnded)
m := diam.NewRequest(272, 4, nil)
m.NewAVP(avp.SessionID, avp.Mbit, 0, datatype.UTF8String(sid))
m.NewAVP(avp.OriginHost, avp.Mbit, 0, datatype.DiameterIdentity(originHost))
m.NewAVP(avp.OriginRealm, avp.Mbit, 0, datatype.DiameterIdentity(originRealm))
m.NewAVP(avp.DestinationHost, avp.Mbit, 0, datatype.DiameterIdentity(originHost))
m.NewAVP(avp.DestinationRealm, avp.Mbit, 0, datatype.DiameterIdentity(originRealm))
m.NewAVP(avp.AuthApplicationID, avp.Mbit, 0, datatype.Unsigned32(4))
m.NewAVP(avp.ServiceContextID, avp.Mbit, 0, datatype.UTF8String("[email protected]"))
m.NewAVP(avp.CCRequestType, avp.Mbit, 0, datatype.Enumerated(reqType))
m.NewAVP(avp.CCRequestNumber, avp.Mbit, 0, datatype.Enumerated(reqNr))
m.NewAVP(avp.EventTimestamp, avp.Mbit, 0, datatype.Time(cdr.AnswerTime))
m.NewAVP(avp.SubscriptionID, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.SubscriptionIDType, avp.Mbit, 0, datatype.Enumerated(0)),
diam.NewAVP(avp.SubscriptionIDData, avp.Mbit, 0, datatype.UTF8String(cdr.Account)),
}})
m.NewAVP(avp.SubscriptionID, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.SubscriptionIDType, avp.Mbit, 0, datatype.Enumerated(1)),
diam.NewAVP(avp.SubscriptionIDData, avp.Mbit, 0, datatype.UTF8String("20921006232651")),
}})
m.NewAVP(avp.ServiceIdentifier, avp.Mbit, 0, datatype.Unsigned32(0))
m.NewAVP(avp.RequestedServiceUnit, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(avp.CCTime, avp.Mbit, 0, datatype.Unsigned32(ccTime))}})
/*
m.NewAVP(avp.ServiceInformation, avp.Mbit, 0, &diam.GroupedAVP{
AVP: []*diam.AVP{
diam.NewAVP(20300, avp.Mbit, 0, &diam.GroupedAVP{ // IN-Information
AVP: []*diam.AVP{
diam.NewAVP(avp.CallingPartyAddress, avp.Mbit, 0, datatype.UTF8String(cdr.Account)),
diam.NewAVP(avp.CalledPartyAddress, avp.Mbit, 0, datatype.UTF8String(cdr.Destination)),
diam.NewAVP(20327, avp.Mbit, 0, datatype.UTF8String(cdr.Destination)), // Real-Called-Number
diam.NewAVP(20339, avp.Mbit, 0, datatype.Unsigned32(0)), // Charge-Flow-Type
diam.NewAVP(20302, avp.Mbit, 0, datatype.UTF8String("33657954968")), // Calling-Vlr-Number
diam.NewAVP(20303, avp.Mbit, 0, datatype.UTF8String("31901485301525")), // Calling-CellID-Or-SAI
diam.NewAVP(avp.BearerCapability, avp.Mbit, 0, datatype.UTF8String("31901485301525")),
diam.NewAVP(20321, avp.Mbit, 0, datatype.UTF8String("31901485301525")), // Call-Reference-Number
diam.NewAVP(avp.MSCAddress, avp.Mbit, 0, datatype.UTF8String("")),
diam.NewAVP(20324, avp.Mbit, 0, datatype.UTF8String("0")), // Time-Zone
diam.NewAVP(20385, avp.Mbit, 0, datatype.UTF8String("")), // Called-Party-NP
diam.NewAVP(20386, avp.Mbit, 0, datatype.UTF8String("20091020120101")), // SSP-Time
},
}),
}})
*/
return m
}