emqx_request_sender.erl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. %% Copyright (c) 2013-2019 EMQ Technologies Co., Ltd. All Rights Reserved.
  2. %%
  3. %% Licensed under the Apache License, Version 2.0 (the "License");
  4. %% you may not use this file except in compliance with the License.
  5. %% You may obtain a copy of the License at
  6. %%
  7. %% http://www.apache.org/licenses/LICENSE-2.0
  8. %%
  9. %% Unless required by applicable law or agreed to in writing, software
  10. %% distributed under the License is distributed on an "AS IS" BASIS,
  11. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. %% See the License for the specific language governing permissions and
  13. %% limitations under the License.
  14. %% @doc This module implements a request sender based on emqx_client.
  15. %% A request sender is a MQTT client which sends messages to a request
  16. %% topic, and subscribes to another topic for responses.
  17. %% This code is in test directory because request and response are pure
  18. %% client-side behaviours.
  19. -module(emqx_request_sender).
  20. -export([start_link/3, stop/1, send/6]).
  21. -include("emqx_client.hrl").
  22. start_link(ResponseTopic, QoS, Options0) ->
  23. Parent = self(),
  24. MsgHandler = make_msg_handler(Parent),
  25. Options = [{msg_handler, MsgHandler} | Options0],
  26. case emqx_client:start_link(Options) of
  27. {ok, Pid} ->
  28. {ok, _} = emqx_client:connect(Pid),
  29. try subscribe(Pid, ResponseTopic, QoS) of
  30. ok -> {ok, Pid};
  31. {error, _} = Error -> Error
  32. catch
  33. C : E : S ->
  34. emqx_client:stop(Pid),
  35. {error, {C, E, S}}
  36. end;
  37. {error, _} = Error -> Error
  38. end.
  39. %% @doc Send a message to request topic with correlation-data `CorrData'.
  40. %% Response should be delivered as a `{response, CorrData, Payload}'
  41. send(Client, ReqTopic, RspTopic, CorrData, Payload, QoS) ->
  42. Props = #{'Response-Topic' => RspTopic,
  43. 'Correlation-Data' => CorrData
  44. },
  45. Msg = #mqtt_msg{qos = QoS,
  46. topic = ReqTopic,
  47. props = Props,
  48. payload = Payload
  49. },
  50. case emqx_client:publish(Client, Msg) of
  51. ok -> ok; %% QoS = 0
  52. {ok, _} -> ok;
  53. {error, _} = E -> E
  54. end.
  55. stop(Pid) ->
  56. emqx_client:disconnect(Pid).
  57. subscribe(Client, Topic, QoS) ->
  58. case emqx_client:subscribe(Client, Topic, QoS) of
  59. {ok, _, _} -> ok;
  60. {error, _} = Error -> Error
  61. end.
  62. make_msg_handler(Parent) ->
  63. #{publish => fun(Msg) -> handle_msg(Msg, Parent) end,
  64. puback => fun(_Ack) -> ok end,
  65. disconnected => fun(_Reason) -> ok end
  66. }.
  67. handle_msg(Msg, Parent) ->
  68. #{properties := Props, payload := Payload} = Msg,
  69. CorrData = maps:get('Correlation-Data', Props),
  70. Parent ! {response, CorrData, Payload},
  71. ok.