Qt6 migration: LtsWidgets API fixes, LtsWebSocket logger init, RTI Docker install

- LtsWidgets: Fixed Qt6 API compatibility issues
  - QWheelEvent::delta() → angleDelta().y()
  - QStyleOption::init() → initFrom()
  - QComboBox activated signal signature change
  - QTextCodec removal (UTF-8 default in Qt6)
  - Missing widget includes added

- LtsWebSocket: Fixed m_logger reference initialization
  - Added NGLLoggerBootstrap includes
  - Proper initialization in constructor initializer lists

- CMake: Added BUILD_LTSWEBSOCKET option for temporary exclusion

- Docker: RTI Connext permanent installation script added
  - ci/install-rti-in-docker.sh for persistent RTI setup
  - Removes need for volume mounting RTI into container

- BUILD options: FilterManagement, RTI, Codification, Grantlee, LtsWebSocket
  can be disabled for partial builds during migration

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
torstenandCopilot committed 2026-08-19 14:04:26 +02:00
1 parent 3bb6e89496
commit bafed3705f
69 files changed
+12765 -14202

No files matched your search

@@ -19,7 +19,7 @@ SET(NGLLogger_HDRS
ADD_DEFINITIONS(-DBUILDING_NGLLOGGER)
NGL_ADD_LIBRARY2(ALL NGLLogger SHARED ${NGLLogger_SRCS} ${NGLLogger_HDRS})
NGL_TARGET_INCLUDE_DIRECTORIES(NGLLogger
INTERFACE
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../
${CMAKE_CURRENT_BINARY_DIR}
+172 -187
View File
@@ -1,59 +1,56 @@
/**
* Logger interface
* $Id$
* $HeadURL$
* $Author$
*/
* Logger interface
* $Id$
* $HeadURL$
* $Author$
*/
#pragma once
#include <string>
#include <sstream>
#include <QString>
#include <QList>
#include <QPointF>
#include <QSize>
#include <QRect>
#include <QRectF>
#include <QList>
#include "ngllogger_export.h"
#include <QSize>
#include <QString>
#include <ngllogger_export.h>
#include <sstream>
#include <string>
namespace NGL
{
class INGLLogger;
// keep in sync with log4qt/level.h
enum class LogLevel
{
Undefined = 0,
Trace = 64,
Debug = 96,
Info = 128,
Warning = 150,
Error = 182,
Fatal = 214,
enum class LogLevel {
Undefined = 0,
Trace = 64,
Debug = 96,
Info = 128,
Warning = 150,
Error = 182,
Fatal = 214,
};
class LogStreamHelper
{
public:
inline LogStreamHelper(INGLLogger &logger, LogLevel level, const char *const file, int line, const char *const function)
: m_logger(logger)
, m_level(level)
, m_line(line)
, m_file(file)
, m_func(function)
{}
inline ~LogStreamHelper(); // after INGLLogger class definition
public:
inline LogStreamHelper(INGLLogger& logger, LogLevel level, const char* const file, int line,
const char* const function)
: m_logger(logger), m_level(level), m_line(line), m_file(file), m_func(function)
{
}
template<typename T>
inline LogStreamHelper &operator<<(const T &val);
inline ~LogStreamHelper(); // after INGLLogger class definition
private:
std::ostringstream m_buffer;
NGL::INGLLogger &m_logger;
const LogLevel m_level;
const int m_line;
const char *const m_file;
const char *const m_func;
template <typename T> inline LogStreamHelper& operator<<(const T& val);
private:
std::ostringstream m_buffer;
NGL::INGLLogger& m_logger;
const LogLevel m_level;
const int m_line;
const char* const m_file;
const char* const m_func;
};
// Interface class for source code logging.
@@ -64,58 +61,64 @@ private:
// information on logger-hierarchies, logging-levels, and configuration files.
class INGLLogger
{
public:
INGLLogger(const INGLLogger &) = delete;
INGLLogger &operator=(const INGLLogger &) = delete;
INGLLogger() = default;
virtual ~INGLLogger() = default;
public:
INGLLogger(const INGLLogger&) = delete;
INGLLogger& operator=(const INGLLogger&) = delete;
INGLLogger() = default;
virtual ~INGLLogger() = default;
using String = std::string;
using String = std::string;
// Creates an instance of an INGLLogger interface. The fullqualifiedname is
// a dottet sequence (i.e. "NGL.thisandthat.mylogger"). The same name will
// always return the same interface-instance. It is not required to create
// the parent nodes.
virtual INGLLogger &getNGLLogger(const char *fullqualifiedname) = 0;
virtual INGLLogger &getNGLLogger(const std::string &fullqualifiedname) = 0;
// Creates an instance of an INGLLogger interface. The fullqualifiedname is
// a dottet sequence (i.e. "NGL.thisandthat.mylogger"). The same name will
// always return the same interface-instance. It is not required to create
// the parent nodes.
virtual INGLLogger& getNGLLogger(const char* fullqualifiedname) = 0;
virtual INGLLogger& getNGLLogger(const std::string& fullqualifiedname) = 0;
// True if this level is enabled.
virtual bool isTraceEnabled() const = 0;
virtual bool isDebugEnabled() const = 0;
virtual bool isInfoEnabled() const = 0;
virtual bool isWarningEnabled() const = 0;
virtual bool isErrorEnabled() const = 0;
virtual bool isFatalEnabled() const = 0;
virtual bool isLevelEnabled(LogLevel level) const = 0;
// True if this level is enabled.
virtual bool isTraceEnabled() const = 0;
virtual bool isDebugEnabled() const = 0;
virtual bool isInfoEnabled() const = 0;
virtual bool isWarningEnabled() const = 0;
virtual bool isErrorEnabled() const = 0;
virtual bool isFatalEnabled() const = 0;
virtual bool isLevelEnabled(LogLevel level) const = 0;
// Submit a LOG message.
// Use __FILE__ and __NAME__ macros.
virtual void log(LogLevel level, const INGLLogger::String& message, const char* file = nullptr, int line = -1,
const char* function = nullptr) = 0;
// Submit a LOG message.
// Use __FILE__ and __NAME__ macros.
virtual void log(LogLevel level, const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0;
// Submit an TRACE message.
virtual void logTrace(const INGLLogger::String& message, const char* file = nullptr, int line = -1,
const char* function = nullptr) = 0;
// Submit an DEBUG message.
virtual void logDebug(const INGLLogger::String& message, const char* file = nullptr, int line = -1,
const char* function = nullptr) = 0;
// Submit an INFO message.
virtual void logInfo(const INGLLogger::String& message, const char* file = nullptr, int line = -1,
const char* function = nullptr) = 0;
// Submit a WARNING message.
virtual void logWarning(const INGLLogger::String& message, const char* file = nullptr, int line = -1,
const char* function = nullptr) = 0;
// Submit an ERROR message.
virtual void logError(const INGLLogger::String& message, const char* file = nullptr, int line = -1,
const char* function = nullptr) = 0;
// Submit a FATAL message.
virtual void logFatal(const INGLLogger::String& message, const char* file = nullptr, int line = -1,
const char* function = nullptr) = 0;
// Submit an TRACE message.
virtual void logTrace(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0;
// Submit an DEBUG message.
virtual void logDebug(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0;
// Submit an INFO message.
virtual void logInfo(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0;
// Submit a WARNING message.
virtual void logWarning(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0;
// Submit an ERROR message.
virtual void logError(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0;
// Submit a FATAL message.
virtual void logFatal(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0;
// Writes a startup message into the logfile
virtual void logStartup(const char* file, int line, const char* function = nullptr) = 0;
// Writes a shutdown message into the logfile
virtual void logShutdown(const char* file, int line, const char* function = nullptr) = 0;
// Writes a startup message into the logfile
virtual void logStartup(const char *file, int line, const char *function = nullptr) = 0;
// Writes a shutdown message into the logfile
virtual void logShutdown(const char *file, int line, const char *function = nullptr) = 0;
// Get this loggers fullqualified name.
virtual std::string getLoggerName() const = 0;
// Get this loggers fullqualified name.
virtual std::string getLoggerName() const = 0;
virtual void setNoSpace(bool bNoSpace) = 0;
virtual bool noSpace() const = 0;
virtual void setNoSpace(bool bNoSpace) = 0;
virtual bool noSpace() const = 0;
};
//
@@ -123,112 +126,90 @@ public:
//
inline LogStreamHelper::~LogStreamHelper()
{
m_logger.log(m_level, m_buffer.str(), m_file, m_line, m_func);
m_logger.log(m_level, m_buffer.str(), m_file, m_line, m_func);
}
template<typename T>
inline LogStreamHelper &LogStreamHelper::operator<<(const T &val)
template <typename T> inline LogStreamHelper& LogStreamHelper::operator<<(const T& val)
{
m_buffer << val;
if (!m_logger.noSpace())
m_buffer << " ";
return *this;
m_buffer << val;
if (!m_logger.noSpace())
m_buffer << " ";
return *this;
}
} // namespace NGL
} //namespace NGL
inline std::ostream &operator<<(std::ostream &stream, const QByteArray &str)
inline std::ostream& operator<<(std::ostream& stream, const QByteArray& str)
{
stream << std::string(str.data(), str.size());
return stream;
stream << std::string(str.data(), str.size());
return stream;
}
//
// operator to ease logging (at least via NglLogging)
//
inline std::ostream &operator<<(std::ostream &stream,
const QPointF &value)
inline std::ostream& operator<<(std::ostream& stream, const QPointF& value)
{
stream << "QPointF(" << QString::number(value.x(), 'f', 6).toStdString() << "#" << QString::number(value.y(), 'f', 6).toStdString() << ")";
return stream;
stream << "QPointF(" << QString::number(value.x(), 'f', 6).toStdString() << "#"
<< QString::number(value.y(), 'f', 6).toStdString() << ")";
return stream;
}
//----------------------------------------------------------------------------
inline std::ostream &operator<< (std::ostream &stream,
const QPoint &value)
inline std::ostream& operator<<(std::ostream& stream, const QPoint& value)
{
stream << "QPoint(" << value.x() << "#" << value.y() << ")";
return stream;
stream << "QPoint(" << value.x() << "#" << value.y() << ")";
return stream;
}
inline std::ostream &operator<< (std::ostream &stream,
const QSize &value)
inline std::ostream& operator<<(std::ostream& stream, const QSize& value)
{
stream << "QSize(w=" << value.width() << ";h=" << value.height() << ")";
return stream;
}
inline std::ostream &operator<< (std::ostream &stream,
const QSizeF &value)
{
stream << "QSize(w="
<< QString::number(value.width(), 'f', 6).toStdString() << ";h="
<< QString::number(value.height(), 'f', 6).toStdString()
<< ")";
return stream;
stream << "QSize(w=" << value.width() << ";h=" << value.height() << ")";
return stream;
}
inline std::ostream &operator<< (std::ostream &stream,
const QRect &value)
inline std::ostream& operator<<(std::ostream& stream, const QSizeF& value)
{
stream << "QRect(center=" << value.center()
<< " w=" << value.width()
<< ";h=" << value.height()
<< " bottomLeft=" << value.bottomLeft()
<< " topRight=" << value.topRight()
<< ")";
return stream;
stream << "QSize(w=" << QString::number(value.width(), 'f', 6).toStdString()
<< ";h=" << QString::number(value.height(), 'f', 6).toStdString() << ")";
return stream;
}
inline std::ostream &operator<< (std::ostream &stream,
const QRectF &value)
inline std::ostream& operator<<(std::ostream& stream, const QRect& value)
{
stream << "QRectF(center=" << value.center()
<< " w=" << QString::number(value.width(), 'f', 3).toStdString()
<< ";h=" << QString::number(value.height(), 'f', 3).toStdString()
<< " bottomLeft=" << value.bottomLeft()
<< " topRight=" << value.topRight()
<< ")";
return stream;
stream << "QRect(center=" << value.center() << " w=" << value.width() << ";h=" << value.height()
<< " bottomLeft=" << value.bottomLeft() << " topRight=" << value.topRight() << ")";
return stream;
}
inline std::ostream &operator<< (std::ostream &stream,
const QList<int> &value)
inline std::ostream& operator<<(std::ostream& stream, const QRectF& value)
{
stream << "QList<int>(" << value.size() << "): ";
for (int i : value)
stream << i << ";";
return stream;
stream << "QRectF(center=" << value.center() << " w=" << QString::number(value.width(), 'f', 3).toStdString()
<< ";h=" << QString::number(value.height(), 'f', 3).toStdString() << " bottomLeft=" << value.bottomLeft()
<< " topRight=" << value.topRight() << ")";
return stream;
}
inline std::ostream &operator<< (std::ostream &stream,
const QList<QPointF> &value)
inline std::ostream& operator<<(std::ostream& stream, const QList<int>& value)
{
stream << "QList<QPointF>(" << value.size() << "): ";
for (const QPointF &item : value)
stream << item << ";";
return stream;
stream << "QList<int>(" << value.size() << "): ";
for (int i : value)
stream << i << ";";
return stream;
}
inline std::ostream &operator<<(std::ostream &stream, const QString &str)
inline std::ostream& operator<<(std::ostream& stream, const QList<QPointF>& value)
{
return stream << str.toUtf8();
stream << "QList<QPointF>(" << value.size() << "): ";
for (const QPointF& item : value)
stream << item << ";";
return stream;
}
inline std::ostream& operator<<(std::ostream& stream, const QString& str)
{
return stream << str.toUtf8();
}
namespace NGL
@@ -236,15 +217,12 @@ namespace NGL
namespace NGLLoggerHelper
{
NGLLOGGER_EXPORT void hexDumpHelper(NGL::INGLLogger &logger,
NGL::LogLevel level,
const void *a_data,
NGLLOGGER_EXPORT void hexDumpHelper(NGL::INGLLogger& logger, NGL::LogLevel level, const void* a_data,
const unsigned int a_size);
inline void hexDumpHelper(NGL::INGLLogger &logger,
NGL::LogLevel level,
const QByteArray &data)
inline void hexDumpHelper(NGL::INGLLogger& logger, NGL::LogLevel level, const QByteArray& data)
{
return hexDumpHelper(logger, level, data.data(), data.size());
return hexDumpHelper(logger, level, data.data(), data.size());
}
} // namespace NGLLoggerHelper
@@ -260,41 +238,46 @@ NGLLOGGER_EXPORT std::string getMemoryData();
#define LOGFUNCTION Q_FUNC_INFO
// log this output every time, no matter what log level is enabled
#define NOTICE_LOG(logger) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Info, __FILE__,__LINE__, Q_FUNC_INFO)
#define NOTICE_LOG(logger) NGL::LogStreamHelper(logger, NGL::LogLevel::Info, __FILE__, __LINE__, Q_FUNC_INFO)
#ifdef LTS_DEBUG
#define TRACE_LOG(logger) \
for (bool log_level_enabled = (logger).isTraceEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Trace, __FILE__,__LINE__, Q_FUNC_INFO)
#define TRACE_LOG(logger) \
for (bool log_level_enabled = (logger).isTraceEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Trace, __FILE__, __LINE__, Q_FUNC_INFO)
#else
#define TRACE_LOG(logger) \
for ( ; false; ) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Trace, __FILE__,__LINE__, Q_FUNC_INFO)
#define TRACE_LOG(logger) \
for (; false;) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Trace, __FILE__, __LINE__, Q_FUNC_INFO)
#endif
#define DEBUG_LOG(logger) \
for (bool log_level_enabled = (logger).isDebugEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Debug, __FILE__,__LINE__, Q_FUNC_INFO)
#define DEBUG_LOG(logger) \
for (bool log_level_enabled = (logger).isDebugEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Debug, __FILE__, __LINE__, Q_FUNC_INFO)
#define INFO_LOG(logger) \
for (bool log_level_enabled = (logger).isInfoEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Info, __FILE__,__LINE__, Q_FUNC_INFO)
#define INFO_LOG(logger) \
for (bool log_level_enabled = (logger).isInfoEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Info, __FILE__, __LINE__, Q_FUNC_INFO)
#define WARNING_LOG(logger) \
for (bool log_level_enabled = (logger).isWarningEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Warning, __FILE__,__LINE__, Q_FUNC_INFO)
#define WARNING_LOG(logger) \
for (bool log_level_enabled = (logger).isWarningEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Warning, __FILE__, __LINE__, Q_FUNC_INFO)
#define ERROR_LOG(logger) \
for (bool log_level_enabled = (logger).isErrorEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Error, __FILE__,__LINE__, Q_FUNC_INFO)
#define ERROR_LOG(logger) \
for (bool log_level_enabled = (logger).isErrorEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Error, __FILE__, __LINE__, Q_FUNC_INFO)
#define FATAL_LOG(logger) \
for (bool log_level_enabled = (logger).isFatalEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Fatal, __FILE__,__LINE__, Q_FUNC_INFO)
#define FATAL_LOG(logger) \
for (bool log_level_enabled = (logger).isFatalEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(logger, NGL::LogLevel::Fatal, __FILE__, __LINE__, Q_FUNC_INFO)
#define STARTUP_LOG(logger) { (logger).logStartup(__FILE__, __LINE__, Q_FUNC_INFO); }
#define SHUTDOWN_LOG(logger) { (logger).logShutdown(__FILE__, __LINE__, Q_FUNC_INFO); }
#define STARTUP_LOG(logger) \
{ \
(logger).logStartup(__FILE__, __LINE__, Q_FUNC_INFO); \
}
#define SHUTDOWN_LOG(logger) \
{ \
(logger).logShutdown(__FILE__, __LINE__, Q_FUNC_INFO); \
}
//==============================================================================
@@ -303,8 +286,10 @@ NGLLOGGER_EXPORT std::string getMemoryData();
// do something nasty
// MEMORY_LOG() << " after something nasty";
#define MEMORY_LOG() \
for (bool log_level_enabled = (NGL::getMemoryLogger()).isInfoEnabled(); log_level_enabled; log_level_enabled = false) \
NGL::LogStreamHelper(NGL::getMemoryLogger(), NGL::LogLevel::Info, __FILE__,__LINE__, Q_FUNC_INFO) << NGL::getMemoryData()
#define MEMORY_LOG() \
for (bool log_level_enabled = (NGL::getMemoryLogger()).isInfoEnabled(); log_level_enabled; \
log_level_enabled = false) \
NGL::LogStreamHelper(NGL::getMemoryLogger(), NGL::LogLevel::Info, __FILE__, __LINE__, Q_FUNC_INFO) \
<< NGL::getMemoryData()
//==============================================================================
+18 -32
View File
@@ -1,11 +1,10 @@
#ifndef NGLLOGGERBOOTSTRAP_H
#define NGLLOGGERBOOTSTRAP_H
#include <NGLLogger/INGLLogger.h>
#include "ngllogger_export.h"
#include <ngllogger_export.h>
namespace NGL
{
@@ -14,46 +13,33 @@ namespace NGLLoggerBootstrap
{
class AlreadyCreatedError final : public std::exception
{
const char* what() const noexcept override
{
return "NGLLogger rootlogger already created!";
}
const char* what() const noexcept override
{
return "NGLLogger rootlogger already created!";
}
};
class NotCreatedError final : public std::exception
{
const char* what() const noexcept override
{
return "NGLLogger rootlogger not yet created!";
}
const char* what() const noexcept override
{
return "NGLLogger rootlogger not yet created!";
}
};
NGLLOGGER_EXPORT INGLLogger& getNGLRootLogger();
NGLLOGGER_EXPORT INGLLogger& createNGLRootLogger(const QString& configfilename, bool ignorealreadycreated,
const QString& logOutputName = QString(), bool quiet = false);
NGLLOGGER_EXPORT INGLLogger &getNGLRootLogger();
NGLLOGGER_EXPORT INGLLogger &createNGLRootLogger(const QString &configfilename,
bool ignorealreadycreated,
const QString &logOutputName = QString(),
bool quiet = false);
inline INGLLogger &createNGLRootLogger(const char *configfilename,
bool ignorealreadycreated,
const QString &logOutputName = QString())
inline INGLLogger& createNGLRootLogger(const char* configfilename, bool ignorealreadycreated,
const QString& logOutputName = QString())
{
return createNGLRootLogger(QString::fromUtf8(configfilename), ignorealreadycreated, logOutputName);
return createNGLRootLogger(QString::fromUtf8(configfilename), ignorealreadycreated, logOutputName);
}
}
} // namespace NGLLoggerBootstrap
}
} // namespace NGL
#endif
#endif
+242 -261
View File
@@ -1,360 +1,341 @@
#include "nglcomp_bzip2iodevice.h"
#include <QIODevice>
#include <QString>
#include <bzlib.h>
class BZip2IODevice::Private
{
public:
Private(BZip2IODevice *parent_, QIODevice *dev, int bs)
: parent(parent_)
, device(dev)
, iCurPos(0)
, iMaxBufSize(1024 * 1024)
, blocksize(bs)
, initialized(false)
, decomressStreamEnd(false)
{
buffer.resize(iMaxBufSize);
}
public:
Private(BZip2IODevice* parent_, QIODevice* dev, int bs)
: parent(parent_), device(dev), iCurPos(0), iMaxBufSize(1024 * 1024), blocksize(bs), initialized(false),
decomressStreamEnd(false)
{
buffer.resize(iMaxBufSize);
}
bool initializeCompress();
qint64 doCompress(const char *in, qint64 iMaxLen);
bool finishCompress();
bool initializeDecompress();
qint64 doDecompress(char *out, qint64 iMaxLen);
bool finishDecompress();
static QString bzipError2String(int errCode);
bool initializeCompress();
qint64 doCompress(const char* in, qint64 iMaxLen);
bool finishCompress();
bool initializeDecompress();
qint64 doDecompress(char* out, qint64 iMaxLen);
bool finishDecompress();
static QString bzipError2String(int errCode);
QAtomicInt aborted { 0 };
BZip2IODevice *parent;
QIODevice *device;
unsigned int iCurPos;
unsigned int iMaxBufSize;
unsigned int blocksize;
bool initialized;
bool decomressStreamEnd;
QByteArray buffer;
bz_stream stream;
QAtomicInt aborted{0};
BZip2IODevice* parent;
QIODevice* device;
unsigned int iCurPos;
unsigned int iMaxBufSize;
unsigned int blocksize;
bool initialized;
bool decomressStreamEnd;
QByteArray buffer;
bz_stream stream;
};
bool BZip2IODevice::Private::initializeCompress()
{
if (initialized)
return true;
memset(&stream, 0, sizeof(stream));
int blockSize = (blocksize > 0 && blocksize < 10) ? blocksize : 10;
int ret = BZ2_bzCompressInit(&stream, blockSize, 0, 0);
if (ret != BZ_OK)
{
parent->setErrorString(QStringLiteral("Error initializing bzip2 %1").arg(bzipError2String(ret)));
initialized = false;
}
else
initialized = true;
if (initialized)
return true;
memset(&stream, 0, sizeof(stream));
int blockSize = (blocksize > 0 && blocksize < 10) ? blocksize : 10;
int ret = BZ2_bzCompressInit(&stream, blockSize, 0, 0);
if (ret != BZ_OK)
{
parent->setErrorString(QStringLiteral("Error initializing bzip2 %1").arg(bzipError2String(ret)));
initialized = false;
}
else
initialized = true;
return initialized;
return initialized;
}
qint64 BZip2IODevice::Private::doCompress(const char *in, qint64 iMaxLen)
qint64 BZip2IODevice::Private::doCompress(const char* in, qint64 iMaxLen)
{
if (!initialized)
{
if (parent->errorString().isEmpty())
parent->setErrorString(QLatin1String("Internal Error - bzip not initialized"));
finishCompress();
return -1;
}
if (iMaxLen > 2 * 1024 * 1024 * 1024LL)
{
finishCompress();
parent->setErrorString(QLatin1String("Currently can't handle more than 2GB!"));
return -1;
}
stream.next_in = const_cast<char *>(in);
stream.avail_in = iMaxLen;
do
{
if (aborted) return 0;
stream.next_out = buffer.data();
stream.avail_out = buffer.size();
const int ret = BZ2_bzCompress(&stream, BZ_RUN);
if (ret != BZ_RUN_OK)
if (!initialized)
{
finishCompress();
parent->setErrorString(QStringLiteral("Error executing BZ2_bzCompress: %1").arg(bzipError2String(ret)));
return -1;
}
int iBytesToWrite = buffer.size() - stream.avail_out;
if (iBytesToWrite)
{
if (device->write(buffer.constData(), iBytesToWrite) != iBytesToWrite)
{
if (parent->errorString().isEmpty())
parent->setErrorString(QLatin1String("Internal Error - bzip not initialized"));
finishCompress();
parent->setErrorString(QLatin1String("Error writing to QIODevice"));
return -1;
}
}
}
while (stream.avail_in != 0);
if (iMaxLen > 2 * 1024 * 1024 * 1024LL)
{
finishCompress();
parent->setErrorString(QLatin1String("Currently can't handle more than 2GB!"));
return -1;
}
return stream.next_in - in;
stream.next_in = const_cast<char*>(in);
stream.avail_in = iMaxLen;
do
{
if (aborted)
return 0;
stream.next_out = buffer.data();
stream.avail_out = buffer.size();
const int ret = BZ2_bzCompress(&stream, BZ_RUN);
if (ret != BZ_RUN_OK)
{
finishCompress();
parent->setErrorString(QStringLiteral("Error executing BZ2_bzCompress: %1").arg(bzipError2String(ret)));
return -1;
}
int iBytesToWrite = buffer.size() - stream.avail_out;
if (iBytesToWrite)
{
if (device->write(buffer.constData(), iBytesToWrite) != iBytesToWrite)
{
finishCompress();
parent->setErrorString(QLatin1String("Error writing to QIODevice"));
return -1;
}
}
} while (stream.avail_in != 0);
return stream.next_in - in;
}
bool BZip2IODevice::Private::finishCompress()
{
if (!initialized || aborted)
if (!initialized || aborted)
return true;
int ret = -1;
do
{
stream.next_out = buffer.data();
stream.avail_out = buffer.size();
ret = BZ2_bzCompress(&stream, BZ_FINISH);
if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
{
finishCompress();
parent->setErrorString(QStringLiteral("Error executing BZ2_bzCompress: %1").arg(bzipError2String(ret)));
return false;
}
int iBytesToWrite = buffer.size() - stream.avail_out;
if (device->write(buffer.constData(), iBytesToWrite) != iBytesToWrite)
{
finishCompress();
parent->setErrorString(QLatin1String("Error writing to QIODevice"));
return false;
}
} while (ret != BZ_STREAM_END);
BZ2_bzCompressEnd(&stream);
initialized = false;
return true;
int ret = -1;
do
{
stream.next_out = buffer.data();
stream.avail_out = buffer.size();
ret = BZ2_bzCompress(&stream, BZ_FINISH);
if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
{
finishCompress();
parent->setErrorString(QStringLiteral("Error executing BZ2_bzCompress: %1").arg(bzipError2String(ret)));
return false;
}
int iBytesToWrite = buffer.size() - stream.avail_out;
if (device->write(buffer.constData(), iBytesToWrite) != iBytesToWrite)
{
finishCompress();
parent->setErrorString(QLatin1String("Error writing to QIODevice"));
return false;
}
}
while (ret != BZ_STREAM_END);
BZ2_bzCompressEnd(&stream);
initialized = false;
return true;
}
bool BZip2IODevice::Private::initializeDecompress()
{
if (initialized)
return true;
memset(&stream, 0, sizeof(stream));
int ret = BZ2_bzDecompressInit(&stream, 0, 0);
if (ret != BZ_OK)
{
parent->setErrorString(QStringLiteral("Error initializing bzip2: %1").arg(bzipError2String(ret)));
initialized = false;
}
else
initialized = true;
decomressStreamEnd = false;
return initialized;
if (initialized)
return true;
memset(&stream, 0, sizeof(stream));
int ret = BZ2_bzDecompressInit(&stream, 0, 0);
if (ret != BZ_OK)
{
parent->setErrorString(QStringLiteral("Error initializing bzip2: %1").arg(bzipError2String(ret)));
initialized = false;
}
else
initialized = true;
decomressStreamEnd = false;
return initialized;
}
qint64 BZip2IODevice::Private::doDecompress(char *out, qint64 iMaxLen)
qint64 BZip2IODevice::Private::doDecompress(char* out, qint64 iMaxLen)
{
int ret = -1;
if (!initialized)
{
if (parent->errorString().isEmpty())
parent->setErrorString(QLatin1String("Internal Error - bzip not initialized"));
finishDecompress();
return -1;
}
if (decomressStreamEnd)
return 0;
if (iMaxLen > 2 * 1024 * 1024 * 1024LL)
{
finishCompress();
parent->setErrorString(QLatin1String("Currently can't handle more than 2GB!"));
return -1;
}
stream.next_out = out;
stream.avail_out = iMaxLen;
do
{
if (ret != BZ_STREAM_END && stream.avail_in == 0)
int ret = -1;
if (!initialized)
{
qint64 iReadCnt = device->read(buffer.data(), buffer.size());
if (iReadCnt <= 0)
if (parent->errorString().isEmpty())
parent->setErrorString(QLatin1String("Internal Error - bzip not initialized"));
finishDecompress();
return -1;
}
if (decomressStreamEnd)
return 0;
if (iMaxLen > 2 * 1024 * 1024 * 1024LL)
{
finishCompress();
parent->setErrorString(QLatin1String("Currently can't handle more than 2GB!"));
return -1;
stream.next_in = buffer.data();
stream.avail_in = iReadCnt;
}
ret = BZ2_bzDecompress(&stream);
if (ret != BZ_OK && ret != BZ_STREAM_END)
stream.next_out = out;
stream.avail_out = iMaxLen;
do
{
parent->setErrorString(QStringLiteral("Error executing BZ2_bzCompress: %1").arg(bzipError2String(ret)));
finishDecompress();
return -1;
}
}
while (stream.avail_out != 0 && ret != BZ_STREAM_END);
decomressStreamEnd = (ret == BZ_STREAM_END);
return stream.next_out - out;
if (ret != BZ_STREAM_END && stream.avail_in == 0)
{
qint64 iReadCnt = device->read(buffer.data(), buffer.size());
if (iReadCnt <= 0)
return -1;
stream.next_in = buffer.data();
stream.avail_in = iReadCnt;
}
ret = BZ2_bzDecompress(&stream);
if (ret != BZ_OK && ret != BZ_STREAM_END)
{
parent->setErrorString(QStringLiteral("Error executing BZ2_bzCompress: %1").arg(bzipError2String(ret)));
finishDecompress();
return -1;
}
} while (stream.avail_out != 0 && ret != BZ_STREAM_END);
decomressStreamEnd = (ret == BZ_STREAM_END);
return stream.next_out - out;
}
bool BZip2IODevice::Private::finishDecompress()
{
if (!initialized)
return true;
BZ2_bzDecompressEnd(&stream);
if (!initialized)
return true;
BZ2_bzDecompressEnd(&stream);
initialized = false;
return true;
initialized = false;
return true;
}
QString BZip2IODevice::Private::bzipError2String(int errCode)
{
switch (errCode)
{
case BZ_SEQUENCE_ERROR:
return QLatin1String("Sequence error");
case BZ_PARAM_ERROR:
return QLatin1String("Parameter not correct");
case BZ_MEM_ERROR:
return QLatin1String("Memory allocation error");
case BZ_DATA_ERROR:
return QLatin1String("Invalid input data");
case BZ_DATA_ERROR_MAGIC:
return QLatin1String("Not a bzip2 file");
case BZ_IO_ERROR:
return QLatin1String("I/O error");
case BZ_UNEXPECTED_EOF:
return QLatin1String("Unexpected end of file");
case BZ_OUTBUFF_FULL:
return QLatin1String("Output buffer full");
case BZ_CONFIG_ERROR:
return QLatin1String("Wrong config parameter");
};
return QString::number(errCode);
switch (errCode)
{
case BZ_SEQUENCE_ERROR:
return QLatin1String("Sequence error");
case BZ_PARAM_ERROR:
return QLatin1String("Parameter not correct");
case BZ_MEM_ERROR:
return QLatin1String("Memory allocation error");
case BZ_DATA_ERROR:
return QLatin1String("Invalid input data");
case BZ_DATA_ERROR_MAGIC:
return QLatin1String("Not a bzip2 file");
case BZ_IO_ERROR:
return QLatin1String("I/O error");
case BZ_UNEXPECTED_EOF:
return QLatin1String("Unexpected end of file");
case BZ_OUTBUFF_FULL:
return QLatin1String("Output buffer full");
case BZ_CONFIG_ERROR:
return QLatin1String("Wrong config parameter");
};
return QString::number(errCode);
}
BZip2IODevice::BZip2IODevice(QIODevice *dev, int blocksize)
: d(new Private(this, dev, blocksize))
{}
BZip2IODevice::BZip2IODevice(QIODevice* dev, int blocksize) : d(new Private(this, dev, blocksize)) {}
BZip2IODevice::~BZip2IODevice()
{
close();
delete d;
close();
delete d;
}
void BZip2IODevice::setBufferSize(unsigned int size)
{
d->iMaxBufSize = size > 1024 ? size : 1024;
d->buffer.resize(d->iMaxBufSize);
d->iMaxBufSize = size > 1024 ? size : 1024;
d->buffer.resize(d->iMaxBufSize);
}
void BZip2IODevice::setBlockSize(unsigned int size)
{
if (isOpen())
return;
d->blocksize = size;
if (isOpen())
return;
d->blocksize = size;
}
bool BZip2IODevice::isSequential() const
{
return true;
return true;
}
bool BZip2IODevice::open(OpenMode mode)
{
if ((mode & QIODevice::ReadWrite) == 0)
{
setErrorString(QStringLiteral("Unsupported OpenMode %1").arg(mode));
return false;
}
if ((mode & QIODevice::ReadWrite) == QIODevice::ReadWrite)
{
setErrorString(QLatin1String("Can't read and write simultaniously"));
return false;
}
if ((mode & QIODevice::ReadOnly) == QIODevice::ReadOnly)
{
if (!d->device)
if ((mode & QIODevice::ReadWrite) == 0)
{
setErrorString(QLatin1String("No QIODevice for reading available"));
return false;
setErrorString(QStringLiteral("Unsupported OpenMode %1").arg(static_cast<int>(mode)));
return false;
}
if ((d->device->openMode() & QIODevice::ReadOnly) == 0)
if ((mode & QIODevice::ReadWrite) == QIODevice::ReadWrite)
{
setErrorString(QLatin1String("QIODevice not opened for reading"));
return false;
setErrorString(QLatin1String("Can't read and write simultaniously"));
return false;
}
if (!d->initializeDecompress())
return false;
}
else
{
if (!d->device)
if ((mode & QIODevice::ReadOnly) == QIODevice::ReadOnly)
{
setErrorString(QLatin1String("No QIODevice for writing available"));
return false;
if (!d->device)
{
setErrorString(QLatin1String("No QIODevice for reading available"));
return false;
}
if ((d->device->openMode() & QIODevice::ReadOnly) == 0)
{
setErrorString(QLatin1String("QIODevice not opened for reading"));
return false;
}
if (!d->initializeDecompress())
return false;
}
if ((d->device->openMode() & QIODevice::WriteOnly) == 0)
else
{
setErrorString(QLatin1String("QIODevice not opened for writing"));
return false;
if (!d->device)
{
setErrorString(QLatin1String("No QIODevice for writing available"));
return false;
}
if ((d->device->openMode() & QIODevice::WriteOnly) == 0)
{
setErrorString(QLatin1String("QIODevice not opened for writing"));
return false;
}
if (!d->initializeCompress())
return false;
}
if (!d->initializeCompress())
return false;
}
setErrorString(QString());
setOpenMode(mode);
return QIODevice::open(mode);
setErrorString(QString());
setOpenMode(mode);
return QIODevice::open(mode);
}
void BZip2IODevice::abort()
{
d->aborted = 1;
d->aborted = 1;
}
void BZip2IODevice::close()
{
if (!isOpen())
return;
if ((openMode() & QIODevice::ReadOnly) == QIODevice::ReadOnly)
d->finishDecompress();
else
d->finishCompress();
QIODevice::close();
if (!isOpen())
return;
if ((openMode() & QIODevice::ReadOnly) == QIODevice::ReadOnly)
d->finishDecompress();
else
d->finishCompress();
QIODevice::close();
}
bool BZip2IODevice::atEnd() const
{
if (!d->device)
return true;
return d->device->atEnd() && d->decomressStreamEnd && QIODevice::atEnd();
if (!d->device)
return true;
return d->device->atEnd() && d->decomressStreamEnd && QIODevice::atEnd();
}
qint64 BZip2IODevice::readData(char *data, qint64 maxSize)
qint64 BZip2IODevice::readData(char* data, qint64 maxSize)
{
return d->doDecompress(data, maxSize);
return d->doDecompress(data, maxSize);
}
qint64 BZip2IODevice::writeData(const char *data, qint64 maxSize)
qint64 BZip2IODevice::writeData(const char* data, qint64 maxSize)
{
return d->doCompress(data, maxSize);
return d->doCompress(data, maxSize);
}
+405 -445
View File
@@ -1,569 +1,529 @@
#include "nglcomp_compress.h"
#include "nglcomp_bzip2iodevice.h"
#include "nglcomp_tarfilter.h"
#include <NGLLogger/NGLLoggerBootstrap.h>
#include <QBuffer>
#include <QtConcurrent>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QFutureWatcher>
#include <QIODevice>
#include <QStringList>
#include <QtConcurrent>
#include <memory>
#include <utility>
#include <QDebug>
namespace NGL
{
Compress::Compress(QObject *parent)
: QObject(parent)
, m_u2_memFiles()
, m_aborted(0)
Compress::Compress(QObject* parent)
: QObject(parent), m_u2_memFiles(), m_aborted(0),
m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("nglcompress"))
{
DEBUG_LOG(m_logger) << "c_tor";
DEBUG_LOG(m_logger) << "c_tor";
QTimer *t = new QTimer(this);
t->setInterval(1000);
connect(t, &QTimer::timeout, this, [](){qWarning() << "still alive ...";});
t->start();
QTimer* t = new QTimer(this);
t->setInterval(1000);
connect(t, &QTimer::timeout, this, []() { qWarning() << "still alive ..."; });
t->start();
}
Compress::~Compress()
{
qWarning() << "## COMPRESS DTOR!!!!";
qWarning() << "## COMPRESS DTOR!!!!";
DEBUG_LOG(m_logger) << "~";
DEBUG_LOG(m_logger) << "~";
abort();
m_future.waitForFinished();
DEBUG_LOG(m_logger) << "~ finished";
abort();
m_future.waitForFinished();
DEBUG_LOG(m_logger) << "~ finished";
}
void Compress::abort()
{
DEBUG_LOG(m_logger) << "ABORT";
DEBUG_LOG(m_logger) << "ABORT";
m_aborted = 1;
m_lastError = tr("Aborted");
m_aborted = 1;
m_lastError = tr("Aborted");
if (m_currentTarFilter != nullptr)
{
DEBUG_LOG(m_logger) << "aborting tar";
m_currentTarFilter->abort();
}
if (m_currentTarFilter != nullptr)
{
DEBUG_LOG(m_logger) << "aborting tar";
m_currentTarFilter->abort();
}
if (m_currentBzipDevice != nullptr)
{
DEBUG_LOG(m_logger) << "aborting bzip";
m_currentBzipDevice->abort();
}
if (m_currentBzipDevice != nullptr)
{
DEBUG_LOG(m_logger) << "aborting bzip";
m_currentBzipDevice->abort();
}
}
void Compress::compress(QIODevice *out,
const QString &filesRootDir,
const QVector<FileInformations> &files,
const QVector<FileInformations> &memFiles,
const QString &destRootDir)
void Compress::compress(QIODevice* out, const QString& filesRootDir, const QVector<FileInformations>& files,
const QVector<FileInformations>& memFiles, const QString& destRootDir)
{
DEBUG_LOG(m_logger) << "compress #2";
reset();
setupFutureWatcher(QtConcurrent::run([this, out, filesRootDir, files, memFiles, destRootDir]() {
return this->_compress(out, filesRootDir, files, memFiles, destRootDir);
}));
DEBUG_LOG(m_logger) << "compress #2";
reset();
setupFutureWatcher(QtConcurrent::run([this, out, filesRootDir, files, memFiles, destRootDir]() {
return this->_compress(out, filesRootDir, files, memFiles, destRootDir);
}));
}
void Compress::compress(QIODevice *out, const QString &sourceDir, const QString &destRootDir)
void Compress::compress(QIODevice* out, const QString& sourceDir, const QString& destRootDir)
{
DEBUG_LOG(m_logger) << "compress #1 source dir: " << sourceDir << ", destRootDir: " << destRootDir;
reset();
DEBUG_LOG(m_logger) << "compress #1 source dir: " << sourceDir << ", destRootDir: " << destRootDir;
reset();
QVector<Compress::FileInformations> inFiles = directoryFileList(sourceDir, true);
QVector<Compress::FileInformations> inFiles = directoryFileList(sourceDir, true);
int len = sourceDir.length();
if (!sourceDir.endsWith(QLatin1Char('/')))
len += 1;
for (auto &fi : inFiles)
{
fi.inputFileName = fi.inputFileName.mid(len);
fi.bAbsInputPath = false;
DEBUG_LOG(m_logger) << "fi: ifm = " << fi.inputFileName << ", abs = " << fi.bAbsInputPath;
}
int len = sourceDir.length();
if (!sourceDir.endsWith(QLatin1Char('/')))
len += 1;
for (auto& fi : inFiles)
{
fi.inputFileName = fi.inputFileName.mid(len);
fi.bAbsInputPath = false;
DEBUG_LOG(m_logger) << "fi: ifm = " << fi.inputFileName << ", abs = " << fi.bAbsInputPath;
}
DEBUG_LOG(m_logger) << "FI list size: " << inFiles.count();
DEBUG_LOG(m_logger) << "FI list size: " << inFiles.count();
compress(out, sourceDir, inFiles, {}, destRootDir);
compress(out, sourceDir, inFiles, {}, destRootDir);
}
bool Compress::_compress(QIODevice *output,
const QString &filesRootDir,
const QVector<FileInformations> &files,
const QVector<FileInformations> &memFiles,
const QString &_destRootDir)
bool Compress::_compress(QIODevice* output, const QString& filesRootDir, const QVector<FileInformations>& files,
const QVector<FileInformations>& memFiles, const QString& _destRootDir)
{
std::shared_ptr<int> invalidatePointersOnScopeLeave(nullptr, [this](int *)
{
DEBUG_LOG(m_logger) << "invalidatePointersOnScopeLeave #1";
m_currentTarFilter = nullptr;
m_currentBzipDevice = nullptr;
});
(void)invalidatePointersOnScopeLeave;
std::shared_ptr<int> invalidatePointersOnScopeLeave(nullptr, [this](int*) {
DEBUG_LOG(m_logger) << "invalidatePointersOnScopeLeave #1";
m_currentTarFilter = nullptr;
m_currentBzipDevice = nullptr;
});
(void)invalidatePointersOnScopeLeave;
if (!output || !output->isWritable())
{
m_lastError = tr("Device is not writeable");
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;;
return false;
}
const QString destRootDir = _destRootDir.endsWith(QLatin1Char('/')) ?
_destRootDir : _destRootDir + QLatin1Char('/');
const qint64 maxCount = files.count() + memFiles.count();
qint64 count = 0;
DEBUG_LOG(m_logger) << "destRootDir = " << destRootDir << ", maxCount = " << maxCount << ", count = " << count;
BZip2IODevice bzip2(output, 9);
m_currentBzipDevice = &bzip2;
if (!bzip2.open(QIODevice::WriteOnly))
{
m_lastError = bzip2.errorString();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
qint64 bytesTotal = 0;
for (const FileInformations &file : std::as_const(files))
{
if (m_aborted) return false;
const QString in = file.bAbsInputPath ? file.inputFileName : filesRootDir + QLatin1Char('/') + file.inputFileName;
const QFileInfo fi(in);
bytesTotal += fi.size();
}
qint64 currentFileSize = 0;
QString currentFileName;
auto readCallback = [this, &maxCount, &count, &currentFileSize, &currentFileName, &bytesTotal](const qint64 iSize)
{
DEBUG_LOG(m_logger) << "readCallback: maxCount = " << maxCount << ", count = " << count
<< ", currentFileSize = " << currentFileSize << ", currentFileName = " << currentFileName << ", iSize = " << iSize;
emit compressInfoEx(maxCount, count, currentFileSize, (currentFileSize - iSize), currentFileName, bytesTotal);
};
TarFilter tf(&bzip2, readCallback);
m_currentTarFilter = &tf;
const QStringList paths = destRootDir.split(QLatin1Char('/'), Qt::SkipEmptyParts);
QString curPath;
DEBUG_LOG(m_logger) << "prepairing tar filter";
for (const QString &path : paths)
{
DEBUG_LOG(m_logger) << "path = " << path;
if (m_aborted) return false;
if (curPath.isEmpty())
curPath = path + QLatin1Char('/');
else
curPath += path + QLatin1Char('/');
DEBUG_LOG(m_logger) << "curPath = " << curPath << ", filesRootDir.isEmpty() = " << filesRootDir.isEmpty();
if (filesRootDir.isEmpty())
if (!output || !output->isWritable())
{
if (!tf.addData(curPath, QByteArray(), true))
{
m_lastError = tf.lastError();
m_lastError = tr("Device is not writeable");
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
;
return false;
}
}
else
const QString destRootDir =
_destRootDir.endsWith(QLatin1Char('/')) ? _destRootDir : _destRootDir + QLatin1Char('/');
const qint64 maxCount = files.count() + memFiles.count();
qint64 count = 0;
DEBUG_LOG(m_logger) << "destRootDir = " << destRootDir << ", maxCount = " << maxCount << ", count = " << count;
BZip2IODevice bzip2(output, 9);
m_currentBzipDevice = &bzip2;
if (!bzip2.open(QIODevice::WriteOnly))
{
if (!tf.addFile(filesRootDir, curPath))
{
m_lastError = tf.lastError();
m_lastError = bzip2.errorString();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
}
}
m_c_sizeInfo.realSize = 0;
for (const FileInformations &file : std::as_const(files))
{
DEBUG_LOG(m_logger) << "file = " << file.inputFileName;
emit compressInfo(maxCount, ++count, file.inputFileName);
if (m_aborted) return false;
QString in = file.bAbsInputPath ? file.inputFileName : filesRootDir + QLatin1Char('/') + file.inputFileName;
QString out = destRootDir + (file.outputFileName.isEmpty() ? file.inputFileName : file.outputFileName);
DEBUG_LOG(m_logger) << "in = " << in << ", out = " << out;
QFileInfo fi(in);
currentFileName = fi.fileName();
currentFileSize = fi.size();
m_c_sizeInfo.realSize += currentFileSize;
DEBUG_LOG(m_logger) << "currentFileName = " << currentFileName << ", currentFileSize = " << currentFileSize << "m_c_sizeInfo.realSize" << m_c_sizeInfo.realSize;
if (!tf.addFile(in, out))
qint64 bytesTotal = 0;
for (const FileInformations& file : std::as_const(files))
{
m_lastError = tf.lastError();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
if (m_aborted)
return false;
const QString in =
file.bAbsInputPath ? file.inputFileName : filesRootDir + QLatin1Char('/') + file.inputFileName;
const QFileInfo fi(in);
bytesTotal += fi.size();
}
}
for (const FileInformations &file : std::as_const(memFiles))
{
DEBUG_LOG(m_logger) << "(mem) file = " << file.inputFileName;
emit compressInfo(maxCount, ++count, file.inputFileName);
if (m_aborted) return false;
qint64 currentFileSize = 0;
QString currentFileName;
auto readCallback = [this, &maxCount, &count, &currentFileSize, &currentFileName, &bytesTotal](const qint64 iSize) {
DEBUG_LOG(m_logger) << "readCallback: maxCount = " << maxCount << ", count = " << count
<< ", currentFileSize = " << currentFileSize << ", currentFileName = " << currentFileName
<< ", iSize = " << iSize;
emit compressInfoEx(maxCount, count, currentFileSize, (currentFileSize - iSize), currentFileName, bytesTotal);
};
currentFileName = file.inputFileName;
currentFileSize = file.fileData.size();
m_c_sizeInfo.compressedSize += currentFileSize;
TarFilter tf(&bzip2, readCallback);
m_currentTarFilter = &tf;
const QStringList paths = destRootDir.split(QLatin1Char('/'), Qt::SkipEmptyParts);
QString curPath;
DEBUG_LOG(m_logger) << "(mem) currentFileName = " << currentFileName << ", currentFileSize = " << currentFileSize << "m_c_sizeInfo.realSize" << m_c_sizeInfo.realSize;
if (!tf.addData(file.inputFileName, file.fileData))
DEBUG_LOG(m_logger) << "prepairing tar filter";
for (const QString& path : paths)
{
m_lastError = tf.lastError();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
DEBUG_LOG(m_logger) << "path = " << path;
if (m_aborted)
return false;
if (curPath.isEmpty())
curPath = path + QLatin1Char('/');
else
curPath += path + QLatin1Char('/');
DEBUG_LOG(m_logger) << "curPath = " << curPath << ", filesRootDir.isEmpty() = " << filesRootDir.isEmpty();
if (filesRootDir.isEmpty())
{
if (!tf.addData(curPath, QByteArray(), true))
{
m_lastError = tf.lastError();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
}
else
{
if (!tf.addFile(filesRootDir, curPath))
{
m_lastError = tf.lastError();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
}
}
}
m_c_sizeInfo.realSize = 0;
for (const FileInformations& file : std::as_const(files))
{
DEBUG_LOG(m_logger) << "file = " << file.inputFileName;
emit compressInfo(maxCount, ++count, file.inputFileName);
DEBUG_LOG(m_logger) << "writing eos";
tf.writeEOS();
if (m_aborted)
return false;
DEBUG_LOG(m_logger) << "closing bzip";
bzip2.close();
QString in = file.bAbsInputPath ? file.inputFileName : filesRootDir + QLatin1Char('/') + file.inputFileName;
QString out = destRootDir + (file.outputFileName.isEmpty() ? file.inputFileName : file.outputFileName);
m_c_sizeInfo.compressedSize = output->size();
DEBUG_LOG(m_logger) << "m_c_sizeInfo.compressedSize = " << m_c_sizeInfo.compressedSize;
DEBUG_LOG(m_logger) << "in = " << in << ", out = " << out;
m_success = true;
QFileInfo fi(in);
DEBUG_LOG(m_logger) << "DONE!";
currentFileName = fi.fileName();
currentFileSize = fi.size();
m_c_sizeInfo.realSize += currentFileSize;
return true;
DEBUG_LOG(m_logger) << "currentFileName = " << currentFileName << ", currentFileSize = " << currentFileSize
<< "m_c_sizeInfo.realSize" << m_c_sizeInfo.realSize;
if (!tf.addFile(in, out))
{
m_lastError = tf.lastError();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
}
for (const FileInformations& file : std::as_const(memFiles))
{
DEBUG_LOG(m_logger) << "(mem) file = " << file.inputFileName;
emit compressInfo(maxCount, ++count, file.inputFileName);
if (m_aborted)
return false;
currentFileName = file.inputFileName;
currentFileSize = file.fileData.size();
m_c_sizeInfo.compressedSize += currentFileSize;
DEBUG_LOG(m_logger) << "(mem) currentFileName = " << currentFileName
<< ", currentFileSize = " << currentFileSize << "m_c_sizeInfo.realSize"
<< m_c_sizeInfo.realSize;
if (!tf.addData(file.inputFileName, file.fileData))
{
m_lastError = tf.lastError();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
}
DEBUG_LOG(m_logger) << "writing eos";
tf.writeEOS();
DEBUG_LOG(m_logger) << "closing bzip";
bzip2.close();
m_c_sizeInfo.compressedSize = output->size();
DEBUG_LOG(m_logger) << "m_c_sizeInfo.compressedSize = " << m_c_sizeInfo.compressedSize;
m_success = true;
DEBUG_LOG(m_logger) << "DONE!";
return true;
}
void Compress::uncompress(QIODevice *in, const QString &outDir)
void Compress::uncompress(QIODevice* in, const QString& outDir)
{
DEBUG_LOG(m_logger) << "uncompress #1";
reset();
setupFutureWatcher(QtConcurrent::run([this, in, outDir](){ return this->_uncompressDirectory(in, outDir); }));
DEBUG_LOG(m_logger) << "uncompress #1";
reset();
setupFutureWatcher(QtConcurrent::run([this, in, outDir]() { return this->_uncompressDirectory(in, outDir); }));
}
bool Compress::_uncompressDirectory(QIODevice *input, const QString &outDir)
bool Compress::_uncompressDirectory(QIODevice* input, const QString& outDir)
{
DEBUG_LOG(m_logger) << "_uncompressDirectory";
DEBUG_LOG(m_logger) << "_uncompressDirectory";
std::shared_ptr<int> invalidatePointersOnScopeLeave(nullptr, [this](int *)
{
DEBUG_LOG(m_logger) << "invalidatePointersOnScopeLeave #2";
m_currentTarFilter = nullptr;
m_currentBzipDevice = nullptr;
});
(void)invalidatePointersOnScopeLeave;
std::shared_ptr<int> invalidatePointersOnScopeLeave(nullptr, [this](int*) {
DEBUG_LOG(m_logger) << "invalidatePointersOnScopeLeave #2";
m_currentTarFilter = nullptr;
m_currentBzipDevice = nullptr;
});
(void)invalidatePointersOnScopeLeave;
if (!input || !input->isReadable())
{
m_lastError = tr("Device is not readable");
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
BZip2IODevice bzip2(input, 9);
m_currentBzipDevice = &bzip2;
if (!bzip2.open(QIODevice::ReadOnly))
{
m_lastError = bzip2.errorString();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
TarFilter tf(&bzip2);
m_currentTarFilter = &tf;
TarFilter::FileInformations tfi;
QDir dir(outDir);
DEBUG_LOG(m_logger) << "dir = " << dir.path();
while (tf.getData(tfi))
{
DEBUG_LOG(m_logger) << "tfi.fileName = " << tfi.fileName;
emit uncompressInfo(input->size(), input->pos(), tfi.fileName);
if (m_aborted) return false;
QString path = outDir + QLatin1Char('/') + tfi.fileName;
DEBUG_LOG(m_logger) << "path = " << path;
if (tfi.fileType == TarFilter::directory)
if (!input || !input->isReadable())
{
DEBUG_LOG(m_logger) << "tfi.fileType == TarFilter::directory";
if (!dir.exists(path) && !dir.mkpath(path))
{
m_lastError = tr("Error creating directory %1").arg(outDir);
m_lastError = tr("Device is not readable");
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
}
else
{
QString pathName = QFileInfo(path).absolutePath();
if (!dir.exists(pathName) && !dir.mkpath(pathName))
{
m_lastError = tr("Error creating directory %1").arg(pathName);
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
QFile f(path);
if (!f.open(QIODevice::WriteOnly))
{
m_lastError = tr("Error opening %1 for writing (%2)").arg(path, f.errorString());
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
if (!tf.getData(&f))
{
m_lastError = tf.lastError();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
}
}
DEBUG_LOG(m_logger) << "uncompress done!";
m_success = true;
return true;
return false;
}
BZip2IODevice bzip2(input, 9);
m_currentBzipDevice = &bzip2;
if (!bzip2.open(QIODevice::ReadOnly))
{
m_lastError = bzip2.errorString();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
TarFilter tf(&bzip2);
m_currentTarFilter = &tf;
TarFilter::FileInformations tfi;
QDir dir(outDir);
DEBUG_LOG(m_logger) << "dir = " << dir.path();
while (tf.getData(tfi))
{
DEBUG_LOG(m_logger) << "tfi.fileName = " << tfi.fileName;
emit uncompressInfo(input->size(), input->pos(), tfi.fileName);
if (m_aborted)
return false;
QString path = outDir + QLatin1Char('/') + tfi.fileName;
DEBUG_LOG(m_logger) << "path = " << path;
if (tfi.fileType == TarFilter::directory)
{
DEBUG_LOG(m_logger) << "tfi.fileType == TarFilter::directory";
if (!dir.exists(path) && !dir.mkpath(path))
{
m_lastError = tr("Error creating directory %1").arg(outDir);
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
}
else
{
QString pathName = QFileInfo(path).absolutePath();
if (!dir.exists(pathName) && !dir.mkpath(pathName))
{
m_lastError = tr("Error creating directory %1").arg(pathName);
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
QFile f(path);
if (!f.open(QIODevice::WriteOnly))
{
m_lastError = tr("Error opening %1 for writing (%2)").arg(path, f.errorString());
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
if (!tf.getData(&f))
{
m_lastError = tf.lastError();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
}
}
DEBUG_LOG(m_logger) << "uncompress done!";
m_success = true;
return true;
}
void Compress::uncompress(QIODevice *in)
void Compress::uncompress(QIODevice* in)
{
DEBUG_LOG(m_logger) << "uncompress #2!";
reset();
setupFutureWatcher(QtConcurrent::run([this, in](){ return this->_uncompressBuffer(in); }));
DEBUG_LOG(m_logger) << "uncompress #2!";
reset();
setupFutureWatcher(QtConcurrent::run([this, in]() { return this->_uncompressBuffer(in); }));
}
bool Compress::_uncompressBuffer(QIODevice *input)
bool Compress::_uncompressBuffer(QIODevice* input)
{
DEBUG_LOG(m_logger) << "uncompressBuffer";
DEBUG_LOG(m_logger) << "uncompressBuffer";
std::shared_ptr<int> invalidatePointersOnScopeLeave(nullptr, [this](int *)
{
DEBUG_LOG(m_logger) << "invalidatePointersOnScopeLeave #2";
m_currentTarFilter = nullptr;
m_currentBzipDevice = nullptr;
});
(void)invalidatePointersOnScopeLeave;
std::shared_ptr<int> invalidatePointersOnScopeLeave(nullptr, [this](int*) {
DEBUG_LOG(m_logger) << "invalidatePointersOnScopeLeave #2";
m_currentTarFilter = nullptr;
m_currentBzipDevice = nullptr;
});
(void)invalidatePointersOnScopeLeave;
if (!input->isReadable())
{
m_lastError = tr("Device is not readable");
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
BZip2IODevice bzip2(input, 9);
m_currentBzipDevice = &bzip2;
if (!bzip2.open(QIODevice::ReadOnly))
{
m_lastError = bzip2.errorString();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
TarFilter tf(&bzip2);
m_currentTarFilter = &tf;
TarFilter::FileInformations tfi;
while (tf.getData(tfi))
{
DEBUG_LOG(m_logger) << "tfi.fileName = " << tfi.fileName;
emit uncompressInfo(input->size(), input->pos(), tfi.fileName);
if (m_aborted) return false;
FileInformations fi;
fi.outputFileName = tfi.fileName;
DEBUG_LOG(m_logger) << "fi.outputFileName = " << fi.outputFileName;
if (tfi.fileType == TarFilter::directory)
if (!input->isReadable())
{
fi.outputFileName += QLatin1Char('/');
DEBUG_LOG(m_logger) << "> fi.outputFileName = " << fi.outputFileName;
}
else
{
DEBUG_LOG(m_logger) << "stuff";
m_lastError = tr("Device is not readable");
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
QBuffer buf(&fi.fileData);
if (!buf.open(QIODevice::WriteOnly))
{
m_lastError = tr("Error opening buffer for writing (%1)").arg(buf.errorString());
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
if (!tf.getData(&buf))
{
m_lastError = tf.lastError();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
}
fi.bAbsInputPath = fi.outputFileName.startsWith(QLatin1Char('/'));
DEBUG_LOG(m_logger) << "added fi.bAbsInputPath = " << fi.bAbsInputPath;
m_u2_memFiles += fi;
}
BZip2IODevice bzip2(input, 9);
m_currentBzipDevice = &bzip2;
if (!bzip2.open(QIODevice::ReadOnly))
{
m_lastError = bzip2.errorString();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
m_success = true;
return true;
return false;
}
TarFilter tf(&bzip2);
m_currentTarFilter = &tf;
TarFilter::FileInformations tfi;
while (tf.getData(tfi))
{
DEBUG_LOG(m_logger) << "tfi.fileName = " << tfi.fileName;
emit uncompressInfo(input->size(), input->pos(), tfi.fileName);
if (m_aborted)
return false;
FileInformations fi;
fi.outputFileName = tfi.fileName;
DEBUG_LOG(m_logger) << "fi.outputFileName = " << fi.outputFileName;
if (tfi.fileType == TarFilter::directory)
{
fi.outputFileName += QLatin1Char('/');
DEBUG_LOG(m_logger) << "> fi.outputFileName = " << fi.outputFileName;
}
else
{
DEBUG_LOG(m_logger) << "stuff";
QBuffer buf(&fi.fileData);
if (!buf.open(QIODevice::WriteOnly))
{
m_lastError = tr("Error opening buffer for writing (%1)").arg(buf.errorString());
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
if (!tf.getData(&buf))
{
m_lastError = tf.lastError();
ERROR_LOG(m_logger) << "compress failed: " << m_lastError;
return false;
}
}
fi.bAbsInputPath = fi.outputFileName.startsWith(QLatin1Char('/'));
DEBUG_LOG(m_logger) << "added fi.bAbsInputPath = " << fi.bAbsInputPath;
m_u2_memFiles += fi;
}
m_success = true;
return true;
}
bool Compress::success() const
{
ERROR_LOG(m_logger) << "success? " << (m_future.isFinished() && !m_future.isCanceled() && m_success);
ERROR_LOG(m_logger) << "success details: "
<< "success m_future.isFinished() = " << (m_future.isFinished())
<< " !m_future.isCanceled() = " << !m_future.isCanceled()
<< " m_success = " << m_success;
return (m_future.isFinished() && !m_future.isCanceled() && m_success);
ERROR_LOG(m_logger) << "success? " << (m_future.isFinished() && !m_future.isCanceled() && m_success);
ERROR_LOG(m_logger) << "success details: "
<< "success m_future.isFinished() = " << (m_future.isFinished())
<< " !m_future.isCanceled() = " << !m_future.isCanceled() << " m_success = " << m_success;
return (m_future.isFinished() && !m_future.isCanceled() && m_success);
}
bool Compress::waitForFinished()
{
DEBUG_LOG(m_logger) << "waitForFinished...";
m_future.waitForFinished();
DEBUG_LOG(m_logger) << "waitForFinished... done!";
return success();
DEBUG_LOG(m_logger) << "waitForFinished...";
m_future.waitForFinished();
DEBUG_LOG(m_logger) << "waitForFinished... done!";
return success();
}
QVector<Compress::FileInformations> Compress::directoryFileList(const QString &dir, bool recursive)
QVector<Compress::FileInformations> Compress::directoryFileList(const QString& dir, bool recursive)
{
DEBUG_LOG(m_logger) << "directoryFileList";
DEBUG_LOG(m_logger) << "directoryFileList";
QVector<FileInformations> sl_absolute;
QDir d(dir);
QVector<FileInformations> sl_absolute;
QDir d(dir);
if (d.exists())
{
DEBUG_LOG(m_logger) << "exists";
const auto entries = d.entryList(QDir::Files);
sl_absolute.reserve(sl_absolute.size() + entries.size());
for (const QString &entry : entries)
sl_absolute << FileInformations(QFileInfo(d, entry).absoluteFilePath(), true);
}
if (d.exists())
{
DEBUG_LOG(m_logger) << "exists";
const auto entries = d.entryList(QDir::Files);
sl_absolute.reserve(sl_absolute.size() + entries.size());
for (const QString& entry : entries)
sl_absolute << FileInformations(QFileInfo(d, entry).absoluteFilePath(), true);
}
if (recursive)
{
DEBUG_LOG(m_logger) << "recursive";
const auto entries = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
sl_absolute.reserve(sl_absolute.size() + entries.size());
for (const QString &s : entries)
sl_absolute << directoryFileList(dir + QLatin1Char('/') + s, true);
}
if (recursive)
{
DEBUG_LOG(m_logger) << "recursive";
const auto entries = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
sl_absolute.reserve(sl_absolute.size() + entries.size());
for (const QString& s : entries)
sl_absolute << directoryFileList(dir + QLatin1Char('/') + s, true);
}
DEBUG_LOG(m_logger) << "done";
return sl_absolute;
DEBUG_LOG(m_logger) << "done";
return sl_absolute;
}
void Compress::reset()
{
DEBUG_LOG(m_logger) << "reset";
m_u2_memFiles.clear();
DEBUG_LOG(m_logger) << "reset";
m_u2_memFiles.clear();
m_lastError.clear();
m_lastError.clear();
m_c_sizeInfo.realSize = 0;
m_c_sizeInfo.compressedSize = 0;
m_c_sizeInfo.realSize = 0;
m_c_sizeInfo.compressedSize = 0;
m_success = false;
m_success = false;
}
void Compress::setupFutureWatcher(const QFuture<bool> &future)
void Compress::setupFutureWatcher(const QFuture<bool>& future)
{
DEBUG_LOG(m_logger) << "setupFutureWatcher";
m_future = future;
QFutureWatcher<bool> *fw = new QFutureWatcher<bool>(this);
QObject::connect(fw, &QObject::destroyed, this, [this]() { DEBUG_LOG(m_logger) << "Future Watcher destroyed"; });
QObject::connect(fw, &QFutureWatcher<bool>::finished, this, &Compress::finished);
QObject::connect(fw, &QFutureWatcher<bool>::finished, fw, &QObject::deleteLater);
QObject::connect(fw, &QFutureWatcher<bool>::finished, fw, [this]() { DEBUG_LOG(m_logger) << "Future Watcher about to be deleted"; });
fw->setFuture(m_future);
DEBUG_LOG(m_logger) << "setupFutureWatcher";
m_future = future;
QFutureWatcher<bool>* fw = new QFutureWatcher<bool>(this);
QObject::connect(fw, &QObject::destroyed, this, [this]() { DEBUG_LOG(m_logger) << "Future Watcher destroyed"; });
QObject::connect(fw, &QFutureWatcher<bool>::finished, this, &Compress::finished);
QObject::connect(fw, &QFutureWatcher<bool>::finished, fw, &QObject::deleteLater);
QObject::connect(fw, &QFutureWatcher<bool>::finished, fw,
[this]() { DEBUG_LOG(m_logger) << "Future Watcher about to be deleted"; });
fw->setFuture(m_future);
}
}
} // namespace NGL
+6 -9
View File
@@ -1,19 +1,16 @@
#pragma once
#include "commandinterface.h"
#include "sv_export.h"
#include "tcpserver_export.h"
class TCPSERVER_EXPORT CBI_Help : public CommandInterface
{
Q_OBJECT
public:
explicit CBI_Help(unsigned short port);
Q_OBJECT
public:
explicit CBI_Help(unsigned short port);
void handleCommandInternal(const QVector<QByteArray> &args) override;
QString help() const override;
void handleCommandInternal(const QVector<QByteArray>& args) override;
QString help() const override;
};
+6 -9
View File
@@ -1,19 +1,16 @@
#pragma once
#include "commandinterface.h"
#include "sv_export.h"
#include "tcpserver_export.h"
class CBI_PingPong : public CommandInterface
{
Q_OBJECT
public:
explicit CBI_PingPong(unsigned short port);
Q_OBJECT
public:
explicit CBI_PingPong(unsigned short port);
void handleCommandInternal(const QVector<QByteArray> &args) override;
QString help() const override;
void handleCommandInternal(const QVector<QByteArray>& args) override;
QString help() const override;
};
+6 -9
View File
@@ -1,19 +1,16 @@
#pragma once
#include "commandinterface.h"
#include "sv_export.h"
#include "tcpserver_export.h"
class TCPSERVER_EXPORT CBI_Quit : public CommandInterface
{
Q_OBJECT
public:
explicit CBI_Quit(unsigned short port);
Q_OBJECT
public:
explicit CBI_Quit(unsigned short port);
void handleCommandInternal(const QVector<QByteArray> &args) override;
QString help() const override;
void handleCommandInternal(const QVector<QByteArray>& args) override;
QString help() const override;
};
+12 -10
View File
@@ -1,23 +1,25 @@
#pragma once
#include "commandinterface.h"
#include "sv_export.h"
#include "tcpserver_export.h"
#include <QMap>
#include <QPair>
class QIODevice;
class CBI_Recvfile : public CommandInterface
{
Q_OBJECT
public:
explicit CBI_Recvfile(unsigned short port);
Q_OBJECT
public:
explicit CBI_Recvfile(unsigned short port);
void handleCommandInternal(const QVector<QByteArray> &args) override;
QString help() const override;
void newPlainData() override;
protected:
QMap<WorkerBase*, QPair<QIODevice*, int> > m_wbMap;
void handleCommandInternal(const QVector<QByteArray>& args) override;
QString help() const override;
void newPlainData() override;
protected:
QMap<WorkerBase*, QPair<QIODevice*, int>> m_wbMap;
};
+6 -7
View File
@@ -1,17 +1,16 @@
#pragma once
#include "commandinterface.h"
#include "sv_export.h"
#include "tcpserver_export.h"
class CBI_Sendfile : public CommandInterface
{
Q_OBJECT
public:
explicit CBI_Sendfile(unsigned short port);
Q_OBJECT
public:
explicit CBI_Sendfile(unsigned short port);
void handleCommandInternal(const QVector<QByteArray> &args) override;
QString help() const override;
void handleCommandInternal(const QVector<QByteArray>& args) override;
QString help() const override;
};
+10 -13
View File
@@ -1,25 +1,22 @@
#pragma once
#include "commandinterface.h"
#include "sv_export.h"
#include "tcpserver_export.h"
class TCPSERVER_EXPORT CBI_Shutdown : public CommandInterface
{
Q_OBJECT
public:
explicit CBI_Shutdown(unsigned short port);
Q_OBJECT
public:
explicit CBI_Shutdown(unsigned short port);
void handleCommandInternal(const QVector<QByteArray> &args) override;
QString help() const override;
void handleCommandInternal(const QVector<QByteArray>& args) override;
QString help() const override;
Q_SIGNALS:
Q_SIGNALS:
void shutdownRequest();
private:
void shutdownRequest();
private:
};
+51 -102
View File
@@ -1,158 +1,107 @@
#pragma once
#include "tcpserver_export.h"
#include <QObject>
#include <QString>
#include <QVector>
#include "sv_export.h"
class QAbstractSocket;
class QByteArray;
class WorkerBase;
class TCPSERVER_EXPORT CommandInterface : public QObject
{
Q_OBJECT
public:
Q_OBJECT
public:
explicit CommandInterface(unsigned short port, const QByteArray& command = QByteArray());
explicit CommandInterface(unsigned short port, const QByteArray &command = QByteArray());
virtual ~CommandInterface();
void handleCommand(const QVector<QByteArray>& args);
virtual ~CommandInterface();
virtual void newPlainData();
void handleCommand(const QVector<QByteArray> &args);
virtual QString help() const = 0;
virtual void newPlainData();
QByteArray command() const;
virtual QString help() const = 0;
void setAutoWriteEOL(bool newAutoWriteEOL);
QByteArray command() const;
bool autoWriteEOL() const;
bool errorOccured() const;
void setAutoWriteEOL(bool newAutoWriteEOL);
static void setDefaultSuccessMessage(const QString& msg);
static void setDefaultErrorMessage(const QString& msg);
bool autoWriteEOL() const;
template <class T> static CommandInterface* factory(unsigned short port)
{
return new T(port);
}
virtual void aboutToDestroy() {}
bool errorOccured() const;
protected:
virtual void handleCommandInternal(const QVector<QByteArray>& args) = 0;
void setWriteTimeout(int iMS);
static void setDefaultSuccessMessage(const QString &msg);
void writeData(const QByteArray& payload);
void writeData(const char* payload);
static void setDefaultErrorMessage(const QString &msg);
void writeData(const QString& payload);
template<class T>
static CommandInterface *factory(unsigned short port) { return new T(port); }
void writeData(const QStringList& payload);
void writeData(const QList<QByteArray>& payload);
virtual void aboutToDestroy() {}
protected:
void writeOk(const QString& additionalMsg = QString());
virtual void handleCommandInternal(const QVector<QByteArray> &args) = 0;
void writeError(const QString& additionalMsg = QString());
void setWriteTimeout(int iMS);
void writeEOL();
void writeData(const QByteArray &payload);
QByteArray quote(const QByteArray& payload);
void writeData(const char *payload);
QAbstractSocket* socket();
void writeData(const QString &payload);
void setSocket(QAbstractSocket* socket);
void writeData(const QStringList &payload);
WorkerBase* workerBase();
void writeData(const QList<QByteArray> &payload);
void setWorkerBase(WorkerBase* wb);
void writeOk(const QString &additionalMsg = QString());
void conversionError(const QByteArray& arg, const QByteArray& type);
void writeError(const QString &additionalMsg = QString());
bool getValue(const QByteArray& arg, bool& value);
void writeEOL();
bool getValue(const QByteArray& arg, long& value);
QByteArray quote(const QByteArray &payload);
bool getValue(const QByteArray& arg, unsigned long& value);
QAbstractSocket *socket();
bool getValue(const QByteArray& arg, int& value);
void setSocket(QAbstractSocket *socket);
bool getValue(const QByteArray& arg, unsigned int& value);
WorkerBase *workerBase();
bool getValue(const QByteArray& arg, float& value);
void setWorkerBase(WorkerBase *wb);
bool getValue(const QByteArray& arg, double& value);
void conversionError(const QByteArray &arg, const QByteArray &type);
unsigned short port() const;
bool getValue(const QByteArray &arg, bool &value);
void closeConnection();
bool getValue(const QByteArray &arg, long &value);
bool socketIsOpen() const;
bool getValue(const QByteArray &arg, unsigned long &value);
private:
friend class WorkerBase;
bool getValue(const QByteArray &arg, int &value);
class Private;
bool getValue(const QByteArray &arg, unsigned int &value);
bool getValue(const QByteArray &arg, float &value);
bool getValue(const QByteArray &arg, double &value);
unsigned short port() const;
void closeConnection();
bool socketIsOpen() const;
private:
friend class WorkerBase;
class Private;
Private *const d;
Private* const d;
};
+30 -62
View File
@@ -1,96 +1,64 @@
#pragma once
#include <QTcpServer>
#include "tcpserver_export.h"
#include "sv_export.h"
#include <QTcpServer>
class WorkerInstance;
class CommandInterface;
class TCPSERVER_EXPORT TcpServer : public QTcpServer
{
Q_OBJECT
public:
typedef QVector<CommandInterface*(*)(unsigned short) > CommandInterfaceList;
public:
Q_OBJECT
public:
typedef QVector<CommandInterface* (*)(unsigned short)> CommandInterfaceList;
virtual ~TcpServer();
public:
virtual ~TcpServer();
static TcpServer *instance(unsigned short port);
static TcpServer* instance(unsigned short port);
bool listen(const QHostAddress& address = QHostAddress::Any);
bool listen(const QHostAddress &address = QHostAddress::Any);
void setUseWorkerThreads(bool bEnable);
bool useWorkerThreads() const;
void setUseWorkerThreads(bool bEnable);
QVector<WorkerInstance*> workers() const;
unsigned short getListenPort() const;
bool useWorkerThreads() const;
static bool registerCommand(unsigned short port, CommandInterface* (*factory)(unsigned short));
static bool unregisterCommand(unsigned short port, CommandInterface* (*factory)(unsigned short));
QVector<WorkerInstance *> workers() const;
static CommandInterfaceList registeredCommands(unsigned short port);
void setUserData(void* userData);
unsigned short getListenPort() const;
void* userData() const;
Q_SIGNALS:
static bool registerCommand(unsigned short port, CommandInterface * (*factory)(unsigned short));
void aboutToQuit(unsigned short port);
public Q_SLOTS:
static bool unregisterCommand(unsigned short port, CommandInterface * (*factory)(unsigned short));
void shutdownRequest();
void workerFinished();
static CommandInterfaceList registeredCommands(unsigned short port);
protected:
void incomingConnection(qintptr socketDescriptor);
private:
class Private;
void setUserData(void *userData);
Private* const d;
explicit TcpServer(unsigned short port);
void *userData() const;
Q_SIGNALS:
void aboutToQuit(unsigned short port);
public Q_SLOTS:
void shutdownRequest();
void workerFinished();
protected:
void incomingConnection(qintptr socketDescriptor);
private:
class Private;
Private *const d;
explicit TcpServer(unsigned short port);
bool listen(const QHostAddress &address, quint16 port);
bool listen(const QHostAddress& address, quint16 port);
};
+41 -82
View File
@@ -1,128 +1,87 @@
#pragma once
#include "tcpserver_export.h"
#include <QByteArray>
#include <QMap>
#include <QPointer>
#include <QObject>
#include "sv_export.h"
#include <QPointer>
class QAbstractSocket;
class CommandInterface;
class TCPSERVER_EXPORT WorkerBase
{
public:
typedef QMap< QByteArray, CommandInterface *> CommandMap;
public:
public:
typedef QMap<QByteArray, CommandInterface*> CommandMap;
WorkerBase(qintptr sd, unsigned short port);
public:
WorkerBase(qintptr sd, unsigned short port);
virtual ~WorkerBase() = default;
virtual ~WorkerBase() = default;
QAbstractSocket *connection();
QAbstractSocket* connection();
const QAbstractSocket* connection() const;
const QAbstractSocket *connection() const;
void redirectDataHandling(bool bDoRedirect, CommandInterface* newDataHandler = nullptr);
static QByteArray formatForDebug(const QByteArray& data);
void redirectDataHandling(bool bDoRedirect, CommandInterface *newDataHandler = nullptr);
static QByteArray formatForDebug(const QAbstractSocket* dev);
CommandMap getCommands() const;
static QByteArray formatForDebug(const QByteArray &data);
protected:
void newDataInternal();
int initCommands(unsigned short port);
static QByteArray formatForDebug(const QAbstractSocket *dev);
void cleanupCommands();
qintptr m_socketDescriptor;
CommandMap getCommands() const;
QPointer<QAbstractSocket> m_connection;
protected:
unsigned short m_port;
void newDataInternal();
QByteArray m_connString;
int initCommands(unsigned short port);
bool m_bRedirectIncomingData;
CommandInterface* m_bRedirectDataHandler;
void cleanupCommands();
qintptr m_socketDescriptor;
QPointer<QAbstractSocket> m_connection;
unsigned short m_port;
QByteArray m_connString;
bool m_bRedirectIncomingData;
CommandInterface *m_bRedirectDataHandler;
CommandMap m_registeredCommands;
CommandMap m_registeredCommands;
};
class WorkerInstance final : public QObject, public WorkerBase
{
Q_OBJECT
public:
WorkerInstance(qintptr sd, unsigned short port, QObject *parent = nullptr);
virtual ~WorkerInstance();
Q_OBJECT
public:
WorkerInstance(qintptr sd, unsigned short port, QObject* parent = nullptr);
virtual ~WorkerInstance();
bool isRunning() const;
bool isRunning() const;
bool isFinished() const;
bool isFinished() const;
public Q_SLOTS:
public Q_SLOTS:
void quit();
void quit();
void doWork();
protected Q_SLOTS:
void doWork();
void newData();
protected Q_SLOTS:
void connectionDisconnected();
void newData();
Q_SIGNALS:
void finished();
void connectionDisconnected();
Q_SIGNALS:
void finished();
private:
bool m_bFinished;
private:
bool m_bFinished;
};