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

sync encode test

上级 d5732003
......@@ -23,6 +23,7 @@ extern "C" {
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
#include "sync.h"
#include "syncRaftEntry.h"
#include "taosdef.h"
......@@ -52,7 +53,7 @@ typedef struct SyncPing {
SRaftId srcId;
SRaftId destId;
uint32_t dataLen;
char* data;
char data[];
} SyncPing;
#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 {
SRaftId srcId;
SRaftId destId;
uint32_t dataLen;
char* data;
char data[];
} SyncPingReply;
typedef struct SyncClientRequest {
......@@ -128,6 +129,8 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing);
void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg);
cJSON* syncPing2Json(const SyncPing* pSyncPing);
#ifdef __cplusplus
}
#endif
......
......@@ -41,9 +41,11 @@ void syncPingSerialize(const SyncPing* pSyncPing, char* buf, uint32_t bufLen) {
}
void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) {
/*
uint32_t* pU32 = (uint32_t*)buf;
uint32_t bytes = *pU32;
pSyncPing = (SyncPing*)malloc(bytes);
*/
memcpy(pSyncPing, buf, len);
assert(len == pSyncPing->bytes);
assert(pSyncPing->bytes == SYNC_PING_FIX_LEN + pSyncPing->dataLen);
......@@ -60,23 +62,28 @@ void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg) {
free(buf);
}
/*
typedef struct SRaftId {
SyncNodeId addr; // typedef uint64_t SyncNodeId;
SyncGroupId vgId; // typedef int32_t SyncGroupId;
} SRaftId;
typedef struct SyncPing {
uint32_t bytes;
uint32_t msgType;
SRaftId srcId;
SRaftId destId;
uint32_t dataLen;
char* data;
} SyncPing;
*/
cJSON* syncPing2Json(const SyncPing* pSyncPing) {
cJSON* pRoot = cJSON_CreateObject();
cJSON_AddNumberToObject(pRoot, "bytes", pSyncPing->bytes);
cJSON_AddNumberToObject(pRoot, "msgType", pSyncPing->msgType);
/*
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) {
*bufLen = sizeof(SyncPing) + pSyncPing->dataLen;
*ppBuf = (char*)malloc(*bufLen);
......@@ -144,4 +151,4 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) {
pStart = taosDecodeFixedU32(pStart, &u32);
pSyncPing->dataLen = u32;
}
*/
\ No newline at end of file
#endif
\ No newline at end of file
add_executable(syncTest "")
add_executable(syncEnvTest "")
add_executable(syncPingTest "")
add_executable(syncEncodeTest "")
target_sources(syncTest
......@@ -15,6 +16,10 @@ target_sources(syncPingTest
PRIVATE
"syncPingTest.cpp"
)
target_sources(syncEncodeTest
PRIVATE
"syncEncodeTest.cpp"
)
target_include_directories(syncTest
......@@ -32,6 +37,11 @@ target_include_directories(syncPingTest
"${CMAKE_SOURCE_DIR}/include/libs/sync"
"${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
......@@ -46,6 +56,10 @@ target_link_libraries(syncPingTest
sync
gtest_main
)
target_link_libraries(syncEncodeTest
sync
gtest_main
)
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.
先完成此消息的编辑!
想要评论请 注册