XNSim/XNMonitorServer/XNMonitorInterface.cpp

473 lines
12 KiB
C++
Raw Normal View History

/**
* @file XNMonitorInterface.cpp
* @brief
*/
#include "XNMonitorInterface.h"
#include "TopicManager.h"
#include <string>
#include <mutex>
#include "SystemInfoMonitor.h"
2025-05-14 20:18:44 +08:00
#include "ModelInfoMonitor.h"
// 全局变量
static bool g_initialized = false;
SystemInfoMonitor *systemInfoMonitor = nullptr;
bool g_systemInfoMonitorStarted = false;
2025-05-14 20:18:44 +08:00
ModelInfoMonitor *modelInfoMonitor = nullptr;
bool g_modelInfoMonitorStarted = false;
// 初始化函数实现
int XN_Initialize(const char *domainId, int domainIdLen, char *errorMsg, int errorMsgSize)
{
if (g_initialized) {
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, "DDSMonitor Initialized Successfully", errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return 0;
}
try {
if (!domainId || domainIdLen <= 0) {
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, "Invalid domainId", errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
// 创建临时字符串确保以null结尾
std::string domainIdStr(domainId, domainIdLen);
int domainIdInt = std::stoi(domainIdStr);
if (domainIdInt < 0 || domainIdInt > 232) {
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, "Invalid domainId", errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
XNDDSErrorCode ret = TopicManager::Instance()->initializeParticipant(domainIdInt);
if (ret != XNDDSErrorCode::SUCCESS) {
if (errorMsg && errorMsgSize > 0) {
std::string error = "Failed to initialize DDSMonitor, error code: "
+ std::to_string(static_cast<int>(ret));
strncpy(errorMsg, error.c_str(), errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
g_initialized = true;
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, "DDSMonitor Initialized Successfully", errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return 0;
} catch (const std::exception &e) {
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, e.what(), errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
}
int XN_StartMonitorSystemInfo(char *errorMsg, int errorMsgSize)
{
if (!g_initialized) {
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, "DDSMonitor Not Initialized", errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
2025-05-14 20:18:44 +08:00
if (g_systemInfoMonitorStarted) {
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, "System Info Monitor Already Started", errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
try {
systemInfoMonitor = new SystemInfoMonitor();
std::string ret = systemInfoMonitor->Initialize();
if (ret == "Success") {
g_systemInfoMonitorStarted = true;
if (errorMsg && errorMsgSize > 0) {
2025-05-14 20:18:44 +08:00
strncpy(errorMsg, "System Info Monitor Started Successfully", errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return 0;
} else {
2025-05-14 20:18:44 +08:00
delete systemInfoMonitor;
systemInfoMonitor = nullptr;
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, ret.c_str(), errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
} catch (const std::exception &e) {
2025-05-14 20:18:44 +08:00
if (systemInfoMonitor) {
delete systemInfoMonitor;
systemInfoMonitor = nullptr;
}
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, e.what(), errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
}
int XN_GetSystemInfo(char *infoMsg, int infoMsgSize)
{
if (!g_systemInfoMonitorStarted) {
if (infoMsg && infoMsgSize > 0) {
strncpy(infoMsg, "System Info Monitor Not Started", infoMsgSize - 1);
infoMsg[infoMsgSize - 1] = '\0';
}
return -1;
}
try {
std::string info = systemInfoMonitor->GetSystemInfo();
if (infoMsg && infoMsgSize > 0) {
strncpy(infoMsg, info.c_str(), infoMsgSize - 1);
infoMsg[infoMsgSize - 1] = '\0';
}
return 0;
} catch (const std::exception &e) {
if (infoMsg && infoMsgSize > 0) {
strncpy(infoMsg, e.what(), infoMsgSize - 1);
infoMsg[infoMsgSize - 1] = '\0';
}
return -1;
}
}
int XN_GetAllThreadInfo(char *infoMsg, int infoMsgSize)
{
if (!g_systemInfoMonitorStarted) {
if (infoMsg && infoMsgSize > 0) {
strncpy(infoMsg, "System Info Monitor Not Started", infoMsgSize - 1);
infoMsg[infoMsgSize - 1] = '\0';
}
return -1;
}
try {
std::string info = systemInfoMonitor->GetAllThreadInfo();
if (infoMsg && infoMsgSize > 0) {
strncpy(infoMsg, info.c_str(), infoMsgSize - 1);
infoMsg[infoMsgSize - 1] = '\0';
}
return 0;
} catch (const std::exception &e) {
if (infoMsg && infoMsgSize > 0) {
strncpy(infoMsg, e.what(), infoMsgSize - 1);
infoMsg[infoMsgSize - 1] = '\0';
}
return -1;
}
}
void XN_StopMonitorSystemInfo()
{
if (g_systemInfoMonitorStarted) {
delete systemInfoMonitor;
systemInfoMonitor = nullptr;
g_systemInfoMonitorStarted = false;
}
}
2025-05-14 20:18:44 +08:00
int XN_StartMonitorModelInfo(char *errorMsg, int errorMsgSize)
{
if (!g_initialized) {
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, "DDSMonitor Not Initialized", errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
if (g_modelInfoMonitorStarted) {
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, "Model Info Monitor Already Started", errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
try {
modelInfoMonitor = new ModelInfoMonitor();
std::string ret = modelInfoMonitor->Initialize();
if (ret == "Success") {
g_modelInfoMonitorStarted = true;
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, "Model Info Monitor Started Successfully", errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return 0;
} else {
delete modelInfoMonitor;
modelInfoMonitor = nullptr;
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, ret.c_str(), errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
} catch (const std::exception &e) {
if (modelInfoMonitor) {
delete modelInfoMonitor;
modelInfoMonitor = nullptr;
}
if (errorMsg && errorMsgSize > 0) {
strncpy(errorMsg, e.what(), errorMsgSize - 1);
errorMsg[errorMsgSize - 1] = '\0';
}
return -1;
}
}
int XN_GetModelInfo(char *infoMsg, int infoMsgSize)
{
if (!g_modelInfoMonitorStarted) {
if (infoMsg && infoMsgSize > 0) {
strncpy(infoMsg, "Model Info Monitor Not Started", infoMsgSize - 1);
infoMsg[infoMsgSize - 1] = '\0';
}
return -1;
}
try {
std::string info = modelInfoMonitor->GetAllModelInfo();
if (infoMsg && infoMsgSize > 0) {
strncpy(infoMsg, info.c_str(), infoMsgSize - 1);
infoMsg[infoMsgSize - 1] = '\0';
}
return 0;
} catch (const std::exception &e) {
if (infoMsg && infoMsgSize > 0) {
strncpy(infoMsg, e.what(), infoMsgSize - 1);
infoMsg[infoMsgSize - 1] = '\0';
}
return -1;
}
}
void XN_StopMonitorModelInfo()
{
if (g_modelInfoMonitorStarted) {
delete modelInfoMonitor;
modelInfoMonitor = nullptr;
g_modelInfoMonitorStarted = false;
}
}
// 清理函数实现
void XN_Cleanup()
{
if (g_initialized) {
2025-05-14 20:18:44 +08:00
// 停止并清理系统信息监控
if (g_systemInfoMonitorStarted) {
XN_StopMonitorSystemInfo();
}
// 停止并清理模型信息监控
if (g_modelInfoMonitorStarted) {
XN_StopMonitorModelInfo();
}
// 清理DDS参与者
TopicManager::cleanupParticipant();
g_initialized = false;
}
}
// // 注册发布者实现
// XNDDSErrorCode XN_RegisterPublisher(const char *topicName, void **dataWriter)
// {
// if (!g_initialized) {
// SetLastError("Not initialized");
// return XNDDSErrorCode::NOT_INITIALIZED;
// }
// if (!topicName || !dataWriter) {
// SetLastError("Invalid parameters");
// return XNDDSErrorCode::INVALID_PARAM;
// }
// try {
// auto *writer =
// TopicManager::Instance()->registerPublisher<XNRuntimeData>(topicName);
// if (!writer) {
// SetLastError("Failed to create publisher");
// return XNDDSErrorCode::PUBLISHER_CREATE_FAILED;
// }
// *dataWriter = writer;
// return XNDDSErrorCode::SUCCESS;
// } catch (const std::exception &e) {
// SetLastError(e.what());
// return XNDDSErrorCode::PUBLISHER_CREATE_FAILED;
// }
// }
// // 注销发布者实现
// XNDDSErrorCode XN_UnregisterPublisher(const char *topicName)
// {
// if (!g_initialized) {
// SetLastError("Not initialized");
// return XNDDSErrorCode::NOT_INITIALIZED;
// }
// if (!topicName) {
// SetLastError("Invalid parameters");
// return XNDDSErrorCode::INVALID_PARAM;
// }
// try {
// TopicManager::Instance()->unregisterPublisher(topicName);
// return XNDDSErrorCode::SUCCESS;
// } catch (const std::exception &e) {
// SetLastError(e.what());
// return XNDDSErrorCode::INVALID_PARAM;
// }
// }
// // 数据写入实现
// XNDDSErrorCode XN_WriteData(void *dataWriter, const void *data, size_t dataSize)
// {
// if (!g_initialized) {
// SetLastError("Not initialized");
// return XNDDSErrorCode::NOT_INITIALIZED;
// }
// if (!dataWriter || !data || dataSize != sizeof(XNRuntimeData)) {
// SetLastError("Invalid parameters");
// return XNDDSErrorCode::INVALID_PARAM;
// }
// try {
// auto *writer = static_cast<XNDataWriter *>(dataWriter);
// if (writer->write(data) != eprosima::fastdds::dds::RETCODE_OK) {
// SetLastError("Failed to write data");
// return XNDDSErrorCode::INVALID_PARAM;
// }
// return XNDDSErrorCode::SUCCESS;
// } catch (const std::exception &e) {
// SetLastError(e.what());
// return XNDDSErrorCode::INVALID_PARAM;
// }
// }
// // 订阅回调包装器
// class SubscriberCallbackWrapper
// {
// public:
// SubscriberCallbackWrapper(XNDataCallback callback, void *userData)
// : callback_(callback), userData_(userData)
// {
// }
// void operator()(const XNRuntimeData &data)
// {
// if (callback_) {
// callback_(&data, sizeof(XNRuntimeData), userData_);
// }
// }
// private:
// XNDataCallback callback_;
// void *userData_;
// };
// // 注册订阅者实现
// XNDDSErrorCode XN_RegisterSubscriber(const char *topicName, XNDataCallback callback, void *userData)
// {
// if (!g_initialized) {
// SetLastError("Not initialized");
// return XNDDSErrorCode::NOT_INITIALIZED;
// }
// if (!topicName || !callback) {
// SetLastError("Invalid parameters");
// return XNDDSErrorCode::INVALID_PARAM;
// }
// try {
// // auto wrapper = std::make_shared<SubscriberCallbackWrapper>(callback, userData);
// // TopicManager::Instance()->registerSubscriber<XNRuntimeData>(
// // topicName, [wrapper](const XNRuntimeData &data) { (*wrapper)(data); });
// return XNDDSErrorCode::SUCCESS;
// } catch (const std::exception &e) {
// SetLastError(e.what());
// return XNDDSErrorCode::SUBSCRIBER_CREATE_FAILED;
// }
// }
// // 注销订阅者实现
// XNDDSErrorCode XN_UnregisterSubscriber(const char *topicName)
// {
// if (!g_initialized) {
// SetLastError("Not initialized");
// return XNDDSErrorCode::NOT_INITIALIZED;
// }
// if (!topicName) {
// SetLastError("Invalid parameters");
// return XNDDSErrorCode::INVALID_PARAM;
// }
// try {
// TopicManager::Instance()->unregisterSubscriber(topicName);
// return XNDDSErrorCode::SUCCESS;
// } catch (const std::exception &e) {
// SetLastError(e.what());
// return XNDDSErrorCode::INVALID_PARAM;
// }
// }
// // 获取运行时数据实现
// XNDDSErrorCode XN_GetRuntimeData(const char *name, XNRuntimeData *data)
// {
// if (!g_initialized) {
// SetLastError("Not initialized");
// return XNDDSErrorCode::NOT_INITIALIZED;
// }
// if (!name || !data) {
// SetLastError("Invalid parameters");
// return XNDDSErrorCode::INVALID_PARAM;
// }
// // TODO: 实现从数据存储中获取运行时数据
// return XNDDSErrorCode::SUCCESS;
// }
// // 设置运行时数据实现
// XNDDSErrorCode XN_SetRuntimeData(const char *name, const XNRuntimeData *data)
// {
// if (!g_initialized) {
// SetLastError("Not initialized");
// return XNDDSErrorCode::NOT_INITIALIZED;
// }
// if (!name || !data) {
// SetLastError("Invalid parameters");
// return XNDDSErrorCode::INVALID_PARAM;
// }
// // TODO: 实现将运行时数据保存到数据存储中
// return XNDDSErrorCode::SUCCESS;
// }
// // 获取最后错误信息实现
// const char *XN_GetLastError()
// {
// std::lock_guard<std::mutex> lock(g_errorMutex);
// return g_lastError.c_str();
// }