±à¼ÍƼö: |
±¾ÎÄÊ×ÏÈí¡ÏîÄ¿½øÐÐÁ˽éÉÜ£¬½ÓÏÂÀ´¾ÍÊǽéÉÜÁËÈçϼ¸¸ö°¸Àý´òÓ¡²å¼þ±àд¡¢ÈÕÖ¾²å¼þ±àд¡¢½çÃæÀàµÈÏà¹ØÄÚÈÝ¡£
ÎÄÕÂÀ´×ÔÓÚcsdn£¬ÓÉ»ðÁú¹ûAnna±à¼ÍƼö¡£ |
|
Ò»¡¢ÏîÄ¿½éÉÜ
1¡¢Ö÷ҪΪÁËÔÚ´ËСÐͲâÊÔÏîÄ¿ÖÐÓõ½¼¸ºõËùÓÐctkµÄ³£ÓÃ֪ʶ£º
²å¼þ¼äͨÐÅ¡¢·þÎñ¹¤³§¡¢·þÎñ×·×Ù¡¢Ê¼þ¼àÌý¡¢¿ò¼Ü·â×°
2¡¢ÏîÄ¿¹²°üº¬3¸ö²å¼þ
ÈÕÖ¾²å¼þ£ºÓÃÓÚÄ£Ä⽫½ÓÊÕµÄÐÅÏ¢´æ´¢µ½ÈÕÖ¾ÖС¾ÕâÀïÖ»×ö¼òµ¥µÄ´òÓ¡¡¿£¬²¢ÇÒÄܹ»½ÓÊÕÐÅÏ¢¡¾Ö÷ÒªÀ´×Ô½çÃæ²å¼þ¡¿
´òÓ¡²å¼þ£º¸ù¾Ý²»Í¬µÄ²å¼þ·µ»ØÌṩ²»Í¬´òÓ¡·þÎñ
½çÃæ²å¼þ£º·â×°Ò»¸ö½çÃæ²å¼þ£¬Äܹ»Ïò¿ò¼Ü·¢ËÍÐÅÏ¢¡¾À෽ʽºÍÐźŲ۷½Ê½¡¿
3¡¢¼Ü¹¹Í¼

¶þ¡¢´òÓ¡²å¼þ±àд
1¡¢¹¤³Ì½á¹¹

