Merge remote-tracking branch 'origin/v1.2.0_dev' into v1.2.0_dev

This commit is contained in:
dongwenze 2023-03-15 18:13:37 +08:00
commit 087dd7d5de
18 changed files with 189 additions and 245 deletions

View File

@ -2,12 +2,19 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.10 FATAL_ERROR)
INCLUDE(CMakeDependentOption)
OPTION(VCPE_AGENT "Enable vCPE agent test application" OFF)
OPTION(USED_LWIP "PPPoE of LWIP support for vCPE" OFF)
OPTION(VCPE_PPPOE "Enable vCPE work befof PPPoE mode" OFF)
OPTION(USED_OPENDHCPD "DHCP server for vCPE" OFF)
OPTION(USED_OPENDHCPDDNS "DHCP And DNS server for vCPE" OFF)
OPTION(USED_USER_VNI "Support pass user vni id from console command line" OFF)
OPTION(USED_JSON_VALIDATE "Support json protocol field validate" OFF)
OPTION(BUILD_TESTING "Enable tests" OFF)
#
OPTION(USED_REDIS "Add redis database support for vCPE" OFF)
OPTION(USED_MYSQL "Add mysql database support for vCPE" OFF)
OPTION(USED_SQLITE "Add sqlite3 database support for vCPE" OFF)
CMAKE_DEPENDENT_OPTION(USED_HTTP_SVR "Build-in http(s) server support" ON "USED_OPENDHCPD OR USED_OPENDHCPDDNS" OFF)
CMAKE_DEPENDENT_OPTION(USED_LWIP "PPPoE of LWIP support for vCPE" ON "VCPE_PPPOE" OFF)
CMAKE_DEPENDENT_OPTION(USED_ZMQ "ZeroMQ support for vCPE" ON "VCPE_PPPOE" OFF)
LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/depend)
IF (BUILD_TESTING)
@ -15,14 +22,8 @@ IF (BUILD_TESTING)
ENDIF ()
INCLUDE(system_libs)
SET(VCPE_VERSION "1.0.1")
SET(VCPE_VERSION "1.2.0")
PROJECT(vCPE VERSION "${VCPE_VERSION}")
CMAKE_DEPENDENT_OPTION(USED_HTTP_SVR "Build-in http(s) server support" ON "USED_OPENDHCPD OR USED_OPENDHCPDDNS" OFF)
#
OPTION(USED_REDIS "Add redis database support for vCPE" OFF)
OPTION(USED_MYSQL "Add mysql database support for vCPE" OFF)
OPTION(USED_SQLITE "Add sqlite3 database support for vCPE" OFF)
SET(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:/usr/local/lib/pkgconfig")
SET(URI_HOME "${CMAKE_SOURCE_DIR}/libs")
@ -84,7 +85,7 @@ INCLUDE(./depend/third_libs.cmake)
IF (USED_JSON_VALIDATE)
LIST(APPEND COMMON_DEFINE "-DJSON_SCHEMA_ON")
ENDIF()
ENDIF ()
IF (USED_USER_VNI)
LIST(APPEND COMMON_DEFINE "-DUSERVNI_ON")
@ -96,6 +97,16 @@ IF (USED_HTTP_SVR)
MESSAGE("Select Option USED_HTTP_SVR")
ENDIF ()
IF (USED_ZMQ)
LIST(APPEND COMMON_DEFINE "-DZEROMQ_ON")
MESSAGE("Select Option USED_ZMQ")
ENDIF ()
IF (VCPE_PPPOE)
LIST(APPEND COMMON_DEFINE "-DPPPOE_ON")
MESSAGE("Select Option VCPE_PPPOE")
ENDIF ()
IF (USED_LWIP)
LIST(APPEND COMMON_DEFINE "-DLWIP_ON")
MESSAGE("Select Option USED_LWIP")

View File

@ -14,7 +14,7 @@ application:
agent: {
iptv_report_url = "http://10.0.0.1:50012/vcpe/dhcp/info"; # IPTV 设备上报接口
moniter_rep_url = "http://10.0.0.1:50012/vcpe/dhcp/restart"; # vCPE设备上下线接口
moniter_rep_url = ""; # vCPE设备上下线接口
};
# 硬件监控相关配置内容
@ -24,11 +24,6 @@ application:
memory = true; # 是否开启内存监控
disk = true; # 是否开启磁盘空间占用监控
sensor = true; # 是否开启传感器状态监控
cpu_refresh = 1; # CPU 状态刷新频率(秒)
mem_refresh = 10; # 内存状态刷新频率(秒)
disk_refresh = 10; # 磁盘状态刷新频率(秒)
sensor_refresh = 10; # 传感器状态刷新频率(秒)
};
# 数据库相关配置

