161 lines
4.7 KiB
C++
161 lines
4.7 KiB
C++
|
#include "MonitorThread.h"
|
||
|
#include "DataReaderListenerImpl.h"
|
||
|
#include <QProcess>
|
||
|
#include <QDebug>
|
||
|
|
||
|
MonitorThread::MonitorThread(QObject *parent) : QThread(parent)
|
||
|
{
|
||
|
isAbort = false;
|
||
|
int m_DomainID = 10;
|
||
|
eprosima::fastdds::dds::DomainParticipantQos participantQos;
|
||
|
participantQos.name("XNRunner");
|
||
|
m_Participant =
|
||
|
eprosima::fastdds::dds::DomainParticipantFactory::get_instance()->create_participant(
|
||
|
m_DomainID, participantQos);
|
||
|
eprosima::fastdds::dds::TypeSupport typeSupport(
|
||
|
new XNSim::XNSimStatus::XNEngineStatusPubSubType());
|
||
|
typeSupport.register_type(m_Participant);
|
||
|
m_EngineStatusTopic = m_Participant->create_topic("XNSim::XNSimStatus::XNEngineStatus",
|
||
|
typeSupport.get_type_name(),
|
||
|
eprosima::fastdds::dds::TOPIC_QOS_DEFAULT);
|
||
|
if (m_EngineStatusTopic == nullptr) {
|
||
|
qInfo() << "Failed to create engine status topic";
|
||
|
return;
|
||
|
}
|
||
|
m_EngineStatusSubscriber =
|
||
|
m_Participant->create_subscriber(eprosima::fastdds::dds::SUBSCRIBER_QOS_DEFAULT);
|
||
|
if (m_EngineStatusSubscriber == nullptr) {
|
||
|
qInfo() << "Failed to create engine status subscriber";
|
||
|
return;
|
||
|
}
|
||
|
eprosima::fastdds::dds::DataReaderQos dataReaderQos;
|
||
|
dataReaderQos.durability().kind = eprosima::fastdds::dds::VOLATILE_DURABILITY_QOS;
|
||
|
auto func = std::bind(&MonitorThread::OnReceiveSimStatus, this, std::placeholders::_1);
|
||
|
listener = new DataReaderListenerImpl<XNSim::XNSimStatus::XNEngineStatus>(func);
|
||
|
m_EngineStatusDataReader =
|
||
|
m_EngineStatusSubscriber->create_datareader(m_EngineStatusTopic, dataReaderQos, listener);
|
||
|
if (m_EngineStatusDataReader == nullptr) {
|
||
|
qInfo() << "Failed to create engine status datareader";
|
||
|
}
|
||
|
|
||
|
eprosima::fastdds::dds::TypeSupport typeSupport2(
|
||
|
new XNSim::XNSimControl::XNRuntimeControlPubSubType());
|
||
|
typeSupport2.register_type(m_Participant);
|
||
|
m_ControlTopic = m_Participant->create_topic("XNSim::XNSimControl::XNRuntimeControl",
|
||
|
typeSupport2.get_type_name(),
|
||
|
eprosima::fastdds::dds::TOPIC_QOS_DEFAULT);
|
||
|
if (m_ControlTopic == nullptr) {
|
||
|
qInfo() << "Failed to create control topic";
|
||
|
return;
|
||
|
}
|
||
|
m_ControlPublisher =
|
||
|
m_Participant->create_publisher(eprosima::fastdds::dds::PUBLISHER_QOS_DEFAULT);
|
||
|
if (m_ControlPublisher == nullptr) {
|
||
|
qInfo() << "Failed to create control publisher";
|
||
|
return;
|
||
|
}
|
||
|
eprosima::fastdds::dds::DataWriterQos dataWriterQos;
|
||
|
dataWriterQos.durability().kind = eprosima::fastdds::dds::VOLATILE_DURABILITY_QOS;
|
||
|
m_ControlDataWriter = m_ControlPublisher->create_datawriter(m_ControlTopic, dataWriterQos);
|
||
|
if (m_ControlDataWriter == nullptr) {
|
||
|
qInfo() << "Failed to create control datawriter";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
MonitorThread::~MonitorThread()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void MonitorThread::OnReceiveSimStatus(const XNSim::XNSimStatus::XNEngineStatus &status)
|
||
|
{
|
||
|
QMutexLocker locker(&m_Mutex);
|
||
|
m_RunStatusUpdate = true;
|
||
|
if (m_RunStatus != status.XNEngineSt()) {
|
||
|
m_RunStatus = status.XNEngineSt();
|
||
|
emit runStatusChanged(m_RunStatus);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MonitorThread::run()
|
||
|
{
|
||
|
while (true) {
|
||
|
{
|
||
|
QMutexLocker locker(&m_Mutex);
|
||
|
if (isAbort) {
|
||
|
break;
|
||
|
}
|
||
|
if (!m_RunStatusUpdate) {
|
||
|
QProcess process;
|
||
|
process.start("pgrep", QStringList() << "XNEngine");
|
||
|
process.waitForFinished();
|
||
|
QString output = process.readAllStandardOutput();
|
||
|
if (!output.isEmpty()) {
|
||
|
m_RunStatus = 4;
|
||
|
emit runStatusChanged(m_RunStatus);
|
||
|
} else {
|
||
|
m_RunStatus = 3;
|
||
|
emit runStatusChanged(m_RunStatus);
|
||
|
isAbort = true;
|
||
|
emit abortProcess();
|
||
|
}
|
||
|
}
|
||
|
m_RunStatusUpdate = false;
|
||
|
}
|
||
|
msleep(2000);
|
||
|
}
|
||
|
if (m_ControlDataWriter) {
|
||
|
m_ControlPublisher->delete_datawriter(m_ControlDataWriter);
|
||
|
}
|
||
|
if (m_ControlTopic) {
|
||
|
m_Participant->delete_topic(m_ControlTopic);
|
||
|
}
|
||
|
if (m_ControlPublisher) {
|
||
|
m_Participant->delete_publisher(m_ControlPublisher);
|
||
|
}
|
||
|
if (m_EngineStatusDataReader) {
|
||
|
m_EngineStatusSubscriber->delete_datareader(m_EngineStatusDataReader);
|
||
|
delete listener;
|
||
|
}
|
||
|
if (m_EngineStatusTopic) {
|
||
|
m_Participant->delete_topic(m_EngineStatusTopic);
|
||
|
}
|
||
|
if (m_EngineStatusSubscriber) {
|
||
|
m_Participant->delete_subscriber(m_EngineStatusSubscriber);
|
||
|
}
|
||
|
|
||
|
eprosima::fastdds::dds::DomainParticipantFactory::get_instance()->delete_participant(
|
||
|
m_Participant);
|
||
|
}
|
||
|
|
||
|
void MonitorThread::OnPause()
|
||
|
{
|
||
|
if (m_ControlDataWriter) {
|
||
|
XNSim::XNSimControl::XNRuntimeControl control;
|
||
|
control.XNSimCmd(1);
|
||
|
control.XNThrCmd(0);
|
||
|
m_ControlDataWriter->write(&control);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MonitorThread::OnContinue()
|
||
|
{
|
||
|
if (m_ControlDataWriter) {
|
||
|
XNSim::XNSimControl::XNRuntimeControl control;
|
||
|
control.XNSimCmd(2);
|
||
|
control.XNThrCmd(0);
|
||
|
m_ControlDataWriter->write(&control);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MonitorThread::OnAbort()
|
||
|
{
|
||
|
if (m_ControlDataWriter) {
|
||
|
XNSim::XNSimControl::XNRuntimeControl control;
|
||
|
control.XNSimCmd(3);
|
||
|
control.XNThrCmd(0);
|
||
|
m_ControlDataWriter->write(&control);
|
||
|
QMutexLocker locker(&m_Mutex);
|
||
|
isAbort = true;
|
||
|
}
|
||
|
}
|