Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClientId option on MQTT connect message #303

Merged
merged 8 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/mqtt.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
-record(connect_options, {
protocol_name = ?PROTOCOL_NAME,
protocol_version = ?PROTOCOL_VERSION,
client_id,
client_id = undefined,
clean_start = true,
will,
keepalive = ?DEFAULT_KEEPALIVE,
Expand Down
3 changes: 2 additions & 1 deletion include/ts_mqtt.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
retained = false,
payload,
username,
password
password,
client_id
}).

-record(mqtt_dyndata, {
Expand Down
28 changes: 28 additions & 0 deletions src/test/ts_test_mqtt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
-compile(export_all).

-include("ts_profile.hrl").
-include("ts_mqtt.hrl").
-include("mqtt.hrl").
-include("ts_config.hrl").
-include_lib("eunit/include/eunit.hrl").
Expand All @@ -51,6 +52,33 @@ encode_connect_test() ->
116,111,112,105,99,0,12,119,105,108,108,95,109,101,115,115,
97,103,101>>, EncodedData).

get_message_with_default_client_id_test() ->
Session = #mqtt_session{},
State = #state_rcv{session = Session},
Req = #mqtt_request{type = connect,
will_qos = 0,
will_retain = true,
keepalive = 10},
{Encoded,_} = ts_mqtt:get_message(Req,State),
{#mqtt{type = Type, arg = Args}, _} = mqtt_frame:decode(Encoded),
#connect_options{client_id = ClientId} = Args,
?assertEqual(?CONNECT, Type),
?assertMatch({match,_}, re:run(ClientId, "tsung-")).

get_message_with_custom_client_id_test() ->
Session = #mqtt_session{},
State = #state_rcv{session = Session},
Req = #mqtt_request{type = connect,
will_qos = 0,
will_retain = true,
keepalive = 10,
client_id = "custom-client-id"},
{Encoded,_} = ts_mqtt:get_message(Req,State),
{#mqtt{type = Type, arg = Args}, _} = mqtt_frame:decode(Encoded),
#connect_options{client_id = ClientId} = Args,
?assertEqual(?CONNECT, Type),
?assertEqual("custom-client-id", ClientId).

decode_connect_test() ->
Data = <<16,53,0,6,77,81,73,115,100,112,3,6,0,10,0,13,116,115,117,110,
103,45,116,101,115,116,45,105,100,0,10,119,105,108,108,95,
Expand Down
14 changes: 10 additions & 4 deletions src/tsung/ts_mqtt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ dump(A, B) ->
%% Args: record
%% Returns: binary
%%----------------------------------------------------------------------
get_message(Req0 = #mqtt_request{type = connect, client_id = undefined}, StateRcv) ->
ClientId = ["tsung-", ts_utils:randombinstr(10)],
Req1 = Req0#mqtt_request{client_id = ClientId},
get_message(Req1, StateRcv);
get_message(#mqtt_request{type = connect, clean_start = CleanStart,
keepalive = KeepAlive, will_topic = WillTopic,
will_qos = WillQos, will_msg = WillMsg,
will_retain = WillRetain, username = UserName, password = Password},
will_retain = WillRetain, username = UserName,
password = Password, client_id = ClientId},
#state_rcv{session = MqttSession}) ->
ClientId = ["tsung-", ts_utils:randombinstr(10)],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very deeply familiar with MQTT, but if client ID is required, we could generate it in case it is not provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had thought about that, but it seems that anyone using the protocol may quickly generate it using Tsung facilities.

Do you reckon it would be better to have another function clause which automatically generates the Client ID, when it's not provided?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I said, if this is required, then I'd say yes. Otherwise this would be a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've submitted a new commit which hopefully takes care of this. However, I'll need someone to validate it, since my Erlang foo is quite low.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also added two unit tests scenarios, providing a custom client ID or using the default implementation. So far it does not seem to be a breaking change.

PublishOptions = mqtt_frame:set_publish_options([{qos, WillQos},
{retain, WillRetain}]),
Will = #will{topic = WillTopic, message = WillMsg,
Expand Down Expand Up @@ -265,17 +269,19 @@ add_dynparams(true, {DynVars, _S},
keepalive = KeepAlive, will_topic = WillTopic,
will_qos = WillQos, will_msg = WillMsg,
will_retain = WillRetain, username = UserName,
password = Password},
password = Password, client_id = ClientId},
_HostData) ->
NewUserName = ts_search:subst(UserName, DynVars),
NewPassword = ts_search:subst(Password, DynVars),
NewClientId = ts_search:subst(ClientId, DynVars),
Param#mqtt_request{ type = connect,
clean_start = CleanStart,
keepalive = KeepAlive, will_topic = WillTopic,
will_qos = WillQos, will_msg = WillMsg,
will_retain = WillRetain,
username = NewUserName,
password = NewPassword };
password = NewPassword,
client_id = NewClientId};
add_dynparams(true, {DynVars, _S},
Param = #mqtt_request{type = publish, topic = Topic,
payload = Payload},
Expand Down
5 changes: 4 additions & 1 deletion src/tsung_controller/ts_config_mqtt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ parse_config(Element = #xmlElement{name = mqtt},
username, undefined),
Password = ts_config:getAttr(string, Element#xmlElement.attributes,
password, undefined),
ClientId = ts_config:getAttr(string, Element#xmlElement.attributes,
client_id, undefined),
KeepAlive = ts_config:getAttr(float_or_integer, Element#xmlElement.attributes,
keepalive, 10),
WillTopic = ts_config:getAttr(string, Element#xmlElement.attributes,
Expand Down Expand Up @@ -82,7 +84,8 @@ parse_config(Element = #xmlElement{name = mqtt},
keepalive = KeepAlive, will_topic = WillTopic,
will_qos = WillQos, will_msg = WillMsg,
will_retain = WillRetain, topic = Topic, qos = Qos,
retained = RetainValue, payload = Payload, username = UserName, password = Password},
retained = RetainValue, payload = Payload, username = UserName,
password = Password, client_id = ClientId},
Ack = case {Type, Qos} of
{publish, 0} -> no_ack;
{disconnect, _} -> no_ack;
Expand Down
3 changes: 2 additions & 1 deletion tsung-1.0.dtd
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ repeat | if | change_type | foreach | set_option | interaction | abort )*>
retained CDATA "false"
timeout CDATA "1"
username CDATA ""
password CDATA "">
password CDATA ""
client_id CDATA "">

<!ELEMENT modification (attr*) >
<!ATTLIST modification
Expand Down