Skip to content

Commit

Permalink
Merge pull request #303 from Talkdesk/develop
Browse files Browse the repository at this point in the history
Add ClientId option on MQTT connect message
  • Loading branch information
nniclausse authored May 21, 2018
2 parents 5996f33 + 3603c3b commit fe0fe29
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 10 deletions.
6 changes: 4 additions & 2 deletions docs/conf-sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ MQTT
It supports publish messages, subscribe and unsubscribe topics,
Available request types:

* connect (with options like clean_start, will_topic, username, password, etc.)
* connect (with options like client_id, clean_start, will_topic, username, password, etc.)
* disconnect
* publish (with topic name, qos level and retain flag)
* subscribe (with topic name and qos level)
Expand All @@ -996,7 +996,7 @@ Example with MQTT as a session type:
<session name="mqtt-example" probability="100" type="ts_mqtt">
<request>
<mqtt type="connect" clean_start="true" keepalive="10" will_topic="will_topic" will_qos="0" will_msg="will_msg" will_retain="false"></mqtt>
<mqtt type="connect" client_id="client_id" clean_start="true" keepalive="10" will_topic="will_topic" will_qos="0" will_msg="will_msg" will_retain="false"></mqtt>
</request>
<for from="1" to="10" incr="1" var="loops">
Expand All @@ -1023,6 +1023,8 @@ Example with MQTT as a session type:
</request>
</session>
Note that if a ``client_id`` is omitted upon connecting Tsung will create a random one, prefixed with ``tsung-``.

LDAP
^^^^

Expand Down
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)],
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

0 comments on commit fe0fe29

Please sign in to comment.