cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
Cutelyst::Engine Class Referenceabstract

The Cutelyst Engine. More...

#include <Cutelyst/Engine>

Inheritance diagram for Cutelyst::Engine:

Signals

void processRequestAsync (Cutelyst::EngineRequest *request)
 

Public Member Functions

 Engine (Application *app, int workerCore, const QVariantMap &opts)
 
virtual ~Engine ()
 
Applicationapp () const
 
QVariantMap config (const QString &entity) const
 
bool isZeroWorker () const
 
QVariantMap opts () const
 
void processRequest (EngineRequest *request)
 
void setConfig (const QVariantMap &config)
 
int workerCore () const
 
virtual int workerId () const =0
 
- Public Member Functions inherited from QObject
 QObject (QObject *parent)
 
bool blockSignals (bool block)
 
const QObjectListchildren () const const
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
void deleteLater ()
 
void destroyed (QObject *obj)
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectInfo () const const
 
void dumpObjectTree () const const
 
QList< QByteArraydynamicPropertyNames () const const
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (Qt::FindChildOptions options) const const
 
bool inherits (const char *className) const const
 
void installEventFilter (QObject *filterObj)
 
bool isQuickItemType () const const
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
void killTimer (int id)
 
virtual const QMetaObjectmetaObject () const const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const const
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
QVariant property (const char *name) const const
 
 Q_CLASSINFO (Name, Value)
 
 Q_DISABLE_COPY (Class)
 
 Q_DISABLE_COPY_MOVE (Class)
 
 Q_EMIT Q_EMIT
 
 Q_ENUM (...)
 
 Q_ENUM_NS (...)
 
 Q_ENUMS (...)
 
 Q_FLAG (...)
 
 Q_FLAG_NS (...)
 
 Q_FLAGS (...)
 
 Q_GADGET Q_GADGET
 
 Q_GADGET_EXPORT (EXPORT_MACRO)
 
 Q_INTERFACES (...)
 
 Q_INVOKABLE Q_INVOKABLE
 
 Q_MOC_INCLUDE Q_MOC_INCLUDE
 
 Q_NAMESPACE Q_NAMESPACE
 
 Q_NAMESPACE_EXPORT (EXPORT_MACRO)
 
 Q_OBJECT Q_OBJECT
 
 Q_PROPERTY (...)
 
 Q_REVISION Q_REVISION
 
 Q_SET_OBJECT_NAME (Object)
 
 Q_SIGNAL Q_SIGNAL
 
 Q_SIGNALS Q_SIGNALS
 
 Q_SLOT Q_SLOT
 
 Q_SLOTS Q_SLOTS
 
qobject_cast (const QObject *object)
 
qobject_cast (QObject *object)
 
 QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setObjectName (QAnyStringView name)
 
void setParent (QObject *parent)
 
bool setProperty (const char *name, const QVariant &value)
 
bool signalsBlocked () const const
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
QThreadthread () const const
 

Static Public Member Functions

static const char * httpStatusMessage (quint16 status, int *len=nullptr)
 
static QVariantMap loadIniConfig (const QString &filename)
 
static QVariantMap loadJsonConfig (const QString &filename)
 
- Static Public Member Functions inherited from QObject
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
QString tr (const char *sourceText, const char *disambiguation, int n)
 

Protected Member Functions

HeadersdefaultHeaders ()
 
bool initApplication ()
 
bool postForkApplication ()
 
- Protected Member Functions inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
int receivers (const char *signal) const const
 
QObjectsender () const const
 
int senderSignalIndex () const const
 
virtual void timerEvent (QTimerEvent *event)
 

Friends

class Application
 
class Response
 

Additional Inherited Members

- Public Attributes inherited from QObject
typedef QObjectList
 
- Properties inherited from QObject
 objectName
 

Detailed Description

This class is responsible receiving the request and sending the response. It must be reimplemented by real HTTP engines due some pure virtual methods.

The subclass must create an engine per thread (worker core), if the Application passed to the constructor has a worker core greater than 0 it will issue a new Application instance, failing to do so a fatal error is generated (usually indicating that the Application does not have a Q_INVOKABLE constructor).

Definition at line 19 of file engine.h.

Constructor & Destructor Documentation

◆ Engine()

Engine::Engine ( Cutelyst::Application app,
int  workerCore,
const QVariantMap &  opts 
)
explicit

Constructs an Engine object, where app is the application that might be used to create new instances if workerCore is greater than 1, opts is the options loaded by the engine subclass.

Parameters
appThe application loaded
workerCoreThe thread number
optsThe configuation options

Definition at line 42 of file engine.cpp.

References app(), QObject::connect(), opts(), processRequest(), processRequestAsync(), Qt::QueuedConnection, and workerCore().

◆ ~Engine()

Engine::~Engine ( )
virtual

