Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Services/ServiceManagement/licence_handler.cpp
T
torstenandCopilot b774f4035c Qt6 Migration: DDS-Stubs, Logger-Fixes, Export-Header Korrekturen
- NGL_ADD_LIBRARY2: 87 CMakeLists.txt konvertiert für Export-Header Generierung
- 44 Export-Header Include-Pfade korrigiert (_export.h)
- 81 DDS-Stub-Header generiert (*_dtc.h, *_fts.h, *_dts.h, *_ftc.h)
- INGLLogger: Duplikat gelöscht, m_logger Member entfernt, variadische Makros
- Q_FUNC_INFO Redefinition behoben (Qt6 definiert bereits)
- FATAL_LOG Makro hinzugefügt
- InfrastructureManagement DDS-Stubs (ism.hpp, ism_enums.hpp, ism_ftc.h)
- FilterManagement DDS-Stubs (flt_ftc.h, flt_dts.h)
- AreaEffectSimulation DDS-Stubs (aes.hpp, aes_enums.hpp)
- SymbolAppearance Klasse vollständig definiert mit content() Methode
- BuildingComponentFilter Klasse definiert mit Copy-Konstruktor
- Ftc Factory: nullptr Return (abstract class)
- AesFilter Klasse in aes.hpp hinzugefügt
- Dtc Klasse in ppm.hpp mit participantChanged Signal
- AesType Enum: alle 15 At* Werte definiert
- AesEffectState_def: EsReceived hinzugefügt
- DataModification Enum: eAdded/eModified/eRemoved
- function_reply_object.h → core_types.h Include-Pfad (72 Dateien)
- Enum-Class-Streaming Template-Operator in INGLLogger.h
- ism_mounting_point.h: QImage → QtGui/QImage, QGuiApplication/QScreen
- ism_data_plugin_interface.h: QSet Include, Include-Reihenfolge
- QRegExp → QRegularExpression in tpm_js_parser.cpp
- .underlying() Aufrufe zu direktem Enum-Vergleich korrigiert
- Include-Pfade: aes.hpp in symbol_appearance_settings_impl.h und aes_display_filter_impl.h

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-19 06:06:24 +02:00

340 lines
11 KiB
C++
Executable File

