コード例 #1
1
ファイル: sm_test.go プロジェクト: rqlwx/go-diameter
func testResultCode(m *diam.Message, want uint32) bool {
	rc, err := m.FindAVP("Result-Code")
	if err != nil {
		return false
	}
	if code, ok := rc.Data.(datatype.Unsigned32); ok {
		return uint32(code) == want
	}
	return false
}
コード例 #2
0
ファイル: libdmt.go プロジェクト: bhepp/cgrates
// Handler for meta functions
func metaHandler(m *diam.Message, tag, arg string, dur time.Duration) (string, error) {
	switch tag {
	case META_CCR_USAGE:
		var ok bool
		var reqType datatype.Enumerated
		var reqNr, reqUnit, usedUnit datatype.Unsigned32
		if ccReqTypeAvp, err := m.FindAVP("CC-Request-Type", 0); err != nil {
			return "", err
		} else if ccReqTypeAvp == nil {
			return "", errors.New("CC-Request-Type not found")
		} else if reqType, ok = ccReqTypeAvp.Data.(datatype.Enumerated); !ok {
			return "", fmt.Errorf("CC-Request-Type must be Enumerated and not %v", ccReqTypeAvp.Data.Type())
		}
		if ccReqNrAvp, err := m.FindAVP("CC-Request-Number", 0); err != nil {
			return "", err
		} else if ccReqNrAvp == nil {
			return "", errors.New("CC-Request-Number not found")
		} else if reqNr, ok = ccReqNrAvp.Data.(datatype.Unsigned32); !ok {
			return "", fmt.Errorf("CC-Request-Number must be Unsigned32 and not %v", ccReqNrAvp.Data.Type())
		}
		switch reqType {
		case datatype.Enumerated(1), datatype.Enumerated(2):
			if reqUnitAVPs, err := m.FindAVPsWithPath([]interface{}{"Requested-Service-Unit", "CC-Time"}, dict.UndefinedVendorID); err != nil {
				return "", err
			} else if len(reqUnitAVPs) == 0 {
				return "", errors.New("Requested-Service-Unit>CC-Time not found")
			} else if reqUnit, ok = reqUnitAVPs[0].Data.(datatype.Unsigned32); !ok {
				return "", fmt.Errorf("Requested-Service-Unit>CC-Time must be Unsigned32 and not %v", reqUnitAVPs[0].Data.Type())
			}
		case datatype.Enumerated(3), datatype.Enumerated(4):
			if usedUnitAVPs, err := m.FindAVPsWithPath([]interface{}{"Used-Service-Unit", "CC-Time"}, dict.UndefinedVendorID); err != nil {
				return "", err
			} else if len(usedUnitAVPs) != 0 {
				if usedUnit, ok = usedUnitAVPs[0].Data.(datatype.Unsigned32); !ok {
					return "", fmt.Errorf("Used-Service-Unit>CC-Time must be Unsigned32 and not %v", usedUnitAVPs[0].Data.Type())
				}
			}
		}
		usage := usageFromCCR(int(reqType), int(reqNr), int(reqUnit), int(usedUnit), dur)
		return strconv.FormatFloat(usage.Seconds(), 'f', -1, 64), nil
	}
	return "", nil
}