diff --git a/Product/common/common.h b/Product/common/common.h index b86a0e3de..b83d4eb8c 100644 --- a/Product/common/common.h +++ b/Product/common/common.h @@ -10,7 +10,10 @@ #endif #define MAX_NAME_LEN (32 * 4) +#define MAX_SESSION (32 + 1) #define MAX_PATH (256) +#define MAX_PAGE_ITEMS (10) + #endif #define GET_FILE_SIZE(path, size) \ diff --git a/Product/user/object_manager/json_interface.c b/Product/user/object_manager/json_interface.c index fa7d5aa1e..276e40be4 100644 --- a/Product/user/object_manager/json_interface.c +++ b/Product/user/object_manager/json_interface.c @@ -398,11 +398,475 @@ static int __obj_del_decode(const char *pJsonS, void **pStruct) return RET_OK; } +static const char *__obj_mod_encode(void *pData) +{ + const char *pJsonS; + PIFC_RET_MSG p = (PIFC_RET_MSG)pData; + cJSON *pRoot = cJSON_CreateObject(); + + if(!p) { + return NULL; + } + + cJSON_AddNumberToObject(pRoot, "retCode", p->ret_code); + + if(p->mesg) { + cJSON_AddStringToObject(pRoot, "mesg", p->mesg); + } + + if(p->n_items > 0) { + cJSON *pArray = cJSON_AddArrayToObject(pRoot, "data"); + + if(pArray) { + int i; + + for(i = 0; i < p->n_items; i++) { + cJSON *pItem = cJSON_CreateObject(); + + if(!pItem) { + continue; + } + + cJSON_AddStringToObject(pItem, "name", p->data[i].name); + cJSON_AddNumberToObject(pItem, "retCode", p->data[i].ret_code); + + if(p->data[i].mesg) { + cJSON_AddStringToObject(pItem, "mesg", p->data[i].mesg); + } + + cJSON_AddItemToArray(pArray, pItem); + } + } + } + + pJsonS = cJSON_Print(pRoot); + cJSON_Delete(pRoot); + return pJsonS; +} + +static int __obj_mod_decode(const char *pJsonS, void **pStruct) +{ + cJSON *pRoot, *pVal; + PIFACE_ADD_OBJ pData; + int arraySize; + int i, j; + *pStruct = malloc(sizeof(IFACE_ADD_OBJ)); + + if(*pStruct == NULL) { + return -RET_NOMEM; + } + + pData = (PIFACE_ADD_OBJ) * pStruct; + pRoot = cJSON_Parse(pJsonS); + + if(!pRoot) { + return -RET_SYSERR; + } + + if(!cJSON_IsArray(pRoot)) { + cJSON_Delete(pRoot); + return -RET_INPUTERR; + } + + memset(pData, 0, sizeof(IFACE_ADD_OBJ)); + arraySize = cJSON_GetArraySize(pRoot); + pData->pCtx = (PIFC_ADD_CTX)malloc(sizeof(IFC_ADD_CTX) * arraySize); + + if(pData->pCtx == NULL) { + LOG_EX(LOG_Error, "Malloc memory %d error\n", sizeof(IFC_ADD_CTX) * arraySize); + cJSON_Delete(pRoot); + return -RET_NOMEM; + } + + memset(pData->pCtx, 0, sizeof(IFC_ADD_CTX) * arraySize); + + for(i = 0; i < arraySize; i++) { + pData->pCtx[i].pObj = malloc(sizeof(CMHI_OBJECT)); + + if(pData->pCtx[i].pObj == NULL) { + for(j = 0; j < i; j++) { + if(pData->pCtx[j].pObj) { + free(pData->pCtx[j].pObj); + } + } + + LOG_EX(LOG_Error, "Malloc memory (%d):%d error\n", i, + sizeof(CMHI_OBJECT) * arraySize); + cJSON_Delete(pRoot); + free(pData->pCtx); + return -RET_NOMEM; + } + + memset(pData->pCtx[i].pObj, 0, sizeof(CMHI_OBJECT)); + } + + pData->n_obj = arraySize; + + for(i = 0; i < arraySize; i++) { + PCMHI_OBJECT pObj = pData->pCtx[i].pObj; + POBJECT_K pObjK = &pData->pCtx[i].objk; + cJSON *pSub = cJSON_GetArrayItem(pRoot, i); + cJSON *pSubContent = cJSON_GetObjectItem(pSub, "objContent"); + + if(!cJSON_IsArray(pSubContent)) { + LOG_EX(LOG_Error, "Don't found (objContent) content\n"); + cJSON_Delete(pRoot); + + for(j = 0; j < arraySize; j++) { + if(pData->pCtx[j].pObj) { + free(pData->pCtx[j].pObj); + } + } + + free(pData->pCtx); + return -RET_INPUTERR; + } + + pObjK->ctx_num = cJSON_GetArraySize(pSubContent); + + if(pObjK->ctx_num <= 0) { + LOG_EX(LOG_Error, "The objContent has no member\n"); + + for(j = 0; j < arraySize; j++) { + if(pData->pCtx[j].pObj) { + free(pData->pCtx[j].pObj); + } + } + + free(pData->pCtx); + cJSON_Delete(pRoot); + return -RET_INPUTERR; + } + + pVal = cJSON_GetObjectItem(pSub, "prio"); + + if(cJSON_IsNumber(pVal)) { + pObj->prio = pVal->valueint; + } + + pVal = cJSON_GetObjectItem(pSub, "type"); + + if(cJSON_IsNumber(pVal)) { + pObjK->type = pVal->valueint; + } + + pVal = cJSON_GetObjectItem(pSub, "name"); + + if(cJSON_IsString(pVal)) { + strncpy(pObj->name, pVal->valuestring, MAX_NAME_LEN - 1); + } + + pVal = cJSON_GetObjectItem(pSub, "desc"); + + if(cJSON_IsString(pVal)) { + strncpy(pObj->desc, pVal->valuestring, MAX_DESC - 1); + } + + for(j = 0; j < pObjK->ctx_num; j++) { + cJSON *pItem = cJSON_GetArrayItem(pSubContent, j); + + switch(pObjK->type / 10) { + case OBJ_TYPE_SERVER: + pVal = cJSON_GetObjectItem(pItem, "proType"); + + if(cJSON_IsNumber(pVal)) { + pObjK->objs.svr_obj[j].pro_type = pVal->valueint; + } + + pVal = cJSON_GetObjectItem(pItem, "porPort"); + + if(cJSON_IsString(pVal)) { + pObjK->objs.svr_obj[j].str_val = strdup(pVal->valuestring); + } + + break; + + case OBJ_TYPE_ADDR: + pVal = cJSON_GetObjectItem(pItem, "ipAddr"); + + if(cJSON_IsString(pVal)) { + pObjK->objs.addr_obj[j].str_val = strdup(pVal->valuestring); + } + + break; + + case OBJ_TYPE_DATETIME: + pVal = cJSON_GetObjectItem(pItem, "repType"); + + if(cJSON_IsNumber(pVal)) { + pObjK->objs.dt_obj[j].rep_mode = pVal->valueint; + } + + pVal = cJSON_GetObjectItem(pItem, "timeValue"); + + if(cJSON_IsString(pVal)) { + pObjK->objs.dt_obj[j].str_val = strdup(pVal->valuestring); + } + + break; + } + } + } + + cJSON_Delete(pRoot); + return RET_OK; +} + +static const char *__obj_query_list_encode(void *pData) +{ + const char *pJsonS; + PIFC_RET_PAGE_MSG p = (PIFC_RET_PAGE_MSG)pData; + cJSON *pRoot = cJSON_CreateObject(); + + if(!p) { + return NULL; + } + + cJSON_AddNumberToObject(pRoot, "retCode", p->ret_code); + + if(p->mesg) { + cJSON_AddStringToObject(pRoot, "mesg", p->mesg); + } + + if(p->ret_code == RET_OK) { + if(p->session) { + cJSON_AddStringToObject(pRoot, "session", p->session); + } + + cJSON_AddNumberToObject(pRoot, "type", p->type); + cJSON_AddNumberToObject(pRoot, "tolItems", p->tolItems); + cJSON_AddNumberToObject(pRoot, "start", p->start); + cJSON_AddNumberToObject(pRoot, "end", p->end); + cJSON_AddNumberToObject(pRoot, "n_items", p->n_items); + + if(p->n_items > 0) { + cJSON *pArray = cJSON_AddArrayToObject(pRoot, "data"); + + if(pArray) { + int i; + + for(i = 0; i < p->n_items; i++) { + cJSON *pItem = cJSON_CreateObject(); + + if(!pItem) { + continue; + } + + cJSON_AddStringToObject(pItem, "name", p->data[i].name); + cJSON_AddNumberToObject(pItem, "type", p->data[i].type); + cJSON_AddNumberToObject(pItem, "ref_count", p->data[i].ref_count); + + if(p->data[i].desc) { + cJSON_AddStringToObject(pItem, "desc", p->data[i].desc); + } + + cJSON_AddItemToArray(pArray, pItem); + } + } + } + } + + pJsonS = cJSON_Print(pRoot); + cJSON_Delete(pRoot); + return pJsonS; +} + +static int __obj_query_list_decode(const char *pJsonS, void **pStruct) +{ + cJSON *pRoot, *pVal; + PIFACE_QUERY_LIST pData; + *pStruct = malloc(sizeof(IFACE_QUERY_LIST)); + + if(*pStruct == NULL) { + return -RET_NOMEM; + } + + pData = (PIFACE_QUERY_LIST) * pStruct; + pRoot = cJSON_Parse(pJsonS); + + if(!pRoot) { + return -RET_SYSERR; + } + + memset(pData, 0, sizeof(IFACE_QUERY_LIST)); + pVal = cJSON_GetObjectItem(pRoot, "type"); + + if(cJSON_IsNumber(pVal)) { + pData->type = pVal->valueint; + } + + pVal = cJSON_GetObjectItem(pRoot, "start"); + + if(cJSON_IsNumber(pVal)) { + pData->start = pVal->valueint; + } + + pVal = cJSON_GetObjectItem(pRoot, "end"); + + if(cJSON_IsNumber(pVal)) { + pData->end = pVal->valueint; + } + + pVal = cJSON_GetObjectItem(pRoot, "session"); + + if(cJSON_IsString(pVal)) { + strncpy(pData->session, pVal->valuestring, MAX_SESSION - 1); + } + + cJSON_Delete(pRoot); + return RET_OK; +} + +static const char *__obj_detail_encode(void *pData) +{ + const char *pJsonS; + PIFC_RET_DETAIL_MSG p = (PIFC_RET_DETAIL_MSG)pData; + cJSON *pRoot = cJSON_CreateObject(); + + if(!p) { + return NULL; + } + + cJSON_AddNumberToObject(pRoot, "retCode", p->ret_code); + + if(p->mesg) { + cJSON_AddStringToObject(pRoot, "mesg", p->mesg); + } + + if(p->n_items > 0 && p->ret_code == RET_OK) { + cJSON *pArray = cJSON_AddArrayToObject(pRoot, "data"); + + if(pArray) { + int i; + + for(i = 0; i < p->n_items; i++) { + cJSON *pObjArray; + cJSON *pItem = cJSON_CreateObject(); + + if(!pItem) { + continue; + } + + pObjArray = cJSON_AddArrayToObject(pItem, "objContent"); + cJSON_AddStringToObject(pItem, "name", p->data[i].name); + cJSON_AddNumberToObject(pItem, "retCode", p->data[i].ret_code); + + if(p->data[i].mesg) { + cJSON_AddStringToObject(pItem, "mesg", p->data[i].mesg); + } + + if(p->data[i].mesg) { + cJSON_AddStringToObject(pItem, "desc", p->data[i].desc); + } + + cJSON_AddNumberToObject(pItem, "type", p->data[i].pObjK->type); + cJSON_AddNumberToObject(pItem, "ref_count", p->data[i].pObjK->ref_count); + cJSON_AddNumberToObject(pItem, "prio", p->data[i].prio); + + if(pObjArray) { + int j; + + for(j = 0; j < p->data[i].pObjK->ctx_num; j++) { + cJSON *pObjItem = cJSON_CreateObject(); + PSVR_OBJECT pSvr = &p->data[i].pObjK->objs.svr_obj[j]; + PADDR_OBJECT pAddr = &p->data[i].pObjK->objs.addr_obj[j]; + PDT_OBJECT pDt = &p->data[i].pObjK->objs.dt_obj[j]; + + if(!pObjItem) { + continue; + } + + switch(p->data[i].pObjK->type / 10) { + case OBJ_TYPE_SERVER: + cJSON_AddNumberToObject(pObjItem, "proType", pSvr->pro_type); + if(pSvr->str_val && strlen(pSvr->str_val) > 0) { + cJSON_AddStringToObject(pObjItem, "porPort", pSvr->str_val); + } + + break; + + case OBJ_TYPE_ADDR: + if(pAddr->str_val && strlen(pDt->str_val) > 0) { + cJSON_AddStringToObject(pObjItem, "ipAddr", pAddr->str_val); + } + + break; + + case OBJ_TYPE_DATETIME: + cJSON_AddNumberToObject(pObjItem, "repType", pDt->rep_mode); + if(pDt->str_val && strlen(pDt->str_val) > 0) { + cJSON_AddStringToObject(pObjItem, "timeValue", pDt->str_val); + } + + break; + } + + cJSON_AddItemToArray(pObjArray, pObjItem); + } + } + + cJSON_AddItemToArray(pArray, pItem); + } + } + } + + pJsonS = cJSON_Print(pRoot); + cJSON_Delete(pRoot); + return pJsonS; +} + +static int __obj_detail_decode(const char *pJsonS, void **pStruct) +{ + cJSON *pRoot; + PIFACE_DETAIL_OBJ pData; + int arraySize; + int i; + *pStruct = malloc(sizeof(IFACE_DETAIL_OBJ)); + + if(*pStruct == NULL) { + return -RET_NOMEM; + } + + pData = (PIFACE_DETAIL_OBJ) * pStruct; + pRoot = cJSON_Parse(pJsonS); + + if(!pRoot) { + return -RET_SYSERR; + } + + if(!cJSON_IsArray(pRoot)) { + cJSON_Delete(pRoot); + return -RET_INPUTERR; + } + + memset(pData, 0, sizeof(IFACE_ADD_OBJ)); + arraySize = cJSON_GetArraySize(pRoot); + pData->n_obj = arraySize; + + for(i = 0; i < arraySize; i++) { + cJSON *pSub = cJSON_GetArrayItem(pRoot, i); + + if(pSub) { + cJSON *pVal = cJSON_GetObjectItem(pSub, "name"); + + if(cJSON_IsString(pVal)) { + strncpy(pData->name[i], pVal->valuestring, MAX_NAME_LEN - 1); + } + } + } + + cJSON_Delete(pRoot); + return RET_OK; +} + static JSON_ENGINE g_jSonEngine[] = { - {JE_INTERFACE, __interface_encode, __interface_decode, NULL}, - {OBJ_CMD_ADD, __obj_add_encode, __obj_add_decode, NULL}, - {JE_OBJ_DEL, __obj_del_encode, __obj_del_decode, NULL}, - {JE_OBJ_MOD, NULL, NULL, NULL} + {JE_INTERFACE, __interface_encode, __interface_decode, NULL}, + {OBJ_CMD_ADD, __obj_add_encode, __obj_add_decode, NULL}, + {JE_OBJ_DEL, __obj_del_encode, __obj_del_decode, NULL}, + {JE_OBJ_MOD, __obj_mod_encode, __obj_mod_decode, NULL}, + {JE_OBJ_QUERYLIST, __obj_query_list_encode, __obj_query_list_decode, NULL}, + {JE_OBJ_QUERYDETAIL, __obj_detail_encode, __obj_detail_decode, NULL}, + {JSON_ENGINE_MAX, NULL, NULL, NULL} }; int Json2Struct(const char *pJsonStr, void *pData, JSON_ENGINE_TYPE type, diff --git a/Product/user/object_manager/json_interface.h b/Product/user/object_manager/json_interface.h index 1a985eae2..11b331ec5 100644 --- a/Product/user/object_manager/json_interface.h +++ b/Product/user/object_manager/json_interface.h @@ -49,7 +49,14 @@ typedef struct { typedef struct { char name[100][MAX_NAME_LEN]; int n_obj; -} IFACE_DEL_OBJ, *PIFACE_DEL_OBJ; +} IFACE_DEL_OBJ, IFACE_DETAIL_OBJ, *PIFACE_DETAIL_OBJ, *PIFACE_DEL_OBJ; + +typedef struct { + char session[MAX_SESSION]; + int type; + int start; + int end; +} IFACE_QUERY_LIST, *PIFACE_QUERY_LIST; typedef struct { char *name; @@ -61,9 +68,44 @@ typedef struct { int ret_code; char *mesg; int n_items; - IFC_RET_LIST data[100]; + IFC_RET_LIST data[MAX_PAGE_ITEMS]; } IFC_RET_MSG, *PIFC_RET_MSG; +typedef struct { + char *name; + char *mesg; + char* *desc; + int prio; + int ret_code; + POBJECT_K pObjK; +} IFC_RET_DETAIL_LIST, *PIFC_RET_DETAIL_LIST; + +typedef struct { + int ret_code; + char *mesg; + int n_items; + IFC_RET_DETAIL_LIST data[MAX_PAGE_ITEMS]; +} IFC_RET_DETAIL_MSG, *PIFC_RET_DETAIL_MSG; + +typedef struct { + char *name; + char *desc; + int type; + int ref_count; +} IFC_RET_PAGE_LIST, *PIFC_RET_PAGE_LIST; + +typedef struct { + int ret_code; + char *mesg; + char *session; + int type; + int tolItems; + int start; + int end; + int n_items; + IFC_RET_PAGE_LIST data[MAX_PAGE_ITEMS]; +} IFC_RET_PAGE_MSG, *PIFC_RET_PAGE_MSG; + int Json2Struct(const char *pJsonStr, void *pData, JSON_ENGINE_TYPE type, int enBase64); const char *Struct2Json(void *pStruct, JSON_ENGINE_TYPE type, int enBase64, diff --git a/Product/user/object_manager/json_test/detail.json b/Product/user/object_manager/json_test/detail.json new file mode 100644 index 000000000..f3018eea1 --- /dev/null +++ b/Product/user/object_manager/json_test/detail.json @@ -0,0 +1,7 @@ +{ + "cmdId": 104, + "ver": 1, + "cryptoType": 0, + "timeStamp": 1526625689, + "msgContent": "[{\"name\":\"RTP管理\"},{\"name\":\"研发部\"},{\"name\":\"工作时间\"}]" +} \ No newline at end of file diff --git a/Product/user/object_manager/json_test/page_queue.json b/Product/user/object_manager/json_test/page_queue.json new file mode 100644 index 000000000..51e7fee69 --- /dev/null +++ b/Product/user/object_manager/json_test/page_queue.json @@ -0,0 +1,7 @@ +{ + "cmdId": 103, + "ver": 1, + "cryptoType": 0, + "timeStamp": 1526625689, + "msgContent": "{\"session\":\"\",\"type\":0,\"start\":0,\"end\":30}" +} \ No newline at end of file diff --git a/Product/user/object_manager/main.c b/Product/user/object_manager/main.c index 6db23cb4b..38c2956f2 100644 --- a/Product/user/object_manager/main.c +++ b/Product/user/object_manager/main.c @@ -19,14 +19,33 @@ #include "regex_table.h" #define MAX_OBJ (300) -#define ADD_JS_FILE ("/home/hx/secogateway/Product/user/object_manager/json_test/add.json") -#define DEL_JS_FILE ("/home/hx/secogateway/Product/user/object_manager/json_test/del.json") -#define MOD_JS_FILE ("/home/hx/secogateway/Product/user/object_manager/json_test/mod.json") +#define JS_FILE_ROOT "/mnt/e/wsl" +#define ADD_JS_FILE (JS_FILE_ROOT"/secogateway/Product/user/object_manager/json_test/add.json") +#define DEL_JS_FILE (JS_FILE_ROOT"/secogateway/Product/user/object_manager/json_test/del.json") +#define MOD_JS_FILE (JS_FILE_ROOT"/secogateway/Product/user/object_manager/json_test/mod.json") +#define QUERY_LIST_JS_FILE (JS_FILE_ROOT"/secogateway/Product/user/object_manager/json_test/page_queue.json") +#define DETAIL_JS_FILE (JS_FILE_ROOT"/secogateway/Product/user/object_manager/json_test/detail.json") + +static PSPLIT_PAGE g_pSplitPage = NULL; static PCMHI_OBJECT g_pObject = NULL; static OBJECT_K g_objItem[MAX_OBJ]; static pthread_mutex_t g_obj_lock = PTHREAD_MUTEX_INITIALIZER; +static void random_string(unsigned char *buf, int size) +{ + int i; + const char *asc_char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + int max = strlen(asc_char); + srand(time(NULL)); + + for(i = 0; i < size && buf; i++) { + buf[i] = asc_char[rand() % max]; + } + + LOG_EX(LOG_Debug, "random string(%d): %s\n", strlen(buf), (char *)buf); +} + static int split_params(char *pInput, char **pFirst, char **pSecond, const char *split) { @@ -78,7 +97,7 @@ static void dump_object(void) LOG_EX(LOG_Info, "items: %d\n", g_objItem[pObj->obj_index].ctx_num); for(i = 0; i < g_objItem[pObj->obj_index].ctx_num; i++) { - struct tm* pTm; + struct tm *pTm; char buf[256]; time_t t; POBJECT_K pObjK = &g_objItem[pObj->obj_index]; @@ -142,6 +161,92 @@ static void dump_object(void) pthread_mutex_unlock(&g_obj_lock); } +int object_split_page(char *pSession, int type, char *outSession) +{ + PSPLIT_PAGE p; + + if(pSession == NULL || strlen(pSession) == 0) { + PCMHI_OBJECT pObj, pTmp; + int sess_type = OBJ_TYPE_MAX; + PSPLIT_PAGE pNew = (PSPLIT_PAGE)malloc(sizeof(SPLIT_PAGE)); + + if(!pNew) { + LOG_EX(LOG_Error, "Malloc memory error\n"); + return -RET_NOMEM; + } + + memset(pNew, 0, sizeof(SPLIT_PAGE)); + random_string(pNew->session, MAX_SESSION - 1); + + if(outSession) { + strcpy(outSession, pNew->session); + } + + HASH_ADD_STR(g_pSplitPage, session, pNew); + + if(type == 1) { + sess_type = OBJ_TYPE_SERVER; + } else if(type == 2) { + sess_type = OBJ_TYPE_ADDR; + } else if(type == 3) { + sess_type = OBJ_TYPE_DATETIME; + } + + pthread_mutex_lock(&g_obj_lock); + HASH_ITER(hh, g_pObject, pObj, pTmp) { + if(g_objItem[pObj->obj_index].type / 10 == sess_type + || sess_type == OBJ_TYPE_MAX) { + char *pStr = strdup(pObj->name); + pNew->last_time = time(NULL); + + if(pNew->name_list == NULL) { + utarray_new(pNew->name_list, &ut_str_icd); + } + + LOG_EX(LOG_Debug, "Add String: %s\n", pStr); + utarray_push_back(pNew->name_list, &pStr); + } + } + pthread_mutex_unlock(&g_obj_lock); + return RET_OK; + } else { + LOG_EX(LOG_Error, "Input params error\n"); + return -RET_INPUTERR; + } +} + +int get_page_items(char *pSession, int type, int start, int end) +{ + int ret = RET_OK; + PSPLIT_PAGE p; + char **pStr; + int i, iCount = 0; + char session[MAX_SESSION]; + + if(pSession == NULL || strlen(pSession) == 0) { + memset(session, 0, MAX_SESSION); + ret = object_split_page(pSession, type, session); + + if(ret != RET_OK) { + return ret; + } + + pSession = session; + } + + HASH_FIND_STR(g_pSplitPage, pSession, p); + + if(p == NULL) { + LOG_EX(LOG_Debug, "Can't found session: %s\n", pSession); + return -RET_NOTFOUND; + } + + for(i = start; i < end && iCount < MAX_PAGE_ITEMS && iCount < utarray_len(p->name_list); i++) { + pStr = (char **)utarray_eltptr(p->name_list, i); + LOG_EX(LOG_Debug, "%s[%03d]: %s\n", p->session, iCount++, *pStr); + } +} + int object_add(PCMHI_OBJECT pObj, POBJECT_K pObjK) { int i, pos; @@ -613,6 +718,7 @@ int object_mod(PCMHI_OBJECT pObj, POBJECT_K pObjK) memcpy(&g_objItem[p->obj_index].objs, &pObjK->objs, sizeof(g_objItem[p->obj_index].objs)); return RET_OK; } + static const char *read_json_file(const char *pPath) { FILE *pFile; @@ -645,6 +751,7 @@ static const char *read_json_file(const char *pPath) pBuf[f_size] = 0; return (const char *)pBuf; } + static void test_del_object(void) { int i, ret; @@ -765,6 +872,7 @@ static void test_del_object(void) free(p); free((void *)pJson); } + static void test_add_object(void) { int i, ret; @@ -857,7 +965,23 @@ static void test_add_object(void) retCtx.data[i].mesg = get_err_message(ret); } - dump_object(); + for(i = 0; i < 100; i++) { + PCMHI_OBJECT pObjItem = malloc(sizeof(CMHI_OBJECT)); + POBJECT_K pObjKItem = malloc(sizeof(OBJECT_K)); + + if(pObjItem && pObjKItem) { + memcpy(pObjItem, pAdd->pCtx[0].pObj, sizeof(CMHI_OBJECT)); + memcpy(pObjKItem, &pAdd->pCtx[i].objk, sizeof(OBJECT_K)); + sprintf(pObjItem->name, "%s_%02d", pAdd->pCtx[0].pObj->name, i); + pObjKItem->type = 1; + pObjKItem->ctx_num = 1; + pObjKItem->objs.svr_obj[0].pro_type = 1; + pObjKItem->objs.svr_obj[0].str_val = strdup("10010-10011"); + object_add(pObjItem, pObjKItem); + } + } + + //dump_object(); retCtx.mesg = get_err_message(retCtx.ret_code); pRetJson = Struct2Json(&retCtx, JE_OBJ_ADD, FALSE, &ret); @@ -899,6 +1023,7 @@ static void test_add_object(void) free(p); free((void *)pJson); } + static void test_mod_object(void) { int i, ret; @@ -925,7 +1050,7 @@ static void test_mod_object(void) retCtx.ret_code = -RET_JSONERR; retCtx.mesg = get_err_message(RET_JSONERR); retCtx.n_items = 0; - pRetJson = Struct2Json(&retCtx, JE_OBJ_ADD, FALSE, &ret); + pRetJson = Struct2Json(&retCtx, JE_OBJ_MOD, FALSE, &ret); if(!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); @@ -942,7 +1067,7 @@ static void test_mod_object(void) LOG_EX(LOG_Info, "cryptoType: %d\n", p->cryptoType); LOG_EX(LOG_Info, "timeStamp: %d\n", p->timeStamp); LOG_EX(LOG_Info, "msgContent: %s\n", p->msgContent); - ret = Json2Struct(p->msgContent, &pAdd, JE_OBJ_ADD, FALSE); + ret = Json2Struct(p->msgContent, &pAdd, JE_OBJ_MOD, FALSE); if(ret != RET_OK || pAdd == NULL || pAdd->pCtx == NULL) { LOG_EX(LOG_Error, "Decode json error: %d\n", ret); @@ -964,7 +1089,7 @@ static void test_mod_object(void) retCtx.ret_code = -RET_JSONERR; retCtx.mesg = get_err_message(RET_JSONERR); retCtx.n_items = 0; - pRetJson = Struct2Json(&retCtx, JE_OBJ_ADD, FALSE, &ret); + pRetJson = Struct2Json(&retCtx, JE_OBJ_MOD, FALSE, &ret); if(!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); @@ -993,7 +1118,7 @@ static void test_mod_object(void) dump_object(); retCtx.mesg = get_err_message(retCtx.ret_code); - pRetJson = Struct2Json(&retCtx, JE_OBJ_ADD, FALSE, &ret); + pRetJson = Struct2Json(&retCtx, JE_OBJ_MOD, FALSE, &ret); if(!pRetJson || ret != RET_OK) { LOG_EX(LOG_Error, "Json format error: %d\n", ret); @@ -1033,6 +1158,7 @@ static void test_mod_object(void) free(p); free((void *)pJson); } + static void test_strsep(char *pVal) { char *pStart = NULL, *pEnd = NULL; @@ -1045,6 +1171,7 @@ static void test_strsep(char *pVal) LOG_EX(LOG_Debug, "[%s] Split to First = %s\n", pVal, pStart); } } + static void test_regex(char *pVal) { if(pcre_match(REGEX_IP_ADDR, pVal)) { @@ -1055,6 +1182,7 @@ static void test_regex(char *pVal) //LOG_EX(LOG_Debug, "Ret : [%s] ==> %d\n", pVal, pcre_match(REGEX_SVR_PORT, pVal)); } + static void test_ipaddr_format(char *pVal) { int ret = 0; @@ -1078,6 +1206,7 @@ static void test_ipaddr_format(char *pVal) LOG_EX(LOG_Debug, "Convert %s to ip address ret: %d\n", pVal, ret); } + static void test_str_time(char *pVal) { struct tm tm; @@ -1110,6 +1239,358 @@ static void test_str_time(char *pVal) strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", &tm); LOG_EX(LOG_Debug, "asctime: %s / %d\n", buf, mktime(&tm)); } + +static void test_random_string(char *pVal) +{ + int first; + unsigned char *pBuf; + + if(!pVal || strlen(pVal) == 0) { + first = 64; + } else { + first = strtoul(pVal, NULL, 10); + } + + pBuf = malloc(first + 1); + LOG_EX(LOG_Debug, "Make %d bytes\n", first); + + if(pBuf) { + memset(pBuf, 0, first + 1); + random_string(pBuf, first); + free(pBuf); + } +} + +static void test_page_object(char *pVal) +{ + int i, ret; + char **pStr; + PSPLIT_PAGE pPage; + PCMHI_OBJECT pObj; + const char *pRetJson; + IFC_RET_PAGE_MSG retCtx; + char session[MAX_SESSION]; + int iCount = 0; + PIFACE_QUERY_LIST pQuery = NULL; + PJSON_INTERFACE p = NULL; + const char *pJson = read_json_file(QUERY_LIST_JS_FILE); + ret = Json2Struct(pJson, &p, JE_INTERFACE, FALSE); + memset(&retCtx, 0, sizeof(IFC_RET_PAGE_MSG)); + + if(ret != RET_OK || p == NULL) { + LOG_EX(LOG_Error, "Decode json error: %d\n", ret); + free((void *)pJson); + + if(p) { + if(p->msgContent) { + free((void *)p->msgContent); + } + + free(p); + } + + retCtx.ret_code = -RET_JSONERR; + retCtx.mesg = get_err_message(RET_JSONERR); + retCtx.n_items = 0; + pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); + + if(!pRetJson || ret != RET_OK) { + LOG_EX(LOG_Error, "Json format error: %d\n", ret); + return; + } + + LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); + free((void *)pRetJson); + return; + } + + LOG_EX(LOG_Info, "cmdId: %d\n", p->cmdId); + LOG_EX(LOG_Info, "ver: %d\n", p->ver); + LOG_EX(LOG_Info, "cryptoType: %d\n", p->cryptoType); + LOG_EX(LOG_Info, "timeStamp: %d\n", p->timeStamp); + LOG_EX(LOG_Info, "msgContent: %s\n", p->msgContent); + ret = Json2Struct(p->msgContent, &pQuery, JE_OBJ_QUERYLIST, FALSE); + + if(ret != RET_OK || pQuery == NULL) { + LOG_EX(LOG_Error, "Decode json error: %d\n", ret); + + if(pQuery) { + free(pQuery); + } + + if(p->msgContent) { + free((void *)p->msgContent); + } + + free(p); + free((void *)pJson); + retCtx.ret_code = -RET_JSONERR; + retCtx.mesg = get_err_message(RET_JSONERR); + retCtx.n_items = 0; + pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); + + if(!pRetJson || ret != RET_OK) { + LOG_EX(LOG_Error, "Json format error: %d\n", ret); + return; + } + + LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); + free((void *)pRetJson); + return; + } + + if(strlen(pQuery->session) == 0) { + memset(pQuery->session, 0, MAX_SESSION); + ret = object_split_page("", pQuery->type, pQuery->session); + + if(ret != RET_OK) { + LOG_EX(LOG_Error, "object_split_page error: %d\n", ret); + + if(pQuery) { + free(pQuery); + } + + if(p->msgContent) { + free((void *)p->msgContent); + } + + free(p); + free((void *)pJson); + retCtx.ret_code = ret; + retCtx.mesg = get_err_message(retCtx.ret_code); + retCtx.n_items = 0; + pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); + + if(!pRetJson || ret != RET_OK) { + LOG_EX(LOG_Error, "Json format error: %d\n", ret); + return; + } + + LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); + free((void *)pRetJson); + return; + } + } + + HASH_FIND_STR(g_pSplitPage, pQuery->session, pPage); + + if(pPage == NULL) { + LOG_EX(LOG_Debug, "Can't found session: %s\n", pQuery->session); + + if(pQuery) { + free(pQuery); + } + + if(p->msgContent) { + free((void *)p->msgContent); + } + + free(p); + free((void *)pJson); + retCtx.ret_code = -RET_NOTFOUND; + retCtx.mesg = get_err_message(retCtx.ret_code); + retCtx.n_items = 0; + pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); + + if(!pRetJson || ret != RET_OK) { + LOG_EX(LOG_Error, "Json format error: %d\n", ret); + return; + } + + LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); + free((void *)pRetJson); + return; + } + + retCtx.ret_code = RET_OK; + retCtx.mesg = get_err_message(retCtx.ret_code); + retCtx.session = pQuery->session; + retCtx.type = pQuery->type; + + if(pPage->name_list) { + retCtx.tolItems = utarray_len(pPage->name_list); + } else { + retCtx.tolItems = 0; + } + + if(pQuery->start > retCtx.tolItems) { + retCtx.start = 0; + } else { + retCtx.start = pQuery->start; + } + + retCtx.n_items = 0; + + for(i = retCtx.start; i < pQuery->end && iCount < MAX_PAGE_ITEMS && iCount < retCtx.tolItems; i++) { + pStr = (char **)utarray_eltptr(pPage->name_list, i); + pthread_mutex_lock(&g_obj_lock); + HASH_FIND_STR(g_pObject, *pStr, pObj); + pthread_mutex_unlock(&g_obj_lock); + + if(pObj) { + retCtx.data[retCtx.n_items].name = pObj->name; + retCtx.data[retCtx.n_items].desc = pObj->desc; + retCtx.data[retCtx.n_items].ref_count = g_objItem[pObj->obj_index].ref_count; + retCtx.data[retCtx.n_items].type = g_objItem[pObj->obj_index].type; + retCtx.n_items++; + } + + LOG_EX(LOG_Debug, "%s[%03d]: %s\n", pQuery->session, iCount++, *pStr); + } + + retCtx.end = retCtx.start + iCount - 1; + + pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYLIST, FALSE, &ret); + + if(!pRetJson || ret != RET_OK) { + LOG_EX(LOG_Error, "Json format error: %d\n", ret); + return; + } + + LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); + free((void *)pRetJson); + + /* char **pStr; + int iCount = 0; + PSPLIT_PAGE p, pTmp; + test_add_object(); + get_page_items("", 1, 30, 100); */ + /* object_split_page("", 1); + HASH_ITER(hh, g_pSplitPage, p, pTmp) { + while((pStr = (char **)utarray_next(p->name_list, pStr))) { + LOG_EX(LOG_Debug, "%s[%03d]: %s\n", p->session, iCount++, *pStr); + } + } */ + + if(p->msgContent) { + free((void *)p->msgContent); + } + + free(p); + free((void *)pJson); +} + +static void test_detail_object(void) +{ + int i, ret; + const char *pRetJson; + IFC_RET_DETAIL_MSG retCtx; + PIFACE_DETAIL_OBJ pDetail = NULL; + PJSON_INTERFACE p = NULL; + const char *pJson = read_json_file(DETAIL_JS_FILE); + ret = Json2Struct(pJson, &p, JE_INTERFACE, FALSE); + memset(&retCtx, 0, sizeof(IFC_RET_DETAIL_MSG)); + + if(ret != RET_OK || p == NULL) { + LOG_EX(LOG_Error, "Decode json error: %d\n", ret); + free((void *)pJson); + + if(p) { + if(p->msgContent) { + free((void *)p->msgContent); + } + + free(p); + } + + retCtx.ret_code = -RET_JSONERR; + retCtx.mesg = get_err_message(RET_JSONERR); + retCtx.n_items = 0; + pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYDETAIL, FALSE, &ret); + + if(!pRetJson || ret != RET_OK) { + LOG_EX(LOG_Error, "Json format error: %d\n", ret); + return; + } + + LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); + free((void *)pRetJson); + return; + } + + LOG_EX(LOG_Info, "cmdId: %d\n", p->cmdId); + LOG_EX(LOG_Info, "ver: %d\n", p->ver); + LOG_EX(LOG_Info, "cryptoType: %d\n", p->cryptoType); + LOG_EX(LOG_Info, "timeStamp: %d\n", p->timeStamp); + LOG_EX(LOG_Info, "msgContent: %s\n", p->msgContent); + ret = Json2Struct(p->msgContent, &pDetail, JE_OBJ_QUERYDETAIL, FALSE); + + if(ret != RET_OK || pDetail == NULL) { + LOG_EX(LOG_Error, "Decode json error: %d\n", ret); + + if(pDetail) { + free(pDetail); + } + + if(p->msgContent) { + free((void *)p->msgContent); + } + + free(p); + free((void *)pJson); + retCtx.ret_code = -RET_JSONERR; + retCtx.mesg = get_err_message(RET_JSONERR); + retCtx.n_items = 0; + pRetJson = Struct2Json(&retCtx, JE_OBJ_QUERYDETAIL, FALSE, &ret); + + if(!pRetJson || ret != RET_OK) { + LOG_EX(LOG_Error, "Json format error: %d\n", ret); + return; + } + + LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); + free((void *)pRetJson); + return; + } else { + retCtx.n_items = pDetail->n_obj; + } + + for(i = 0; i < pDetail->n_obj; i++) { + ret = object_del(pDetail->name[i]); + + if(ret != RET_OK && retCtx.ret_code == RET_OK) { + retCtx.ret_code = ret; + } + + retCtx.data[i].name = pDetail->name[i]; + retCtx.data[i].ret_code = ret; + retCtx.data[i].mesg = get_err_message(ret); + } + + dump_object(); + retCtx.mesg = get_err_message(retCtx.ret_code); + pRetJson = Struct2Json(&retCtx, JE_OBJ_DEL, FALSE, &ret); + + if(!pRetJson || ret != RET_OK) { + LOG_EX(LOG_Error, "Json format error: %d\n", ret); + + if(pRetJson) { + free((void *)pRetJson); + } + + free(pDetail); + + if(p->msgContent) { + free((void *)p->msgContent); + } + + free(p); + free((void *)pJson); + return; + } + + LOG_EX(LOG_Debug, "Respons:\n%s\n", pRetJson); + free((void *)pRetJson); + free(pDetail); + + if(p->msgContent) { + free((void *)p->msgContent); + } + + free(p); + free((void *)pJson); +} + /** * @brief 应用程序主函数 * @param argc 输入参数个数 @@ -1120,14 +1601,15 @@ int main(int argc, char **argv) { int c, optidx = 0; static const struct option long_opts[] = { - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'v' }, + { "help", no_argument, NULL, 'h'}, + { "version", no_argument, NULL, 'v'}, // TODO 添加其它需要处理的参数配置 - { "interface", no_argument, NULL, 'i' }, - { "regex", 1, NULL, 'r' }, - { "split_str", 1, NULL, 's' }, - { "ip_addr", 1, NULL, 'p' }, - { "str_time", 1, NULL, 't' }, + { "interface", no_argument, NULL, 'i'}, + { "regex", required_argument, NULL, 'r'}, + { "split_str", required_argument, NULL, 's'}, + { "ip_addr", required_argument, NULL, 'p'}, + { "str_time", required_argument, NULL, 't'}, + { "page_split", optional_argument, NULL, 'a'}, {NULL, 0, NULL, 0} }; IHW_InitLOG("obj", NULL, TRUE); @@ -1141,7 +1623,7 @@ int main(int argc, char **argv) LOG_EX(LOG_Debug, "SVR_OBJECT = %u bytes\n", sizeof(SVR_OBJECT) * MAX_OBJ_CONTENT); LOG_EX(LOG_Debug, "ADDR_OBJECT = %u bytes\n", sizeof(ADDR_OBJECT) * MAX_OBJ_CONTENT); - while((c = getopt_long(argc, argv, "hvir:s:p:t:", long_opts, &optidx)) != -1) { + while((c = getopt_long(argc, argv, "ir:s:p:t:a::hv", long_opts, &optidx)) != -1) { switch(c) { case 'v': LOG_EX(LOG_Info, "User demo version: %s(%s)\n", sGATE_GIT_TAGS, sGATE_GIT_VERS); @@ -1160,7 +1642,7 @@ int main(int argc, char **argv) case 'i': test_add_object(); //test_del_object(); - test_mod_object(); + //test_mod_object(); break; case 's': @@ -1178,6 +1660,11 @@ int main(int argc, char **argv) case 't': test_str_time(optarg); break; + + case 'a': + test_add_object(); + test_page_object(optarg); + break; } } diff --git a/Product/user/object_manager/object_manager.h b/Product/user/object_manager/object_manager.h index 435b600ca..eb2abaea1 100644 --- a/Product/user/object_manager/object_manager.h +++ b/Product/user/object_manager/object_manager.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "../../common/common.h" #define MAX_OBJ_CONTENT (20) @@ -106,4 +107,13 @@ typedef struct { UT_hash_handle hh; ///< Hash链表头 } CMHI_OBJECT, *PCMHI_OBJECT; +typedef struct { + char session[MAX_SESSION]; ///< 对象分页sessionS + + UT_array *name_list; + long last_time; + + UT_hash_handle hh; ///< Hash链表头 +} SPLIT_PAGE, *PSPLIT_PAGE; + #endif \ No newline at end of file