CTK插件框架学习-服务工厂(06)https://mp.csdn.net/mp_blog/creation/editor/137295686
一、简介
事件监听指当事件发生变化时所产生的通信,是动态的,对于已经发生过的事件无法监听
二、事件类型
1、框架事件
监听框架状态变化,因为监听在框架初始化之后所以只能监听框架结束事件
事件类型 |
FRAMEWORK_STARTED |
PLUGIN_ERROR |
PLUGIN_WARNING |
PLUGIN_INFO |
FRAMEWORK_STOPPED |
FRAMEWORK_STOPPED_UPDATE |
FRAMEWORK_WAIT_TIMEDOUT |
2、插件事件
监听插件状态变化,如插件安装,插件启动
事件类型 |
---|
INSTALLED |
RESOLVED |
LAZY_ACTIVATION |
STARTING |
STARTED |
STOPPING |
STOPPED |
UPDATED |
UNRESOLVED |
UNINSTALLED |
3、服务事件
在服务内进行插件注册、销毁等触发
事件类型 |
---|
REGISTERED |
MODIFIED |
MODIFIED_ENDMATCH |
UNREGISTERING |
三、监听功能测试
EventListener类
============================EventListener.h==================================
#pragma once
#include <QObject>
#include <ctkPluginFrameworkEvent.h>
#include <ctkPluginEvent.h>
#include <ctkServiceEvent.h>class EventListener :public QObject
{Q_OBJECTpublic:explicit EventListener(QObject *parent = Q_NULLPTR);~EventListener();public slots:void slotFrameWorkEvent(const ctkPluginFrameworkEvent& event);void slotPluginEvent(const ctkPluginEvent& event);void slotServiceEvent(const ctkServiceEvent & event);
};============================EventListener.cpp================================#include "EventListener.h"
#include <qdebug.h>EventListener::EventListener(QObject * parent)
{
}EventListener::~EventListener()
{
}void EventListener::slotFrameWorkEvent(const ctkPluginFrameworkEvent & event)
{if (event.isNull()){qDebug() << event.getPlugin()->getSymbolicName() << ": " << event.getErrorString();return;}qDebug() << event.getPlugin()->getSymbolicName() << ": " << event.getType();
}void EventListener::slotPluginEvent(const ctkPluginEvent & event)
{if (event.isNull()){qDebug() << event.getPlugin()->getSymbolicName() << "is null";return;}qDebug() << event.getPlugin()->getSymbolicName() << ": " << event.getType();
}void EventListener::slotServiceEvent(const ctkServiceEvent & event)
{if (event.isNull()){ qDebug() << "ctkServiceEvent is null";return;}ctkServiceReference ref = event.getServiceReference();QSharedPointer<ctkPlugin> plugin = ref.getPlugin();qDebug() << plugin->getSymbolicName() << ": " << event.getType() << " usingPlugins:" << ref.getUsingPlugins();
}
main.cpp
#include "CTKPlugin.h"
#include <QtWidgets/QApplication>#include <iostream>
#include <QStyleFactory>
#include <QDir>
#include <QDirIterator>
#include <QDebug>#include "ctkPluginFrameworkFactory.h"
#include "ctkPluginFramework.h"
#include "ctkPluginException.h"
#include "ctkPluginContext.h"
#include "ctkPluginFrameworkLauncher.h"#include "EventListener.h"#include "../TestPlugin/iTestPlugin.h"
#include "../TestPlugin2/IService1.h"
#include "../TestPlugin2/IService2.h"
#include "../TestPlugin3/IService.h"
#include "../TestServiceFactory/IPrintfService.h"/*
* 1、注意:Plugin-SymbolicName要满足这里的前缀是:TARGET/META-INF格式。TARGET的名字最好和工程名一致,不然可能出现device not open错误。
* 2、如果CTK初始化、插件安装启动等是在一个类中,则与CTK相关的变量应定义成类的属性,不能是成员变量,否则获取不到服务
* 3、CTK插件组成:
(1)每个插件有自己的注册器Activator,继承自QObject和ctkPluginActivator的一个类,并实现ctkPluginActivator的start、stop函数
(2)每个插件必须有一个资源文件,名称一般与插件名称一致,前缀必须为TARGET/META-INF,例:插件名称/META-INF
(3)每个插件必须添加一个元数据文件,名字必须为MANIFEST.MF,并添加到资源文件中
* 4、QSharedPointer framework这个对象既可以作为对象也可以作为对象指针,但要作为插件框架使用必须要用指针方法调用
* 5、生成的插件名(TARGET)不要有下划线,因为CTK会默认将插件名中的下划线替换成点号,最后导致找不到插件
*/int main(int argc, char *argv[])
{QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);QApplication a(argc, argv);a.setApplicationName("ctktest");//Linux下没有名称报错QString path = QCoreApplication::applicationDirPath();//启用通信插件框架
#ifdef _DEBUGctkPluginFrameworkLauncher::addSearchPath(path + "/CTKPlugins");//ctkPluginFrameworkLauncher::addSearchPath("E:/Demo(Qt5)/08_CTKPlugin/CTKPlugin/CTK/lib/ctk-0.1/plugins");
#elsectkPluginFrameworkLauncher::addSearchPath(path + "/CTKPlugins");
#endif // _DEBUG// 设置并启动 CTK 插件框架try {ctkPluginFrameworkLauncher::start("org.commontk.eventadmin");}catch (ctkException e){std::cout << e.message().toStdString() << std::endl;}// 启动插件工厂ctkPluginFrameworkFactory* ctkFrameWorkFactory = new ctkPluginFrameworkFactory;QSharedPointer<ctkPluginFramework> framework = ctkFrameWorkFactory->getFramework();try {framework->init();framework->start();}catch (const ctkPluginException& e){std::cout << "framework init fail" << std::endl;}//在框架初始化启动成功后再调用事件监听EventListener eventListener;framework->getPluginContext()->connectFrameworkListener(&eventListener, SLOT(slotFrameWorkEvent(ctkPluginFrameworkEvent)));framework->getPluginContext()->connectPluginListener(&eventListener, SLOT(slotPluginEvent(const ctkPluginEvent&)));framework->getPluginContext()->connectServiceListener(&eventListener, "slotServiceEvent");//QString dir = QCoreApplication::applicationDirPath();//dir += "/plugins/TestPlugin.dll";//QUrl url = QUrl::fromLocalFile(dir);QSharedPointer<ctkPlugin> plugin;
#if 1QDirIterator iter(path + "/plugins/", { "*.dll" }, QDir::NoFilter, QDirIterator::Subdirectories);while (iter.hasNext()) {//qDebug() << iter.next();QString dllPath = iter.next();QUrl url = QUrl::fromLocalFile(dllPath);try{plugin = framework->getPluginContext()->installPlugin(url);qDebug() << QString("Plugin[%1_%2] installed...").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());//获取MANIFEST.MF中的数据QHash<QString, QString> headers = plugin->getHeaders();ctkVersion version = ctkVersion::parseVersion(headers.value(ctkPluginConstants::PLUGIN_VERSION));QString name = headers.value(ctkPluginConstants::PLUGIN_NAME);}catch (ctkPluginException e) {std::cout << e.message().toStdString() << e.getType() << std::endl;}try {plugin->start(ctkPlugin::START_TRANSIENT);//表示立即启用插件,不设置参数的话加载后也不会立即打印输出qDebug() << QString("Plugin[%1_%2] started").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());}catch (ctkPluginException e) {std::cout << e.message().toStdString() << e.getType() << std::endl;}}
#else#endifctkServiceReference ref = framework->getPluginContext()->getServiceReference<IPrintfService>();if (ref){IPrintfService* service = qobject_cast<IPrintfService*>(framework->getPluginContext()->getService(ref));if (service != nullptr){service->printf();}}//ctkPlugin::State sta = plugin->getState();//ctkPluginFrameworkLauncher::stop();//plugin->stop(); //plugin->uninstall();//sta = plugin->getState();CTKPlugin c;c.show();return a.exec();
}