Skip to content

Commit

Permalink
feat: add get_mi_bool_like_param
Browse files Browse the repository at this point in the history
  • Loading branch information
wangdd committed May 12, 2023
1 parent 9afa2ee commit e871882
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 60 deletions.
25 changes: 25 additions & 0 deletions mi/item.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,31 @@ int get_mi_string_param(const mi_params_t *params, char *name,
return -1;
}


int get_mi_bool_like_param(const mi_params_t *params, char *name,
int default_value)
{
str tmp;

if (get_mi_string_param(params, name, &tmp.s, &tmp.len) < 0) {
return default_value;
}

if (tmp.len != 1) {
return default_value;
}

if (tmp.s[0] == '0' || tmp.s[0] == 'n' || tmp.s[0] == 'N') {
return 0;
}

if (tmp.s[0] == '1' || tmp.s[0] == 'y' || tmp.s[0] == 'Y') {
return 1;
}

return default_value;
}

int try_get_mi_array_param(const mi_params_t *params, char *name,
mi_item_t **value, int *no_items)
{
Expand Down
3 changes: 3 additions & 0 deletions mi/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ void free_shm_mi_item(mi_item_t *response);
int get_mi_string_param(const mi_params_t *params, char *name,
char **value, int *value_len);

int get_mi_bool_like_param(const mi_params_t *params, char *name,
int default_value);

int get_mi_int_param(const mi_params_t *params, char *name, int *value);

int get_mi_array_param(const mi_params_t *params, char *name,
Expand Down
2 changes: 1 addition & 1 deletion modules/dispatcher/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ int ds_reload_db(ds_partition_t *partition, int initial, int is_inherit_state)
if (old_data) {
/* copy the state of the destinations from the old set
* (for the matching ids) */
if (is_inherit_state == INHERIT_STATE_YES) {
if (is_inherit_state) {
ds_inherit_state( old_data, new_data);
}

Expand Down
5 changes: 0 additions & 5 deletions modules/dispatcher/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@

#define MI_FULL_LISTING (1<<0)


#define INHERIT_STATE_YES 1
#define INHERIT_STATE_NO 0


extern int ds_persistent_state;

typedef struct _ds_dest
Expand Down
34 changes: 3 additions & 31 deletions modules/dispatcher/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ static int mod_init(void)
}

/* do the actual data load */
if (ds_reload_db(partition, 1, INHERIT_STATE_YES)!=0) {
if (ds_reload_db(partition, 1, 1)!=0) {
LM_ERR("failed to load data from DB\n");
return -1;
}
Expand Down Expand Up @@ -1373,21 +1373,7 @@ mi_response_t *ds_mi_reload(const mi_params_t *params,
struct mi_handler *async_hdl)
{
ds_partition_t *part_it;
str inherit_state;
int is_inherit_state = INHERIT_STATE_YES;

if (get_mi_string_param(params, "inherit_state", &inherit_state.s, &inherit_state.len) >= 0) {
LM_DBG("inherit_state is: %s \n", inherit_state.s);

if (inherit_state.s[0] == '0' || inherit_state.s[0] == 'n' || inherit_state.s[0] == 'N') {
is_inherit_state = INHERIT_STATE_NO;
}
else if (inherit_state.s[0] == '1' || inherit_state.s[0] == 'y' || inherit_state.s[0] == 'Y') {
is_inherit_state = INHERIT_STATE_YES;
} else {
LM_WARN("inherit_state values was not recognized, ignored \n");
}
}
int is_inherit_state = get_mi_bool_like_param(params, "inherit_state", 1);

LM_DBG("is_inherit_state is: %d \n", is_inherit_state);

Expand All @@ -1406,24 +1392,10 @@ mi_response_t *ds_mi_reload_1(const mi_params_t *params,
{
ds_partition_t *partition;
str partname;
str inherit_state;
int is_inherit_state = INHERIT_STATE_YES;
int is_inherit_state = get_mi_bool_like_param(params, "inherit_state", 1);

if (get_mi_string_param(params, "partition", &partname.s, &partname.len) < 0)
return init_mi_param_error();

if (get_mi_string_param(params, "inherit_state", &inherit_state.s, &inherit_state.len) >= 0) {
LM_DBG("inherit_state is: %s \n", inherit_state.s);

if (inherit_state.s[0] == '0' || inherit_state.s[0] == 'n' || inherit_state.s[0] == 'N') {
is_inherit_state = INHERIT_STATE_NO;
}
else if (inherit_state.s[0] == '1' || inherit_state.s[0] == 'y' || inherit_state.s[0] == 'Y') {
is_inherit_state = INHERIT_STATE_YES;
} else {
LM_WARN("inherit_state values was not recognized, ignored \n");
}
}

LM_DBG("is_inherit_state is: %d \n", is_inherit_state);

Expand Down
26 changes: 3 additions & 23 deletions modules/drouting/drouting.c
Original file line number Diff line number Diff line change
Expand Up @@ -2329,32 +2329,12 @@ static mi_response_t *mi_dr_get_partition(const mi_params_t *params,
return NULL;
}

static inline int get_inherit_state (const mi_params_t *params) {
str inherit_state;
int is_inherit_state = 1; // default is inhert old state

if (get_mi_string_param(params, "inherit_state", &inherit_state.s, &inherit_state.len) >= 0) {
LM_DBG("inherit_state is: %s \n", inherit_state.s);

if (inherit_state.s[0] == '0' || inherit_state.s[0] == 'n' || inherit_state.s[0] == 'N') {
is_inherit_state = 0;
}
else if (inherit_state.s[0] == '1' || inherit_state.s[0] == 'y' || inherit_state.s[0] == 'Y') {
is_inherit_state = 1;
} else {
LM_WARN("inherit_state values was not recognized, ignored, use default 1\n");
}
}

return is_inherit_state;
}

mi_response_t *dr_reload_cmd(const mi_params_t *params,
struct mi_handler *async_hdl)
{
LM_INFO("dr_reload MI command received!\n");
int is_inherit_state = get_mi_bool_like_param(params, "inherit_state", 1);

int is_inherit_state = get_inherit_state(params);
LM_INFO("dr_reload MI command received!\n");

if (dr_reload_data(0, is_inherit_state) != 0) {
LM_CRIT("failed to load routing data\n");
Expand All @@ -2372,9 +2352,9 @@ mi_response_t *dr_reload_cmd_1(const mi_params_t *params,
{
struct head_db *part;
mi_response_t *resp;
int is_inherit_state = get_mi_bool_like_param(params, "inherit_state", 1);

LM_INFO("dr_reload MI command received!\n");
int is_inherit_state = get_inherit_state(params);

resp = mi_dr_get_partition(params, &part);
if (resp)
Expand Down

0 comments on commit e871882

Please sign in to comment.