void rpc_mtree_reload(rpc_t* rpc, void* c) { str tname = {0, 0}; m_tree_t *pt = NULL; int treloaded = 0; if(db_table.len>0) { /* re-loading all information from database */ if(mt_load_db_trees()!=0) { LM_ERR("cannot re-load mtrees from database\n"); goto error; } } else { if(!mt_defined_trees()) { LM_ERR("empty mtree list\n"); goto error; } /* read tree name */ if (rpc->scan(c, "S", &tname) != 1) { tname.s = 0; tname.len = 0; } else { if(*tname.s=='.') { tname.s = 0; tname.len = 0; } } pt = mt_get_first_tree(); while(pt!=NULL) { if(tname.s==NULL || (tname.s!=NULL && pt->tname.len>=tname.len && strncmp(pt->tname.s, tname.s, tname.len)==0)) { /* re-loading table from database */ if(mt_load_db(pt)!=0) { LM_ERR("cannot re-load mtree from database\n"); goto error; } treloaded = 1; } pt = pt->next; } if(treloaded == 0) { rpc->fault(c, 500, "No Mtree Name Matching"); } } return; error: rpc->fault(c, 500, "Mtree Reload Failed"); }
/** * "mt_reload" syntax : * \n */ static struct mi_root* mt_mi_reload(struct mi_root *cmd_tree, void *param) { str tname = {0, 0}; m_tree_t *pt; struct mi_node* node = NULL; if(db_table.len>0) { /* re-loading all information from database */ if(mt_load_db_trees()!=0) { LM_ERR("cannot re-load info from database\n"); goto error; } } else { if(!mt_defined_trees()) { LM_ERR("empty tree list\n"); return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN); } /* read tree name */ node = cmd_tree->node.kids; if(node != NULL) { tname = node->value; if(tname.s == NULL || tname.len== 0) return init_mi_tree( 404, "domain not found", 16); if(*tname.s=='.') { tname.s = 0; tname.len = 0; } } pt = mt_get_first_tree(); while(pt!=NULL) { if(tname.s==NULL || (tname.s!=NULL && pt->tname.len>=tname.len && strncmp(pt->tname.s, tname.s, tname.len)==0)) { /* re-loading table from database */ if(mt_load_db(pt)!=0) { LM_ERR("cannot re-load info from database\n"); goto error; } } pt = pt->next; } } return init_mi_tree( 200, MI_OK_S, MI_OK_LEN); error: return init_mi_tree( 500, "Failed to reload",16); }
/** * init module function */ static int mod_init(void) { m_tree_t *pt = NULL; if(mtree_init_rpc()!=0) { LM_ERR("failed to register RPC commands\n"); return -1; } if(pv_parse_spec(&value_param, &pv_value)<00 || !(pv_is_w(&pv_value))) { LM_ERR("cannot parse value pv or is read only\n"); return -1; } if (pv_parse_spec(&values_param, &pv_values) <0 || pv_values.type != PVT_AVP) { LM_ERR("cannot parse values avp\n"); return -1; } if(pv_parse_spec(&dstid_param, &pv_dstid)<0 || pv_dstid.type!=PVT_AVP) { LM_ERR("cannot parse dstid avp\n"); return -1; } if(pv_parse_spec(&weight_param, &pv_weight)<0 || pv_weight.type!=PVT_AVP) { LM_ERR("cannot parse dstid avp\n"); return -1; } if(pv_parse_spec(&count_param, &pv_count)<0 || !(pv_is_w(&pv_weight))) { LM_ERR("cannot parse count pv or is read-only\n"); return -1; } if(mt_fetch_rows<=0) mt_fetch_rows = 1000; if(mt_char_list.len<=0) { LM_ERR("invalid prefix char list\n"); return -1; } LM_DBG("mt_char_list=%s \n", mt_char_list.s); mt_char_table_init(); /* binding to mysql module */ if(db_bind_mod(&db_url, &mt_dbf)) { LM_ERR("database module not found\n"); return -1; } if (!DB_CAPABILITY(mt_dbf, DB_CAP_ALL)) { LM_ERR("database module does not " "implement all functions needed by the module\n"); return -1; } /* open a connection with the database */ db_con = mt_dbf.init(&db_url); if(db_con==NULL) { LM_ERR("failed to connect to the database\n"); return -1; } LM_DBG("database connection opened successfully\n"); if ( (mt_lock=lock_alloc())==0) { LM_CRIT("failed to alloc lock\n"); goto error1; } if (lock_init(mt_lock)==0 ) { LM_CRIT("failed to init lock\n"); goto error1; } if(mt_defined_trees()) { LM_DBG("static trees defined\n"); pt = mt_get_first_tree(); while(pt!=NULL) { LM_DBG("loading from tree <%.*s>\n", pt->tname.len, pt->tname.s); /* loading all information from database */ if(mt_load_db(pt)!=0) { LM_ERR("cannot load info from database\n"); goto error1; } pt = pt->next; } } else { if(db_table.len<=0) { LM_ERR("no trees table defined\n"); goto error1; } if(mt_init_list_head()<0) { LM_ERR("unable to init trees list head\n"); goto error1; } /* loading all information from database */ if(mt_load_db_trees()!=0) { LM_ERR("cannot load trees from database\n"); goto error1; } } mt_dbf.close(db_con); db_con = 0; #if 0 mt_print_tree(mt_get_first_tree()); #endif /* success code */ return 0; error1: if (mt_lock) { lock_destroy( mt_lock ); lock_dealloc( mt_lock ); mt_lock = 0; } mt_destroy_trees(); if(db_con!=NULL) mt_dbf.close(db_con); db_con = 0; return -1; }