emqx_request_sender.erl 2.6 KB

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