ctk 通信机制

CTK 是一个成熟的插件化框架,在使用前需要链接 CTK 的两个库 Core 和 PluginFramwork,本文将针对 CTK 框架的通信机制进行介绍。

插进之间的消息机制

消息订阅

	//事件管理服务
	ctkServiceReference eventAdminRef = context->getServiceReference<ctkEventAdmin>();
	m_eventAdmin = qobject_cast<ctkEventAdmin*>(context->getService(eventAdminRef));
	if(!m_eventAdmin){
	qDebug() << "事件管理服务获取失败!";
	}

	//消息订阅
	ctkDictionary props;
	props.insert(ctkEventConstants::EVENT_TOPIC, 		"cn/qtio/eventAdmin/subscriber/handleEvent");
	context->registerService<ctkEventHandler>(this, props);

发布消息

	ctkServiceReference ref = context->getServiceReference<ctkEventAdmin>();
	ctkEventAdmin *eventAdmin = qobject_cast<ctkEventAdmin*>(context->getService(ref));
	ctkProperties props;
	props.insert("type", "消息类型");
	ctkEvent event("cn/qtio/eventAdmin/subscriber/handleEvent", props);
	if (eventAdmin) {
	    //sendEvent:同步通信
	    eventAdmin->sendEvent(event);
	}

在框架内部,提供一个专门用于事件驱动的模块,各个插件可以通过发送事件的方式将内部数据发送出去。

调用接口

    //插件管理器
    ctkServiceReference pluginAdminRef = context->getServiceReference<PluginAdmin>();
    m_pluginAdmin = qobject_cast<PluginAdmin *>(context->getService(pluginAdminRef));
    if(!m_pluginAdmin){
        qDebug() << "获取插件管理器失败!";
    }

调用信号

 	//消息订阅
    ctkDictionary props;
    props.insert(ctkEventConstants::EVENT_TOPIC, "cn/qtio/eventAdmin/MainWindow/handleEvent");
    context->registerService<ctkEventHandler>(this, props);

    //注册异步信号
    m_eventAdmin->publishSignal(this, SIGNAL(queuedPublished(ctkDictionary)), "cn/qtio/eventAdmin/subscriber/handleEvent", Qt::QueuedConnection);
    //注册同步信号
    m_eventAdmin->publishSignal(this, SIGNAL(directPublished(ctkDictionary)), "cn/qtio/eventAdmin/subscriber/handleEvent", Qt::DirectConnection);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值