emqttd_message:make, hooks
This commit is contained in:
parent
669a717bb2
commit
895c9ddaed
|
@ -121,12 +121,10 @@ terminate(_, _) ->
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
alarm_msg(Type, AlarmId, Json) ->
|
alarm_msg(Type, AlarmId, Json) ->
|
||||||
#mqtt_message{from = alarm,
|
Msg = emqttd_message:make(alarm,
|
||||||
qos = 1,
|
topic(Type, AlarmId),
|
||||||
sys = true,
|
iolist_to_binary(Json)),
|
||||||
topic = topic(Type, AlarmId),
|
emqttd_message:set_flag(sys, Msg).
|
||||||
payload = iolist_to_binary(Json),
|
|
||||||
timestamp = os:timestamp()}.
|
|
||||||
|
|
||||||
topic(alert, AlarmId) ->
|
topic(alert, AlarmId) ->
|
||||||
emqttd_topic:systop(<<"alarms/", AlarmId/binary, "/alert">>);
|
emqttd_topic:systop(<<"alarms/", AlarmId/binary, "/alert">>);
|
||||||
|
|
|
@ -81,7 +81,7 @@ init([Node, SubTopic, Options]) ->
|
||||||
true ->
|
true ->
|
||||||
true = erlang:monitor_node(Node, true),
|
true = erlang:monitor_node(Node, true),
|
||||||
State = parse_opts(Options, #state{node = Node, subtopic = SubTopic}),
|
State = parse_opts(Options, #state{node = Node, subtopic = SubTopic}),
|
||||||
emqttd_pubsub:subscribe({SubTopic, ?QOS_0}),
|
emqttd_pubsub:subscribe({SubTopic, State#state.qos}),
|
||||||
{ok, State};
|
{ok, State};
|
||||||
false ->
|
false ->
|
||||||
{stop, {cannot_connect, Node}}
|
{stop, {cannot_connect, Node}}
|
||||||
|
@ -107,7 +107,7 @@ handle_cast(_Msg, State) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
handle_info({dispatch, Msg}, State = #state{node = Node, status = down}) ->
|
handle_info({dispatch, Msg}, State = #state{node = Node, status = down}) ->
|
||||||
lager:warning("Bridge Dropped Msg for ~p Down:~n~p", [Node, Msg]),
|
lager:error("Bridge Dropped Msg for ~p Down: ~s", [Node, emqttd_message:format(Msg)]),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_info({dispatch, Msg}, State = #state{node = Node, status = up}) ->
|
handle_info({dispatch, Msg}, State = #state{node = Node, status = up}) ->
|
||||||
|
@ -159,14 +159,7 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
%%%=============================================================================
|
%%%=============================================================================
|
||||||
|
|
||||||
%TODO: qos is not right...
|
transform(Msg = #mqtt_message{topic = Topic}, #state{topic_prefix = Prefix,
|
||||||
transform(Msg = #mqtt_message{topic = Topic}, #state{qos = Qos,
|
|
||||||
topic_prefix = Prefix,
|
|
||||||
topic_suffix = Suffix}) ->
|
topic_suffix = Suffix}) ->
|
||||||
Msg1 =
|
Msg#mqtt_message{topic = <<Prefix/binary, Topic/binary, Suffix/binary>>}.
|
||||||
if
|
|
||||||
Qos =:= undefined -> Msg;
|
|
||||||
true -> Msg#mqtt_message{qos = Qos}
|
|
||||||
end,
|
|
||||||
Msg1#mqtt_message{topic = <<Prefix/binary, Topic/binary, Suffix/binary>>}.
|
|
||||||
|
|
||||||
|
|
|
@ -186,7 +186,7 @@ foldl_hooks(Hook, Args, Acc0) ->
|
||||||
case ets:lookup(?BROKER_TAB, {hook, Hook}) of
|
case ets:lookup(?BROKER_TAB, {hook, Hook}) of
|
||||||
[{_, Hooks}] ->
|
[{_, Hooks}] ->
|
||||||
lists:foldl(fun({_Name, {M, F, A}}, Acc) ->
|
lists:foldl(fun({_Name, {M, F, A}}, Acc) ->
|
||||||
apply(M, F, [Acc, Args++A])
|
apply(M, F, lists:append([Args, [Acc], A]))
|
||||||
end, Acc0, Hooks);
|
end, Acc0, Hooks);
|
||||||
[] ->
|
[] ->
|
||||||
Acc0
|
Acc0
|
||||||
|
@ -286,23 +286,15 @@ create_topic(Topic) ->
|
||||||
|
|
||||||
retain(brokers) ->
|
retain(brokers) ->
|
||||||
Payload = list_to_binary(string:join([atom_to_list(N) || N <- running_nodes()], ",")),
|
Payload = list_to_binary(string:join([atom_to_list(N) || N <- running_nodes()], ",")),
|
||||||
publish(#mqtt_message{from = broker,
|
Msg = emqttd_message:make(broker, <<"$SYS/brokers">>, Payload),
|
||||||
retain = true,
|
emqttd_pubsub:publish(emqttd_message:set_flag(sys, Msg)).
|
||||||
topic = <<"$SYS/brokers">>,
|
|
||||||
payload = Payload}).
|
|
||||||
|
|
||||||
retain(Topic, Payload) when is_binary(Payload) ->
|
retain(Topic, Payload) when is_binary(Payload) ->
|
||||||
publish(#mqtt_message{from = broker,
|
Msg = emqttd_message:make(broker, emqttd_topic:systop(Topic), Payload),
|
||||||
retain = true,
|
emqttd_pubsub:publish(emqttd_message:set_flag(retain, Msg)).
|
||||||
topic = emqttd_topic:systop(Topic),
|
|
||||||
payload = Payload}).
|
|
||||||
|
|
||||||
publish(Topic, Payload) when is_binary(Payload) ->
|
publish(Topic, Payload) when is_binary(Payload) ->
|
||||||
publish( #mqtt_message{from = broker,
|
Msg = emqttd_message:make(broker, emqttd_topic:systop(Topic), Payload),
|
||||||
topic = emqttd_topic:systop(Topic),
|
|
||||||
payload = Payload}).
|
|
||||||
|
|
||||||
publish(Msg) ->
|
|
||||||
emqttd_pubsub:publish(Msg).
|
emqttd_pubsub:publish(Msg).
|
||||||
|
|
||||||
uptime(#state{started_at = Ts}) ->
|
uptime(#state{started_at = Ts}) ->
|
||||||
|
|
|
@ -49,14 +49,11 @@ handle_request('POST', "/mqtt/publish", Req) ->
|
||||||
Qos = int(get_value("qos", Params, "0")),
|
Qos = int(get_value("qos", Params, "0")),
|
||||||
Retain = bool(get_value("retain", Params, "0")),
|
Retain = bool(get_value("retain", Params, "0")),
|
||||||
Topic = list_to_binary(get_value("topic", Params)),
|
Topic = list_to_binary(get_value("topic", Params)),
|
||||||
Message = list_to_binary(get_value("message", Params)),
|
Payload = list_to_binary(get_value("message", Params)),
|
||||||
case {validate(qos, Qos), validate(topic, Topic)} of
|
case {validate(qos, Qos), validate(topic, Topic)} of
|
||||||
{true, true} ->
|
{true, true} ->
|
||||||
emqttd_pubsub:publish(#mqtt_message{from = http,
|
Msg = emqttd_message:make(http, Qos, Topic, Payload),
|
||||||
qos = Qos,
|
emqttd_pubsub:publish(Msg#mqtt_message{retain = Retain}),
|
||||||
retain = Retain,
|
|
||||||
topic = Topic,
|
|
||||||
payload = Message}),
|
|
||||||
Req:ok({"text/plan", <<"ok\n">>});
|
Req:ok({"text/plan", <<"ok\n">>});
|
||||||
{false, _} ->
|
{false, _} ->
|
||||||
Req:respond({400, [], <<"Bad QoS">>});
|
Req:respond({400, [], <<"Bad QoS">>});
|
||||||
|
|
|
@ -32,12 +32,39 @@
|
||||||
|
|
||||||
-include("emqttd_protocol.hrl").
|
-include("emqttd_protocol.hrl").
|
||||||
|
|
||||||
-export([from_packet/1, from_packet/2, to_packet/1]).
|
-export([make/3, make/4, from_packet/1, from_packet/2, to_packet/1]).
|
||||||
|
|
||||||
-export([set_flag/1, set_flag/2, unset_flag/1, unset_flag/2]).
|
-export([set_flag/1, set_flag/2, unset_flag/1, unset_flag/2]).
|
||||||
|
|
||||||
-export([format/1]).
|
-export([format/1]).
|
||||||
|
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
%% @doc Make a message
|
||||||
|
%% @end
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
-spec make(From, Topic, Payload) -> mqtt_message() when
|
||||||
|
From :: atom() | binary(),
|
||||||
|
Topic :: binary(),
|
||||||
|
Payload :: binary().
|
||||||
|
make(From, Topic, Payload) ->
|
||||||
|
#mqtt_message{topic = Topic,
|
||||||
|
from = From,
|
||||||
|
payload = Payload,
|
||||||
|
timestamp = os:timestamp()}.
|
||||||
|
|
||||||
|
-spec make(From, Qos, Topic, Payload) -> mqtt_message() when
|
||||||
|
From :: atom() | binary(),
|
||||||
|
Qos :: mqtt_qos(),
|
||||||
|
Topic :: binary(),
|
||||||
|
Payload :: binary().
|
||||||
|
make(From, Qos, Topic, Payload) ->
|
||||||
|
#mqtt_message{msgid = msgid(Qos),
|
||||||
|
topic = Topic,
|
||||||
|
from = From,
|
||||||
|
qos = Qos,
|
||||||
|
payload = Payload,
|
||||||
|
timestamp = os:timestamp()}.
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% @doc Message from Packet
|
%% @doc Message from Packet
|
||||||
%% @end
|
%% @end
|
||||||
|
@ -50,12 +77,14 @@ from_packet(#mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
|
||||||
variable = #mqtt_packet_publish{topic_name = Topic,
|
variable = #mqtt_packet_publish{topic_name = Topic,
|
||||||
packet_id = PacketId},
|
packet_id = PacketId},
|
||||||
payload = Payload}) ->
|
payload = Payload}) ->
|
||||||
#mqtt_message{msgid = PacketId,
|
#mqtt_message{msgid = msgid(Qos),
|
||||||
|
pktid = PacketId,
|
||||||
qos = Qos,
|
qos = Qos,
|
||||||
retain = Retain,
|
retain = Retain,
|
||||||
dup = Dup,
|
dup = Dup,
|
||||||
topic = Topic,
|
topic = Topic,
|
||||||
payload = Payload};
|
payload = Payload,
|
||||||
|
timestamp = os:timestamp()};
|
||||||
|
|
||||||
from_packet(#mqtt_packet_connect{will_flag = false}) ->
|
from_packet(#mqtt_packet_connect{will_flag = false}) ->
|
||||||
undefined;
|
undefined;
|
||||||
|
@ -64,38 +93,44 @@ from_packet(#mqtt_packet_connect{will_retain = Retain,
|
||||||
will_qos = Qos,
|
will_qos = Qos,
|
||||||
will_topic = Topic,
|
will_topic = Topic,
|
||||||
will_msg = Msg}) ->
|
will_msg = Msg}) ->
|
||||||
#mqtt_message{retain = Retain,
|
#mqtt_message{msgid = msgid(Qos),
|
||||||
qos = Qos,
|
topic = Topic,
|
||||||
topic = Topic,
|
retain = Retain,
|
||||||
dup = false,
|
qos = Qos,
|
||||||
payload = Msg}.
|
dup = false,
|
||||||
|
payload = Msg,
|
||||||
|
timestamp = os:timestamp()}.
|
||||||
|
|
||||||
from_packet(ClientId, Packet) ->
|
from_packet(ClientId, Packet) ->
|
||||||
Msg = from_packet(Packet), Msg#mqtt_message{from = ClientId}.
|
Msg = from_packet(Packet), Msg#mqtt_message{from = ClientId}.
|
||||||
|
|
||||||
|
msgid(?QOS_0) ->
|
||||||
|
undefined;
|
||||||
|
msgid(_Qos) ->
|
||||||
|
emqttd_guid:gen().
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% @doc Message to packet
|
%% @doc Message to packet
|
||||||
%% @end
|
%% @end
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
-spec to_packet(mqtt_message()) -> mqtt_packet().
|
-spec to_packet(mqtt_message()) -> mqtt_packet().
|
||||||
to_packet(#mqtt_message{msgid = MsgId,
|
to_packet(#mqtt_message{pktid = PkgId,
|
||||||
qos = Qos,
|
qos = Qos,
|
||||||
retain = Retain,
|
retain = Retain,
|
||||||
dup = Dup,
|
dup = Dup,
|
||||||
topic = Topic,
|
topic = Topic,
|
||||||
payload = Payload}) ->
|
payload = Payload}) ->
|
||||||
|
|
||||||
PacketId = if
|
|
||||||
Qos =:= ?QOS_0 -> undefined;
|
|
||||||
true -> MsgId
|
|
||||||
end,
|
|
||||||
|
|
||||||
#mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
|
#mqtt_packet{header = #mqtt_packet_header{type = ?PUBLISH,
|
||||||
qos = Qos,
|
qos = Qos,
|
||||||
retain = Retain,
|
retain = Retain,
|
||||||
dup = Dup},
|
dup = Dup},
|
||||||
variable = #mqtt_packet_publish{topic_name = Topic,
|
variable = #mqtt_packet_publish{topic_name = Topic,
|
||||||
packet_id = PacketId},
|
packet_id = if
|
||||||
|
Qos =:= ?QOS_0 -> undefined;
|
||||||
|
true -> PkgId
|
||||||
|
end
|
||||||
|
},
|
||||||
payload = Payload}.
|
payload = Payload}.
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
@ -109,6 +144,8 @@ set_flag(Msg) ->
|
||||||
-spec set_flag(atom(), mqtt_message()) -> mqtt_message().
|
-spec set_flag(atom(), mqtt_message()) -> mqtt_message().
|
||||||
set_flag(dup, Msg = #mqtt_message{dup = false}) ->
|
set_flag(dup, Msg = #mqtt_message{dup = false}) ->
|
||||||
Msg#mqtt_message{dup = true};
|
Msg#mqtt_message{dup = true};
|
||||||
|
set_flag(sys, Msg = #mqtt_message{sys = false}) ->
|
||||||
|
Msg#mqtt_message{sys = true};
|
||||||
set_flag(retain, Msg = #mqtt_message{retain = false}) ->
|
set_flag(retain, Msg = #mqtt_message{retain = false}) ->
|
||||||
Msg#mqtt_message{retain = true};
|
Msg#mqtt_message{retain = true};
|
||||||
set_flag(Flag, Msg) when Flag =:= dup orelse Flag =:= retain -> Msg.
|
set_flag(Flag, Msg) when Flag =:= dup orelse Flag =:= retain -> Msg.
|
||||||
|
@ -133,7 +170,7 @@ unset_flag(Flag, Msg) when Flag =:= dup orelse Flag =:= retain -> Msg.
|
||||||
%% @doc Format MQTT Message
|
%% @doc Format MQTT Message
|
||||||
%% @end
|
%% @end
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
format(#mqtt_message{msgid=MsgId, qos=Qos, retain=Retain, dup=Dup, topic=Topic}) ->
|
format(#mqtt_message{msgid=MsgId, pktid = PktId, qos=Qos, retain=Retain, dup=Dup, topic=Topic}) ->
|
||||||
io_lib:format("Message(MsgId=~p, Qos=~p, Retain=~s, Dup=~s, Topic=~s)",
|
io_lib:format("Message(MsgId=~p, PktId=~p, Qos=~p, Retain=~s, Dup=~s, Topic=~s)",
|
||||||
[MsgId, Qos, Retain, Dup, Topic]).
|
[MsgId, PktId, Qos, Retain, Dup, Topic]).
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@
|
||||||
-define(SYSTOP_MESSAGES, [
|
-define(SYSTOP_MESSAGES, [
|
||||||
{counter, 'messages/received'}, % Messages received
|
{counter, 'messages/received'}, % Messages received
|
||||||
{counter, 'messages/sent'}, % Messages sent
|
{counter, 'messages/sent'}, % Messages sent
|
||||||
{gauge, 'messages/retained/count'},% Messagea retained
|
{gauge, 'messages/retained'}, % Messagea retained
|
||||||
{gauge, 'messages/stored/count'}, % Messages stored
|
{gauge, 'messages/stored/count'}, % Messages stored
|
||||||
{counter, 'messages/dropped'} % Messages dropped
|
{counter, 'messages/dropped'} % Messages dropped
|
||||||
]).
|
]).
|
||||||
|
@ -222,9 +222,9 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
%%%=============================================================================
|
%%%=============================================================================
|
||||||
|
|
||||||
publish(Metric, Val) ->
|
publish(Metric, Val) ->
|
||||||
emqttd_pubsub:publish(#mqtt_message{topic = metric_topic(Metric),
|
Payload = emqttd_util:integer_to_binary(Val),
|
||||||
from = metrics,
|
Msg = emqttd_message:make(metrics, metric_topic(Metric), Payload),
|
||||||
payload = emqttd_util:integer_to_binary(Val)}).
|
emqttd_pubsub:publish(Msg).
|
||||||
|
|
||||||
create_metric({gauge, Name}) ->
|
create_metric({gauge, Name}) ->
|
||||||
ets:insert(?METRIC_TAB, {{Name, 0}, 0});
|
ets:insert(?METRIC_TAB, {{Name, 0}, 0});
|
||||||
|
|
|
@ -41,7 +41,7 @@
|
||||||
|
|
||||||
load(Opts) ->
|
load(Opts) ->
|
||||||
Topics = [{list_to_binary(Topic), Qos} || {Topic, Qos} <- Opts, 0 =< Qos, Qos =< 2],
|
Topics = [{list_to_binary(Topic), Qos} || {Topic, Qos} <- Opts, 0 =< Qos, Qos =< 2],
|
||||||
emqttd_broker:hook(client_connected, {?MODULE, client_connected},
|
emqttd_broker:hook('client.connected', {?MODULE, client_connected},
|
||||||
{?MODULE, client_connected, [Topics]}),
|
{?MODULE, client_connected, [Topics]}),
|
||||||
{ok, #state{topics = Topics}}.
|
{ok, #state{topics = Topics}}.
|
||||||
|
|
||||||
|
@ -53,6 +53,5 @@ client_connected(_ConnAck, _Client, _Topics) ->
|
||||||
ignore.
|
ignore.
|
||||||
|
|
||||||
unload(_Opts) ->
|
unload(_Opts) ->
|
||||||
emqttd_broker:unhook(client_connected, {?MODULE, client_connected}).
|
emqttd_broker:unhook('client.connected', {?MODULE, client_connected}).
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,8 +35,8 @@
|
||||||
-export([client_connected/3, client_disconnected/3]).
|
-export([client_connected/3, client_disconnected/3]).
|
||||||
|
|
||||||
load(Opts) ->
|
load(Opts) ->
|
||||||
emqttd_broker:hook(client_connected, {?MODULE, client_connected}, {?MODULE, client_connected, [Opts]}),
|
emqttd_broker:hook('client.connected', {?MODULE, client_connected}, {?MODULE, client_connected, [Opts]}),
|
||||||
emqttd_broker:hook(client_disconnected, {?MODULE, client_disconnected}, {?MODULE, client_disconnected, [Opts]}),
|
emqttd_broker:hook('client.disconnected', {?MODULE, client_disconnected}, {?MODULE, client_disconnected, [Opts]}),
|
||||||
{ok, Opts}.
|
{ok, Opts}.
|
||||||
|
|
||||||
client_connected(ConnAck, #mqtt_client{client_id = ClientId,
|
client_connected(ConnAck, #mqtt_client{client_id = ClientId,
|
||||||
|
@ -55,24 +55,25 @@ client_connected(ConnAck, #mqtt_client{client_id = ClientId,
|
||||||
{protocol, ProtoVer},
|
{protocol, ProtoVer},
|
||||||
{connack, ConnAck},
|
{connack, ConnAck},
|
||||||
{ts, emqttd_util:now_to_secs()}]),
|
{ts, emqttd_util:now_to_secs()}]),
|
||||||
Message = #mqtt_message{from = presence,
|
Msg = emqttd_message:make(presence,
|
||||||
qos = proplists:get_value(qos, Opts, 0),
|
proplists:get_value(qos, Opts, 0),
|
||||||
topic = topic(connected, ClientId),
|
topic(connected, ClientId),
|
||||||
payload = iolist_to_binary(Json)},
|
iolist_to_binary(Json)),
|
||||||
emqttd_pubsub:publish(Message).
|
emqttd_pubsub:publish(Msg).
|
||||||
|
|
||||||
client_disconnected(Reason, ClientId, Opts) ->
|
client_disconnected(Reason, ClientId, Opts) ->
|
||||||
Json = mochijson2:encode([{clientid, ClientId},
|
Json = mochijson2:encode([{clientid, ClientId},
|
||||||
{reason, reason(Reason)},
|
{reason, reason(Reason)},
|
||||||
{ts, emqttd_util:now_to_secs()}]),
|
{ts, emqttd_util:now_to_secs()}]),
|
||||||
emqttd_pubsub:publish(#mqtt_message{from = presence,
|
Msg = emqttd_message:make(presence,
|
||||||
qos = proplists:get_value(qos, Opts, 0),
|
proplists:get_value(qos, Opts, 0),
|
||||||
topic = topic(disconnected, ClientId),
|
topic(disconnected, ClientId),
|
||||||
payload = iolist_to_binary(Json)}).
|
iolist_to_binary(Json)),
|
||||||
|
emqttd_pubsub:publish(Msg).
|
||||||
|
|
||||||
unload(_Opts) ->
|
unload(_Opts) ->
|
||||||
emqttd_broker:unhook(client_connected, {?MODULE, client_connected}),
|
emqttd_broker:unhook('client.connected', {?MODULE, client_connected}),
|
||||||
emqttd_broker:unhook(client_disconnected, {?MODULE, client_disconnected}).
|
emqttd_broker:unhook('client.disconnected', {?MODULE, client_disconnected}).
|
||||||
|
|
||||||
topic(connected, ClientId) ->
|
topic(connected, ClientId) ->
|
||||||
emqttd_topic:systop(list_to_binary(["clients/", ClientId, "/connected"]));
|
emqttd_topic:systop(list_to_binary(["clients/", ClientId, "/connected"]));
|
||||||
|
|
|
@ -35,7 +35,7 @@
|
||||||
|
|
||||||
-export([load/1, reload/1, unload/1]).
|
-export([load/1, reload/1, unload/1]).
|
||||||
|
|
||||||
-export([rewrite/2]).
|
-export([rewrite/3, rewrite/4]).
|
||||||
|
|
||||||
%%%=============================================================================
|
%%%=============================================================================
|
||||||
%%% API
|
%%% API
|
||||||
|
@ -45,22 +45,22 @@ load(Opts) ->
|
||||||
File = proplists:get_value(file, Opts),
|
File = proplists:get_value(file, Opts),
|
||||||
{ok, Terms} = file:consult(File),
|
{ok, Terms} = file:consult(File),
|
||||||
Sections = compile(Terms),
|
Sections = compile(Terms),
|
||||||
emqttd_broker:hook(client_subscribe, {?MODULE, rewrite_subscribe},
|
emqttd_broker:hook('client.subscribe', {?MODULE, rewrite_subscribe},
|
||||||
{?MODULE, rewrite, [subscribe, Sections]}),
|
{?MODULE, rewrite, [subscribe, Sections]}),
|
||||||
emqttd_broker:hook(client_unsubscribe, {?MODULE, rewrite_unsubscribe},
|
emqttd_broker:hook('client.unsubscribe', {?MODULE, rewrite_unsubscribe},
|
||||||
{?MODULE, rewrite, [unsubscribe, Sections]}),
|
{?MODULE, rewrite, [unsubscribe, Sections]}),
|
||||||
emqttd_broker:hook(client_publish, {?MODULE, rewrite_publish},
|
emqttd_broker:hook('client.publish', {?MODULE, rewrite_publish},
|
||||||
{?MODULE, rewrite, [publish, Sections]}).
|
{?MODULE, rewrite, [publish, Sections]}).
|
||||||
|
|
||||||
rewrite(TopicTable, [subscribe, Sections]) ->
|
rewrite(_ClientId, TopicTable, subscribe, Sections) ->
|
||||||
lager:info("rewrite subscribe: ~p", [TopicTable]),
|
lager:info("rewrite subscribe: ~p", [TopicTable]),
|
||||||
[{match_topic(Topic, Sections), Qos} || {Topic, Qos} <- TopicTable];
|
[{match_topic(Topic, Sections), Qos} || {Topic, Qos} <- TopicTable];
|
||||||
|
|
||||||
rewrite(Topics, [unsubscribe, Sections]) ->
|
rewrite(_ClientId, Topics, unsubscribe, Sections) ->
|
||||||
lager:info("rewrite unsubscribe: ~p", [Topics]),
|
lager:info("rewrite unsubscribe: ~p", [Topics]),
|
||||||
[match_topic(Topic, Sections) || Topic <- Topics];
|
[match_topic(Topic, Sections) || Topic <- Topics].
|
||||||
|
|
||||||
rewrite(Message=#mqtt_message{topic = Topic}, [publish, Sections]) ->
|
rewrite(Message=#mqtt_message{topic = Topic}, publish, Sections) ->
|
||||||
%%TODO: this will not work if the client is always online.
|
%%TODO: this will not work if the client is always online.
|
||||||
RewriteTopic =
|
RewriteTopic =
|
||||||
case get({rewrite, Topic}) of
|
case get({rewrite, Topic}) of
|
||||||
|
@ -83,9 +83,9 @@ reload(File) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
unload(_) ->
|
unload(_) ->
|
||||||
emqttd_broker:unhook(client_subscribe, {?MODULE, rewrite_subscribe}),
|
emqttd_broker:unhook('client.subscribe', {?MODULE, rewrite_subscribe}),
|
||||||
emqttd_broker:unhook(client_unsubscribe, {?MODULE, rewrite_unsubscribe}),
|
emqttd_broker:unhook('client.unsubscribe', {?MODULE, rewrite_unsubscribe}),
|
||||||
emqttd_broker:unhook(client_publish, {?MODULE, rewrite_publish}).
|
emqttd_broker:unhook('client.publish', {?MODULE, rewrite_publish}).
|
||||||
|
|
||||||
%%%=============================================================================
|
%%%=============================================================================
|
||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
|
@ -116,7 +116,6 @@ match_rule(Topic, []) ->
|
||||||
match_rule(Topic, [{rewrite, MP, Dest} | Rules]) ->
|
match_rule(Topic, [{rewrite, MP, Dest} | Rules]) ->
|
||||||
case re:run(Topic, MP, [{capture, all_but_first, list}]) of
|
case re:run(Topic, MP, [{capture, all_but_first, list}]) of
|
||||||
{match, Captured} ->
|
{match, Captured} ->
|
||||||
%%TODO: stupid??? how to replace $1, $2?
|
|
||||||
Vars = lists:zip(["\\$" ++ integer_to_list(I) || I <- lists:seq(1, length(Captured))], Captured),
|
Vars = lists:zip(["\\$" ++ integer_to_list(I) || I <- lists:seq(1, length(Captured))], Captured),
|
||||||
iolist_to_binary(lists:foldl(
|
iolist_to_binary(lists:foldl(
|
||||||
fun({Var, Val}, Acc) ->
|
fun({Var, Val}, Acc) ->
|
||||||
|
|
|
@ -207,7 +207,7 @@ handle(?SUBSCRIBE_PACKET(PacketId, TopicTable), State = #proto_state{client_id =
|
||||||
lager:error("SUBSCRIBE from '~s' Denied: ~p", [ClientId, TopicTable]),
|
lager:error("SUBSCRIBE from '~s' Denied: ~p", [ClientId, TopicTable]),
|
||||||
{ok, State};
|
{ok, State};
|
||||||
false ->
|
false ->
|
||||||
TopicTable1 = emqttd_broker:foldl_hooks(client_subscribe, [], TopicTable),
|
TopicTable1 = emqttd_broker:foldl_hooks('client.subscribe', [ClientId], TopicTable),
|
||||||
%%TODO: GrantedQos should be renamed.
|
%%TODO: GrantedQos should be renamed.
|
||||||
{ok, GrantedQos} = emqttd_session:subscribe(Session, TopicTable1),
|
{ok, GrantedQos} = emqttd_session:subscribe(Session, TopicTable1),
|
||||||
send(?SUBACK_PACKET(PacketId, GrantedQos), State)
|
send(?SUBACK_PACKET(PacketId, GrantedQos), State)
|
||||||
|
@ -221,8 +221,9 @@ handle({subscribe, TopicTable}, State = #proto_state{session = Session}) ->
|
||||||
handle(?UNSUBSCRIBE_PACKET(PacketId, []), State) ->
|
handle(?UNSUBSCRIBE_PACKET(PacketId, []), State) ->
|
||||||
send(?UNSUBACK_PACKET(PacketId), State);
|
send(?UNSUBACK_PACKET(PacketId), State);
|
||||||
|
|
||||||
handle(?UNSUBSCRIBE_PACKET(PacketId, Topics), State = #proto_state{session = Session}) ->
|
handle(?UNSUBSCRIBE_PACKET(PacketId, Topics), State = #proto_state{client_id = ClientId,
|
||||||
Topics1 = emqttd_broker:foldl_hooks(client_unsubscribe, [], Topics),
|
session = Session}) ->
|
||||||
|
Topics1 = emqttd_broker:foldl_hooks('client.unsubscribe', [ClientId], Topics),
|
||||||
ok = emqttd_session:unsubscribe(Session, Topics1),
|
ok = emqttd_session:unsubscribe(Session, Topics1),
|
||||||
send(?UNSUBACK_PACKET(PacketId), State);
|
send(?UNSUBACK_PACKET(PacketId), State);
|
||||||
|
|
||||||
|
|
|
@ -157,19 +157,18 @@ cast(Msg) ->
|
||||||
%% @end
|
%% @end
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
-spec publish(Msg :: mqtt_message()) -> ok.
|
-spec publish(Msg :: mqtt_message()) -> ok.
|
||||||
publish(#mqtt_message{topic=Topic, from = From} = Msg) ->
|
publish(#mqtt_message{from = From} = Msg) ->
|
||||||
trace(publish, From, Msg),
|
trace(publish, From, Msg),
|
||||||
|
|
||||||
%%TODO:call hooks here...
|
Msg1 = #mqtt_message{topic = Topic} = emqttd_broker:foldl_hooks('client.publish', [], Msg),
|
||||||
%%Msg1 = emqttd_broker:foldl_hooks(client_publish, [], Msg),
|
|
||||||
|
|
||||||
%% Retain message first. Don't create retained topic.
|
%% Retain message first. Don't create retained topic.
|
||||||
case emqttd_msg_store:retain(Msg) of
|
case emqttd_retained:retain(Msg1) of
|
||||||
ok ->
|
ok ->
|
||||||
%TODO: why unset 'retain' flag?
|
%TODO: why unset 'retain' flag?
|
||||||
publish(Topic, emqttd_message:unset_flag(Msg));
|
publish(Topic, emqttd_message:unset_flag(Msg1));
|
||||||
ignore ->
|
ignore ->
|
||||||
publish(Topic, Msg)
|
publish(Topic, Msg1)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
publish(<<"$Q/", _/binary>> = Queue, #mqtt_message{qos = Qos} = Msg) ->
|
publish(<<"$Q/", _/binary>> = Queue, #mqtt_message{qos = Qos} = Msg) ->
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
%%%
|
%%%
|
||||||
%%% @end
|
%%% @end
|
||||||
%%%-----------------------------------------------------------------------------
|
%%%-----------------------------------------------------------------------------
|
||||||
-module(emqttd_msg_store).
|
-module(emqttd_retained).
|
||||||
|
|
||||||
-author("Feng Lee <feng@emqtt.io>").
|
-author("Feng Lee <feng@emqtt.io>").
|
||||||
|
|
||||||
|
@ -37,21 +37,23 @@
|
||||||
-copy_mnesia({mnesia, [copy]}).
|
-copy_mnesia({mnesia, [copy]}).
|
||||||
|
|
||||||
%% API Function Exports
|
%% API Function Exports
|
||||||
-export([retain/1, redeliver/2]).
|
-export([retain/1, dispatch/2]).
|
||||||
|
|
||||||
|
-record(mqtt_retained, {topic, message}).
|
||||||
|
|
||||||
%%%=============================================================================
|
%%%=============================================================================
|
||||||
%%% Mnesia callbacks
|
%%% Mnesia callbacks
|
||||||
%%%=============================================================================
|
%%%=============================================================================
|
||||||
|
|
||||||
mnesia(boot) ->
|
mnesia(boot) ->
|
||||||
ok = emqttd_mnesia:create_table(message, [
|
ok = emqttd_mnesia:create_table(retained, [
|
||||||
{type, ordered_set},
|
{type, ordered_set},
|
||||||
{ram_copies, [node()]},
|
{ram_copies, [node()]},
|
||||||
{record_name, mqtt_message},
|
{record_name, mqtt_retained},
|
||||||
{attributes, record_info(fields, mqtt_message)}]);
|
{attributes, record_info(fields, mqtt_retained)}]);
|
||||||
|
|
||||||
mnesia(copy) ->
|
mnesia(copy) ->
|
||||||
ok = emqttd_mnesia:copy_table(message).
|
ok = emqttd_mnesia:copy_table(retained).
|
||||||
|
|
||||||
%%%=============================================================================
|
%%%=============================================================================
|
||||||
%%% API
|
%%% API
|
||||||
|
@ -66,7 +68,7 @@ retain(#mqtt_message{retain = false}) -> ignore;
|
||||||
|
|
||||||
%% RETAIN flag set to 1 and payload containing zero bytes
|
%% RETAIN flag set to 1 and payload containing zero bytes
|
||||||
retain(#mqtt_message{retain = true, topic = Topic, payload = <<>>}) ->
|
retain(#mqtt_message{retain = true, topic = Topic, payload = <<>>}) ->
|
||||||
mnesia:async_dirty(fun mnesia:delete/1, [{message, Topic}]);
|
mnesia:async_dirty(fun mnesia:delete/1, [{retained, Topic}]);
|
||||||
|
|
||||||
retain(Msg = #mqtt_message{topic = Topic,
|
retain(Msg = #mqtt_message{topic = Topic,
|
||||||
retain = true,
|
retain = true,
|
||||||
|
@ -74,10 +76,10 @@ retain(Msg = #mqtt_message{topic = Topic,
|
||||||
TabSize = mnesia:table_info(message, size),
|
TabSize = mnesia:table_info(message, size),
|
||||||
case {TabSize < limit(table), size(Payload) < limit(payload)} of
|
case {TabSize < limit(table), size(Payload) < limit(payload)} of
|
||||||
{true, true} ->
|
{true, true} ->
|
||||||
|
Retained = #mqtt_retained{topic = Topic, message = Msg},
|
||||||
lager:debug("Retained ~s", [emqttd_message:format(Msg)]),
|
lager:debug("Retained ~s", [emqttd_message:format(Msg)]),
|
||||||
mnesia:async_dirty(fun mnesia:write/3, [message, Msg, write]),
|
mnesia:async_dirty(fun mnesia:write/3, [retained, Retained, write]),
|
||||||
emqttd_metrics:set('messages/retained/count',
|
emqttd_metrics:set('messages/retained', mnesia:table_info(retained, size));
|
||||||
mnesia:table_info(message, size));
|
|
||||||
{false, _}->
|
{false, _}->
|
||||||
lager:error("Dropped retained message(topic=~s) for table is full!", [Topic]);
|
lager:error("Dropped retained message(topic=~s) for table is full!", [Topic]);
|
||||||
{_, false}->
|
{_, false}->
|
||||||
|
@ -99,31 +101,25 @@ env() ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%%%-----------------------------------------------------------------------------
|
%%%-----------------------------------------------------------------------------
|
||||||
%% @doc Redeliver retained messages to subscribed client
|
%% @doc Deliver retained messages to subscribed client
|
||||||
%% @end
|
%% @end
|
||||||
%%%-----------------------------------------------------------------------------
|
%%%-----------------------------------------------------------------------------
|
||||||
-spec redeliver(Topic, CPid) -> any() when
|
-spec dispatch(Topic, CPid) -> any() when
|
||||||
Topic :: binary(),
|
Topic :: binary(),
|
||||||
CPid :: pid().
|
CPid :: pid().
|
||||||
redeliver(Topic, CPid) when is_binary(Topic) andalso is_pid(CPid) ->
|
dispatch(Topic, CPid) when is_binary(Topic) andalso is_pid(CPid) ->
|
||||||
|
Msgs =
|
||||||
case emqttd_topic:wildcard(Topic) of
|
case emqttd_topic:wildcard(Topic) of
|
||||||
false ->
|
false ->
|
||||||
dispatch(CPid, mnesia:dirty_read(message, Topic));
|
[Msg || #mqtt_retained{message = Msg} <- mnesia:dirty_read(retained, Topic)];
|
||||||
true ->
|
true ->
|
||||||
Fun = fun(Msg = #mqtt_message{topic = Name}, Acc) ->
|
Fun = fun(#mqtt_retained{topic = Name, message = Msg}, Acc) ->
|
||||||
case emqttd_topic:match(Name, Topic) of
|
case emqttd_topic:match(Name, Topic) of
|
||||||
true -> [Msg|Acc];
|
true -> [Msg|Acc];
|
||||||
false -> Acc
|
false -> Acc
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
Msgs = mnesia:async_dirty(fun mnesia:foldl/3, [Fun, [], message]),
|
mnesia:async_dirty(fun mnesia:foldl/3, [Fun, [], retained])
|
||||||
dispatch(CPid, lists:reverse(Msgs))
|
end,
|
||||||
end.
|
[CPid ! {dispatch, Msg} || Msg <- Msgs].
|
||||||
|
|
||||||
dispatch(_CPid, []) ->
|
|
||||||
ignore;
|
|
||||||
dispatch(CPid, Msgs) when is_list(Msgs) ->
|
|
||||||
[CPid ! {dispatch, Msg} || Msg <- Msgs];
|
|
||||||
dispatch(CPid, Msg) when is_record(Msg, mqtt_message) ->
|
|
||||||
CPid ! {dispatch, Msg}.
|
|
||||||
|
|
||||||
|
|
|
@ -78,8 +78,8 @@
|
||||||
%% Client Pid linked with session
|
%% Client Pid linked with session
|
||||||
client_pid :: pid(),
|
client_pid :: pid(),
|
||||||
|
|
||||||
%% Last message id of the session
|
%% Last packet id of the session
|
||||||
message_id = 1,
|
packet_id = 1,
|
||||||
|
|
||||||
%% Client’s subscriptions.
|
%% Client’s subscriptions.
|
||||||
subscriptions :: list(),
|
subscriptions :: list(),
|
||||||
|
@ -182,21 +182,21 @@ publish(Session, Msg = #mqtt_message{qos = ?QOS_2}) ->
|
||||||
%% @doc PubAck message
|
%% @doc PubAck message
|
||||||
%% @end
|
%% @end
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
-spec puback(pid(), mqtt_msgid()) -> ok.
|
-spec puback(pid(), mqtt_packet_id()) -> ok.
|
||||||
puback(Session, MsgId) ->
|
puback(Session, PktId) ->
|
||||||
gen_server:cast(Session, {puback, MsgId}).
|
gen_server:cast(Session, {puback, PktId}).
|
||||||
|
|
||||||
-spec pubrec(pid(), mqtt_msgid()) -> ok.
|
-spec pubrec(pid(), mqtt_packet_id()) -> ok.
|
||||||
pubrec(Session, MsgId) ->
|
pubrec(Session, PktId) ->
|
||||||
gen_server:cast(Session, {pubrec, MsgId}).
|
gen_server:cast(Session, {pubrec, PktId}).
|
||||||
|
|
||||||
-spec pubrel(pid(), mqtt_msgid()) -> ok.
|
-spec pubrel(pid(), mqtt_packet_id()) -> ok.
|
||||||
pubrel(Session, MsgId) ->
|
pubrel(Session, PktId) ->
|
||||||
gen_server:cast(Session, {pubrel, MsgId}).
|
gen_server:cast(Session, {pubrel, PktId}).
|
||||||
|
|
||||||
-spec pubcomp(pid(), mqtt_msgid()) -> ok.
|
-spec pubcomp(pid(), mqtt_packet_id()) -> ok.
|
||||||
pubcomp(Session, MsgId) ->
|
pubcomp(Session, PktId) ->
|
||||||
gen_server:cast(Session, {pubcomp, MsgId}).
|
gen_server:cast(Session, {pubcomp, PktId}).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% @doc Unsubscribe Topics
|
%% @doc Unsubscribe Topics
|
||||||
|
@ -258,7 +258,7 @@ handle_call({subscribe, Topics}, _From, Session = #session{client_id = ClientId,
|
||||||
%% <MQTT V3.1.1>: 3.8.4
|
%% <MQTT V3.1.1>: 3.8.4
|
||||||
%% Where the Topic Filter is not identical to any existing Subscription’s filter,
|
%% Where the Topic Filter is not identical to any existing Subscription’s filter,
|
||||||
%% a new Subscription is created and all matching retained messages are sent.
|
%% a new Subscription is created and all matching retained messages are sent.
|
||||||
emqttd_msg_store:redeliver(Topic, self()),
|
emqttd_retained:dispatch(Topic, self()),
|
||||||
[{Topic, Qos} | Acc]
|
[{Topic, Qos} | Acc]
|
||||||
end
|
end
|
||||||
end, Subscriptions, Topics),
|
end, Subscriptions, Topics),
|
||||||
|
@ -284,14 +284,14 @@ handle_call({unsubscribe, Topics}, _From, Session = #session{client_id = ClientI
|
||||||
|
|
||||||
{reply, ok, Session#session{subscriptions = Subscriptions1}};
|
{reply, ok, Session#session{subscriptions = Subscriptions1}};
|
||||||
|
|
||||||
handle_call({publish, Msg = #mqtt_message{qos = ?QOS_2, msgid = MsgId}}, _From,
|
handle_call({publish, Msg = #mqtt_message{qos = ?QOS_2, pktid = PktId}}, _From,
|
||||||
Session = #session{client_id = ClientId,
|
Session = #session{client_id = ClientId,
|
||||||
awaiting_rel = AwaitingRel,
|
awaiting_rel = AwaitingRel,
|
||||||
await_rel_timeout = Timeout}) ->
|
await_rel_timeout = Timeout}) ->
|
||||||
case check_awaiting_rel(Session) of
|
case check_awaiting_rel(Session) of
|
||||||
true ->
|
true ->
|
||||||
TRef = timer(Timeout, {timeout, awaiting_rel, MsgId}),
|
TRef = timer(Timeout, {timeout, awaiting_rel, PktId}),
|
||||||
AwaitingRel1 = maps:put(MsgId, {Msg, TRef}, AwaitingRel),
|
AwaitingRel1 = maps:put(PktId, {Msg, TRef}, AwaitingRel),
|
||||||
{reply, ok, Session#session{awaiting_rel = AwaitingRel1}};
|
{reply, ok, Session#session{awaiting_rel = AwaitingRel1}};
|
||||||
false ->
|
false ->
|
||||||
lager:critical([{client, ClientId}], "Session ~s dropped Qos2 message "
|
lager:critical([{client, ClientId}], "Session ~s dropped Qos2 message "
|
||||||
|
@ -326,7 +326,7 @@ handle_cast({resume, ClientId, ClientPid}, Session) ->
|
||||||
true = link(ClientPid),
|
true = link(ClientPid),
|
||||||
|
|
||||||
%% Redeliver PUBREL
|
%% Redeliver PUBREL
|
||||||
[ClientPid ! {redeliver, {?PUBREL, MsgId}} || MsgId <- maps:keys(AwaitingComp)],
|
[ClientPid ! {redeliver, {?PUBREL, PktId}} || PktId <- maps:keys(AwaitingComp)],
|
||||||
|
|
||||||
%% Clear awaiting_ack timers
|
%% Clear awaiting_ack timers
|
||||||
[cancel_timer(TRef) || {_, TRef} <- maps:values(AwaitingAck)],
|
[cancel_timer(TRef) || {_, TRef} <- maps:values(AwaitingAck)],
|
||||||
|
@ -349,54 +349,54 @@ handle_cast({resume, ClientId, ClientPid}, Session) ->
|
||||||
{noreply, dequeue(Session2), hibernate};
|
{noreply, dequeue(Session2), hibernate};
|
||||||
|
|
||||||
%% PUBRAC
|
%% PUBRAC
|
||||||
handle_cast({puback, MsgId}, Session = #session{client_id = ClientId, awaiting_ack = Awaiting}) ->
|
handle_cast({puback, PktId}, Session = #session{client_id = ClientId, awaiting_ack = Awaiting}) ->
|
||||||
case maps:find(MsgId, Awaiting) of
|
case maps:find(PktId, Awaiting) of
|
||||||
{ok, {_, TRef}} ->
|
{ok, {_, TRef}} ->
|
||||||
cancel_timer(TRef),
|
cancel_timer(TRef),
|
||||||
Session1 = acked(MsgId, Session),
|
Session1 = acked(PktId, Session),
|
||||||
{noreply, dequeue(Session1)};
|
{noreply, dequeue(Session1)};
|
||||||
error ->
|
error ->
|
||||||
lager:error("Session ~s cannot find PUBACK '~p'!", [ClientId, MsgId]),
|
lager:error("Session ~s cannot find PUBACK '~p'!", [ClientId, PktId]),
|
||||||
{noreply, Session}
|
{noreply, Session}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% PUBREC
|
%% PUBREC
|
||||||
handle_cast({pubrec, MsgId}, Session = #session{client_id = ClientId,
|
handle_cast({pubrec, PktId}, Session = #session{client_id = ClientId,
|
||||||
awaiting_ack = AwaitingAck,
|
awaiting_ack = AwaitingAck,
|
||||||
awaiting_comp = AwaitingComp,
|
awaiting_comp = AwaitingComp,
|
||||||
await_rel_timeout = Timeout}) ->
|
await_rel_timeout = Timeout}) ->
|
||||||
case maps:find(MsgId, AwaitingAck) of
|
case maps:find(PktId, AwaitingAck) of
|
||||||
{ok, {_, TRef}} ->
|
{ok, {_, TRef}} ->
|
||||||
cancel_timer(TRef),
|
cancel_timer(TRef),
|
||||||
TRef1 = timer(Timeout, {timeout, awaiting_comp, MsgId}),
|
TRef1 = timer(Timeout, {timeout, awaiting_comp, PktId}),
|
||||||
Session1 = acked(MsgId, Session#session{awaiting_comp = maps:put(MsgId, TRef1, AwaitingComp)}),
|
Session1 = acked(PktId, Session#session{awaiting_comp = maps:put(PktId, TRef1, AwaitingComp)}),
|
||||||
{noreply, dequeue(Session1)};
|
{noreply, dequeue(Session1)};
|
||||||
error ->
|
error ->
|
||||||
lager:error("Session ~s cannot find PUBREC '~p'!", [ClientId, MsgId]),
|
lager:error("Session ~s cannot find PUBREC '~p'!", [ClientId, PktId]),
|
||||||
{noreply, Session}
|
{noreply, Session}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% PUBREL
|
%% PUBREL
|
||||||
handle_cast({pubrel, MsgId}, Session = #session{client_id = ClientId,
|
handle_cast({pubrel, PktId}, Session = #session{client_id = ClientId,
|
||||||
awaiting_rel = AwaitingRel}) ->
|
awaiting_rel = AwaitingRel}) ->
|
||||||
case maps:find(MsgId, AwaitingRel) of
|
case maps:find(PktId, AwaitingRel) of
|
||||||
{ok, {Msg, TRef}} ->
|
{ok, {Msg, TRef}} ->
|
||||||
cancel_timer(TRef),
|
cancel_timer(TRef),
|
||||||
emqttd_pubsub:publish(Msg),
|
emqttd_pubsub:publish(Msg),
|
||||||
{noreply, Session#session{awaiting_rel = maps:remove(MsgId, AwaitingRel)}};
|
{noreply, Session#session{awaiting_rel = maps:remove(PktId, AwaitingRel)}};
|
||||||
error ->
|
error ->
|
||||||
lager:error("Session ~s cannot find PUBREL: msgid=~p!", [ClientId, MsgId]),
|
lager:error("Session ~s cannot find PUBREL: pktid=~p!", [ClientId, PktId]),
|
||||||
{noreply, Session}
|
{noreply, Session}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% PUBCOMP
|
%% PUBCOMP
|
||||||
handle_cast({pubcomp, MsgId}, Session = #session{client_id = ClientId, awaiting_comp = AwaitingComp}) ->
|
handle_cast({pubcomp, PktId}, Session = #session{client_id = ClientId, awaiting_comp = AwaitingComp}) ->
|
||||||
case maps:find(MsgId, AwaitingComp) of
|
case maps:find(PktId, AwaitingComp) of
|
||||||
{ok, TRef} ->
|
{ok, TRef} ->
|
||||||
cancel_timer(TRef),
|
cancel_timer(TRef),
|
||||||
{noreply, Session#session{awaiting_comp = maps:remove(MsgId, AwaitingComp)}};
|
{noreply, Session#session{awaiting_comp = maps:remove(PktId, AwaitingComp)}};
|
||||||
error ->
|
error ->
|
||||||
lager:error("Session ~s cannot find PUBCOMP: MsgId=~p", [ClientId, MsgId]),
|
lager:error("Session ~s cannot find PUBCOMP: PktId=~p", [ClientId, PktId]),
|
||||||
{noreply, Session}
|
{noreply, Session}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
@ -428,51 +428,51 @@ handle_info({dispatch, Msg = #mqtt_message{qos = QoS}},
|
||||||
{noreply, Session#session{message_queue = emqttd_mqueue:in(Msg, MsgQ)}}
|
{noreply, Session#session{message_queue = emqttd_mqueue:in(Msg, MsgQ)}}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_info({timeout, awaiting_ack, MsgId}, Session = #session{client_pid = undefined,
|
handle_info({timeout, awaiting_ack, PktId}, Session = #session{client_pid = undefined,
|
||||||
awaiting_ack = AwaitingAck}) ->
|
awaiting_ack = AwaitingAck}) ->
|
||||||
%% just remove awaiting
|
%% just remove awaiting
|
||||||
{noreply, Session#session{awaiting_ack = maps:remove(MsgId, AwaitingAck)}};
|
{noreply, Session#session{awaiting_ack = maps:remove(PktId, AwaitingAck)}};
|
||||||
|
|
||||||
handle_info({timeout, awaiting_ack, MsgId}, Session = #session{client_id = ClientId,
|
handle_info({timeout, awaiting_ack, PktId}, Session = #session{client_id = ClientId,
|
||||||
inflight_queue = InflightQ,
|
inflight_queue = InflightQ,
|
||||||
awaiting_ack = AwaitingAck}) ->
|
awaiting_ack = AwaitingAck}) ->
|
||||||
case maps:find(MsgId, AwaitingAck) of
|
case maps:find(PktId, AwaitingAck) of
|
||||||
{ok, {{0, _Timeout}, _TRef}} ->
|
{ok, {{0, _Timeout}, _TRef}} ->
|
||||||
Session1 = Session#session{inflight_queue = lists:keydelete(MsgId, 1, InflightQ),
|
Session1 = Session#session{inflight_queue = lists:keydelete(PktId, 1, InflightQ),
|
||||||
awaiting_ack = maps:remove(MsgId, AwaitingAck)},
|
awaiting_ack = maps:remove(PktId, AwaitingAck)},
|
||||||
{noreply, dequeue(Session1)};
|
{noreply, dequeue(Session1)};
|
||||||
{ok, {{Retries, Timeout}, _TRef}} ->
|
{ok, {{Retries, Timeout}, _TRef}} ->
|
||||||
TRef = timer(Timeout, {timeout, awaiting_ack, MsgId}),
|
TRef = timer(Timeout, {timeout, awaiting_ack, PktId}),
|
||||||
AwaitingAck1 = maps:put(MsgId, {{Retries-1, Timeout*2}, TRef}, AwaitingAck),
|
AwaitingAck1 = maps:put(PktId, {{Retries-1, Timeout*2}, TRef}, AwaitingAck),
|
||||||
{noreply, Session#session{awaiting_ack = AwaitingAck1}};
|
{noreply, Session#session{awaiting_ack = AwaitingAck1}};
|
||||||
error ->
|
error ->
|
||||||
lager:error([{client, ClientId}], "Session ~s "
|
lager:error([{client, ClientId}], "Session ~s "
|
||||||
"cannot find Awaiting Ack:~p", [ClientId, MsgId]),
|
"cannot find Awaiting Ack:~p", [ClientId, PktId]),
|
||||||
{noreply, Session}
|
{noreply, Session}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_info({timeout, awaiting_rel, MsgId}, Session = #session{client_id = ClientId,
|
handle_info({timeout, awaiting_rel, PktId}, Session = #session{client_id = ClientId,
|
||||||
awaiting_rel = AwaitingRel}) ->
|
awaiting_rel = AwaitingRel}) ->
|
||||||
case maps:find(MsgId, AwaitingRel) of
|
case maps:find(PktId, AwaitingRel) of
|
||||||
{ok, {Msg, _TRef}} ->
|
{ok, {Msg, _TRef}} ->
|
||||||
lager:error([{client, ClientId}], "Session ~s AwaitingRel Timout!~n"
|
lager:error([{client, ClientId}], "Session ~s AwaitingRel Timout!~n"
|
||||||
"Drop Message:~p", [ClientId, Msg]),
|
"Drop Message:~p", [ClientId, Msg]),
|
||||||
{noreply, Session#session{awaiting_rel = maps:remove(MsgId, AwaitingRel)}};
|
{noreply, Session#session{awaiting_rel = maps:remove(PktId, AwaitingRel)}};
|
||||||
error ->
|
error ->
|
||||||
lager:error([{client, ClientId}], "Session ~s Cannot find AwaitingRel: MsgId=~p", [ClientId, MsgId]),
|
lager:error([{client, ClientId}], "Session ~s Cannot find AwaitingRel: PktId=~p", [ClientId, PktId]),
|
||||||
{noreply, Session}
|
{noreply, Session}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_info({timeout, awaiting_comp, MsgId}, Session = #session{client_id = ClientId,
|
handle_info({timeout, awaiting_comp, PktId}, Session = #session{client_id = ClientId,
|
||||||
awaiting_comp = Awaiting}) ->
|
awaiting_comp = Awaiting}) ->
|
||||||
case maps:find(MsgId, Awaiting) of
|
case maps:find(PktId, Awaiting) of
|
||||||
{ok, _TRef} ->
|
{ok, _TRef} ->
|
||||||
lager:error([{client, ClientId}], "Session ~s "
|
lager:error([{client, ClientId}], "Session ~s "
|
||||||
"Awaiting PUBCOMP Timout: MsgId=~p!", [ClientId, MsgId]),
|
"Awaiting PUBCOMP Timout: PktId=~p!", [ClientId, PktId]),
|
||||||
{noreply, Session#session{awaiting_comp = maps:remove(MsgId, Awaiting)}};
|
{noreply, Session#session{awaiting_comp = maps:remove(PktId, Awaiting)}};
|
||||||
error ->
|
error ->
|
||||||
lager:error([{client, ClientId}], "Session ~s "
|
lager:error([{client, ClientId}], "Session ~s "
|
||||||
"Cannot find Awaiting PUBCOMP: MsgId=~p", [ClientId, MsgId]),
|
"Cannot find Awaiting PUBCOMP: PktId=~p", [ClientId, PktId]),
|
||||||
{noreply, Session}
|
{noreply, Session}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
@ -566,13 +566,13 @@ dequeue2(Session = #session{message_queue = Q}) ->
|
||||||
deliver(Msg = #mqtt_message{qos = ?QOS_0}, Session = #session{client_pid = ClientPid}) ->
|
deliver(Msg = #mqtt_message{qos = ?QOS_0}, Session = #session{client_pid = ClientPid}) ->
|
||||||
ClientPid ! {deliver, Msg}, Session;
|
ClientPid ! {deliver, Msg}, Session;
|
||||||
|
|
||||||
deliver(Msg = #mqtt_message{qos = QoS}, Session = #session{message_id = MsgId,
|
deliver(Msg = #mqtt_message{qos = QoS}, Session = #session{packet_id = PktId,
|
||||||
client_pid = ClientPid,
|
client_pid = ClientPid,
|
||||||
inflight_queue = InflightQ})
|
inflight_queue = InflightQ})
|
||||||
when QoS =:= ?QOS_1 orelse QoS =:= ?QOS_2 ->
|
when QoS =:= ?QOS_1 orelse QoS =:= ?QOS_2 ->
|
||||||
Msg1 = Msg#mqtt_message{msgid = MsgId, dup = false},
|
Msg1 = Msg#mqtt_message{pktid = PktId, dup = false},
|
||||||
ClientPid ! {deliver, Msg1},
|
ClientPid ! {deliver, Msg1},
|
||||||
await(Msg1, next_msgid(Session#session{inflight_queue = [{MsgId, Msg1}|InflightQ]})).
|
await(Msg1, next_packet_id(Session#session{inflight_queue = [{PktId, Msg1}|InflightQ]})).
|
||||||
|
|
||||||
redeliver(Msg = #mqtt_message{qos = ?QOS_0}, Session) ->
|
redeliver(Msg = #mqtt_message{qos = ?QOS_0}, Session) ->
|
||||||
deliver(Msg, Session);
|
deliver(Msg, Session);
|
||||||
|
@ -585,23 +585,23 @@ redeliver(Msg = #mqtt_message{qos = QoS}, Session = #session{client_pid = Client
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Awaiting ack for qos1, qos2 message
|
%% Awaiting ack for qos1, qos2 message
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
await(#mqtt_message{msgid = MsgId}, Session = #session{awaiting_ack = Awaiting,
|
await(#mqtt_message{pktid = PktId}, Session = #session{awaiting_ack = Awaiting,
|
||||||
unack_retries = Retries,
|
unack_retries = Retries,
|
||||||
unack_timeout = Timeout}) ->
|
unack_timeout = Timeout}) ->
|
||||||
TRef = timer(Timeout, {timeout, awaiting_ack, MsgId}),
|
TRef = timer(Timeout, {timeout, awaiting_ack, PktId}),
|
||||||
Awaiting1 = maps:put(MsgId, {{Retries, Timeout}, TRef}, Awaiting),
|
Awaiting1 = maps:put(PktId, {{Retries, Timeout}, TRef}, Awaiting),
|
||||||
Session#session{awaiting_ack = Awaiting1}.
|
Session#session{awaiting_ack = Awaiting1}.
|
||||||
|
|
||||||
acked(MsgId, Session = #session{inflight_queue = InflightQ,
|
acked(PktId, Session = #session{inflight_queue = InflightQ,
|
||||||
awaiting_ack = Awaiting}) ->
|
awaiting_ack = Awaiting}) ->
|
||||||
Session#session{inflight_queue = lists:keydelete(MsgId, 1, InflightQ),
|
Session#session{inflight_queue = lists:keydelete(PktId, 1, InflightQ),
|
||||||
awaiting_ack = maps:remove(MsgId, Awaiting)}.
|
awaiting_ack = maps:remove(PktId, Awaiting)}.
|
||||||
|
|
||||||
next_msgid(Session = #session{message_id = 16#ffff}) ->
|
next_packet_id(Session = #session{packet_id = 16#ffff}) ->
|
||||||
Session#session{message_id = 1};
|
Session#session{packet_id = 1};
|
||||||
|
|
||||||
next_msgid(Session = #session{message_id = MsgId}) ->
|
next_packet_id(Session = #session{packet_id = Id}) ->
|
||||||
Session#session{message_id = MsgId + 1}.
|
Session#session{packet_id = Id + 1}.
|
||||||
|
|
||||||
timer(Timeout, TimeoutMsg) ->
|
timer(Timeout, TimeoutMsg) ->
|
||||||
erlang:send_after(Timeout * 1000, self(), TimeoutMsg).
|
erlang:send_after(Timeout * 1000, self(), TimeoutMsg).
|
||||||
|
|
|
@ -175,9 +175,9 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
%%%=============================================================================
|
%%%=============================================================================
|
||||||
publish(Stat, Val) ->
|
publish(Stat, Val) ->
|
||||||
emqttd_pubsub:publish(#mqtt_message{from = stats,
|
Msg = emqttd_message:make(stats, stats_topic(Stat),
|
||||||
topic = stats_topic(Stat),
|
emqttd_util:integer_to_binary(Val)),
|
||||||
payload = emqttd_util:integer_to_binary(Val)}).
|
emqttd_pubsub:publish(Msg).
|
||||||
|
|
||||||
stats_topic(Stat) ->
|
stats_topic(Stat) ->
|
||||||
emqttd_topic:systop(list_to_binary(lists:concat(['stats/', Stat]))).
|
emqttd_topic:systop(list_to_binary(lists:concat(['stats/', Stat]))).
|
||||||
|
|
Loading…
Reference in New Issue