2¡¢½Ó¿ÚÀà
absprintserver.h
#ifndef ABSPRINTSERVER_H
#define ABSPRINTSERVER_H
#include <QObject>
class AbsPrintServer
{
public:
virtual ~AbsPrintServer(){}
virtual void print(QString) = 0;
};
Q_DECLARE_INTERFACE (AbsPrintServer, "judesmorning.zxy.AbsPrintServer")
#endif // ABSPRINTSERVER_H |
3¡¢ÊµÏÖÀà1£¬ÎªÈÕÖ¾²å¼þÌṩ·þÎñ
printserver.h
#ifndef PRINTSERVER_H
#define PRINTSERVER_H
#include <QObject>
#include "absprintserver.h"
class PrintServer : public QObject, public
AbsPrintServer
{
Q_OBJECT
Q_INTERFACES(AbsPrintServer)
public:
PrintServer();
void print(QString info) override;
};
#endif // PRINTSERVER_H |
printserver.cpp
#include "printserver.h"
#include <QDebug>
PrintServer::PrintServer()
{
}
void PrintServer::print(QString info)
{
qDebug()<<info+"----print plugin
for FirstPlugin";
} |
4¡¢ÊµÏÖÀà2£¬Îª½çÃæ²å¼þÌṩ·þÎñ
printservert.h
#ifndef PRINTSERVERT_H
#define PRINTSERVERT_H
#include <QObject>
#include "absprintserver.h"
class PrintServerT : public QObject, public
AbsPrintServer
{
Q_OBJECT
Q_INTERFACES(AbsPrintServer)
public:
PrintServerT();
void print(QString info) override;
};
#endif // PRINTSERVERT_H |
printservert.cpp
#include "printservert.h"
#include <QDebug>
#include <QTime>
PrintServerT::PrintServerT()
{
}
void PrintServerT::print(QString info)
{
qDebug()<<QTime:: currentTime().toString ("HH:mm:ss
")<<info+" ----print plugin for
CreateCtkUiPlugin";
} |
5¡¢ÊµÏÖÀà3£¬ÎªÆäËû²å¼þÌṩ·þÎñ
printserverd.h
#ifndef PRINTSERVERD_H
#define PRINTSERVERD_H
#include <QObject>
#include "absprintserver.h"
class PrintServerD : public QObject, public
AbsPrintServer
{
Q_OBJECT
Q_INTERFACES(AbsPrintServer)
public:
PrintServerD();
void print(QString info) override;
};
#endif // PRINTSERVERD_H |
printserverd.cpp
#include "printserverd.h"
#include <QDebug>
PrintServerD::PrintServerD()
{
}
void PrintServerD::print(QString info)
{
qDebug()<<info+"....----print plugin
for unknown plugin";;
} |
6¡¢µ¥¶ÀΪ½Ó¿Ú±àд·þÎñ×·×ÙÀà
tracker.h
#ifndef TRACKER_H
#define TRACKER_H
#include "ctkServiceTracker.h"
#include "absprintserver.h"
#include "ctkPluginContext.h"
class Tracker : public ctkServiceTracker<AbsPrintServer*>
{
public:
Tracker(ctkPluginContext* context);
protected:
AbsPrintServer* addingService (const ctkServiceReference&
reference) override;
void modifiedService (const ctkServiceReference&
reference, AbsPrintServer* service) override;
void removedService (const ctkServiceReference&
reference, AbsPrintServer* service) override;
private:
ctkPluginContext* context;
};
#endif // TRACKER_H |
tracker.cpp
#include "tracker.h"
Tracker::Tracker(ctkPluginContext *context)
: ctkServiceTracker<AbsPrintServer*> (context)
{
}
AbsPrintServer *Tracker::addingService (const
ctkServiceReference &reference)
{
AbsPrintServer* service = static_cast<AbsPrintServer*>
(ctkServiceTracker::addingService(reference));
return service;
}
void Tracker::modifiedService (const ctkServiceReference
&reference, AbsPrintServer* service)
{
ctkServiceTracker::modifiedService (reference,service);
}
void Tracker::removedService (const ctkServiceReference
&reference, AbsPrintServer* service)
{
ctkServiceTracker:: removedService (reference,service);
} |
×¢ÒâÕâ¸ö·þÎñ×·×ÙÀàÊÇÔÚÈÕÖ¾²å¼þÀïʹÓõģ¬Ö»ÊÇ´ÓÖ°ÔðÉÏÀ´½²Õâ¸öÀàÓ¦¸Ã±àд´Ë²å¼þµÄÈËÀ´±àд£¬½µµÍñîºÏÐÔ
Èý¡¢ÈÕÖ¾²å¼þ±àд
1¡¢¹¤³Ì½á¹¹

2¡¢½Ó¿ÚÀà
abslogservice.h
#include "tracker.h"
Tracker::Tracker(ctkPluginContext *context)
: ctkServiceTracker<AbsPrintServer*> (context)
{
}
AbsPrintServer *Tracker::addingService (const
ctkServiceReference &reference)
{
AbsPrintServer* service = static_cast<AbsPrintServer*>
(ctkServiceTracker::addingService(reference));
return service;
}
void Tracker::modifiedService (const ctkServiceReference
&reference, AbsPrintServer* service)
{
ctkServiceTracker::modifiedService (reference,service);
}
void Tracker::removedService (const ctkServiceReference
&reference, AbsPrintServer* service)
{
ctkServiceTracker:: removedService (reference,service);
} |
3¡¢ÊµÏÖÀà
logservice.h
#ifndef LOGSERVICE_H
#define LOGSERVICE_H
#include <QObject>
#include "includes.h"
#include "abslogservice.h"
#include "service/event/ctkEventHandler.h"
#include "ctkPluginFrameworkEvent.h"
#include "ctkPluginEvent.h"
#include "ctkServiceEvent.h"
#include "tracker.h"
class ctkPluginContext;
class LogService : public QObject, public AbsLogService,
public ctkEventHandler
{
Q_OBJECT
Q_INTERFACES(AbsLogService)
Q_INTERFACES(ctkEventHandler)
public:
LogService(ctkPluginContext* context);
void log(QString info) override;
void handleEvent(const ctkEvent& event)
override;
private slots:
// ¼àÌý¿ò¼Üʼþ
void onFrameworkEvent(const ctkPluginFrameworkEvent&
event);
// ¼àÌý²å¼þʼþ
void onPluginEvent(const ctkPluginEvent&
event);
// ¼àÌý·þÎñʼþ
void onServiceEvent(const ctkServiceEvent&
event);
private:
QString getLevelStr(const _Log_Level& level)
const;//»ñÈ¡ÈÕÖ¾ÀàÐÍ×Ö·û´®
void print(QString info);//ʹÓôòÓ¡²å¼þ´òÓ¡¶«Î÷
private:
ctkPluginContext* context;
QScopedPointer<Tracker> p_tracker;
};
#endif // LOGSERVICE_H |
logservice.cpp
#include "logservice.h"
#include <QTime>
#include <QDebug>
#include "ctkPluginContext.h"
#include "absprintserver.h"
#include "ctkServiceTracker.h"
LogService::LogService(ctkPluginContext* context)
:context(context)
{
#if 1 //ͨ¹ý·þÎñ×·×Ù·½Ê½»ñÈ¡´òÓ¡²å¼þ
p_tracker.reset(new Tracker(context));
p_tracker->open();
// AbsPrintServer* printService = static_cast
<AbsPrintServer*>(p_tracker->getService());
// printService- >print ("use tracker
to get print plugin--------log plugin");
#endif
#if 1 //¼àÌýctkʼþ
context->connectFrameworkListener(this, SLOT(onFrameworkEvent(ctkPluginFrameworkEvent)));
context->connectPluginListener (this, SLOT
(onPluginEvent(ctkPluginEvent)));
//QString filter = QString("(%1=%2)").arg(ctkPluginConstants:
:OBJECTCLASS).arg ("org.commontk.eventadmin");//
¹ýÂË ctkEventAdmin ·þÎñ
//context->connectServiceListener (this,SLOT
(onServiceEvent(ctkServiceEvent))); //, filter);
context->connectServiceListener (this,"onServiceEvent");
//, filter);
#endif
}
void LogService::log(QString info)
{
qDebug()<<"log plugin save a log
--- >"+ QTime::currentTime().toString
("HH:mm:ss ") + info;
}
void LogService::handleEvent(const ctkEvent
&event)
{
qDebug()<<"log plugin rcv a event
----------log plugin";
_Log_Level level = static_cast<_Log_Level>
(event.getProperty("level").toInt());
QString pluginName = event.getProperty ("pluginName").toString();
QString info = event.getProperty("info").toString();
QString rcvLogInfo = QString("%1 %2 %3.")
.arg(getLevelStr(level))
.arg(pluginName)
.arg(info);
log(rcvLogInfo);
}
QString LogService::getLevelStr (const _Log_Level
&level) const
{
QString ret;
switch (static_cast<int>(level))
{
case LOG_LEVEL_DEBUG:
ret = "DEBUG";
break;
case LOG_LEVEL_INFO:
ret = "INFO";
break;
case LOG_LEVEL_WARNING:
ret = "WARNING";
break;
case LOG_LEVEL_ERROR:
ret = "ERROR";
break;
case LOG_LEVEL_CRITICAL:
ret = "CRITICAL";
break;
default:
ret = "UKNOWN";
break;
}
return ret;
}
void LogService::print(QString info)
{
#if 0 //Ö±½ÓÏòctk¿ò¼ÜË÷Òª·þÎñ
ctkServiceReference reference = context->
getServiceReference<AbsPrintServer>();
if (reference)
{
// »ñȡָ¶¨ ctkServiceReference ÒýÓõķþÎñ¶ÔÏó
AbsPrintServer* service = qobject_cast<AbsPrintServer*>
(context->getService(reference));
if (service != Q_NULLPTR)
{
// µ÷Ó÷þÎñ
service->print(info);
}
}
#endif
#if 1 //ͨ¹ýtrackerÏòctkË÷Òª·þÎñ
AbsPrintServer* service = static_cast<AbsPrintServer*> (p_tracker->getService());
if (service != Q_NULLPTR)
{
service->print("log plugin with tracker:"+info);
}
else
{
qDebug()<<"get AbsPrintServer from
tracker failed";
}
#endif
}
void LogService:: onFrameworkEvent (const ctkPluginFrameworkEvent
&event)
{
if (!event.isNull())
{
QSharedPointer<ctkPlugin> plugin = event.getPlugin();
qDebug() << "FrameworkEvent: ["
<< plugin->getSymbolicName() <<
"]" << event.getType() <<
event.getErrorString();
}
else
{
qDebug() << "The framework event
is null";
}
}
void LogService::onPluginEvent (const ctkPluginEvent
&event)
{
if (!event.isNull())
{
QSharedPointer<ctkPlugin> plugin = event.getPlugin();
qDebug() << "PluginEvent: ["
<< plugin->getSymbolicName() <<
"]" << event.getType();
}
else
{
qDebug() << "The plugin event is
null";
}
}
void LogService::onServiceEvent (const ctkServiceEvent
&event)
{
if (!event.isNull())
{
ctkServiceReference ref = event.getServiceReference();
QSharedPointer<ctkPlugin> plugin = ref.getPlugin();
qDebug() << "ServiceEvent: ["
<< plugin->getSymbolicName() <<
"]" << event.getType() <<
ref.getUsingPlugins();
}
else
{
qDebug() << "The service event is
null";
}
} |
4¡¢¼¤»îÀà
firstpluginactivator.h
#ifndef FIRSTPLUGINACTIVATOR_H
#define FIRSTPLUGINACTIVATOR_H
#include <QObject>
#include "ctkPluginActivator.h"
#include "ctkPluginContext.h"
#include "logservice.h"
class FirstPluginActivator : public QObject,
public ctkPluginActivator
{
Q_OBJECT
Q_INTERFACES(ctkPluginActivator)
Q_PLUGIN_METADATA(IID "LogPlugin")
public:
FirstPluginActivator();
void start(ctkPluginContext *context);
void stop(ctkPluginContext *context);
private:
QScopedPointer<AbsLogService> m_log;
};
#endif // FIRSTPLUGINACTIVATOR_H |
firstpluginactivator.cpp
#ifndef FIRSTPLUGINACTIVATOR_H
#define FIRSTPLUGINACTIVATOR_H
#include <QObject>
#include "ctkPluginActivator.h"
#include "ctkPluginContext.h"
#include "logservice.h"
class FirstPluginActivator : public QObject,
public ctkPluginActivator
{
Q_OBJECT
Q_INTERFACES(ctkPluginActivator)
Q_PLUGIN_METADATA(IID "LogPlugin")
public:
FirstPluginActivator();
void start(ctkPluginContext *context);
void stop(ctkPluginContext *context);
private:
QScopedPointer<AbsLogService> m_log;
};
#endif // FIRSTPLUGINACTIVATOR_H |
firstpluginactivator.cpp
#include "firstpluginactivator.h"
#include <QDebug>
#include "service/event/ctkEventHandler.h"
#include "service/event/ctkEventConstants.h"
#include "absprintserver.h"
FirstPluginActivator:: FirstPluginActivator()
{
}
//×¢²á²å¼þ²¢¶©ÔÄʼþ
void FirstPluginActivator::start (ctkPluginContext
*context)
{
// qDebug() << " FirstPluginActivator
start";
LogService* service = new LogService(context);
m_log.reset(service);
ctkDictionary props;
props [ctkEventConstants::EVENT_TOPIC] = "kdhy/yunwei/generalevent/log";
props [ctkEventConstants::EVENT_FILTER] = "(pluginName=CreateCtkUiPlugin)";
context->registerService <ctkEventHandler>(service,
props);
context->registerService <AbsLogService>(service);
}
void FirstPluginActivator::stop (ctkPluginContext
*context)
{
Q_UNUSED(context)
} |
5¡¢²å¼þ¼äͨÐŵĽṹÌ嶨Òå
includes.h
#ifndef
INCLUDES_H
#define INCLUDES_H
#include <QString>
/*¹«¹²¶¨Òå*/
//ÈÕÖ¾Ïà¹Ø
enum _Log_Level{
LOG_LEVEL_DEBUG = 0,
LOG_LEVEL_INFO,
LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR,
LOG_LEVEL_CRITICAL
};//ÈÕÖ¾µÈ¼¶
typedef struct _LogInfo {
_Log_Level level; //ÈÕÖ¾ÀàÐÍ
QString pluginName; //²å¼þÃû³Æ
QString info; //ÈÕÖ¾ÐÅÏ¢
} LogInfo;//ÈÕÖ¾ÏêÇé
#endif // INCLUDES_H
|
ËÄ¡¢½çÃæÀà
1¡¢¹¤³Ì½á¹¹

