#ifndef USMODULE_H #define USMODULE_H #include #include "type/CommandRequest.h" #include "type/CommandResponse.h" #include "UsEventReceiver.h" #include "UsEventSender.h" #include "logger/Logger.h" #include "UsHelper.h" #include "UsPacket.h" #include "utils/Utils.h" //forward decleration class ApplicationStarter; //macro defenition //Place this in each and every class that inherits from UsModule //so that you dont have to type all this again #define US_MODULE \ protected: \ virtual void commandHandler(const CommandRequest& request) override; \ virtual void packetHandler(const UsPacket& packet) override; \ virtual void init() override; \ virtual void start() override; \ virtual void afterThreadStart() override; \ private: \ template \ void connectCommandPath() \ { \ connect(T::getInstance()->getEventReceiver(), \ &UsEventReceiver::commandResponse, \ _eventSender, \ &UsEventSender::newCommandResponse, Qt::QueuedConnection); \ connect(_eventSender, \ &UsEventSender::commandRequest, \ T::getInstance()->getEventReceiver(), \ &UsEventReceiver::newCommandRequest, Qt::QueuedConnection); \ } \ template \ void connectDataPath() \ { \ connect(this, SIGNAL(packetReady(const UsPacket&)), \ T::getInstance(), SLOT(newPacket(const UsPacket&))); \ } \ /*************************************************************************************************/ /** * @brief The UsModule class * @details This class is the bone of our ultrasound framework(if we can call it), it includes all * the basic functionalities that we expect from a UsModule and provides the developers tools * and helpers to ease the development of UsModules, * @note UsModule in its finest defenition, is just a wrapper around some hidden internal * functionalities, this wrapper provides means of communication(data and command), utilities * such as logger and helpers. Each UsModule runs in its own thread, has some lifecycle hooks and * is a singleton * Some of the features, for example having a special folder layout, was only forced * to ease further development and readability and was not part of first design goals * @author Hessamoddin Hediyehloo(H-4nd-H) * @date 2019/12/15(1398/9/24) */ /*************************************************************************************************/ class UsModule : public QObject { Q_OBJECT US_EVENT_SENDER(protected) US_EVENT_RECEIVER(protected) private: void initialize(); protected: virtual void afterThreadStart() = 0; //hook after object is moved to destination thread virtual void init() = 0; //hook after all USMs created and moved to their threads virtual void start() = 0; //hook after all USMs gone under init virtual void commandHandler(const CommandRequest& request) = 0; virtual void packetHandler(const UsPacket& packet) = 0; void log(ESeverityLevel::eSeverityLevel severity, ELogID::eLogID logID, QString message); void fail(const CommandRequest& request); void fail(const CommandRequest& request, QList args); void pass(const CommandRequest& request); void pass(const CommandRequest& request, QList args); void sendAsyncCommand(ECommandType::eCommandType commandType, QString receiver); void sendAsyncCommand(ECommandType::eCommandType commandType, QString receiver, QList args); void sendAsyncCommand(ECommandType::eCommandType commandType, QString receiver, std::function)> successCallback, std::function)> failureCallback); void sendAsyncCommand(ECommandType::eCommandType commandType, QString receiver, QList args, std::function)> successCallback, std::function)> failureCallback); CommandResult sendSyncCommand(ECommandType::eCommandType commandType, QString receiver); CommandResult sendSyncCommand(ECommandType::eCommandType commandType, QString receiver, QList args); void sendData(const UsPacket& data); public: explicit UsModule(QObject*); virtual ~UsModule() { } signals: //To notify the thread to finish void finished(); //Will be called when object is moved to destination thread void ThreadStartUpRoutineFinished(); //Called to data passing void packetReady(const UsPacket& packet); //uncrustify off public slots: void threadStarted(); void newCommandRequest(const CommandRequest& request); void newPacket(const UsPacket& packet); //uncrustify on }; #endif //USMODULE_H