|
|
@@ -82,7 +82,6 @@
|
|
|
sockname :: {inet:ip_address(), inet:port()},
|
|
|
peername :: {inet:ip_address(), inet:port()},
|
|
|
channel :: maybe(emqx_channel:channel()),
|
|
|
- registry :: emqx_sn_registry:registry(),
|
|
|
clientid :: maybe(binary()),
|
|
|
username :: maybe(binary()),
|
|
|
password :: maybe(binary()),
|
|
|
@@ -147,7 +146,6 @@ kick(GwPid) ->
|
|
|
|
|
|
init([{_, SockPid, Sock}, Peername, Options]) ->
|
|
|
GwId = proplists:get_value(gateway_id, Options),
|
|
|
- Registry = proplists:get_value(registry, Options),
|
|
|
Username = proplists:get_value(username, Options, undefined),
|
|
|
Password = proplists:get_value(password, Options, undefined),
|
|
|
EnableQos3 = proplists:get_value(enable_qos3, Options, false),
|
|
|
@@ -165,7 +163,6 @@ init([{_, SockPid, Sock}, Peername, Options]) ->
|
|
|
sockname = Sockname,
|
|
|
peername = Peername,
|
|
|
channel = Channel,
|
|
|
- registry = Registry,
|
|
|
asleep_timer = emqx_sn_asleep_timer:init(),
|
|
|
enable_stats = EnableStats,
|
|
|
enable_qos3 = EnableQos3,
|
|
|
@@ -205,9 +202,9 @@ idle(cast, {incoming, ?SN_PUBLISH_MSG(_Flag, _TopicId, _MsgId, _Data)}, State =
|
|
|
idle(cast, {incoming, ?SN_PUBLISH_MSG(#mqtt_sn_flags{qos = ?QOS_NEG1,
|
|
|
topic_id_type = TopicIdType
|
|
|
}, TopicId, _MsgId, Data)},
|
|
|
- State = #state{clientid = ClientId, registry = Registry}) ->
|
|
|
+ State = #state{clientid = ClientId}) ->
|
|
|
TopicName = case (TopicIdType =:= ?SN_SHORT_TOPIC) of
|
|
|
- false -> emqx_sn_registry:lookup_topic(Registry, ClientId, TopicId);
|
|
|
+ false -> emqx_sn_registry:lookup_topic(ClientId, TopicId);
|
|
|
true -> <<TopicId:16>>
|
|
|
end,
|
|
|
_ = case TopicName =/= undefined of
|
|
|
@@ -292,9 +289,9 @@ wait_for_will_msg(EventType, EventContent, State) ->
|
|
|
handle_event(EventType, EventContent, wait_for_will_msg, State).
|
|
|
|
|
|
connected(cast, {incoming, ?SN_REGISTER_MSG(_TopicId, MsgId, TopicName)},
|
|
|
- State = #state{clientid = ClientId, registry = Registry}) ->
|
|
|
+ State = #state{clientid = ClientId}) ->
|
|
|
State0 =
|
|
|
- case emqx_sn_registry:register_topic(Registry, ClientId, TopicName) of
|
|
|
+ case emqx_sn_registry:register_topic(ClientId, TopicName) of
|
|
|
TopicId when is_integer(TopicId) ->
|
|
|
?LOG(debug, "register ClientId=~p, TopicName=~p, TopicId=~p", [ClientId, TopicName, TopicId]),
|
|
|
send_message(?SN_REGACK_MSG(TopicId, MsgId, ?SN_RC_ACCEPTED), State);
|
|
|
@@ -580,14 +577,13 @@ handle_event(EventType, EventContent, StateName, State) ->
|
|
|
[StateName, {EventType, EventContent}]),
|
|
|
{keep_state, State}.
|
|
|
|
|
|
-terminate(Reason, _StateName, #state{channel = Channel,
|
|
|
- registry = Registry}) ->
|
|
|
+terminate(Reason, _StateName, #state{channel = Channel}) ->
|
|
|
ClientId = emqx_channel:info(clientid, Channel),
|
|
|
case Reason of
|
|
|
{shutdown, takeovered} ->
|
|
|
ok;
|
|
|
_ ->
|
|
|
- emqx_sn_registry:unregister_topic(Registry, ClientId)
|
|
|
+ emqx_sn_registry:unregister_topic(ClientId)
|
|
|
end,
|
|
|
emqx_channel:terminate(Reason, Channel),
|
|
|
ok.
|
|
|
@@ -723,13 +719,12 @@ mqtt2sn(?PUBCOMP_PACKET(MsgId), _State) ->
|
|
|
mqtt2sn(?UNSUBACK_PACKET(MsgId), _State)->
|
|
|
?SN_UNSUBACK_MSG(MsgId);
|
|
|
|
|
|
-mqtt2sn(?PUBLISH_PACKET(QoS, Topic, PacketId, Payload), #state{registry = Registry,
|
|
|
- channel = Channel}) ->
|
|
|
+mqtt2sn(?PUBLISH_PACKET(QoS, Topic, PacketId, Payload), #state{channel = Channel}) ->
|
|
|
NewPacketId = if QoS =:= ?QOS_0 -> 0;
|
|
|
true -> PacketId
|
|
|
end,
|
|
|
ClientId = emqx_channel:info(clientid, Channel),
|
|
|
- {TopicIdType, TopicContent} = case emqx_sn_registry:lookup_topic_id(Registry, ClientId, Topic) of
|
|
|
+ {TopicIdType, TopicContent} = case emqx_sn_registry:lookup_topic_id(ClientId, Topic) of
|
|
|
{predef, PredefTopicId} ->
|
|
|
{?SN_PREDEFINED_TOPIC, PredefTopicId};
|
|
|
TopicId when is_integer(TopicId) ->
|
|
|
@@ -850,14 +845,13 @@ do_connect(ClientId, CleanStart, WillFlag, Duration, State) ->
|
|
|
|
|
|
do_2nd_connect(Flags, Duration, ClientId, State = #state{sockname = Sockname,
|
|
|
peername = Peername,
|
|
|
- registry = Registry,
|
|
|
channel = Channel}) ->
|
|
|
emqx_logger:set_metadata_clientid(ClientId),
|
|
|
#mqtt_sn_flags{will = Will, clean_start = CleanStart} = Flags,
|
|
|
NChannel = case CleanStart of
|
|
|
true ->
|
|
|
emqx_channel:terminate(normal, Channel),
|
|
|
- emqx_sn_registry:unregister_topic(Registry, ClientId),
|
|
|
+ emqx_sn_registry:unregister_topic(ClientId),
|
|
|
emqx_channel:init(#{socktype => udp,
|
|
|
sockname => Sockname,
|
|
|
peername => Peername,
|
|
|
@@ -870,9 +864,9 @@ do_2nd_connect(Flags, Duration, ClientId, State = #state{sockname = Sockname,
|
|
|
do_connect(ClientId, CleanStart, Will, Duration, NState).
|
|
|
|
|
|
handle_subscribe(?SN_NORMAL_TOPIC, TopicName, QoS, MsgId,
|
|
|
- State=#state{registry = Registry, channel = Channel}) ->
|
|
|
+ State=#state{channel = Channel}) ->
|
|
|
ClientId = emqx_channel:info(clientid, Channel),
|
|
|
- case emqx_sn_registry:register_topic(Registry, ClientId, TopicName) of
|
|
|
+ case emqx_sn_registry:register_topic(ClientId, TopicName) of
|
|
|
{error, too_large} ->
|
|
|
State0 = send_message(?SN_SUBACK_MSG(#mqtt_sn_flags{qos = QoS},
|
|
|
?SN_INVALID_TOPIC_ID,
|
|
|
@@ -886,9 +880,9 @@ handle_subscribe(?SN_NORMAL_TOPIC, TopicName, QoS, MsgId,
|
|
|
end;
|
|
|
|
|
|
handle_subscribe(?SN_PREDEFINED_TOPIC, TopicId, QoS, MsgId,
|
|
|
- State = #state{registry = Registry, channel = Channel}) ->
|
|
|
+ State = #state{channel = Channel}) ->
|
|
|
ClientId = emqx_channel:info(clientid, Channel),
|
|
|
- case emqx_sn_registry:lookup_topic(Registry, ClientId, TopicId) of
|
|
|
+ case emqx_sn_registry:lookup_topic(ClientId, TopicId) of
|
|
|
undefined ->
|
|
|
State0 = send_message(?SN_SUBACK_MSG(#mqtt_sn_flags{qos = QoS},
|
|
|
TopicId,
|
|
|
@@ -917,9 +911,9 @@ handle_unsubscribe(?SN_NORMAL_TOPIC, TopicId, MsgId, State) ->
|
|
|
proto_unsubscribe(TopicId, MsgId, State);
|
|
|
|
|
|
handle_unsubscribe(?SN_PREDEFINED_TOPIC, TopicId, MsgId,
|
|
|
- State = #state{registry = Registry, channel = Channel}) ->
|
|
|
+ State = #state{channel = Channel}) ->
|
|
|
ClientId = emqx_channel:info(clientid, Channel),
|
|
|
- case emqx_sn_registry:lookup_topic(Registry, ClientId, TopicId) of
|
|
|
+ case emqx_sn_registry:lookup_topic(ClientId, TopicId) of
|
|
|
undefined ->
|
|
|
{keep_state, send_message(?SN_UNSUBACK_MSG(MsgId), State)};
|
|
|
PredefinedTopic ->
|
|
|
@@ -941,11 +935,11 @@ do_publish(?SN_NORMAL_TOPIC, TopicName, Data, Flags, MsgId, State) ->
|
|
|
<<TopicId:16>> = TopicName,
|
|
|
do_publish(?SN_PREDEFINED_TOPIC, TopicId, Data, Flags, MsgId, State);
|
|
|
do_publish(?SN_PREDEFINED_TOPIC, TopicId, Data, Flags, MsgId,
|
|
|
- State=#state{registry = Registry, channel = Channel}) ->
|
|
|
+ State=#state{channel = Channel}) ->
|
|
|
#mqtt_sn_flags{qos = QoS, dup = Dup, retain = Retain} = Flags,
|
|
|
NewQoS = get_corrected_qos(QoS),
|
|
|
ClientId = emqx_channel:info(clientid, Channel),
|
|
|
- case emqx_sn_registry:lookup_topic(Registry, ClientId, TopicId) of
|
|
|
+ case emqx_sn_registry:lookup_topic(ClientId, TopicId) of
|
|
|
undefined ->
|
|
|
{keep_state, maybe_send_puback(NewQoS, TopicId, MsgId, ?SN_RC_INVALID_TOPIC_ID,
|
|
|
State)};
|
|
|
@@ -984,13 +978,13 @@ do_publish_will(#state{will_msg = WillMsg, clientid = ClientId}) ->
|
|
|
ok.
|
|
|
|
|
|
do_puback(TopicId, MsgId, ReturnCode, StateName,
|
|
|
- State=#state{registry = Registry, channel = Channel}) ->
|
|
|
+ State=#state{channel = Channel}) ->
|
|
|
case ReturnCode of
|
|
|
?SN_RC_ACCEPTED ->
|
|
|
handle_incoming(?PUBACK_PACKET(MsgId), StateName, State);
|
|
|
?SN_RC_INVALID_TOPIC_ID ->
|
|
|
ClientId = emqx_channel:info(clientid, Channel),
|
|
|
- case emqx_sn_registry:lookup_topic(Registry, ClientId, TopicId) of
|
|
|
+ case emqx_sn_registry:lookup_topic(ClientId, TopicId) of
|
|
|
undefined -> {keep_state, State};
|
|
|
TopicName ->
|
|
|
%%notice that this TopicName maybe normal or predefined,
|
|
|
@@ -1079,10 +1073,10 @@ handle_outgoing(Packets, State) when is_list(Packets) ->
|
|
|
end, State, Packets);
|
|
|
|
|
|
handle_outgoing(PubPkt = ?PUBLISH_PACKET(_, TopicName, _, _),
|
|
|
- State = #state{registry = Registry, channel = Channel}) ->
|
|
|
+ State = #state{channel = Channel}) ->
|
|
|
?LOG(debug, "Handle outgoing publish: ~0p", [PubPkt]),
|
|
|
ClientId = emqx_channel:info(clientid, Channel),
|
|
|
- TopicId = emqx_sn_registry:lookup_topic_id(Registry, ClientId, TopicName),
|
|
|
+ TopicId = emqx_sn_registry:lookup_topic_id(ClientId, TopicName),
|
|
|
case (TopicId == undefined) andalso (byte_size(TopicName) =/= 2) of
|
|
|
true -> register_and_notify_client(PubPkt, State);
|
|
|
false -> send_message(mqtt2sn(PubPkt, State), State)
|
|
|
@@ -1106,11 +1100,11 @@ replay_no_reg_pending_publishes(TopicId, #state{pending_topic_ids = Pendings} =
|
|
|
State#state{pending_topic_ids = maps:remove(TopicId, Pendings)}.
|
|
|
|
|
|
register_and_notify_client(?PUBLISH_PACKET(QoS, TopicName, PacketId, Payload) = PubPkt,
|
|
|
- State = #state{registry = Registry, pending_topic_ids = Pendings, channel = Channel}) ->
|
|
|
+ State = #state{pending_topic_ids = Pendings, channel = Channel}) ->
|
|
|
MsgId = message_id(PacketId),
|
|
|
#mqtt_packet{header = #mqtt_packet_header{dup = Dup, retain = Retain}} = PubPkt,
|
|
|
ClientId = emqx_channel:info(clientid, Channel),
|
|
|
- TopicId = emqx_sn_registry:register_topic(Registry, ClientId, TopicName),
|
|
|
+ TopicId = emqx_sn_registry:register_topic(ClientId, TopicName),
|
|
|
?LOG(debug, "Register TopicId=~p, TopicName=~p, Payload=~p, Dup=~p, QoS=~p, "
|
|
|
"Retain=~p, MsgId=~p", [TopicId, TopicName, Payload, Dup, QoS, Retain, MsgId]),
|
|
|
NewPendings = cache_no_reg_publish_message(Pendings, TopicId, PubPkt, State),
|