2¡¢½Ó¿ÚÀà
absuiservice.h
#ifndef ABSUISERVICE_H
#define ABSUISERVICE_H
#include <QObject>
class AbsUiService{
public:
virtual ~AbsUiService(){}
virtual void init() = 0;
};
Q_DECLARE_INTERFACE (AbsUiService,"judesmorning.zxy.AbsUiService")
#endif // ABSUISERVICE_H
|
3¡¢ÊµÏÖÀà
uiservice.h
#ifndef UISERVICE_H
#define UISERVICE_H
#include <QObject>
#include "absuiservice.h"
#include "includes.h"
#include "myui.h"
class ctkPluginContext;
class UiService : public QObject, public AbsUiService
{
Q_OBJECT
Q_INTERFACES(AbsUiService)
public:
UiService(ctkPluginContext* context);
void init() override;
private slots:
void publishLogEventSlot(LogInfo logInfo);
private:
ctkPluginContext* context;//ctkÉÏÏÂÎĶÔÏó
Myui myui;//½çÃæ¶ÔÏó
};
#endif // UISERVICE_H |
uiservice.cpp
#include "uiservice.h"
#include "ctkPluginContext.h"
#include "service/event/ctkEventAdmin.h"
#include <QDebug>
#include <QDialog>
UiService::UiService(ctkPluginContext* context)
:context(context)
{
context->registerService<AbsUiService>(this);
QObject::connect(&myui,SIGNAL (publishLogEventSignal
(LogInfo)), this,SLOT(publishLogEventSlot (LogInfo)));
//·¢ËÍÈÕ־ʼþµ½ctk¿ò¼Ü,signal·½Ê½
ctkServiceReference ref = context->getServiceReference<ctkEventAdmin>();
if (ref)
{
ctkEventAdmin* eventAdmin = context->getService<ctkEventAdmin>(ref);
eventAdmin->publishSignal (&myui,SIGNAL
(publishLogSignal(ctkDictionary)), "kdhy/yunwei/generalevent/log",Qt
::QueuedConnection);
}
}
void UiService::init()
{
myui.show();
}
//·¢ËÍÈÕ־ʼþµ½ctk¿ò¼Ü,event·½Ê½
void UiService::publishLogEventSlot(LogInfo
logInfo)
{
ctkServiceReference ref = context-> getServiceReference<ctkEventAdmin>();
if (ref) {
ctkEventAdmin* eventAdmin = context-> getService<ctkEventAdmin>(ref);
ctkDictionary props;
props["level"] = logInfo.level;
props["pluginName"] = logInfo.pluginName;
props["info"] = logInfo.info;
ctkEvent event ("kdhy/yunwei/generalevent/log",
props);
qDebug() << "ui plugin send "
<< logInfo.info;
eventAdmin->sendEvent(event);//sendEvent:ͬ²½
postEvent:Òì²½
}
} |
4¡¢Ö÷ÏÔʾ½çÃæ
myui.h
#ifndef MYUI_H
#define MYUI_H
#include <QWidget>
#include "includes.h"
#include "service/event/ctkEventAdmin.h"
namespace Ui {
class Myui;
}
class Myui : public QWidget
{
Q_OBJECT
public:
explicit Myui(QWidget *parent = nullptr);
~Myui();
signals:
void publishLogEventSignal(LogInfo);
void publishLogSignal(ctkDictionary info);
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Myui *ui;
};
#endif // MYUI_H |
myui.cpp
#include "myui.h"
#include "ui_myui.h"
Myui::Myui(QWidget *parent) :
QWidget(parent),
ui(new Ui::Myui)
{
ui->setupUi(this);
}
Myui::~Myui()
{
delete ui;
}
//·¢ËÍeventʼþ
void Myui::on_pushButton_clicked()
{
qDebug()<<"ui plugin send a event";
LogInfo logInfo;
logInfo.level = LOG_LEVEL_INFO;
logInfo.pluginName = "CreateCtkUiPlugin";
logInfo.info = "Event info:"+ui->textEdit->toPlainText();
emit publishLogEventSignal(logInfo);
}
//·¢ËÍÐźÅʼþ
void Myui::on_pushButton_2_clicked()
{
ctkDictionary props;
props["level"] = LOG_LEVEL_INFO;;
props["pluginName"] = "CreateCtkUiPlugin";
props["info"] = "Signal info:"+ui->textEdit->toPlainText();
emit publishLogSignal(props);
} |
myui.ui

