void cg_list_clear(CgList *headList, CG_LIST_DESTRUCTORFUNC destructorFunc) { CgList *list; cg_log_debug_l5("Entering...\n"); if (NULL == headList) return; list = cg_list_next(headList); while(list != NULL) { cg_list_remove(list); //Theo Beisch: use destructorFunc or just free(listElement) if (destructorFunc != NULL){ destructorFunc(list); } else { free(list); } list = cg_list_next(headList); } /*** list header must be deleted by user ***/ /* free(headList); */ cg_log_debug_l5("Leaving...\n"); }
int cg_list_size(CgList *headList) { CgList *list; int listCnt; cg_log_debug_l5("Entering...\n"); if (NULL == headList) return 0; listCnt = 0; for (list = cg_list_next(headList); list != NULL; list = cg_list_next(list)) listCnt++; cg_log_debug_l5("Leaving...\n"); return listCnt; }
CgList *cg_list_get(CgList *headList, int index) { CgList *list; int n; cg_log_debug_l5("Entering...\n"); if (NULL == headList) return NULL; list = cg_list_next(headList); for (n=0; n<index; n++) { if (NULL == list) break; list = cg_list_next(list); } cg_log_debug_l5("Leaving...\n"); return list; }
BOOL cg_http_persistentconnection_put(char *host, int port, void *data) { CgHttpPersistentConnection *new_node = NULL, *node = NULL; cg_log_debug_l4("Entering...\n"); /* If we dont have cache, then just exit */ if (cache == NULL) { cg_log_debug("(put) No cache! Persistent connections not initialized?\n"); return FALSE; } /* Check if we already have this one cached */ for (node = (CgHttpPersistentConnection*)cg_list_gets((CgList*)cache); node != NULL; node = (CgHttpPersistentConnection*)cg_list_next((CgList*)node)) { if (cg_strcmp(cg_string_getvalue(node->host), host) == 0 && node->port == port) { /* If also data is the same, then update just timestamp */ if (node->cacheData == data) { node->timestamp = cg_getcurrentsystemtime(); return TRUE; } cg_log_debug_s("Found cached persistent connection for %s:%d\n", cg_string_getvalue(node->host), node->port); new_node = node; cg_list_remove((CgList*)new_node); break; } } /* We didn't find it */ if (new_node == NULL) { /* Check if we have already too many cached things */ if (cg_list_size((CgList*)cache) >= CG_HTTP_PERSISTENT_CACHE_SIZE) { /* Take last node (not refreshed for a long time) */ new_node = (CgHttpPersistentConnection *)cg_list_next((CgList *)cache); cg_list_remove((CgList*)new_node); cg_http_persistentconnection_delete(new_node); new_node = NULL; cg_log_debug_s("Max persistent HTTP connection cache reached.\n"); } if (new_node == NULL) { if (data == NULL) return TRUE; new_node = cg_http_persistentconnection_new(); if (new_node == NULL) return FALSE; cg_log_debug_s("Adding persistent HTTP Connection %s:%d to cache\n", host, port); cg_log_debug_s("Persistent connections: %d\n", cg_list_size((CgList*)cache)); } } if (data != NULL) { /* Set appropriate values for the node */ cg_string_setvalue(new_node->host, host); new_node->port = port; new_node->cacheData = data; new_node->timestamp = cg_getcurrentsystemtime(); cg_list_add((CgList*)cache, (CgList*)new_node); } else { /* remove and delete node */ cg_http_persistentconnection_delete(new_node); } return TRUE; cg_log_debug_l4("Leaving...\n"); }
void *cg_http_persistentconnection_get(char *host, int port) { CgHttpPersistentConnection *node; CgSysTime sys_time = cg_getcurrentsystemtime(); BOOL iterate; cg_log_debug_l4("Entering...\n"); /* If we dont have cache, then just exit */ if (cache == NULL) { cg_log_debug("(get) No cache! Persistent connections not initialized?\n"); return NULL; } /* Clear all expired nodes */ do { iterate = FALSE; for (node = (CgHttpPersistentConnection*)cg_list_gets((CgList*)cache); node != NULL; node = (CgHttpPersistentConnection*)cg_list_next((CgList*)node)) { if (sys_time > node->timestamp + CG_HTTP_PERSISTENT_TIMEOUT_PERIOD) { cg_log_debug_s("Timeout for persistent HTTP Connection to %s:%d " "(timestamp: %d)\n", cg_string_getvalue(node->host), node->port, node->timestamp); cg_list_remove((CgList*)node); cg_http_persistentconnection_delete(node); iterate = TRUE; break; } } } while (iterate); /* Get persistent node */ for (node = (CgHttpPersistentConnection*)cg_list_gets((CgList*)cache); node != NULL; node = (CgHttpPersistentConnection*)cg_list_next((CgList*)node)) { if (cg_strcmp(cg_string_getvalue(node->host), host) == 0 && node->port == port) { /* Node was required, remove and add again to refresh cache */ cg_list_remove((CgList*)node); cg_list_add((CgList*)cache, (CgList*)node); node->timestamp = cg_getcurrentsystemtime(); cg_log_debug_s("Persistent HTTP Connection cache HIT for %s:%d\n", host, port); return node->cacheData; } } cg_log_debug_s("Persistent HTTP Connection cache MISS for %s:%d\n", host, port); return NULL; cg_log_debug_l4("Leaving...\n"); }