/** * @file XNMonitorInterface.cpp * @brief 监控服务器接口实现 */ #include "XNMonitorInterface.h" #include "TopicManager.h" #include #include #include "SystemInfoMonitor.h" // 全局变量 static std::string g_lastError; static std::mutex g_errorMutex; static bool g_initialized = false; SystemInfoMonitor *systemInfoMonitor = nullptr; bool g_systemInfoMonitorStarted = false; // 设置错误信息 static void SetLastError(const char *error) { std::lock_guard lock(g_errorMutex); g_lastError = error; } // 初始化函数实现 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(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; } try { systemInfoMonitor = new SystemInfoMonitor(); std::string ret = systemInfoMonitor->Initialize(); if (ret == "Success") { g_systemInfoMonitorStarted = true; if (errorMsg && errorMsgSize > 0) { strncpy(errorMsg, ret.c_str(), errorMsgSize - 1); errorMsg[errorMsgSize - 1] = '\0'; } return 0; } else { if (errorMsg && errorMsgSize > 0) { strncpy(errorMsg, ret.c_str(), errorMsgSize - 1); errorMsg[errorMsgSize - 1] = '\0'; } return -1; } } catch (const std::exception &e) { 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; } } // 清理函数实现 void XN_Cleanup() { if (g_initialized) { 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(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(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(callback, userData); // // TopicManager::Instance()->registerSubscriber( // // 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 lock(g_errorMutex); // return g_lastError.c_str(); // }