static int frd_load_data(dr_head_p drp, free_list_t **fl) { static const size_t col_count = 16; db_res_t *res = NULL; unsigned int no_rows = 0, row_count, i; db_row_t *rows; db_val_t *values; db_key_t query_cols[] = { &rid_col, &pid_col, &prefix_col, &start_h_col, &end_h_col, &days_col, &cpm_thresh_warn_col, &cpm_thresh_crit_col, &calldur_thresh_warn_col, &calldur_thresh_crit_col, &totalc_thresh_warn_col, &totalc_thresh_crit_col, &concalls_thresh_warn_col, &concalls_thresh_crit_col, &seqcalls_thresh_warn_col, &seqcalls_thresh_crit_col }; if (db_handle == NULL) { LM_ERR("Invalid db handler\n"); return -1; } if (dbf.use_table(db_handle, &table_name) != 0) { LM_ERR("Cannot use table\n"); return -1; } if (DB_CAPABILITY(dbf, DB_CAP_FETCH)) { if (dbf.query(db_handle, 0, 0, 0, query_cols, 0, col_count, 0, 0) != 0) { LM_ERR("Error while querying db\n"); goto error; } /* estimate rows */ no_rows = estimate_available_rows(4 + 64 + 5 + 5 + 64 + 5 * 2 * 4, col_count); if (no_rows == 0) no_rows = 10; if (dbf.fetch_result(db_handle, &res, no_rows) != 0) { LM_ERR("Error while fetching rows\n"); goto error; } } else { /* No fetching capability */ if (dbf.query(db_handle, 0, 0, 0, query_cols, 0, col_count, 0, &res) != 0) { LM_ERR("Error while querying db\n"); goto error; } } /* Process the actual data */ unsigned int rid, pid, j; str prefix, start_time, end_time, days; free_list_t *fl_it = NULL; *fl = NULL; do { row_count = RES_ROW_N(res); rows = RES_ROWS(res); fl_it = pkg_malloc(sizeof(free_list_t)); if (fl_it == NULL) { LM_ERR ("no more pkg memory"); dbf.free_result(db_handle, res); return -1; } fl_it ->next = *fl; *fl = fl_it; fl_it->trec = shm_malloc(sizeof(tmrec_t) * row_count); if (fl_it->trec == NULL) goto no_more_shm; fl_it->thr = shm_malloc(sizeof(frd_thresholds_t) * row_count); if (fl_it->thr == NULL) goto no_more_shm; fl_it->n = row_count; for (i = 0; i < row_count; ++i) { values = ROW_VALUES(rows + i); fl_it->trec[i].byday = NULL; /* rule id */ if (VAL_NULL(values)) { LM_ERR("rule id cannot be NULL - skipping rule\n"); continue; } rid = VAL_INT(values); /* profile id */ if (VAL_NULL(values + 1)) { LM_ERR("profile id cannot be NULL - skipping rule\n"); continue; } pid = VAL_INT(values + 1); get_str_from_dbval(prefix_col.s, values + 2, 1, 1, prefix, null_val); get_str_from_dbval(start_h_col.s, values + 3, 1, 1, start_time, null_val); get_str_from_dbval(end_h_col.s, values + 4, 1, 1, end_time, null_val); get_str_from_dbval(days_col.s, values + 5, 1, 1, days, null_val); if (create_time_rec(&start_time, &end_time, &days, fl_it->trec + i) != 0) goto null_val; /* Now load the thresholds */ for (j = 0; j < 2 * 5; ++j) { if (VAL_NULL(values + 6 + j)) goto null_val; memcpy((char*)fl_it->thr + i * sizeof(frd_thresholds_t) + j * sizeof(unsigned int), &VAL_INT(values + 6 + j), sizeof(unsigned int)); } /* Rule OK, time to put it in DR */ if (drb.add_rule(drp, rid, &prefix, pid, 0, fl_it->trec + i, (void*)(&fl_it->thr[i])) != 0) { LM_ERR("Cannot add rule in dr <%u>. Skipping...\n", rid); } null_val: continue; } if (DB_CAPABILITY(dbf, DB_CAP_FETCH)) { /* any more rows to fetch ? */ if(dbf.fetch_result(db_handle, &res, no_rows)<0) { LM_ERR("error while fetching rows\n"); goto error; } /* success in fetching more rows - continue the loop */ } else break; } while (RES_ROW_N(res) > 0); dbf.free_result(db_handle, res); return 0; no_more_shm: LM_ERR ("no more shm memory\n"); dbf.free_result(db_handle, res); error: return -1; }
/*load groups of destinations from DB*/ int ds_load_db(void) { int i, id, nr_rows, setn, cnt; int flags; int weight; struct socket_info *sock; str uri; str attrs; str host; int port, proto; db_res_t * res; db_val_t * values; db_row_t * rows; db_key_t query_cols[6] = {&ds_set_id_col, &ds_dest_uri_col, &ds_dest_sock_col, &ds_dest_flags_col, &ds_dest_weight_col, &ds_dest_attrs_col}; if( (*crt_idx) != (*next_idx)) { LM_WARN("load command already generated, aborting reload...\n"); return 0; } if(ds_db_handle == NULL){ LM_ERR("invalid DB handler\n"); return -1; } if (ds_dbf.use_table(ds_db_handle, &ds_table_name) < 0) { LM_ERR("error in use_table\n"); return -1; } /*select the whole table and all the columns*/ if(ds_dbf.query(ds_db_handle,0,0,0,query_cols,0,6,0,&res) < 0) { LM_ERR("error while querying database\n"); return -1; } *next_idx = (*crt_idx + 1)%2; destroy_list(*next_idx); nr_rows = RES_ROW_N(res); rows = RES_ROWS(res); if(nr_rows == 0) { LM_WARN("no dispatching data in the db -- empty destination set\n"); goto load_done; } setn = 0; cnt = 0; for(i=0; i<nr_rows; i++) { values = ROW_VALUES(rows+i); /* id */ if (VAL_NULL(values)) { LM_ERR("ds ID column cannot be NULL -> skipping\n"); continue; } id = VAL_INT(values); /* uri */ get_str_from_dbval( "URI", values+1, 1/*not_null*/, 1/*not_empty*/, uri, err2); /* sock */ get_str_from_dbval( "SOCKET", values+2, 0/*not_null*/, 0/*not_empty*/, attrs, err2); if ( attrs.len ) { if (parse_phostport( attrs.s, attrs.len, &host.s, &host.len, &port, &proto)!=0){ LM_ERR("socket description <%.*s> is not valid -> ignoring\n", attrs.len,attrs.s); sock = NULL; } else { sock = grep_sock_info( &host, port, proto); if (sock == NULL) { LM_ERR("socket <%.*s> is not local to opensips (we must " "listen on it) -> ignoring it\n", attrs.len, attrs.s); } } } else { sock = NULL; } /* flags */ if (VAL_NULL(values+3)) { flags = 0; } else { flags = VAL_INT(values+3); } /* weight */ if (VAL_NULL(values+4)) { weight = 1; } else { weight = VAL_INT(values+4); } /* attrs */ get_str_from_dbval( "ATTRIBUTES", values+5, 0/*not_null*/, 0/*not_empty*/, attrs, err2); if(add_dest2list(id, uri, sock, flags, weight, attrs, *next_idx, &setn) != 0) { LM_WARN("failed to add destination <%.*s> in group %d\n",uri.len,uri.s,id); continue; } else { cnt ++; } } if (cnt==0) { LM_WARN("No record loaded from db, running on empty set\n"); } else { if(reindex_dests(*next_idx, setn)!=0) { LM_ERR("error on reindex\n"); goto err2; } } load_done: /*update data*/ _ds_list_nr = setn; *crt_idx = *next_idx; ds_dbf.free_result(ds_db_handle, res); return 0; err2: destroy_list(*next_idx); ds_dbf.free_result(ds_db_handle, res); *next_idx = *crt_idx; return -1; }