/**
* Implementation LicenceHandler class
* $Id$
* $HeadURL$
* $Author$
* Created by hjl, SLSE, 2016-05-11
*/
#include "licence_handler.h"
#include <utility>
#include "dds_handler.h"
#include <QFile>
#include <LtsLicence/aes_crypt.h>
using namespace ServiceManagement;
LicenceHandler::LicenceHandler(const std::shared_ptr<DdsHandler> &ddsHandler, const QString &licenceFile)
: Interface::Fts::GetLicenceRequestHandler(ddsHandler->fts())
, Interface::Fts::FreeLicenceRequestHandler(ddsHandler->fts())
, m_ddsHandler(ddsHandler)
, m_xmlFileName(licenceFile)
{
INFO_LOG() << "Start ServiceManagement LicenceHandler. Filename: " << m_xmlFileName;
} // end of ctor()
//-----------------------------------------------------------------------------
LicenceHandler::~LicenceHandler()
{
for (const LtsLicence::Licence &info : std::as_const(m_licenceVec))
m_ddsHandler->dts()->removeLicenceInformationByKey(info.name.toStdString());
} // end of dtor()
//-----------------------------------------------------------------------------
bool LicenceHandler::init()
{
INFO_LOG() << "init() Start ServiceManagement LicenceHandler.";
// read all licences from licencefile
if (!readLicenceFile())
{
ERROR_LOG() << "Error reading data from licencefile!";
return false;
}
// Publish Data via DTS
publishAllData();
return true;
}// end of init()
//-----------------------------------------------------------------------------
bool LicenceHandler::isServiceLicensed(const QString &licenceName) const
{
for (const auto &licence : m_licenceVec)
if (licence.name == licenceName)
return true;
return false;
} // end of isServiceLicensed()
//-----------------------------------------------------------------------------
void LicenceHandler::onGetLicenceRequest(const rti::core::SampleIdentity &cookie,
const LicenceRequest &request)
{
INFO_LOG() << "Try to getLicence (from DDS) for '" << request.name() << "' ...";
Core::FunctionReplyObject ret = getLicenceInternal(QString::fromStdString(request.name()));
m_ddsHandler->fts()->getLicenceReply(cookie, ret);
return;
} // end of onGetLicenceRequest()
//-----------------------------------------------------------------------------
void LicenceHandler::onFreeLicenceRequest(const rti::core::SampleIdentity &cookie,
const LicenceRequest &request)
{
INFO_LOG() << "Try to freeLicence (from DDS) for '" << request.name() << "' ...";
Core::FunctionReplyObject ret = freeLicenceInternal(QString::fromStdString(request.name()));
m_ddsHandler->fts()->freeLicenceReply(cookie, ret);
return;
} // end of onFreeLicenceRequest()
//-----------------------------------------------------------------------------
bool LicenceHandler::getLicence(const QString &licenceName)
{
INFO_LOG() << "Try to getLicence (from SCM) for '" << licenceName << "' ...";
Core::FunctionReplyObject fro = getLicenceInternal(licenceName);
if (fro.error())
INFO_LOG() << "getLicence() errorText: " << fro.translated();
return !fro.error();
} // end of getLicence()
//-----------------------------------------------------------------------------
bool LicenceHandler::freeLicence(const QString &licenceName)
{
INFO_LOG() << "Try to freeLicence (from SCM) for '" << licenceName << "' ...";
Core::FunctionReplyObject fro = freeLicenceInternal(licenceName);
if (fro.error())
INFO_LOG() << "freeLicence() errorText: " << fro.translated();
return !fro.error();
} // end of freeLicence()
//-----------------------------------------------------------------------------
bool LicenceHandler::readLicenceFile()
{
QFile f(m_xmlFileName);
if (!f.exists())
{
ERROR_LOG() << "Error loading licencefile ... file not found. '" << m_xmlFileName << "'";
return false;
}
if (!f.open(QIODevice::ReadOnly))
{
ERROR_LOG() << "Error loading licencefile ... file open failed.'" << m_xmlFileName << "'";
return false;
}
QByteArray qbaIn(f.readAll());
if (m_xmlFileName.endsWith(QLatin1String(".crl")))
{
qbaIn = LtsLicence::decryptData(qbaIn);
INFO_LOG() << "ReadAll() from encrypted licencefile ... bytesAvailable:" << qbaIn.size();
}
else if (m_xmlFileName.endsWith(QLatin1String(".lic")))
INFO_LOG() << "ReadAll() from unencrypted licencefile ... bytesAvailable:" << qbaIn.size();
f.close();
INFO_LOG() << "Try to parse xml file '" << m_xmlFileName << "'";
m_licenceVec = LtsLicence::readLicences(qbaIn);
if (m_licenceVec.isEmpty())
{
ERROR_LOG() << "Parse error reading xml file '" << m_xmlFileName << "'";
return false;
}
return true;
} // end of readLicenceFile()
//-----------------------------------------------------------------------------
Core::FunctionReplyObject LicenceHandler::getLicenceInternal(const QString &licenceName)
{
for (int i = 0; i < m_licenceVec.size(); ++i)
{
LtsLicence::Licence &licence = m_licenceVec[i];
if (licence.name == licenceName)
{
if (!licence.ignoreExpiration)
{
if (!isLicenceValid(licence))
return Core::FunctionReplyObject(0, "No valid licence available (outdated).");
}
if (!licence.ignoreMaxCount)
{
if (licence.maxCount <= licence.licenceRefCount)
{
WARNING_LOG() << "getLicence() for '" << licence.name << "' failed. - No more licences available.";
return Core::FunctionReplyObject(0, "No more licences available.");
}
else
++licence.licenceRefCount;
}
else
{
++licence.licenceRefCount; // fuer korrekte dumpLicenceInformation()
}
if (m_logger.isInfoEnabled())
{
const QString info = dumpLicenceInformation(licence);
INFO_LOG() << "getLicence() for '" << licenceName << "' ok.\n" << info;
}
publishData(licence);
return Core::FunctionReplyObject(i + 1); // id im FRO >0 ; - m_licenceVec beginnt bei [0]
}
}
ERROR_LOG() << "getLicence() for '" << licenceName << "' failed. - No licence with this name.";
return Core::FunctionReplyObject(0, "No licence with this name.");
} // end of getLicenceInternal()
//-----------------------------------------------------------------------------
Core::FunctionReplyObject LicenceHandler::freeLicenceInternal(const QString &licenceName)
{
for (int i = 0; i < m_licenceVec.size(); ++i)
{
LtsLicence::Licence &licence = m_licenceVec[i];
if (licence.name == licenceName)
{
if (!licence.ignoreExpiration)
{
if (!isLicenceValid(licence))
return Core::FunctionReplyObject(0, "No valid licence available (outdated).");
}
if (!licence.ignoreMaxCount)
{
if (licence.licenceRefCount <= 0)
{
WARNING_LOG() << "freeLicence() for '" << licence.name << "' failed. - Max number of licences are free.";
return Core::FunctionReplyObject(0, "Max number of licences are free.");
}
else
--licence.licenceRefCount;
}
else
{
if (licence.licenceRefCount > 0) // keine negativen frees eintragen
{
--licence.licenceRefCount; // fuer korrekte dumpLicenceInformation()
}
}
if (m_logger.isInfoEnabled())
{
const QString info = dumpLicenceInformation(licence);
INFO_LOG() << "freeLicence() for '" << licenceName << "' ok.\n" << info;
}
publishData(licence);
return Core::FunctionReplyObject(i + 1); // id im FRO >0 ; - m_licenceVec beginnt bei [0]
}
}
ERROR_LOG() << "freeLicence() for '" << licenceName << "' failed. - No licence with this name.";
return Core::FunctionReplyObject(0, "No licence with this name.");
}// end of freeLicence()
//-----------------------------------------------------------------------------
bool LicenceHandler::isLicenceValid(const LtsLicence::Licence &licence) const
{
const QDateTime now(QDateTime::currentDateTimeUtc());
const QDateTime end(licence.expiration);
if (now < licence.expiration)
{
if (!licence.ignoreExpiration)
{
INFO_LOG() << "Licence '" << licence.name << "' is valid for " << now.daysTo(end) << " days."
<< " Expiration: " << licence.expiration.date().toString()
<< " " << licence.expiration.time().toString();
}
return true;
}
WARNING_LOG() << "Licence '" << licence.name << "' is expired since " << end.daysTo(now) << " days."
<< " Expiration: " << licence.expiration.date().toString()
<< " " << licence.expiration.time().toString();
return false;
} // end of isLicenceValid()
//-----------------------------------------------------------------------------
QString LicenceHandler::dumpLicenceInformation(const LtsLicence::Licence &licence)
{
QString info;
info.append(QLatin1String("Licence information for '"));
info.append(licence.name);
info.append(QLatin1String("'"));
if (licence.ignoreMaxCount)
{
info.append(QLatin1String(" Number of licences is unlimited. "));
info.append(QString::number(licence.licenceRefCount));
info.append(QLatin1String(" licences in use."));
}
else
{
info.append(QLatin1String(" Max licences: "));
info.append(QString::number(licence.maxCount));
info.append(QLatin1String(" Number of remaining licences: "));
info.append(QString::number(licence.maxCount - licence.licenceRefCount));
}
if (licence.ignoreExpiration)
info.append(QLatin1String(" Licence never expires. "));
else
{
info.append(QLatin1String(" Expiration: "));
info.append(licence.expiration.date().toString());
info.append(QLatin1String(" "));
info.append(licence.expiration.time().toString());
}
return info;
} // end of licenceInformation()
//-----------------------------------------------------------------------------
void LicenceHandler::publishAllData()
{
for (const LtsLicence::Licence &info : std::as_const(m_licenceVec))
publishData(info);
} // end of publishAllData()
//-----------------------------------------------------------------------------
void LicenceHandler::publishData(const LtsLicence::Licence &data)
{
INFO_LOG() << "Publish '" << data.name << "' Licence-data to dds";
if (!m_ddsHandler->dts()->publish(toDdsData(data)))
ERROR_LOG() << "Error publishing '" << data.name << "' Licence-data to dds";
} // end of publishData()
//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// convert methods
// ----------------------------------------------------------------------------
//ServiceManagement::LicenceInformation LicenceHandler::toDdsData(const ServiceManagement::Licence &data)
ServiceManagement::LicenceInformation LicenceHandler::toDdsData(const LtsLicence::Licence &data)
{
ServiceManagement::LicenceInformation ddsData;
ddsData.name(data.name.toStdString());
ddsData.ignoreMaxCount(data.ignoreMaxCount);
ddsData.maxCount(data.maxCount);
ddsData.ignoreExpiration(data.ignoreExpiration);
ddsData.expiration(data.expiration.toMSecsSinceEpoch());
ddsData.refCount(data.licenceRefCount);
return ddsData;
} // end of toDdsData()
//-----------------------------------------------------------------------------