Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit d82dc2e

Browse files
Merge pull request #172 from rabbitmq/rabbitmq-mqtt-152-client-id-in-connection-metadata
Add client ID to connection metadata
2 parents 8b73aed + 01a833c commit d82dc2e

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/rabbit_mqtt_processor.erl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
%% The Original Code is RabbitMQ.
1212
%%
1313
%% The Initial Developer of the Original Code is GoPivotal, Inc.
14-
%% Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
14+
%% Copyright (c) 2007-2019 Pivotal Software, Inc. All rights reserved.
1515
%%
1616

1717
-module(rabbit_mqtt_processor).
@@ -21,7 +21,8 @@
2121
close_connection/1]).
2222

2323
%% for testing purposes
24-
-export([get_vhost_username/1, get_vhost/3, get_vhost_from_user_mapping/2]).
24+
-export([get_vhost_username/1, get_vhost/3, get_vhost_from_user_mapping/2,
25+
add_client_id_to_adapter_info/2]).
2526

2627
-include_lib("amqp_client/include/amqp_client.hrl").
2728
-include("rabbit_mqtt_frame.hrl").
@@ -73,6 +74,22 @@ process_frame(Frame = #mqtt_frame{ fixed = #mqtt_frame_fixed{ type = Type }},
7374
Ret -> Ret
7475
end.
7576

77+
add_client_id_to_adapter_info(ClientId, #amqp_adapter_info{additional_info = AdditionalInfo0} = AdapterInfo) ->
78+
AdditionalInfo1 = [{variable_map, #{<<"client_id">> => ClientId}}
79+
| AdditionalInfo0],
80+
ClientProperties = proplists:get_value(client_properties, AdditionalInfo1, [])
81+
++ [{client_id, longstr, ClientId}],
82+
AdditionalInfo2 = case lists:keysearch(client_properties, 1, AdditionalInfo1) of
83+
{value, _} ->
84+
lists:keyreplace(client_properties,
85+
1,
86+
AdditionalInfo1,
87+
{client_properties, ClientProperties});
88+
false ->
89+
[{client_properties, ClientProperties} | AdditionalInfo1]
90+
end,
91+
AdapterInfo#amqp_adapter_info{additional_info = AdditionalInfo2}.
92+
7693
process_request(?CONNECT,
7794
#mqtt_frame{ variable = #mqtt_frame_connect{
7895
username = Username,
@@ -83,14 +100,12 @@ process_request(?CONNECT,
83100
keep_alive = Keepalive} = Var},
84101
PState0 = #proc_state{ ssl_login_name = SSLLoginName,
85102
send_fun = SendFun,
86-
adapter_info = AdapterInfo = #amqp_adapter_info{additional_info = Extra} }) ->
103+
adapter_info = AdapterInfo}) ->
87104
ClientId = case ClientId0 of
88105
[] -> rabbit_mqtt_util:gen_client_id();
89106
[_|_] -> ClientId0
90107
end,
91-
AdapterInfo1 = AdapterInfo#amqp_adapter_info{additional_info = [
92-
{variable_map, #{<<"client_id">> => rabbit_data_coercion:to_binary(ClientId)}}
93-
| Extra]},
108+
AdapterInfo1 = add_client_id_to_adapter_info(rabbit_data_coercion:to_binary(ClientId), AdapterInfo),
94109
PState = PState0#proc_state{adapter_info = AdapterInfo1},
95110
{Return, PState1} =
96111
case {lists:member(ProtoVersion, proplists:get_keys(?PROTOCOL_NAMES)),

test/processor_SUITE.erl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
%% The Original Code is RabbitMQ.
1212
%%
1313
%% The Initial Developer of the Original Code is GoPivotal, Inc.
14-
%% Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
14+
%% Copyright (c) 2007-2019 Pivotal Software, Inc. All rights reserved.
1515

1616

1717
-module(processor_SUITE).
1818
-compile([export_all]).
1919

20-
-include_lib("rabbit_common/include/rabbit.hrl").
2120
-include_lib("common_test/include/ct.hrl").
2221
-include_lib("eunit/include/eunit.hrl").
22+
-include_lib("amqp_client/include/amqp_client.hrl").
2323

2424
all() ->
2525
[
@@ -32,7 +32,8 @@ groups() ->
3232
ignores_colons_in_username_if_option_set,
3333
interprets_colons_in_username_if_option_not_set,
3434
get_vhosts_from_global_runtime_parameter,
35-
get_vhost
35+
get_vhost,
36+
add_client_id_to_adapter_info
3637
]}
3738
].
3839

@@ -193,6 +194,17 @@ get_vhost(_Config) ->
193194
clear_vhost_global_parameters(),
194195
ok.
195196

197+
add_client_id_to_adapter_info(_Config) ->
198+
TestFun = fun(AdapterInfo) ->
199+
Info0 = rabbit_mqtt_processor:add_client_id_to_adapter_info(<<"my-client-id">>, AdapterInfo),
200+
AdditionalInfo0 = Info0#amqp_adapter_info.additional_info,
201+
?assertEqual(#{<<"client_id">> => <<"my-client-id">>}, proplists:get_value(variable_map, AdditionalInfo0)),
202+
ClientProperties = proplists:get_value(client_properties, AdditionalInfo0),
203+
?assertEqual([{client_id,longstr,<<"my-client-id">>}], ClientProperties)
204+
end,
205+
lists:foreach(TestFun, [#amqp_adapter_info{}, #amqp_adapter_info{additional_info = [{client_properties, []}]}]),
206+
ok.
207+
196208
set_global_parameter(Key, Term) ->
197209
InsertParameterFun = fun () ->
198210
mnesia:write(rabbit_runtime_parameters, #runtime_parameters{key = Key, value = Term}, write)

0 commit comments

Comments
 (0)