Skip to content

Commit

Permalink
wrap the global dictionary with RwLock
Browse files Browse the repository at this point in the history
  • Loading branch information
lwlee2608 committed Mar 7, 2024
1 parent c2fc19f commit cbea689
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
9 changes: 2 additions & 7 deletions src/avp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,8 @@ impl Avp {
let header_length = if header.flags.vendor { 12 } else { 8 };
let value_length = header.length - header_length;

let avp_type = dictionary::DEFAULT_DICT
.get_avp_type(header.code)
.unwrap_or(&AvpType::Unknown);

// if avp_type == &AvpType::Unknown {
// return Err(Error::UnknownAvpCode(header.code));
// }
let dict = dictionary::DEFAULT_DICT.read().unwrap();
let avp_type = dict.get_avp_type(header.code).unwrap_or(&AvpType::Unknown);

let value = match avp_type {
AvpType::Address => {
Expand Down
4 changes: 3 additions & 1 deletion src/diameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ impl fmt::Display for ApplicationId {

impl fmt::Display for Avp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let avp_name = dictionary::DEFAULT_DICT
let dict = dictionary::DEFAULT_DICT.read().unwrap();

let avp_name = dict
.get_avp_name(self.get_code() as u32)
.unwrap_or("Unknown");

Expand Down
22 changes: 12 additions & 10 deletions src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use serde::Deserialize;
use serde_xml_rs::from_str;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::RwLock;

use crate::avp::AvpType;

#[derive(Debug)]
pub struct Definition {
pub struct Dictionary {
avps: BTreeMap<u32, AvpDefinition>,
applications: HashMap<String, ApplicationId>,
commands: HashMap<String, CommandCode>,
Expand All @@ -23,9 +24,9 @@ pub struct AvpDefinition {
pub m_flag: bool,
}

impl Definition {
pub fn new() -> Definition {
Definition {
impl Dictionary {
pub fn new() -> Self {
Dictionary {
avps: BTreeMap::new(),
applications: HashMap::new(),
commands: HashMap::new(),
Expand Down Expand Up @@ -134,11 +135,11 @@ struct Item {
name: String,
}

pub fn parse(xml: &str) -> Definition {
pub fn parse(xml: &str) -> Dictionary {
let dict: Diameter = from_str(xml).unwrap();

// TODO Definition may need to include avp that matches its application and command code
let mut definition = Definition::new();
// TODO Dictionary may need to include avp that matches its application and command code
let mut definition = Dictionary::new();

dict.applications.iter().for_each(|app| {
let app_id = app.id.parse::<u32>().unwrap();
Expand Down Expand Up @@ -192,9 +193,9 @@ pub fn parse(xml: &str) -> Definition {
}

lazy_static! {
pub static ref DEFAULT_DICT: Definition = {
pub static ref DEFAULT_DICT: RwLock<Dictionary> = {
let xml = &DEFAULT_DICT_XML;
parse(xml)
RwLock::new(parse(xml))
};
pub static ref DEFAULT_DICT_XML: &'static str = {
let xml = r#"
Expand Down Expand Up @@ -1273,7 +1274,8 @@ mod tests {

#[test]
fn test_default_dict() {
let dict = &DEFAULT_DICT;
let dict = &DEFAULT_DICT.read().unwrap();

assert_eq!(dict.get_avp(416).unwrap().name, "CC-Request-Type");
assert_eq!(dict.get_avp(264).unwrap().name, "Origin-Host");
assert_eq!(dict.get_avp(263).unwrap().name, "Session-Id");
Expand Down

0 comments on commit cbea689

Please sign in to comment.