5¡¢¼¤»îÀà
uiactivator.h
#ifndef UIACTIVATOR_H
#define UIACTIVATOR_H
#include <QObject>
#include "ctkPluginActivator.h"
#include "ctkPluginContext.h"
#include "uiservice.h"
class UiActivator: public QObject, public ctkPluginActivator
{
Q_OBJECT
Q_INTERFACES(ctkPluginActivator)
Q_PLUGIN_METADATA(IID "UiPlugin")
public:
UiActivator();
void start(ctkPluginContext *context);
void stop(ctkPluginContext *context);
private:
QScopedPointer<AbsUiService> m_ui;
};
#endif // UIACTIVATOR_H |
uiactivator.cpp
#include "uiactivator.h"
#include <QtDebug>
UiActivator::UiActivator()
{
}
void UiActivator::start(ctkPluginContext *context)
{
// qDebug() << "ui plugin start";
m_ui.reset(new UiService(context));
}
void UiActivator::stop(ctkPluginContext *context)
{
Q_UNUSED(context)
} |
6¡¢²å¼þ¼äͨÐŵĽṹÌ嶨Òå
includes.h
#ifndef INCLUDES_H
#define INCLUDES_H
#include <QString>
/*¹«¹²¶¨Òå*/
//ÈÕÖ¾Ïà¹Ø
enum _Log_Level{
LOG_LEVEL_DEBUG = 0,
LOG_LEVEL_INFO,
LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR,
LOG_LEVEL_CRITICAL
};//ÈÕÖ¾µÈ¼¶
typedef struct _LogInfo {
_Log_Level level; //ÈÕÖ¾ÀàÐÍ
QString pluginName; //²å¼þÃû³Æ
QString info; //ÈÕÖ¾ÐÅÏ¢
} LogInfo;//ÈÕÖ¾ÏêÇé
#endif // INCLUDES_H
|
Î塢ʹÓÃ
1¡¢Ð½¨¿ØÖÆÌ¨¹¤³Ì

