这一次,主要分析client subscribe 某个topic 的处理流程。

由protocol开始

是的,还是要从protocol开始,至于为什么,之前就说过了。

subscribe 类型的packet的处理是:

 %% 直接过滤掉topic 为空的情况
process(?SUBSCRIBE_PACKET(PacketId, []), State) ->
send(?SUBACK_PACKET(PacketId, []), State); process(?SUBSCRIBE_PACKET(PacketId, TopicTable), State = #proto_state{session = Session}) ->
%% 组装client 信息
Client = client(State),
%% 检查ACL
...
%% session 为clientid 对应的session pid
%% TopicTable 为 [{TopicName, QoS}]
emqttd_session:subscribe(Session, PacketId, TopicTable)
...
;

1、过滤掉topictable 为空的情况

2、组装必要的client 信息,完成ACL检查

3、获取clientid 对应的session pid,并调用emqttd_session:subscribe/3 函数

emqttd_session 模块处理

emqttd_session:subscribe/3 只是一个接口函数,实际的处理逻辑是在emqttd_session 模块的handle_cast callback 中实现。

-spec(subscribe(pid(), mqtt_packet_id(), [{binary(), mqtt_qos()}]) -> ok).
subscribe(SessPid, PacketId, TopicTable) ->
From = self(), %%这里的self 是client process id
AckFun = fun(GrantedQos) ->
From ! {suback, PacketId, GrantedQos}
end,
gen_server2:cast(SessPid, {subscribe, TopicTable, AckFun}).

接口函数的定义如上。

handle_cast callback 的实现如下:

handle_cast({subscribe, TopicTable0, AckFun},
Session = #session{client_id = ClientId,
                   %% subscription 是dict
subscriptions = Subscriptions}) ->
%% rewrite topic name 对topic name 做一些处理
Subscriptions1 = lists:foldl(
fun({Topic, Qos}, SubDict) ->
case dict:find(Topic, SubDict) of
{ok, Qos} ->
%% 已经存在,并且QoS 未更新,所以什么都不需要做
SubDict;
{ok, OldQos} ->
%% 已经存在,但是QoS 更新,所以,需要更新一下
emqttd_server:update_subscription(ClientId, Topic, OldQos, Qos),
dict:store(Topic, Qos, SubDict);
error ->
%% 不存在,直接添加
emqttd:subscribe(ClientId, Topic, Qos),
dict:store(Topic, Qos, SubDict)
end
end, Subscriptions, TopicTable),

更新subscribe

更新subscribe,也就是调用emqttd_server:update_subscription/4 。

emqttd_server 也是由pool 组织的gen_server进程,主要作用是subscription 的增删改查,subscription 信息是保存在 subscription mnesia table 中的,subscription mnesia table的字段信息如下:

-record(mqtt_subscription,
{subid :: binary() | atom(),
topic :: binary(),
qos = 0 :: 0 | 1 | 2
}).

其中,subid 即为subscriber id,也就是clientid,topic 即为topic的名称。

而,update subscription 的逻辑:

 %% 外部接口
update_subscription(ClientId, Topic, OldQos, NewQos) ->
call(server(self()), {update_subscription, ClientId, Topic, ?QOS_I(OldQos), ?QOS_I(NewQos)}). handle_call({update_subscription, ClientId, Topic, OldQos, NewQos}, _From, State) ->
if_subsciption(State, fun() ->
OldSub = #mqtt_subscription{subid = ClientId, topic = Topic, qos = OldQos},
NewSub = #mqtt_subscription{subid = ClientId, topic = Topic, qos = NewQos},
%% 使用事物
mnesia:transaction(fun update_subscription_/2, [OldSub, NewSub]),
set_subscription_stats()
end), ok(State); update_subscription_(OldSub, NewSub) ->
%% 删除旧的 subscription
mnesia:delete_object(subscription, OldSub, write),
%% 写入新的 subscription
mnesia:write(subscription, NewSub, write).

因为 subscription mnesia table的类型为bag,也就是一个clientid 可能会和多个topic 相对应,所以,不能依据key 进行delete,必须使用delete_object的方式。

创建subscribe

create subscribe的处理略微有些绕,不知道是作者有意而为之,还是其他什么原因。

首先,create subscribe的入口函数在emqttd module中,

-spec(subscribe(binary(), binary(), mqtt_qos()) -> {ok, mqtt_qos()}).
subscribe(ClientId, Topic, Qos) ->
emqttd_server:subscribe(ClientId, Topic, Qos).

在此调用emqttd_server:subscribe/3 函数,并请求emqttd_server 进程,emqttd_server 进程调用handle_call callback 函数,处理请求。

 %% 外部接口
-spec(subscribe(binary(), binary(), mqtt_qos()) -> ok).
subscribe(ClientId, Topic, Qos) ->
%% 这里的self 是emqttd_session 进程,这个调用是在emqttd_session
%% module 中的 handle_cast callback 发起的
From = self(),
call(server(From), {subscribe, From, ClientId, Topic, ?QOS_I(Qos)}). handle_call({subscribe, SubPid, ClientId, Topic, Qos}, _From, State) ->
%% call pubsub process
pubsub_subscribe_(SubPid, Topic),
if_subsciption(State, fun() ->
%% 将subscription 信息写入到 subscription mnesia table 中
add_subscription_(ClientId, Topic, Qos),
set_subscription_stats()
end),
%% monitor session pid,当起DOWN 之后,去掉subscribe 并移除相关信息
ok(monitor_subscriber_(ClientId, SubPid, State)); %% @private
%% @doc Call pubsub to subscribe
pubsub_subscribe_(SubPid, Topic) ->
case ets:match(subscribed, {SubPid, Topic}) of
[] ->
emqttd_pubsub:async_subscribe(Topic, SubPid),
ets:insert(subscribed, {SubPid, Topic});
[_] ->
false
end.