View File

@ -4,9 +4,13 @@ INCLUDE(FetchContent)
PKG_SEARCH_MODULE(LIBCURL QUIET libcurl)
PKG_SEARCH_MODULE(LIBSSL QUIET libssl)
PKG_SEARCH_MODULE(LIBCRYPTO QUIET libcrypto)
PKG_SEARCH_MODULE(LIBZMQ QUIET libzmq)
IF ((NOT LIBZMQ_FOUND) OR (NOT LIBCRYPTO_FOUND) OR (NOT LIBSSL_FOUND) OR (NOT LIBCURL_FOUND))
IF (USED_ZMQ)
PKG_SEARCH_MODULE(LIBZMQ QUIET libzmq)
IF (NOT LIBZMQ_FOUND)
LINUX_INSTALL_SYSTEM_PACKAGE()
ENDIF ()
ENDIF ()
IF ((NOT LIBCRYPTO_FOUND) OR (NOT LIBSSL_FOUND) OR (NOT LIBCURL_FOUND))
LINUX_INSTALL_SYSTEM_PACKAGE()
ENDIF ()

View File

@ -16,7 +16,7 @@ SET(CMAKE_C_STANDARD 99)
FILE(GLOB VCPE_HEADS include/*.h include/uthash/*.h include/s2j/*.h
./httpserver/include/*.h ./httpserver/src/haywire/*.h ./httpserver/src/haywire/configuration/*.h)
IF (USED_LWIP)
IF (VCPE_PPPOE)
AUX_SOURCE_DIRECTORY(pppoe VCPE_SRC)
AUX_SOURCE_DIRECTORY(user VCPE_SRC)
ENDIF ()

View File

@ -17,7 +17,9 @@ AUX_SOURCE_DIRECTORY(banner C_SRC)
AUX_SOURCE_DIRECTORY(configure C_SRC)
AUX_SOURCE_DIRECTORY(network C_SRC)
AUX_SOURCE_DIRECTORY(task C_SRC)
AUX_SOURCE_DIRECTORY(mq C_SRC)
IF (USED_ZMQ)
AUX_SOURCE_DIRECTORY(mq C_SRC)
ENDIF ()
AUX_SOURCE_DIRECTORY(cmdline C_SRC)
AUX_SOURCE_DIRECTORY(crypto C_SRC)
AUX_SOURCE_DIRECTORY(hardware C_SRC)

View File

@ -186,8 +186,11 @@ static int on_cmd2(void *pTbl[], const char *pName, void *pInfo) {
}
pJsonStr = cJSON_PrintUnformatted(pJsonObj);
ret = mq_cmd_run(pSvr, pJsonStr);
#ifdef ZEROMQ_ON
ret = mq_cmd_run(pSvr, pJsonStr);
#else
ret = ERR_SUCCESS;
#endif
cJSON_Delete(pJsonObj);
free((void *)pJsonStr);

View File

@ -73,11 +73,6 @@ static CFG_ITEM g_cfgItem[] = {
DEF_CFG_ITEM(CFG_WATCH_MEMORY, "watch_params.memory", VAL_BOOL, "1", "Monitor memory information"),
DEF_CFG_ITEM(CFG_WATCH_DISK, "watch_params.disk", VAL_BOOL, "1", "Monitor disk partition information"),
DEF_CFG_ITEM(CFG_WATCH_SENSOR, "watch_params.sensor", VAL_BOOL, "1", "Sensor information refresh frequency"),
/* 系统监控设备刷频率 */
DEF_CFG_ITEM(CFG_CPU_REFRESH, "watch_params.cpu_refresh", VAL_INT, "10", "CPU information refresh frequency"),
DEF_CFG_ITEM(CFG_MEM_REFRESH, "watch_params.mem_refresh", VAL_INT, "10", "Memory information refresh frequency"),
DEF_CFG_ITEM(CFG_DISK_REFRESH, "watch_params.disk_refresh", VAL_INT, "10", "Disk information refresh frequency"),
DEF_CFG_ITEM(CFG_SENSOR_REFRESH, "watch_params.sensor_refresh", VAL_INT, "10", "Sensor information refresh frequency"),
/* 数据库相配置 */
/* Redis配置 */
#ifdef USED_REDIS
@ -98,10 +93,12 @@ static CFG_ITEM g_cfgItem[] = {
DEF_CFG_ITEM(CFG_DB_SQLITE_DB_NAME, "database.sqlite_dbname", VAL_STR, "", "SQLite3 database file name"),
DEF_CFG_ITEM(CFG_DB_SQLITE_PASSWD, "database.sqlite_passwd", VAL_STR, ".main", "SQLite3 database password"),
#endif
#ifdef ZEROMQ_ON
/* 消息队列相配置 */
/* ZeroMq配置 */
DEF_CFG_ITEM(CFG_MQ_SVR_PORT, "zero_mq.svr_port", VAL_INT, "6278", "ZeroMQ server port"),
DEF_CFG_ITEM(CFG_MQ_DATA_PATH, "zero_mq.agent_addr", VAL_STR, "ipc:///tmp/msg_fifo0", "ZeroMQ Agent data path"),
#endif
/* vxLan 隧道配置 */
DEF_CFG_ITEM(CFG_VXLAN_NIC_NAME, "vxlan_wan.nic", VAL_STR, "", "Network card name to send data"),
DEF_CFG_ITEM(CFG_VXLAN_SUPPORT, "vxlan_wan.enable", VAL_BOOL, "1", "Is support vxLan tune"),

View File

@ -121,22 +121,6 @@ int cfg_get_watch_sensor() {
return cfg_get_bool_value(CFG_WATCH_SENSOR);
}
unsigned int cfg_get_cpu_refresh_period() {
return cfg_get_integral_value(CFG_CPU_REFRESH);
}
unsigned int cfg_get_mem_refresh_period() {
return cfg_get_integral_value(CFG_MEM_REFRESH);
}
unsigned int cfg_get_disk_refresh_period() {
return cfg_get_integral_value(CFG_DISK_REFRESH);
}
unsigned int cfg_get_sensor_refresh_period() {
return cfg_get_integral_value(CFG_SENSOR_REFRESH);
}
#ifdef USED_SQLITE
const char *cfg_get_sqlite_db_name() {
return cfg_get_string_value(CFG_DB_SQLITE_DB_NAME);
@ -183,6 +167,7 @@ const char *cfg_get_mysql_database() {
}
#endif
#ifdef ZEROMQ_ON
int cfg_get_zero_mq_port() {
return (unsigned short)cfg_get_integral_value(CFG_MQ_SVR_PORT);
}
@ -190,3 +175,4 @@ int cfg_get_zero_mq_port() {
const char *cfg_get_zero_mq_data_path() {
return cfg_get_string_value(CFG_MQ_DATA_PATH);
}
#endif

View File

@ -20,7 +20,7 @@ typedef struct {
#define CMD_CPU_CORE ("cat /proc/cpuinfo| grep \"cpu cores\" | uniq | awk \'{print $4}\'")
#define CMP_CPU_LOGIC_CORE ("cat /proc/cpuinfo| grep \"cpu cores\" | wc -l")
#define SUM_CPU_TIME(x) ((x).user + (x).nice + (x).sys + (x).idle + (x).irq)
#define SUM_CPU_TIME(x) ((x).user + (x).nice + (x).sys + (x).idle + (x).irq)
static int g_isInit = FALSE;
static CPU_INFO g_cpuInfo;
@ -103,7 +103,7 @@ static int get_cpu_time_info(unsigned long *pTotal, unsigned long *pIdle) {
*pTotal = *pIdle = 0;
for (i = 0; i < nCpu; i++) {
*pIdle += pCpu[i].cpu_times.idle;
*pIdle += pCpu[i].cpu_times.idle;
*pTotal += SUM_CPU_TIME(pCpu[i].cpu_times);
}
@ -135,25 +135,7 @@ static void cpuUsedRefresh() {
}
}
_Noreturn void cpuCalcCb(void *UNUSED(pArg)) {
do {
unsigned int period = cfg_get_cpu_refresh_period();
if (cfg_get_watch_cpu()) {
cpuUsedRefresh();
}
if (period < REFRESH_MAX_PERIOD && period >= 1) {
uv_sleep(1000 * period);
} else {
uv_sleep(1000);
}
} while (TRUE);
}
int cpu_watch_init() {
static uv_thread_t uvThread;
memset(&g_cpuInfo, 0, sizeof(CPU_INFO));
g_cpuInfo.nCpus = get_cpu_number();
@ -163,9 +145,6 @@ int cpu_watch_init() {
get_cpu_desc(&g_cpuInfo.cpuCoreDesc);
g_isInit = TRUE;
uv_thread_create(&uvThread, cpuCalcCb, NULL);
return ERR_SUCCESS;
}
@ -184,6 +163,8 @@ int get_cpu_info(PCPU_INFO pInfo) {
get_cpu_desc(&g_cpuInfo.cpuCoreDesc);
g_isInit = TRUE;
} else if (cfg_get_watch_cpu()) {
cpuUsedRefresh();
}
memcpy(pInfo, &g_cpuInfo, sizeof(CPU_INFO));

View File

@ -12,7 +12,7 @@
#include "uthash/uthash.h"
#include "config.h"
#define MAX_SIZE_LEN (16)
#define MAX_SIZE_LEN (16)
//#define CMD_DISK_FILESYSTEM ("df -h | awk \'{if (NR > 1){print $0}}\'")
#define CMD_DISK_FILESYSTEM ("df -h | awk \'{if (NR > 1){print $1\"|\"$2\"|\"$3\"|\"$4\"|\"$5\"|\"$6}}\'")
@ -105,6 +105,10 @@ int get_disk_info(PDISK_INFO pInfo) {
return -ERR_INPUT_PARAMS;
}
if (cfg_get_watch_disk()) {
disk_info_refresh();
}
uv_rwlock_rdlock(&g_uvLock);
pInfo->nItems = HASH_COUNT(g_diskPartInfo);
pInfo->timestamp = time(NULL);
@ -122,32 +126,12 @@ int get_disk_info(PDISK_INFO pInfo) {
return ERR_SUCCESS;
}
_Noreturn void diskRefreshCb(void *UNUSED(pArg)) {
do {
unsigned int period = cfg_get_disk_refresh_period();
if (cfg_get_watch_disk()) {
disk_info_refresh();
}
if (period < REFRESH_MAX_PERIOD && period >= 1) {
uv_sleep(1000 * period);
} else {
uv_sleep(1000);
}
} while (TRUE);
}
int disk_watch_info() {
static uv_thread_t uvThread;
uv_rwlock_init(&g_uvLock);
if (disk_info_refresh() != ERR_SUCCESS) {
return -ERR_SYS_DISK_GET_INFO;
}
uv_thread_create(&uvThread, diskRefreshCb, NULL);
return ERR_SUCCESS;
}

View File

@ -9,51 +9,48 @@
#include "user_errno.h"
#include "config.h"
#include "misc.h"
#include "task_manager.h"
static HARDWARE_INFO g_hardwareInfo;
_Noreturn void hardwareRefreshCb(void *UNUSED(pArg)) {
do {
unsigned int period = cfg_get_hardware_refresh_period();
void hardwareRefreshCb(uv_timer_t *UNUSED(pArg)) {
if (cfg_get_watch_sensor()) {
get_sensor_info(&g_hardwareInfo.sensorInfo);
}
if (cfg_get_watch_disk()) {
get_disk_info(&g_hardwareInfo.diskInfo);
}
if (cfg_get_watch_cpu()) {
get_cpu_info(&g_hardwareInfo.cpuInfo);
}
if (cfg_get_watch_memory()) {
get_memory_info(&g_hardwareInfo.memInfo);
}
if (cfg_get_watch_sensor()) {
get_sensor_info(&g_hardwareInfo.sensorInfo);
}
if (cfg_get_watch_disk()) {
get_disk_info(&g_hardwareInfo.diskInfo);
}
if (cfg_get_watch_cpu()) {
get_cpu_info(&g_hardwareInfo.cpuInfo);
}
if (cfg_get_watch_memory()) {
get_memory_info(&g_hardwareInfo.memInfo);
}
if (period < REFRESH_MAX_PERIOD && period >= 1) {
uv_sleep(1000 * period);
} else {
uv_sleep(1000);
}
uv_sleep(1000);
} while (TRUE);
//printf("%s\n", get_hardware_json());
}
int init_hardware() {
static uv_thread_t uvThread;
static uv_timer_t uvTm;
unsigned int period = cfg_get_hardware_refresh_period();
if (period >= REFRESH_MAX_PERIOD) {
period = 10000;
} else {
period *= 1000;
}
uv_timer_init(get_task_manager(), &uvTm);
memset(&g_hardwareInfo, 0, sizeof(HARDWARE_INFO));
uv_thread_create(&uvThread, hardwareRefreshCb, NULL);
cpu_watch_init();
memory_watch_init();
disk_watch_info();
sensor_watch_init();
uv_timer_start(&uvTm, hardwareRefreshCb, period, period);
return ERR_SUCCESS;
}

View File

@ -12,10 +12,11 @@
#include "misc.h"
#include "config.h"
#define MAX_SENSOR_STR (32)
#define MAX_SENSOR_STR (32)
//#define CMD_SENSOR_INFO ("ipmitool sensor | awk '{gsub(/\|/, \"\"); print $0}'")
#define CMD_SENSOR_INFO ("ipmitool sensor")
#define CMD_SENSOR_INFO ("ipmitool sensor")
#define CMD_IPMITOOL_ENABLE ("whereis ipmitool | awk '{print $2}'")
typedef struct {
char name[MAX_SENSOR_STR]; ///< 传感器名称
@ -42,9 +43,16 @@ static int sensor_info_refresh() {
int errCode = ERR_SUCCESS;
FILE *fp;
char buf[1024];
char *pRet = NULL;
if (shell_with_output(CMD_IPMITOOL_ENABLE, &pRet) != ERR_SUCCESS || pRet == NULL || strlen(pRet) == 0) {
if (pRet) {
free(pRet);
}
return -ERR_ITEM_UNEXISTS;
}
fp = popen(CMD_SENSOR_INFO, "r");
if (fp == NULL) {
return -ERR_OPEN_FILE;
}
@ -132,6 +140,10 @@ int get_sensor_info(PSENSOR_INFO pInfo) {
return -ERR_INPUT_PARAMS;
}
if (cfg_get_watch_sensor()) {
sensor_info_refresh();
}
uv_rwlock_rdlock(&g_uvLock);
pInfo->nItems = HASH_COUNT(g_pSensorInfo);
pInfo->timestamp = time(NULL);
@ -149,27 +161,9 @@ int get_sensor_info(PSENSOR_INFO pInfo) {
return ERR_SUCCESS;
}
_Noreturn void sensorRefreshCb(void *UNUSED(pArg)) {
do {
unsigned int period = cfg_get_sensor_refresh_period();
if (cfg_get_watch_sensor()) {
sensor_info_refresh();
}
if (period < REFRESH_MAX_PERIOD && period >= 1) {
uv_sleep(1000 * period);
} else {
uv_sleep(1000);
}
} while (TRUE);
}
int sensor_watch_init() {
static uv_thread_t uvThread;
int i, devOk = FALSE;
int n = ARRAY_SIZE(g_ipmiDev);
int i, devOk = FALSE;
int n = ARRAY_SIZE(g_ipmiDev);
uv_rwlock_init(&g_uvLock);
@ -188,7 +182,5 @@ int sensor_watch_init() {
return -ERR_SYS_SENSOR_GET_INFO;
}
uv_thread_create(&uvThread, sensorRefreshCb, NULL);
return ERR_SUCCESS;
}

View File

@ -14,11 +14,11 @@
#define CMD_MEMORY_INFO ("cat /proc/meminfo | awk \'{print $1\"|\"$2\"|\"$3}\'")
#define M_SIZE (1024 * 1024)
#define M_SIZE (1024 * 1024)
#define KEY_NAME (32)
#define KEY_VALUE (32)
#define VALUE_UNIT (8)
#define KEY_NAME (32)
#define KEY_VALUE (32)
#define VALUE_UNIT (8)
typedef struct {
char itemName[KEY_NAME];
@ -95,33 +95,13 @@ static int memory_info_refresh() {
return errCode;
}
_Noreturn void memRefreshCb(void *UNUSED(pArg)) {
do {
unsigned int period = cfg_get_mem_refresh_period();
if (cfg_get_watch_memory()) {
memory_info_refresh();
}
if (period < REFRESH_MAX_PERIOD && period >= 1) {
uv_sleep(1000 * period);
} else {
uv_sleep(1000);
}
} while (TRUE);
}
int memory_watch_init() {
static uv_thread_t uvThread;
uv_rwlock_init(&g_uvLock);
if (memory_info_refresh() != ERR_SUCCESS) {
return -ERR_SYS_DISK_GET_INFO;
}
uv_thread_create(&uvThread, memRefreshCb, NULL);
return ERR_SUCCESS;
}
@ -142,6 +122,10 @@ int get_memory_info(PMEMORY_INFO pInfo) {
return -ERR_INPUT_PARAMS;
}
if (cfg_get_watch_memory()) {
memory_info_refresh();
}
uv_rwlock_rdlock(&g_uvLock);
for (i = 0; i < n; i++) {

View File

@ -41,10 +41,6 @@ typedef enum {
CFG_WATCH_MEMORY,
CFG_WATCH_DISK,
CFG_WATCH_SENSOR,
CFG_CPU_REFRESH,
CFG_MEM_REFRESH,
CFG_DISK_REFRESH,
CFG_SENSOR_REFRESH,
#ifdef USED_REDIS
CFG_DB_REDIS_SERVER,
CFG_DB_REDIS_PORT,
@ -61,8 +57,10 @@ typedef enum {
CFG_DB_SQLITE_DB_NAME,
CFG_DB_SQLITE_PASSWD,
#endif
#ifdef ZEROMQ_ON
CFG_MQ_SVR_PORT,
CFG_MQ_DATA_PATH,
#endif
CFG_VXLAN_NIC_NAME,
CFG_VXLAN_SUPPORT,
CFG_VXLAN_PEER_IP,
@ -92,10 +90,6 @@ int cfg_get_watch_cpu();
int cfg_get_watch_memory();
int cfg_get_watch_disk();
int cfg_get_watch_sensor();
unsigned int cfg_get_cpu_refresh_period();
unsigned int cfg_get_mem_refresh_period();
unsigned int cfg_get_disk_refresh_period();
unsigned int cfg_get_sensor_refresh_period();
#ifdef USED_REDIS
const char *cfg_get_redis_server();
int cfg_get_redis_port();
@ -112,8 +106,10 @@ const char *cfg_get_mysql_database();
const char *cfg_get_sqlite_db_name();
const char *cfg_get_sqlite_passwd();
#endif
#ifdef ZEROMQ_ON
int cfg_get_zero_mq_port();
const char *cfg_get_zero_mq_data_path();
#endif
const char *cfg_get_string_value(CONFIG_ITEM_ID id);
c_vector cfg_get_vector(CONFIG_ITEM_ID id);

View File

@ -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")
@ -134,7 +136,7 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK
if (cfg_get_hardware_watch_enable()) {
init_hardware();
}
#ifdef ZEROMQ_ON
if ((ret = mq_init()) != ERR_SUCCESS) {
LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret);
}
@ -142,6 +144,7 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK
if ((ret = mq_data_init()) != ERR_SUCCESS) {
LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret);
}
#endif
#ifdef HTTPSERVER_ON
http_svr_init();
#endif
@ -156,7 +159,9 @@ void user_uninit() {
#ifdef HTTPSERVER_ON
http_svr_uinit();
#endif
#ifdef ZEROMQ_ON
mq_uninit();
#endif
zlog_fini();
uninit_config_system();
uv_loop_close(get_task_manager());

View File

@ -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;
@ -519,7 +520,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);
@ -796,68 +797,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);
}
}
/**
@ -980,11 +976,12 @@ 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);
for (auto &i : g_routeTable) {
PHTTP_ROUTE_INFO p = &i;
http_add_route(p->routeName, p->method, p->priority, p->cb, p->pUserData);

View File

@ -4,6 +4,7 @@
#include <uv.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "cmdline.h"
#include "task_manager.h"
#include "init.h"
@ -76,7 +77,9 @@ static void on_http_response_cb(void *pData,
}
int main(int argc, char **argv) {
int ret;
int ret;
const char *pRspUrl;
#ifdef OPENDHCPDDNS_ON
return dual_server_main(argc, argv);
#else
@ -103,12 +106,19 @@ int main(int argc, char **argv) {
pppoe_session_init();
#endif
const char *pStrSetup = proto_create_new(create_app_process_status(1), 200);
inet_http_post_async(config_get_agent_moniter_report_url(), pStrSetup, on_http_response_cb, (void *)pStrSetup);
pRspUrl = config_get_agent_moniter_report_url();
if (pRspUrl && strlen(pRspUrl) > 0) {
const char *pStrSetup = proto_create_new(create_app_process_status(1), 200);
inet_http_post_async(config_get_agent_moniter_report_url(), pStrSetup, on_http_response_cb, (void *)pStrSetup);
}
task_manager_run();
const char *pStrExit = proto_create_new(create_app_process_status(0), 200);
inet_http_post_async(config_get_agent_moniter_report_url(), pStrSetup, on_http_response_cb, (void *)pStrExit);
if (pRspUrl && strlen(pRspUrl) > 0) {
const char *pStrExit = proto_create_new(create_app_process_status(0), 200);
inet_http_post_async(config_get_agent_moniter_report_url(), pStrExit, on_http_response_cb, (void *)pStrExit);
}
while (!is_system_cleanup()) {
sleep(1);