/** * Implementation LicenceHandler class * $Id$ * $HeadURL$ * $Author$ * Created by hjl, SLSE, 2016-05-11 */ #include "licence_handler.h" #include "dds_handler.h" #include #include using namespace ServiceManagement; LicenceHandler::LicenceHandler(const std::shared_ptr &ddsHandler, const QString &licenceFile) : Interface::Fts::GetLicenceRequestHandler(ddsHandler->fts()) , Interface::Fts::FreeLicenceRequestHandler(ddsHandler->fts()) , m_ddsHandler(ddsHandler) , m_xmlFileName(licenceFile) , m_logger(getLogger("LicenceHandler")) { INFO_LOG(m_logger) << "Start ServiceManagement LicenceHandler. Filename: " << m_xmlFileName; } // end of ctor() //----------------------------------------------------------------------------- LicenceHandler::~LicenceHandler() { for (const LtsLicence::Licence &info : qAsConst(m_licenceVec)) m_ddsHandler->dts()->removeLicenceInformationByKey(info.name.toStdString()); } // end of dtor() //----------------------------------------------------------------------------- bool LicenceHandler::init() { INFO_LOG(m_logger) << "init() Start ServiceManagement LicenceHandler."; // read all licences from licencefile if (!readLicenceFile()) { ERROR_LOG(m_logger) << "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(m_logger) << "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(m_logger) << "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(m_logger) << "Try to getLicence (from SCM) for '" << licenceName << "' ..."; Core::FunctionReplyObject fro = getLicenceInternal(licenceName); if (fro.error()) INFO_LOG(m_logger) << "getLicence() errorText: " << fro.translated(); return !fro.error(); } // end of getLicence() //----------------------------------------------------------------------------- bool LicenceHandler::freeLicence(const QString &licenceName) { INFO_LOG(m_logger) << "Try to freeLicence (from SCM) for '" << licenceName << "' ..."; Core::FunctionReplyObject fro = freeLicenceInternal(licenceName); if (fro.error()) INFO_LOG(m_logger) << "freeLicence() errorText: " << fro.translated(); return !fro.error(); } // end of freeLicence() //----------------------------------------------------------------------------- bool LicenceHandler::readLicenceFile() { QFile f(m_xmlFileName); if (!f.exists()) { ERROR_LOG(m_logger) << "Error loading licencefile ... file not found. '" << m_xmlFileName << "'"; return false; } if (!f.open(QIODevice::ReadOnly)) { ERROR_LOG(m_logger) << "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(m_logger) << "ReadAll() from encrypted licencefile ... bytesAvailable:" << qbaIn.size(); } else if (m_xmlFileName.endsWith(QLatin1String(".lic"))) INFO_LOG(m_logger) << "ReadAll() from unencrypted licencefile ... bytesAvailable:" << qbaIn.size(); f.close(); INFO_LOG(m_logger) << "Try to parse xml file '" << m_xmlFileName << "'"; m_licenceVec = LtsLicence::readLicences(qbaIn); if (m_licenceVec.isEmpty()) { ERROR_LOG(m_logger) << "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(m_logger) << "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(m_logger) << "getLicence() for '" << licenceName << "' ok.\n" << info; } publishData(licence); return Core::FunctionReplyObject(i + 1); // id im FRO >0 ; - m_licenceVec beginnt bei [0] } } ERROR_LOG(m_logger) << "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(m_logger) << "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(m_logger) << "freeLicence() for '" << licenceName << "' ok.\n" << info; } publishData(licence); return Core::FunctionReplyObject(i + 1); // id im FRO >0 ; - m_licenceVec beginnt bei [0] } } ERROR_LOG(m_logger) << "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(m_logger) << "Licence '" << licence.name << "' is valid for " << now.daysTo(end) << " days." << " Expiration: " << licence.expiration.date().toString() << " " << licence.expiration.time().toString(); } return true; } WARNING_LOG(m_logger) << "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 : qAsConst(m_licenceVec)) publishData(info); } // end of publishAllData() //----------------------------------------------------------------------------- void LicenceHandler::publishData(const LtsLicence::Licence &data) { INFO_LOG(m_logger) << "Publish '" << data.name << "' Licence-data to dds"; if (!m_ddsHandler->dts()->publish(toDdsData(data))) ERROR_LOG(m_logger) << "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() //-----------------------------------------------------------------------------