#define NETWORK_CONGESTION_MON "/proc/net/snmp"
#define NETWORK_CONG_MON_INTERVAL 10*1000
-typedef struct {
- gboolean is_initialized;
- GHashTable *monitoring_cbs;
-} congestion_mon_s;
-
typedef struct {
guint timer_source_id;
inm_congestion_mon_callback cb;
gpointer cb_user_data;
-} congestion_mon_data_s;
-
-congestion_mon_s cong_mon;
-static __thread congestion_mon_data_s *data = NULL;
-static __thread gboolean g_cong_mon_started;
-
-static void __destroy_cong_mon_data(gpointer user_data)
-{
- __INM_FUNC_ENTER__;
- congestion_mon_data_s *mon_data = (congestion_mon_data_s *)user_data;
+} congestion_mon_s;
- g_source_remove(mon_data->timer_source_id);
- g_free(mon_data);
- __INM_FUNC_EXIT__;
- return;
-}
+static congestion_mon_s *g_p_cong_mon = NULL;
static int __inm_check_congestion_status(int *status)
{
{
int status;
- if (!g_cong_mon_started) {
+ if (!g_p_cong_mon) {
INM_LOGW("congestion monitor is stopped");
return FALSE;
}
}
INM_LOGI("Network Congestion Status:[%d]", status);
- if (data->cb)
- data->cb(status, data->cb_user_data);
+ if (g_p_cong_mon->cb)
+ g_p_cong_mon->cb(status, g_p_cong_mon->cb_user_data);
return TRUE;
}
-static int __inm_attatch_cong_mon_file(congestion_mon_data_s *data)
+static int __inm_attatch_cong_mon_file()
{
__INM_FUNC_ENTER__;
- data->timer_source_id = g_timeout_add(NETWORK_CONG_MON_INTERVAL,
- __inm_cong_mon_status_callback, data);
+ if (!g_p_cong_mon) {
+ INM_LOGW("congestion monitor is not started");
+ return -1;
+ }
+
+ g_p_cong_mon->timer_source_id = g_timeout_add(NETWORK_CONG_MON_INTERVAL,
+ __inm_cong_mon_status_callback, NULL);
__INM_FUNC_EXIT__;
return 0;
{
__INM_FUNC_ENTER__;
- if (!cong_mon.is_initialized) {
- INM_LOGW("congestion monitor is not initialized");
+ if (g_p_cong_mon) {
+ INM_LOGW("congestion monitor is started");
__INM_FUNC_EXIT__;
- return INM_CONG_MON_NOT_INITIALIZED;
+ return INM_CONG_MON_ALREADY_STARTED;
}
- if (g_cong_mon_started) {
- INM_LOGW("congestion monitor is already started");
+ g_p_cong_mon = g_try_malloc0(sizeof(congestion_mon_s));
+ if (!g_p_cong_mon) {
__INM_FUNC_EXIT__;
- return INM_CONG_MON_ALREADY_STARTED;
+ return INM_CONG_MON_OPERATION_FAILED;
}
if (!cb) {
return INM_CONG_MON_INVALID_PARAMETER;
}
- data = (congestion_mon_data_s *)g_try_malloc0(sizeof(congestion_mon_data_s));
- if (!data) {
- INM_LOGE("Failed to allocate memory");
- __INM_FUNC_EXIT__;
- return INM_CONG_MON_OPERATION_FAILED;
- }
+ g_p_cong_mon->cb = cb;
+ g_p_cong_mon->cb_user_data = user_data;
- data->cb = cb;
- data->cb_user_data = user_data;
-
- if (__inm_attatch_cong_mon_file(data) < 0) {
+ if (__inm_attatch_cong_mon_file() < 0) {
INM_LOGE("Failed to attach congestion monitor file");
__INM_FUNC_EXIT__;
return INM_CONG_MON_OPERATION_FAILED;
}
- g_cong_mon_started = TRUE;
__INM_FUNC_EXIT__;
return INM_CONG_MON_ERROR_NONE;
int ret = INM_CONG_MON_ERROR_NONE;
__INM_FUNC_ENTER__;
- if (!cong_mon.is_initialized) {
+ if (!g_p_cong_mon) {
INM_LOGE("congestion monitor is not initialized");
__INM_FUNC_EXIT__;
return INM_CONG_MON_NOT_INITIALIZED;
}
- g_cong_mon_started = FALSE;
+
+ REMOVE_G_SOURCE(g_p_cong_mon->timer_source_id);
+ g_free(g_p_cong_mon);
+ g_p_cong_mon = NULL;
__INM_FUNC_EXIT__;
return ret;
int ret = INM_CONG_MON_ERROR_NONE;
__INM_FUNC_ENTER__;
- memset(&cong_mon, 0x00, sizeof(cong_mon));
- cong_mon.is_initialized = TRUE;
-
- cong_mon.monitoring_cbs = g_hash_table_new_full(g_str_hash,
- g_str_equal,
- g_free,
- __destroy_cong_mon_data
- );
-
- if (!cong_mon.monitoring_cbs) {
- cong_mon.is_initialized = FALSE;
+ if (g_p_cong_mon) {
__INM_FUNC_EXIT__;
- return INM_CONG_MON_OPERATION_FAILED;
+ return ret;
}
__INM_FUNC_EXIT__;
int ret = INM_CONG_MON_ERROR_NONE;
__INM_FUNC_ENTER__;
- if (!cong_mon.is_initialized) {
+ if (!g_p_cong_mon) {
__INM_FUNC_EXIT__;
- return INM_CONG_MON_NOT_INITIALIZED;
+ return ret;
}
- cong_mon.is_initialized = FALSE;
- g_hash_table_remove_all(cong_mon.monitoring_cbs);
- g_hash_table_unref(cong_mon.monitoring_cbs);
- cong_mon.monitoring_cbs = NULL;
+ REMOVE_G_SOURCE(g_p_cong_mon->timer_source_id);
+ g_free(g_p_cong_mon);
+ g_p_cong_mon = NULL;
__INM_FUNC_EXIT__;
return ret;