ignore duplicated subscriptions

This commit is contained in:
Feng 2015-09-11 14:18:30 +08:00
parent c8a121ff08
commit ea70389cf7
1 changed files with 31 additions and 27 deletions

View File

@ -282,36 +282,40 @@ prioritise_info(Msg, _Len, _State) ->
handle_call({subscribe, TopicTable0}, _From, Session = #session{client_id = ClientId, handle_call({subscribe, TopicTable0}, _From, Session = #session{client_id = ClientId,
subscriptions = Subscriptions}) -> subscriptions = Subscriptions}) ->
TopicTable = emqttd_broker:foldl_hooks('client.subscribe', [ClientId], TopicTable0), case TopicTable0 -- Subscriptions of
[] ->
{reply, {ok, [Qos || {_, Qos} <- TopicTable0]}, Session};
_ ->
TopicTable = emqttd_broker:foldl_hooks('client.subscribe', [ClientId], TopicTable0),
%% subscribe first and don't care if the subscriptions have been existed
{ok, GrantedQos} = emqttd_pubsub:subscribe(TopicTable),
%% subscribe first and don't care if the subscriptions have been existed emqttd_broker:foreach_hooks('client.subscribe.after', [ClientId, TopicTable]),
{ok, GrantedQos} = emqttd_pubsub:subscribe(TopicTable),
emqttd_broker:foreach_hooks('client.subscribe.after', [ClientId, TopicTable]), lager:info([{client, ClientId}], "Session(~s): subscribe ~p, Granted QoS: ~p",
[ClientId, TopicTable, GrantedQos]),
lager:info([{client, ClientId}], "Session(~s): subscribe ~p, Granted QoS: ~p", Subscriptions1 =
[ClientId, TopicTable, GrantedQos]), lists:foldl(fun({Topic, Qos}, Acc) ->
case lists:keyfind(Topic, 1, Acc) of
Subscriptions1 = {Topic, Qos} ->
lists:foldl(fun({Topic, Qos}, Acc) -> lager:warning([{client, ClientId}], "Session(~s): "
case lists:keyfind(Topic, 1, Acc) of "resubscribe ~s, qos = ~w", [ClientId, Topic, Qos]), Acc;
{Topic, Qos} -> {Topic, OldQos} ->
lager:warning([{client, ClientId}], "Session(~s): " lager:warning([{client, ClientId}], "Session(~s): "
"resubscribe ~s, qos = ~w", [ClientId, Topic, Qos]), Acc; "resubscribe ~s, old qos=~w, new qos=~w", [ClientId, Topic, OldQos, Qos]),
{Topic, OldQos} -> lists:keyreplace(Topic, 1, Acc, {Topic, Qos});
lager:warning([{client, ClientId}], "Session(~s): " false ->
"resubscribe ~s, old qos=~w, new qos=~w", [ClientId, Topic, OldQos, Qos]), %%TODO: the design is ugly, rewrite later...:(
lists:keyreplace(Topic, 1, Acc, {Topic, Qos}); %% <MQTT V3.1.1>: 3.8.4
false -> %% Where the Topic Filter is not identical to any existing Subscriptions filter,
%%TODO: the design is ugly, rewrite later...:( %% a new Subscription is created and all matching retained messages are sent.
%% <MQTT V3.1.1>: 3.8.4 emqttd_retained:dispatch(Topic, self()),
%% Where the Topic Filter is not identical to any existing Subscriptions filter, [{Topic, Qos} | Acc]
%% a new Subscription is created and all matching retained messages are sent. end
emqttd_retained:dispatch(Topic, self()), end, Subscriptions, TopicTable),
[{Topic, Qos} | Acc] {reply, {ok, GrantedQos}, Session#session{subscriptions = Subscriptions1}}
end end;
end, Subscriptions, TopicTable),
{reply, {ok, GrantedQos}, Session#session{subscriptions = Subscriptions1}};
handle_call({unsubscribe, Topics0}, _From, Session = #session{client_id = ClientId, handle_call({unsubscribe, Topics0}, _From, Session = #session{client_id = ClientId,
subscriptions = Subscriptions}) -> subscriptions = Subscriptions}) ->