diff --git a/srcs/libs/hardware/hardware.c b/srcs/libs/hardware/hardware.c index 6b941b9..3f12302 100644 --- a/srcs/libs/hardware/hardware.c +++ b/srcs/libs/hardware/hardware.c @@ -27,7 +27,7 @@ void hardwareRefreshCb(uv_timer_t *UNUSED(pArg)) { get_memory_info(&g_hardwareInfo.memInfo); } - printf("%s\n", get_hardware_json()); + //printf("%s\n", get_hardware_json()); } int init_hardware() { diff --git a/srcs/libs/init/init_runtime.c b/srcs/libs/init/init_runtime.c index a72dd56..a0f99f3 100644 --- a/srcs/libs/init/init_runtime.c +++ b/srcs/libs/init/init_runtime.c @@ -14,11 +14,13 @@ #include "inet_misc.h" #include "crypto.h" #include "hardware.h" -#include "msg_queue.h" #include "http_svr.h" #include "lib_config.h" #include "prj_config.h" #include "zlog_module.h" +#ifdef ZEROMQ_ON +#include "msg_queue.h" +#endif #define DEFAULT_CONFIG_FILE ("vcpe.cfg") #define DEFAULT_CONFIG_DIR ("config") diff --git a/srcs/opendhcp183/query.cpp b/srcs/opendhcp183/query.cpp index 4814432..fe5e883 100644 --- a/srcs/opendhcp183/query.cpp +++ b/srcs/opendhcp183/query.cpp @@ -25,6 +25,7 @@ using namespace std; #include "sds/sds.h" #include "inet_misc.h" #include "http_svr.h" +#include "task_manager.h" extern data2 cfig; extern bool kRunning; @@ -341,7 +342,7 @@ static int add_dhcpd_rangeset(const char **pRsp, const char *pRequest) { logDHCPMess(logBuff, 1); return ERR_PROTO_DECODE; } - + #ifdef JSON_SCHEMA_ON if (errCode == ERR_JSON_VALID_SCH) { *pRsp = pStrContent; @@ -488,7 +489,7 @@ static int delete_dhcpd_rangeset(const char **pRsp, const char *pRequest) { PHASH_MAP s; HASH_FIND_INT(delMap, &st_addr, s); if (s == nullptr) { - s = (PHASH_MAP)malloc(sizeof(HASH_MAP)); + s = (PHASH_MAP)malloc(sizeof(HASH_MAP)); s->key = st_addr; s->value = en_addr; HASH_ADD_INT(delMap, key, s); @@ -765,68 +766,63 @@ static void on_http_response_cb(void *pData, free(pUserData); } -void iptvCacheCb(void *UNUSED(pArg)) { - do { - bool isReport = false; - const char *pUrl = config_get_agent_iptv_report_url(); - PIPTV_DEV_SET report = nullptr; - PIPTV_DEV_SET pDev, pTmp = nullptr; - uv_rwlock_wrlock(&g_uvCacheLock); - HASH_ITER(hh, g_iptvNewDevs, pDev, pTmp) { - PIPTV_DEV_SET pTemp; - const char *pDevMac = strdup(pDev->iptvMAC); - HASH_FIND_STR(g_iptvCacheDevs, pDevMac, pTemp); - // 新发现设备没有被上报过 - if (!pTemp) { - auto pCacheDev = (PIPTV_DEV_SET)malloc(sizeof(IPTV_DEV_SET)); - auto pRepDev = (PIPTV_DEV_SET)malloc(sizeof(IPTV_DEV_SET)); - - memcpy(pCacheDev, pDev, sizeof(IPTV_DEV_SET)); - memcpy(pRepDev, pDev, sizeof(IPTV_DEV_SET)); - HASH_ADD_STR(report, iptvMAC, pRepDev); - - isReport = true; - // 添加到缓存列表供后续查询 - HASH_ADD_STR(g_iptvCacheDevs, iptvMAC, pCacheDev); - LOG_MOD(debug, - ZLOG_MOD_OPENDHCPD, - "Add IPTV device %s vni %d to cache\n", - pCacheDev->iptvMAC, - pCacheDev->vni); - } - - HASH_DEL(g_iptvNewDevs, pDev); - free(pDev); - free((void *)pDevMac); - } - uv_rwlock_wrunlock(&g_uvCacheLock); - - if (isReport && pUrl && strlen(pUrl) > 0) { - cJSON *pRspMsg = cJSON_CreateObject(); - cJSON *pMsgArray = cJSON_CreateArray(); - cJSON_AddItemToObject(pRspMsg, "iptvDevs", pMsgArray); - - HASH_ITER(hh, report, pDev, pTmp) { - cJSON *pRspItem = cJSON_CreateObject(); - cJSON_AddStringToObject(pRspItem, "mac", pDev->iptvMAC); - cJSON_AddNumberToObject(pRspItem, "vni", pDev->vni); - pDev->isReport = 1; - cJSON_AddItemToArray(pMsgArray, pRspItem); - } - - const char *pStrPro = proto_create_new(pRspMsg, 200); - - // Report new IPTV device MAC - inet_http_post_async(pUrl, pStrPro, on_http_response_cb, (void *)pStrPro); - } +void iptvCacheCb(uv_timer_t *UNUSED(pArg)) { + bool isReport = false; + const char *pUrl = config_get_agent_iptv_report_url(); + PIPTV_DEV_SET report = nullptr; + PIPTV_DEV_SET pDev, pTmp = nullptr; + uv_rwlock_wrlock(&g_uvCacheLock); + HASH_ITER(hh, g_iptvNewDevs, pDev, pTmp) uv_sleep(10); + HASH_ITER(hh, report, pDev, pTmp) { + HASH_DEL(report, pDev); + free(pDev); + } + HASH_ITER(hh, report, pDev, pTmp) if (isReport && pUrl && strlen(pUrl) > 0) { + cJSON *pRspMsg = cJSON_CreateObject(); + cJSON *pMsgArray = cJSON_CreateArray(); + cJSON_AddItemToObject(pRspMsg, "iptvDevs", pMsgArray); HASH_ITER(hh, report, pDev, pTmp) { - HASH_DEL(report, pDev); - free(pDev); + cJSON *pRspItem = cJSON_CreateObject(); + cJSON_AddStringToObject(pRspItem, "mac", pDev->iptvMAC); + cJSON_AddNumberToObject(pRspItem, "vni", pDev->vni); + pDev->isReport = 1; + cJSON_AddItemToArray(pMsgArray, pRspItem); } - uv_sleep(10); - } while (true); + const char *pStrPro = proto_create_new(pRspMsg, 200); + + // Report new IPTV device MAC + inet_http_post_async(pUrl, pStrPro, on_http_response_cb, (void *)pStrPro); + } + uv_rwlock_wrunlock(&g_uvCacheLock); + HASH_ITER(hh, g_iptvNewDevs, pDev, pTmp) { + PIPTV_DEV_SET pTemp; + const char *pDevMac = strdup(pDev->iptvMAC); + HASH_FIND_STR(g_iptvCacheDevs, pDevMac, pTemp); + // 新发现设备没有被上报过 + if (!pTemp) { + auto pCacheDev = (PIPTV_DEV_SET)malloc(sizeof(IPTV_DEV_SET)); + auto pRepDev = (PIPTV_DEV_SET)malloc(sizeof(IPTV_DEV_SET)); + + memcpy(pCacheDev, pDev, sizeof(IPTV_DEV_SET)); + memcpy(pRepDev, pDev, sizeof(IPTV_DEV_SET)); + HASH_ADD_STR(report, iptvMAC, pRepDev); + + isReport = true; + // 添加到缓存列表供后续查询 + HASH_ADD_STR(g_iptvCacheDevs, iptvMAC, pCacheDev); + LOG_MOD(debug, + ZLOG_MOD_OPENDHCPD, + "Add IPTV device %s vni %d to cache\n", + pCacheDev->iptvMAC, + pCacheDev->vni); + } + + HASH_DEL(g_iptvNewDevs, pDev); + free(pDev); + free((void *)pDevMac); + } } /** @@ -950,10 +946,13 @@ static HTTP_ROUTE_INFO g_routeTable[] = { void opendhcp_init_http_server() { static int added = FALSE; static uv_thread_t uvThread; - + static uv_timer_t uvTm; if (!added) { uv_rwlock_init(&g_uvCacheLock); - uv_thread_create(&uvThread, iptvCacheCb, nullptr); + uv_timer_init(get_task_manager(), &uvTm); + + uv_timer_start(&uvTm, iptvCacheCb, 1000, 1000); + //uv_thread_create(&uvThread, iptvCacheCb, nullptr); for (auto &i : g_routeTable) { PHTTP_ROUTE_INFO p = &i; http_add_route(p->routeName, p->method, p->priority, p->cb, p->pUserData);