L26处,用了subscribed ets table,记录session pid subscribe 的所有topic,这样在 session pid DOWN的时候,就可以移除所有的topic 中session pid 相关的信息了。

而,emqttd_pubsub 同样是由pool 组织的gen_server 进程。

 %% 外部接口,发起请求
-spec(async_subscribe(binary(), pid()) -> ok).
async_subscribe(Topic, SubPid) when is_binary(Topic) ->
cast(pick(Topic), {subscribe, Topic, SubPid}). handle_cast({subscribe, Topic, SubPid}, State) ->
%% 实际的处理函数
add_subscriber_(Topic, SubPid),
{noreply, setstats(State)}; add_subscriber_(Topic, SubPid) ->
%% 检查该Topic 是否已经存在
%% 若不存在,则先增加{Topic,Node}信息,为多node 场景服务
...
ets:insert(subscriber, {Topic, SubPid}). %% 这里的subscriber 是一张ets table,接下来的publish 主要就是用的这张表 ********

至此,subscribe 操作的处理逻辑就ok了。

总结

应该也不需要,只是这代码贴的有点多了。(示意图待补)

emqtt 3 (我要subscribe 这个topic)的更多相关文章

  1. emqtt 1 (初初初初稿)

    第一篇,先简单分析一下整个emqtt 的大致结构,包括两个部分: 1.message packet 类型 2.message 流向 message packet 类型 P1:mqtt_packet 的 ...

  2. dojo.publish 和 dojo.subscribe

    原文链接:http://www.cnblogs.com/didi/archive/2010/06/13/1757894.html //dojo.publish 和 dojo.subscribe  :d ...

  3. javascript设计模式——Publish/Subscribe

    推荐阅读http://dev.housetrip.com/2014/09/15/decoupling-javascript-apps-using-pub-sub-pattern/ 我们先引出问题的所在 ...

  4. ActiveMQ——activemq的详细说明,queue、topic的区别(精选)

    JMS中定义了两种消息模型:点对点(point to point, queue)和发布/订阅(publish/subscribe,topic).主要区别就是是否能重复消费. 点对点:Queue,不可重 ...

  5. 观察者模式(Observer)和发布(Publish/订阅模式(Subscribe)的区别

    观察者模式(Observer)和发布(Publish/订阅模式(Subscribe)的区别 在翻阅资料的时候,有人把观察者(Observer)模式等同于发布(Publish)/订阅(Subscribe ...

  6. dojo topic 发布与订阅 小例子可以参考下

    <!DOCTYPE html><html> <head> <title></title></head> <body> ...

  7. 【RocketMQ】同一个项目中,同一个topic,可以存在多个消费者么?

    一.问题答案 是不可以的 而且后注册的会替换前注册的,MqConsumer2会替换MqConsumer,并且只结束tag-2的消息 /** * @date 2019/05/28 */ @Compone ...

  8. MQTT研究之EMQ:【CoAP协议应用开发】

    本博文的重点是尝试CoAP协议的应用开发,其中包含CoAP协议中一个重要的开源工具libcoap的安装和遇到的问题调研.当然,为了很好的将EMQ的CoAP协议网关用起来,也调研了下EMQ体系下,CoA ...

  9. 现代DOJO(翻译)

    http://dojotoolkit.org/documentation/tutorials/1.10/modern_dojo/index.html 你可能已经不用doio一段时间了,或者你一直想保持 ...

随机推荐

  1. eclipse部署的web项目没有添加到Tomcat的webapps目录下解决方法

    eclipse没有像myeclipse那样,添加web项目时会自动部署到Tomcat的webapps目录下. 而是部署到了eclipse的.metadata\.plugins\org.eclipse. ...

  2. centos下使用fdisk扩展分区容量大小

    硬盘空间为20G,VMware增加磁盘大小,需要再增加10G空间 扩展完后,重启系统,再次使用fdisk -l查看,会发现硬盘空间变大了: 重新创建分区,调整分区信息 本次实验主要对/dev/sda4 ...

  3. WPF DataGrid 分组

    public ListCollectionView collection; collection = new ListCollectionView(obj.empData); collection.G ...

  4. DBDB的维护和管理

    挂载drbd前首先需要确认当前直接的DRDB分区是primary状态,可以从"cat /proc/drdb" 命令中查询节点状态.例如:[yongsan@mfsmaster drb ...

  5. Python主流框架

    15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框架Django 应该是最出名的Pyth ...

  6. mysql查询结果带上序号

    select (@i:=@i+1) as rownum,t1.id ","from mega_user t1,(select @i:=0) t2 order by t1.gold ...

  7. opencv:通过滑动条调节亮度和对比度

    示例代码: #include <opencv.hpp> using namespace cv; using namespace std; void on_change(int, void* ...

  8. python基础之模块二

    六 logging模块 6.1 函数式简单配置 import logging #导入模块 logging.debug('debug message') #调试消息 logging.debug('inf ...

  9. Android学习之Activity跳转与传值

    Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据. 一.Activity跳转 方法一 Intent intent = new Intent(A.this, ...

  10. CentOS7 编译安装golang和rpm安装golang

    编译安装 1.下载golang二进制安装包: https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz 2.解压安装包到指定目录,此 ...