提交 48bed202 编写于 作者: M Minghao Li

sync encode test

上级 d5732003
...@@ -23,6 +23,7 @@ extern "C" { ...@@ -23,6 +23,7 @@ extern "C" {
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "cJSON.h"
#include "sync.h" #include "sync.h"
#include "syncRaftEntry.h" #include "syncRaftEntry.h"
#include "taosdef.h" #include "taosdef.h"
...@@ -52,7 +53,7 @@ typedef struct SyncPing { ...@@ -52,7 +53,7 @@ typedef struct SyncPing {
SRaftId srcId; SRaftId srcId;
SRaftId destId; SRaftId destId;
uint32_t dataLen; uint32_t dataLen;
char* data; char data[];
} SyncPing; } SyncPing;
#define SYNC_PING_FIX_LEN (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) #define SYNC_PING_FIX_LEN (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t))
...@@ -63,7 +64,7 @@ typedef struct SyncPingReply { ...@@ -63,7 +64,7 @@ typedef struct SyncPingReply {
SRaftId srcId; SRaftId srcId;
SRaftId destId; SRaftId destId;
uint32_t dataLen; uint32_t dataLen;
char* data; char data[];
} SyncPingReply; } SyncPingReply;
typedef struct SyncClientRequest { typedef struct SyncClientRequest {
...@@ -128,6 +129,8 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing); ...@@ -128,6 +129,8 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing);
void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg); void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg);
cJSON* syncPing2Json(const SyncPing* pSyncPing);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -41,9 +41,11 @@ void syncPingSerialize(const SyncPing* pSyncPing, char* buf, uint32_t bufLen) { ...@@ -41,9 +41,11 @@ void syncPingSerialize(const SyncPing* pSyncPing, char* buf, uint32_t bufLen) {
} }
void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) {
/*
uint32_t* pU32 = (uint32_t*)buf; uint32_t* pU32 = (uint32_t*)buf;
uint32_t bytes = *pU32; uint32_t bytes = *pU32;
pSyncPing = (SyncPing*)malloc(bytes); pSyncPing = (SyncPing*)malloc(bytes);
*/
memcpy(pSyncPing, buf, len); memcpy(pSyncPing, buf, len);
assert(len == pSyncPing->bytes); assert(len == pSyncPing->bytes);
assert(pSyncPing->bytes == SYNC_PING_FIX_LEN + pSyncPing->dataLen); assert(pSyncPing->bytes == SYNC_PING_FIX_LEN + pSyncPing->dataLen);
...@@ -60,23 +62,28 @@ void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg) { ...@@ -60,23 +62,28 @@ void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg) {
free(buf); free(buf);
} }
/* cJSON* syncPing2Json(const SyncPing* pSyncPing) {
typedef struct SRaftId { cJSON* pRoot = cJSON_CreateObject();
SyncNodeId addr; // typedef uint64_t SyncNodeId; cJSON_AddNumberToObject(pRoot, "bytes", pSyncPing->bytes);
SyncGroupId vgId; // typedef int32_t SyncGroupId; cJSON_AddNumberToObject(pRoot, "msgType", pSyncPing->msgType);
} SRaftId;
typedef struct SyncPing {
uint32_t bytes;
uint32_t msgType;
SRaftId srcId;
SRaftId destId;
uint32_t dataLen;
char* data;
} SyncPing;
*/
/* cJSON* pSrcId = cJSON_CreateObject();
cJSON_AddNumberToObject(pSrcId, "addr", pSyncPing->srcId.addr);
cJSON_AddNumberToObject(pSrcId, "vgId", pSyncPing->srcId.vgId);
cJSON_AddItemToObject(pRoot, "srcId", pSrcId);
cJSON* pDestId = cJSON_CreateObject();
cJSON_AddNumberToObject(pDestId, "addr", pSyncPing->destId.addr);
cJSON_AddNumberToObject(pDestId, "vgId", pSyncPing->destId.vgId);
cJSON_AddItemToObject(pRoot, "srcId", pDestId);
cJSON_AddNumberToObject(pRoot, "dataLen", pSyncPing->dataLen);
cJSON_AddStringToObject(pRoot, "data", pSyncPing->data);
return pRoot;
}
#if 0
void syncPingSerialize(const SyncPing* pSyncPing, char** ppBuf, uint32_t* bufLen) { void syncPingSerialize(const SyncPing* pSyncPing, char** ppBuf, uint32_t* bufLen) {
*bufLen = sizeof(SyncPing) + pSyncPing->dataLen; *bufLen = sizeof(SyncPing) + pSyncPing->dataLen;
*ppBuf = (char*)malloc(*bufLen); *ppBuf = (char*)malloc(*bufLen);
...@@ -144,4 +151,4 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { ...@@ -144,4 +151,4 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) {
pStart = taosDecodeFixedU32(pStart, &u32); pStart = taosDecodeFixedU32(pStart, &u32);
pSyncPing->dataLen = u32; pSyncPing->dataLen = u32;
} }
*/ #endif
\ No newline at end of file \ No newline at end of file
add_executable(syncTest "") add_executable(syncTest "")
add_executable(syncEnvTest "") add_executable(syncEnvTest "")
add_executable(syncPingTest "") add_executable(syncPingTest "")
add_executable(syncEncodeTest "")
target_sources(syncTest target_sources(syncTest
...@@ -15,6 +16,10 @@ target_sources(syncPingTest ...@@ -15,6 +16,10 @@ target_sources(syncPingTest
PRIVATE PRIVATE
"syncPingTest.cpp" "syncPingTest.cpp"
) )
target_sources(syncEncodeTest
PRIVATE
"syncEncodeTest.cpp"
)
target_include_directories(syncTest target_include_directories(syncTest
...@@ -32,6 +37,11 @@ target_include_directories(syncPingTest ...@@ -32,6 +37,11 @@ target_include_directories(syncPingTest
"${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_SOURCE_DIR}/include/libs/sync"
"${CMAKE_CURRENT_SOURCE_DIR}/../inc" "${CMAKE_CURRENT_SOURCE_DIR}/../inc"
) )
target_include_directories(syncEncodeTest
PUBLIC
"${CMAKE_SOURCE_DIR}/include/libs/sync"
"${CMAKE_CURRENT_SOURCE_DIR}/../inc"
)
target_link_libraries(syncTest target_link_libraries(syncTest
...@@ -46,6 +56,10 @@ target_link_libraries(syncPingTest ...@@ -46,6 +56,10 @@ target_link_libraries(syncPingTest
sync sync
gtest_main gtest_main
) )
target_link_libraries(syncEncodeTest
sync
gtest_main
)
enable_testing() enable_testing()
......
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncMessage.h"
void logTest() {
sTrace("--- sync log test: trace");
sDebug("--- sync log test: debug");
sInfo("--- sync log test: info");
sWarn("--- sync log test: warn");
sError("--- sync log test: error");
sFatal("--- sync log test: fatal");
}
#define PING_MSG_LEN 20
int main() {
// taosInitLog((char*)"syncPingTest.log", 100000, 10);
tsAsyncLog = 0;
sDebugFlag = 143 + 64;
char msg[PING_MSG_LEN];
snprintf(msg, sizeof(msg), "%s", "test ping");
SyncPing* pSyncPing = syncPingBuild(PING_MSG_LEN);
pSyncPing->srcId.addr = 1;
pSyncPing->srcId.vgId = 2;
pSyncPing->destId.addr = 3;
pSyncPing->destId.vgId = 4;
memcpy(pSyncPing->data, msg, PING_MSG_LEN);
{
cJSON* pJson = syncPing2Json(pSyncPing);
char* serialized = cJSON_Print(pJson);
printf("SyncPing: \n%s\n\n", serialized);
free(serialized);
cJSON_Delete(pJson);
}
uint32_t bufLen = pSyncPing->bytes;
char* buf = (char*)malloc(bufLen);
syncPingSerialize(pSyncPing, buf, bufLen);
SyncPing* pSyncPing2 = (SyncPing*)malloc(pSyncPing->bytes);
syncPingDeserialize(buf, bufLen, pSyncPing2);
{
cJSON* pJson = syncPing2Json(pSyncPing2);
char* serialized = cJSON_Print(pJson);
printf("SyncPing2: \n%s\n\n", serialized);
free(serialized);
cJSON_Delete(pJson);
}
syncPingDestroy(pSyncPing);
syncPingDestroy(pSyncPing2);
free(buf);
return 0;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册