From ca842dc02ae6419263fcbf83f5ef1b066c313c3f Mon Sep 17 00:00:00 2001 From: DanB Date: Sun, 15 Nov 2015 10:57:44 +0100 Subject: [PATCH] Adding DiameterClient, tests for 2 ways communication --- agents/dmtagent_it_test.go | 16 +++++++++ agents/dmtclient.go | 66 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 agents/dmtclient.go diff --git a/agents/dmtagent_it_test.go b/agents/dmtagent_it_test.go index 195651647..627098494 100644 --- a/agents/dmtagent_it_test.go +++ b/agents/dmtagent_it_test.go @@ -20,11 +20,14 @@ package agents import ( "flag" + "fmt" "path" "testing" + "time" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" ) var testIntegration = flag.Bool("integration", false, "Perform the tests in integration mode, not by default.") // This flag will be passed here via "go test -local" args @@ -79,6 +82,19 @@ func TestDmtAgentStartEngine(t *testing.T) { } } +func TestDmtAgentStartClient(t *testing.T) { + if !*testIntegration { + return + } + dmtClient, err := NewDiameterClient(daCfg.DiameterAgentCfg().Listen, "UNIT_TEST", + daCfg.DiameterAgentCfg().OriginRealm, daCfg.DiameterAgentCfg().VendorId, daCfg.DiameterAgentCfg().ProductName, utils.DIAMETER_FIRMWARE_REVISION) + if err != nil { + t.Fatal(err) + } + fmt.Printf("%+v", dmtClient) + time.Sleep(time.Duration(10) * time.Second) +} + func TestDmtAgentStopEngine(t *testing.T) { if !*testIntegration { return diff --git a/agents/dmtclient.go b/agents/dmtclient.go new file mode 100644 index 000000000..fd4dec121 --- /dev/null +++ b/agents/dmtclient.go @@ -0,0 +1,66 @@ +/* +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2015 ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package agents + +import ( + "math/rand" + "time" + + "github.com/fiorix/go-diameter/diam" + "github.com/fiorix/go-diameter/diam/avp" + "github.com/fiorix/go-diameter/diam/datatype" + "github.com/fiorix/go-diameter/diam/sm" +) + +func init() { + rand.Seed(time.Now().UnixNano()) +} + +func NewDiameterClient(addr, originHost, originRealm string, vendorId int, productName string, firmwareRev int) (*DiameterClient, error) { + cfg := &sm.Settings{ + OriginHost: datatype.DiameterIdentity(originHost), + OriginRealm: datatype.DiameterIdentity(originRealm), + VendorID: datatype.Unsigned32(vendorId), + ProductName: datatype.UTF8String(productName), + FirmwareRevision: datatype.Unsigned32(firmwareRev), + } + handlers := sm.New(cfg) + cli := &sm.Client{ + Handler: handlers, + MaxRetransmits: 3, + RetransmitInterval: time.Second, + EnableWatchdog: true, + WatchdogInterval: 5 * time.Second, + AcctApplicationID: []*diam.AVP{ + // Advertise that we want support for both + // Accounting applications 4 and 999. + diam.NewAVP(avp.AcctApplicationID, avp.Mbit, 0, datatype.Unsigned32(4)), // RFC 4006 + }, + } + conn, err := cli.Dial(addr) + if err != nil { + return nil, err + } + return &DiameterClient{conn: conn, handlers: handlers}, nil +} + +type DiameterClient struct { + conn diam.Conn + handlers diam.Handler +}