Skip to content

Commit

Permalink
qrouting: Code refactoring
Browse files Browse the repository at this point in the history
    * name the timer function
    * simplify code
    * normalize coding style
  • Loading branch information
liviuchircu authored and razvancrainea committed Feb 11, 2020
1 parent a073d5f commit d9942f5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 51 deletions.
44 changes: 19 additions & 25 deletions modules/qrouting/qr_stats.c
Expand Up @@ -214,31 +214,34 @@ void free_qr_cb(void *param)
/* TODO: thresholds must be freed separatley */

/* creates a rule n_dest destinations (by default marked as gws) */
void qr_create_rule(void *param) {
qr_rule_t *new = NULL;
void qr_create_rule(void *param)
{
qr_rule_t *new;
int r_id;
struct dr_cb_params *cbp = (struct dr_cb_params *)param;
struct dr_reg_init_rule_params *init_rule_params =
struct dr_reg_init_rule_params *irp =
(struct dr_reg_init_rule_params *)*cbp->param;

r_id = init_rule_params->r_id;
r_id = irp->r_id;

if((new = (qr_rule_t*)shm_malloc(sizeof(qr_rule_t))) == NULL) {
LM_ERR("no more shm memory\n");
if (!(new = shm_malloc(sizeof *new))) {
LM_ERR("oom\n");
return;
}
memset(new, 0, sizeof(qr_rule_t));
memset(new, 0, sizeof *new);

/* prepare an array for adding gateways */
if((new->dest = (qr_dst_t*)shm_malloc(init_rule_params->n_dst*
sizeof(qr_dst_t))) == NULL) {
LM_ERR("no more shm memory\n");
if (!(new->dest = shm_malloc(irp->n_dst * sizeof *new->dest))) {
LM_ERR("oom\n");
shm_free(new);
return;
}
new->n = init_rule_params->n_dst; /* save the number of destinations for

new->n = irp->n_dst; /* save the number of destinations for
this rule, as rcvd from dr*/
new->r_id = r_id;
init_rule_params->rule = new; /* send the rule to the dr */
LM_DBG("Rule %d created\n", r_id);
irp->rule = new; /* send the rule to the dr */
LM_DBG("rule %d created\n", r_id);
}

/* marks index_grp destination from the rule as group and creates the gw array */
Expand Down Expand Up @@ -394,19 +397,10 @@ void qr_link_rule_list(void *param)
struct dr_cb_params *cbp = (struct dr_cb_params *)param;
struct dr_link_rule_list_params * rule_lists =
(struct dr_link_rule_list_params *)*cbp->param;
qr_rule_t **first_list = (qr_rule_t**)rule_lists->first_list;
qr_rule_t *second_list = (qr_rule_t*)rule_lists->second_list;
qr_rule_t *rule_it;
qr_rule_t **first_list = (qr_rule_t**)rule_lists->first_list,
*second_list = (qr_rule_t*)rule_lists->second_list;

if(*first_list == NULL) {
*first_list = second_list;
} else {
for(rule_it = *first_list; rule_it->next != NULL;
rule_it = rule_it->next) { /* go to the last rule from the first
list */
}
rule_it->next = second_list; /* link it to the second list */
}
add_last(second_list, *first_list);
}

void qr_search_profile(void *param)
Expand Down
43 changes: 17 additions & 26 deletions modules/qrouting/qrouting.c
Expand Up @@ -65,19 +65,20 @@ static int qr_init_globals(void);
static int qr_check_db(void);
static int qr_init_dr_cb(void);

static void timer_func(void);
static timer_function qr_rotate_samples;


static cmd_export_t cmds[] = {
{0,0,{{0,0,0}},0}
};

static param_export_t params[] = {
{"history", INT_PARAM, &history},
{"sampling_interval", INT_PARAM, &sampling_interval},
{"db_url", STR_PARAM, &db_url.s},
{0, 0, 0}
};


#define HLP1 "Params: [partition_name [, rule_id [, dst_id]]]; List QR statistics"
static mi_export_t mi_cmds[] = {
{ "qr_status", HLP1, 0, 0, {
Expand All @@ -90,7 +91,6 @@ static mi_export_t mi_cmds[] = {
{EMPTY_MI_EXPORT}
};


struct module_exports exports = {
"qrouting",
MOD_TYPE_DEFAULT,/* class of this module */
Expand All @@ -114,6 +114,7 @@ struct module_exports exports = {
0 /* reload confirm function */
};


static int qr_init(void)
{
LM_INFO("qrouting module - initializing\n");
Expand All @@ -125,7 +126,7 @@ static int qr_init(void)
return -1;
}

register_timer(T_PROC_LABEL, (void*)timer_func, NULL,
register_timer(T_PROC_LABEL, qr_rotate_samples, NULL,
sampling_interval, TIMER_FLAG_SKIP_ON_DELAY);

if (qr_check_db() != 0) {
Expand All @@ -151,39 +152,37 @@ static int qr_init(void)
return 0;
}

static int qr_child_init(int rank) {
static int qr_child_init(int rank)
{
db_func_t qr_dbf;
db_con_t *qr_db_hdl = 0;

if(rank == PROC_TCP_MAIN)
if (rank == PROC_TCP_MAIN)
return 0;

/* re-connect to the db */
if(db_bind_mod(&db_url, &qr_dbf)) {
if (db_bind_mod(&db_url, &qr_dbf)) {
LM_CRIT("cannot bind to database module! "
"Did you forget to load a database module ? (%.*s)\n",
db_url.len, db_url.s);
return -1;

}

if((qr_db_hdl = qr_dbf.init(&db_url)) == 0) {
if (!(qr_db_hdl = qr_dbf.init(&db_url)))
LM_ERR("failed to load db url %.*s\n", db_url.len, db_url.s);

}

/* do not change the rank of the process loading
* the db, because it must match the rank of the
* corespoding drouting process to ensure the qr db
* is loaded before the dr db */
if(rank == 1 && qr_load(&qr_dbf, qr_db_hdl) < 0) {
if (rank == 1 && qr_load(&qr_dbf, qr_db_hdl) < 0)
LM_ERR("failed to load data from db\n");
}

return 0;
}

static int qr_exit(void) {
static int qr_exit(void)
{
free_qr_list(*qr_main_list);

/* free the thresholds */
Expand All @@ -195,15 +194,13 @@ static int qr_exit(void) {
return 0;
}

static void timer_func(void) {
static void qr_rotate_samples(unsigned int ticks, void *param)
{
qr_rule_t *it;
int i, j;

if(*n_sampled < qr_n) {
if (*n_sampled < qr_n)
++(*n_sampled); /* the number of intervals sampled */
}



lock_start_read(*rw_lock_qr);
if(*qr_main_list != NULL) { /* if there is a list */
Expand Down Expand Up @@ -343,20 +340,14 @@ static int qr_check_db(void)
db_func_t qr_dbf;
db_con_t *qr_db_hdl;

if (db_url.s != NULL) {
db_url.len = strlen(db_url.s);
} else {
LM_ERR("db_url param not provided for qrouting module\n");
return -1;
}
init_db_url(db_url, 0);

/* test the db */
if (db_bind_mod(&db_url, &qr_dbf)) {
LM_CRIT("cannot bind to database module! "
"Did you forget to load a database module ? (%.*s)\n",
db_url.len, db_url.s);
return -1;

}

if (!(qr_db_hdl = qr_dbf.init(&db_url))) {
Expand Down

0 comments on commit d9942f5

Please sign in to comment.