ÓÉÓÚÓõ½Á˽çÃæ£¬ËùÒÔÕâ¸ö¹¤³Ì²»ÄÜÊÇÎÞ½çÃæµÄ
2¡¢.pro
#-------------------------------------------------
#
# Project created by QtCreator 2020-07-02T18:12:37
#
#-------------------------------------------------
#QT += core
QT += gui widgets
CONFIG += console c++11
CONFIG -= app_bundle
TARGET = CtkFramework
#TEMPLATE = app
# The following define makes your compiler
emit warnings if you use
# any feature of Qt which has been marked as
deprecated (the exact warnings
# depend on your compiler). Please consult the
documentation of the
# deprecated API in order to know how to port
your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile
if you use deprecated APIs.
# In order to do so, uncomment the following
line.
# You can also select to disable deprecated
APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
# disables all the APIs deprecated before Qt
6.0.0
SOURCES += \
main.cpp \
pullservice.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
# CTKÔ´Âë·¾¶
INCLUDEPATH += $$PWD/third_libs/ctk/include/CTK_src/Core
\
+= $$PWD/third_libs/ctk/include/CTK_src/PluginFramework
# CTK°²×°Â·¾¶
INCLUDEPATH += $$PWD/third_libs/ctk/include/CTK_install/Core
\
+= $$PWD/third_libs/ctk/include/CTK_install/PluginFramework
# CTK¿â·¾¶
LIBS += -L$$PWD/third_libs/ctk/libs -lCTKCore
-lCTKPluginFramework
# ²å¼þÍ·Îļþ·¾¶
INCLUDEPATH += $$PWD/third_libs/plugin/include
HEADERS += \
pullservice.h |
3¡¢¿ò¼Ü·â×°Àà
pullservice.h
#ifndef PULLSERVICE_H
#define PULLSERVICE_H
#include <QDebug>
#include <mutex>
#include <QDir>
#include <QTime>
#include "ctkPluginFrameworkLauncher.h"
#include "ctkPluginContext.h"
#include "ctkPluginException.h"
#include "ctkPluginFrameworkFactory.h"
#include "ctkPluginFramework.h"
#include "ctkPluginException.h"
#include "ctkPluginContext.h"
namespace PULLSERVICE{
#define PRINTF_LOCATION() qDebug()<<"ret
in:" << __FILE__ << "
at:"<<__LINE__
#define RET_VALUE_IF_NOT_EAQU(a,b,c) \
do { \
if(a!=b) \
{ \
PRINTF_LOCATION();\
return c; \
} \
} while (false)
#define RET_VALUE_IF_EAQU(a,b,c) \
do { \
if(a==b) \
{ \
PRINTF_LOCATION();\
return c; \
} \
} while (false)
#define RET_IF_NOT_EAQU(a,b) \
do { \
if(a!=b) \
{ \
PRINTF_LOCATION();\
return; \
} \
} while (false)
#define RET_IF_EAQU(a,b) \
do { \
if(a==b) \
{ \
PRINTF_LOCATION();\
return; \
} \
} while (false)
}
using namespace PULLSERVICE;
class AbsUiService;
class AbsLogService;
class PullService
{
protected:
virtual ~PullService();
private:
PullService();
// PullService(const PullService&) = delete;
// PullService& operator=(const PullService&)
= delete;
void print(QString info);
public:
static PullService* getInstance();
//¶ÔÍâ½Ó¿Ú
void initCtkFramework(bool usingEventAdmin =
false);//³õʼ»¯¿ò¼Ü
void initServices();//³õʼ»¯ËùÓзþÎñ
template<class T>//»ñÈ¡·þÎñ,ͨ¹ýÄ£°å
T* getService()
{
T* s = nullptr;
RET_VALUE_IF_EAQU(context,nullptr,s);
ctkServiceReference reference = context->getServiceReference<T>();
if(reference)
{
s = context->getService<T>(reference);//
»ñȡָ¶¨ ctkServiceReference ÒýÓõķþÎñ¶ÔÏó
if (s == nullptr)
{
print("Try to get a invalid service");
}
}
return s;
}
void stopFramework();//¹Ø±Õctk¿ò¼Ü
private:
static PullService* instance;
static std::mutex mMutex;
volatile bool usingEventAdmin = false;
private:
QStringList pluginNames;//ËùÓвå¼þÃû×Ö
//ctkÏà¹ØµÄ±äÁ¿
ctkPluginFrameworkFactory frameworkFactory;
ctkPluginContext* context = nullptr;
private:
Q_DISABLE_COPY(PullService)
};
#endif // PULLSERVICE_H |
pullservice.cpp
#include "pullservice.h"
//²å¼þÍ·Îļþ
#include "abslogservice.h"
#include "absuiservice.h"
#include "signal.h"
#include "slot.h"
#include "absprintserver.h"
#include "abslogtracker.h"
PullService* PullService ::instance = nullptr;
std::mutex PullService::mMutex;
PullService::PullService()
{
print("PullService construct");
pluginNames << "MPrintServer.dll"
<< "ctk-plugin-first.dll"
<< "CreateCtkUiPlugin.dll"
// << "ctksignalplugin.dll"
// << "ctkslotplugin.dll"
<< "ctkplugintracker.dll"
;
}
PullService::~PullService()
{
print("PullService destruct");
}
PullService* PullService::getInstance()
{
if(nullptr == instance)
{
mMutex.lock();
if(nullptr == instance)
{
instance = new PullService;
}
mMutex.unlock();
}
return instance;
}
/*************************/
//×÷Õß:
//º¯ÊýÃû³Æ:³õʼ»¯ctk¿ò¼Ü
//º¯Êý²ÎÊý:usingEventAdminÊÇ·ñʹÓÃeventadmin
//º¯Êý·µ»ØÖµ:NULL
//º¯Êý×÷ÓÃ:NULL
//±¸×¢:NULL
/************************/
void PullService:: initCtkFramework (bool usingEventAdmin)
{
if(nullptr != context)
{
print ("ctkPluginContext is not null at
first time, maybe you have call this method.Try
restart app to resolve this problem");
return;
}
this->usingEventAdmin = usingEventAdmin;
if(usingEventAdmin)
{
QString path = QDir::currentPath() + "/third_libs/ctk/libs";
// »ñÈ¡²å¼þËùÔÚλÖÃ
ctkPluginFrameworkLauncher:: addSearchPath(path);
// ÔÚ²å¼þµÄËÑË÷·¾¶ÁбíÖÐÌí¼ÓÒ»Ìõ·¾¶
ctkPluginFrameworkLauncher:: start ("org.commontk.eventadmin");
context = ctkPluginFrameworkLauncher ::getPluginContext();
}
else
{
QSharedPointer<ctkPluginFramework> framework
= frameworkFactory.getFramework();
// ³õʼ»¯²¢Æô¶¯²å¼þ¿ò¼Ü
try {
framework->init();
framework->start();
context = framework->getPluginContext();
} catch (const ctkPluginException &e) {
this->print ("CTK plugin framework init
failed:"+QString(e.what()));
}
}
}
/**************************/
//×÷Õß:
//º¯ÊýÃû³Æ:³õʼ»¯·þÎñ
//º¯Êý²ÎÊý:NULL
//º¯Êý·µ»ØÖµ:NULL
//º¯Êý×÷ÓÃ:NULL
//±¸×¢:°²×°Æô¶¯²å¼þ
/**********************/
void PullService::initServices()
{
RET_IF_EAQU(context,nullptr);
QString prefixFilePath = QDir::currentPath()+ "/third_libs/plugin/libs/";
QString path;
foreach (QString oneFileName , pluginNames)
{
path = prefixFilePath+oneFileName;
try {
// °²×°²å¼þ
print(QString("Ready to init plugin:%1").arg(oneFileName));
QSharedPointer<ctkPlugin> plugin = context->installPlugin(QUrl::fromLocalFile(path));
print(QString("Plugin[%1_%2] installed...").arg(plugin->getSymbolicName( )).arg(plugin->getVersion().toString()));
// Æô¶¯²å¼þ
plugin->start(ctkPlugin::START_TRANSIENT);
print(QString("Plugin[%1_%2] started...").arg (plugin->getSymbolicName(
)).arg (plugin->getVersion().toString()));
} catch (const ctkPluginException &e) {
print (QString ("Failed install or start
plugin:%1").arg (e.what()));
}
}
}
/**********************/
//×÷Õß:
//º¯ÊýÃû³Æ:Í£Ö¹¿ò¼Ü
//º¯Êý²ÎÊý:NULL
//º¯Êý·µ»ØÖµ:NULL
//º¯Êý×÷ÓÃ:NULL
//±¸×¢:NULL
/************************/
void PullService::stopFramework()
{
if(usingEventAdmin)
{
ctkPluginFrameworkLauncher::stop();
}
else
{
QSharedPointer <ctkPluginFramework> framework
= frameworkFactory.getFramework();
framework->stop();
}
}
/*************************************/
//º¯ÊýÃû³Æ:ÄÚ²¿´òÓ¡º¯Êý
//º¯Êý²ÎÊý:NULL
//º¯Êý·µ»ØÖµ:NULL
//º¯Êý×÷ÓÃ:NULL
//±¸×¢:NULL
/*************************/
void PullService::print(QString info)
{
QString _i = QString("%1 %2 %3")
.arg(__FILE__)
.arg(QTime::currentTime( ).toString("HH:mm:ss"))
.arg(info);
qDebug()<<_i;
} |
3¡¢main.cpp
#include <QApplication>//±¾µØÍ·Îļþ
#include "pullservice.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
(void)PullService:: getInstance()->initCtkFramework(false);
(void)PullService:: getInstance()->initServices();
return a.exec();
} |
4¡¢ÔËÐнá¹û

|