lookup/2, tune_qos
This commit is contained in:
parent
62e4c749d5
commit
b172a78fcd
|
@ -42,8 +42,10 @@
|
||||||
%% API Exports
|
%% API Exports
|
||||||
-export([start_link/4]).
|
-export([start_link/4]).
|
||||||
|
|
||||||
-export([create/2, subscribe/1, subscribe/2,
|
-export([create/2, lookup/2, subscribe/1, subscribe/2,
|
||||||
unsubscribe/1, unsubscribe/2, publish/1]).
|
unsubscribe/1, unsubscribe/2, publish/1, delete/2]).
|
||||||
|
|
||||||
|
%% Subscriptions API
|
||||||
|
|
||||||
%% Local node
|
%% Local node
|
||||||
-export([match/1]).
|
-export([match/1]).
|
||||||
|
@ -154,6 +156,30 @@ create(subscription, {SubId, Topic, Qos}) ->
|
||||||
{aborted, Error} -> {error, Error}
|
{aborted, Error} -> {error, Error}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
%% @doc Lookup Topic or Subscription.
|
||||||
|
%% @end
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
-spec lookup(topic | subscription, binary()) -> list().
|
||||||
|
lookup(topic, Topic) ->
|
||||||
|
mnesia:dirty_read(topic, Topic);
|
||||||
|
|
||||||
|
lookup(subscription, ClientId) ->
|
||||||
|
mnesia:dirty_read(subscription, ClientId).
|
||||||
|
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
%% @doc Delete Topic or Subscription.
|
||||||
|
%% @end
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
delete(topic, _Topic) ->
|
||||||
|
{error, unsupported};
|
||||||
|
|
||||||
|
delete(subscription, ClientId) when is_binary(ClientId) ->
|
||||||
|
mnesia:dirty_deleate({subscription, ClientId});
|
||||||
|
|
||||||
|
delete(subscription, {ClientId, Topic}) when is_binary(ClientId) ->
|
||||||
|
mnesia:async_dirty(fun remove_subscriptions/2, [ClientId, [Topic]]).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% @doc Subscribe Topics
|
%% @doc Subscribe Topics
|
||||||
%% @end
|
%% @end
|
||||||
|
@ -363,7 +389,7 @@ remove_subscriptions(SubId, Topics) ->
|
||||||
lists:foreach(fun(Topic) ->
|
lists:foreach(fun(Topic) ->
|
||||||
Pattern = #mqtt_subscription{subid = SubId, topic = Topic, qos = '_'},
|
Pattern = #mqtt_subscription{subid = SubId, topic = Topic, qos = '_'},
|
||||||
Records = mnesia:match_object(subscription, Pattern, write),
|
Records = mnesia:match_object(subscription, Pattern, write),
|
||||||
[delete_subscription(Record) || Record <- Records]
|
lists:foreach(fun delete_subscription/1, Records)
|
||||||
end, Topics).
|
end, Topics).
|
||||||
|
|
||||||
delete_subscription(Record) ->
|
delete_subscription(Record) ->
|
||||||
|
|
|
@ -483,7 +483,7 @@ handle_cast(Msg, State) ->
|
||||||
%% Dispatch Message
|
%% Dispatch Message
|
||||||
handle_info({dispatch, Topic, Msg}, Session = #session{subscriptions = Subscriptions})
|
handle_info({dispatch, Topic, Msg}, Session = #session{subscriptions = Subscriptions})
|
||||||
when is_record(Msg, mqtt_message) ->
|
when is_record(Msg, mqtt_message) ->
|
||||||
dispatch(fixqos(Topic, Msg, Subscriptions), Session);
|
dispatch(tune_qos(Topic, Msg, Subscriptions), Session);
|
||||||
|
|
||||||
handle_info({timeout, awaiting_ack, PktId}, Session = #session{client_pid = undefined,
|
handle_info({timeout, awaiting_ack, PktId}, Session = #session{client_pid = undefined,
|
||||||
awaiting_ack = AwaitingAck}) ->
|
awaiting_ack = AwaitingAck}) ->
|
||||||
|
@ -603,7 +603,7 @@ dispatch(Msg = #mqtt_message{qos = QoS}, Session = #session{message_queue = MsgQ
|
||||||
hibernate(Session#session{message_queue = emqttd_mqueue:in(Msg, MsgQ)})
|
hibernate(Session#session{message_queue = emqttd_mqueue:in(Msg, MsgQ)})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
fixqos(Topic, Msg = #mqtt_message{qos = PubQos}, Subscriptions) ->
|
tune_qos(Topic, Msg = #mqtt_message{qos = PubQos}, Subscriptions) ->
|
||||||
case dict:find(Topic, Subscriptions) of
|
case dict:find(Topic, Subscriptions) of
|
||||||
{ok, SubQos} when PubQos > SubQos ->
|
{ok, SubQos} when PubQos > SubQos ->
|
||||||
Msg#mqtt_message{qos = SubQos};
|
Msg#mqtt_message{qos = SubQos};
|
||||||
|
|
Loading…
Reference in New Issue