538 lines
14 KiB
C++
538 lines
14 KiB
C++
/**
|
||
* @file XNMonitorInterface.cpp
|
||
* @brief 监控服务器接口实现
|
||
*/
|
||
#include "XNMonitorInterface.h"
|
||
#include "SystemInfoMonitor.h"
|
||
#include "ModelInfoMonitor.h"
|
||
#include "SystemControl.h"
|
||
|
||
// 全局变量
|
||
static bool g_initialized = false;
|
||
|
||
SystemInfoMonitor *systemInfoMonitor = nullptr;
|
||
bool g_systemInfoMonitorStarted = false;
|
||
ModelInfoMonitor *modelInfoMonitor = nullptr;
|
||
bool g_modelInfoMonitorStarted = false;
|
||
SystemControl *systemControl = nullptr;
|
||
bool g_systemControlStarted = 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;
|
||
}
|
||
|
||
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) {
|
||
strncpy(errorMsg, "System Info Monitor Started Successfully", errorMsgSize - 1);
|
||
errorMsg[errorMsgSize - 1] = '\0';
|
||
}
|
||
return 0;
|
||
} else {
|
||
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) {
|
||
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;
|
||
}
|
||
}
|
||
|
||
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) {
|
||
// 停止并清理系统信息监控
|
||
if (g_systemInfoMonitorStarted) {
|
||
XN_StopMonitorSystemInfo();
|
||
}
|
||
// 停止并清理模型信息监控
|
||
if (g_modelInfoMonitorStarted) {
|
||
XN_StopMonitorModelInfo();
|
||
}
|
||
// 清理DDS参与者
|
||
TopicManager::cleanupParticipant();
|
||
g_initialized = false;
|
||
}
|
||
}
|
||
|
||
int XN_InitializeEngineControl(char *errorMsg, int errorMsgSize)
|
||
{
|
||
if (g_systemControlStarted) {
|
||
if (errorMsg && errorMsgSize > 0) {
|
||
strncpy(errorMsg, "Engine Control Already Started", errorMsgSize - 1);
|
||
errorMsg[errorMsgSize - 1] = '\0';
|
||
}
|
||
return 0;
|
||
}
|
||
systemControl = new SystemControl();
|
||
std::string ret = systemControl->Initialize();
|
||
if (ret == "Success") {
|
||
g_systemControlStarted = true;
|
||
if (errorMsg && errorMsgSize > 0) {
|
||
strncpy(errorMsg, "Engine Control Initialized Successfully", errorMsgSize - 1);
|
||
errorMsg[errorMsgSize - 1] = '\0';
|
||
}
|
||
return 0;
|
||
} else {
|
||
delete systemControl;
|
||
systemControl = nullptr;
|
||
if (errorMsg && errorMsgSize > 0) {
|
||
strncpy(errorMsg, ret.c_str(), errorMsgSize - 1);
|
||
errorMsg[errorMsgSize - 1] = '\0';
|
||
}
|
||
return -1;
|
||
}
|
||
}
|
||
|
||
void XN_PauseEngine(char *errorMsg, int errorMsgSize)
|
||
{
|
||
if (!g_systemControlStarted) {
|
||
if (errorMsg && errorMsgSize > 0) {
|
||
strncpy(errorMsg, "Engine Control Not Started", errorMsgSize - 1);
|
||
errorMsg[errorMsgSize - 1] = '\0';
|
||
}
|
||
return;
|
||
}
|
||
systemControl->Pause();
|
||
}
|
||
|
||
void XN_ResumeEngine(char *errorMsg, int errorMsgSize)
|
||
{
|
||
if (!g_systemControlStarted) {
|
||
if (errorMsg && errorMsgSize > 0) {
|
||
strncpy(errorMsg, "Engine Control Not Started", errorMsgSize - 1);
|
||
errorMsg[errorMsgSize - 1] = '\0';
|
||
}
|
||
return;
|
||
}
|
||
systemControl->Resume();
|
||
}
|
||
|
||
void XN_StopEngine(char *errorMsg, int errorMsgSize)
|
||
{
|
||
if (!g_systemControlStarted) {
|
||
if (errorMsg && errorMsgSize > 0) {
|
||
strncpy(errorMsg, "Engine Control Not Started", errorMsgSize - 1);
|
||
errorMsg[errorMsgSize - 1] = '\0';
|
||
}
|
||
return;
|
||
}
|
||
systemControl->Stop();
|
||
}
|
||
|
||
// // 注册发布者实现
|
||
// 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();
|
||
// }
|