Destroys the Engine object.

Definition at line 55 of file engine.cpp.

Member Function Documentation

◆ app()

Application * Engine::app ( ) const

Returns the application associated with this engine.

Definition at line 60 of file engine.cpp.

Referenced by Engine().

◆ config()

QVariantMap Engine::config ( const QString entity) const

Returns a map of key value pairs for the configuration entitiy (section) from your application’s configuration file.

See also
Configure your application

Definition at line 263 of file engine.cpp.

Referenced by Cutelyst::MemcachedSessionStore::MemcachedSessionStore(), setConfig(), Cutelyst::Session::setup(), Cutelyst::CSRFProtection::setup(), Cutelyst::Memcached::setup(), Cutelyst::StaticCompressed::setup(), Cutelyst::LangSelect::setup(), and Cutelyst::Application::setup().

◆ defaultHeaders()

Headers & Engine::defaultHeaders ( )
protected

This is the HTTP default response headers that each request gets.

Definition at line 245 of file engine.cpp.

◆ httpStatusMessage()

const char * Engine::httpStatusMessage ( quint16  status,
int *  len = nullptr 
)
static

Returns the HTTP status message for the given status. If len is not a nullptr, the length of the returned string will be stored to *len.

Definition at line 104 of file engine.cpp.

References QByteArray::number().

Referenced by Cutelyst::ProtoRequestHttp::writeHeaders().

◆ initApplication()

bool Engine::initApplication ( )
protected

This method inits the application and calls init on the engine. It must be called on the engine's thread. Returns true on success.

Definition at line 73 of file engine.cpp.

References QThread::currentThread(), and QObject::thread().

Referenced by Cutelyst::ServerEngine::init().

◆ isZeroWorker()

bool Cutelyst::Engine::isZeroWorker ( ) const
inline

Returns true if this is the Zero worker, ie if workerId() == 0 and workerCore() == 0.

Note
the value returned from this function is only valid when postFork() is issued.

Definition at line 156 of file engine.h.

References workerCore(), and workerId().

◆ loadIniConfig()

QVariantMap Engine::loadIniConfig ( const QString filename)
static

◆ loadJsonConfig()

QVariantMap Engine::loadJsonConfig ( const QString filename)
static

Returns a QVariantMap with the JSON parsed from filename.

Definition at line 299 of file engine.cpp.

References QJsonDocument::fromJson(), QFile::open(), QIODevice::readAll(), QIODeviceBase::ReadOnly, QIODeviceBase::Text, QVariant::toMap(), and QJsonDocument::toVariant().

◆ opts()

QVariantMap Engine::opts ( ) const

Returns the engine options set in the constructor.

Definition at line 257 of file engine.cpp.

Referenced by Engine().

◆ postForkApplication()

bool Engine::postForkApplication ( )
protected

Subclasses must be call after the engine forks by the worker thread, if no forking is involved it must be called once the worker thread has started.

For convenience QThread::currentThread() has it's object name set with the worker core number.

Returns
true if the engine should use this process

Definition at line 90 of file engine.cpp.

References QThread::currentThread(), QString::number(), and QObject::setObjectName().

◆ processRequest()

void Engine::processRequest ( EngineRequest request)

Process the request. The caller must delete the context when the request is finished.

This method allows for engines to keep the Context alive while processing websocket data.

Definition at line 251 of file engine.cpp.

Referenced by Engine().

◆ processRequestAsync

void Cutelyst::Engine::processRequestAsync ( Cutelyst::EngineRequest request)
signal

Process the requst asynchronous. The caller must delete the context when the request is finished.

This method allows for engines to keep the Context alive while processing websocket data.

Referenced by Engine().

◆ setConfig()

void Engine::setConfig ( const QVariantMap &  config)

Sets the configuration to be used by Application.

Definition at line 269 of file engine.cpp.

References config().

◆ workerCore()

int Engine::workerCore ( ) const

Returns the worker core set when constructing the engine.

Each worker process migth have a number of worker cores (threads), a single process with two worker threads will return 0 and 1 for each of the thread respectively.

Definition at line 67 of file engine.cpp.

Referenced by Engine(), isZeroWorker(), and Cutelyst::Application::setup().

◆ workerId()

virtual int Cutelyst::Engine::workerId ( ) const
pure virtual

Reimplement this to get the workerId of the engine subclass, this is the same as processes id.

The id is the number of the spawned engine process, a single process workerId = 0, two process 0 for the first 1 for the second.

Note
The value returned from this function is only valid when postFork() is issued.

Implemented in Cutelyst::ServerEngine.

Referenced by isZeroWorker().

Friends And Related Symbol Documentation

◆ Application

friend class Application
friend

Definition at line 147 of file engine.h.

◆ Response

friend class Response
friend

Definition at line 148 of file engine.h.