emqx_request_handler.erl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 handler based on emqx_client.
  15. %% A request handler is a MQTT client which subscribes to a request topic,
  16. %% processes the requests then send response to another topic which is
  17. %% subscribed by the request sender.
  18. %% This code is in test directory because request and response are pure
  19. %% client-side behaviours.
  20. -module(emqx_request_handler).
  21. -export([start_link/4, stop/1]).
  22. -include("emqx_client.hrl").
  23. -type qos() :: emqx_mqtt_types:qos_name() | emqx_mqtt_types:qos().
  24. -type topic() :: emqx_topic:topic().
  25. -type handler() :: fun((CorrData :: binary(), ReqPayload :: binary()) -> RspPayload :: binary()).
  26. -spec start_link(topic(), qos(), handler(), emqx_client:options()) ->
  27. {ok, pid()} | {error, any()}.
  28. start_link(RequestTopic, QoS, RequestHandler, Options0) ->
  29. Parent = self(),
  30. MsgHandler = make_msg_handler(RequestHandler, Parent),
  31. Options = [{msg_handler, MsgHandler} | Options0],
  32. case emqx_client:start_link(Options) of
  33. {ok, Pid} ->
  34. {ok, _} = emqx_client:connect(Pid),
  35. try subscribe(Pid, RequestTopic, QoS) of
  36. ok -> {ok, Pid};
  37. {error, _} = Error -> Error
  38. catch
  39. C : E : S ->
  40. emqx_client:stop(Pid),
  41. {error, {C, E, S}}
  42. end;
  43. {error, _} = Error -> Error
  44. end.
  45. stop(Pid) ->
  46. emqx_client:disconnect(Pid).
  47. make_msg_handler(RequestHandler, Parent) ->
  48. #{publish => fun(Msg) -> handle_msg(Msg, RequestHandler, Parent) end,
  49. puback => fun(_Ack) -> ok end,
  50. disconnected => fun(_Reason) -> ok end
  51. }.
  52. handle_msg(ReqMsg, RequestHandler, Parent) ->
  53. #{qos := QoS, properties := Props, payload := ReqPayload} = ReqMsg,
  54. case maps:find('Response-Topic', Props) of
  55. {ok, RspTopic} when RspTopic =/= <<>> ->
  56. CorrData = maps:get('Correlation-Data', Props),
  57. RspProps = maps:without(['Response-Topic'], Props),
  58. RspPayload = RequestHandler(CorrData, ReqPayload),
  59. RspMsg = #mqtt_msg{qos = QoS,
  60. topic = RspTopic,
  61. props = RspProps,
  62. payload = RspPayload
  63. },
  64. emqx_logger:debug("~p sending response msg to topic ~s with~n"
  65. "corr-data=~p~npayload=~p",
  66. [?MODULE, RspTopic, CorrData, RspPayload]),
  67. ok = send_response(RspMsg);
  68. _ ->
  69. Parent ! {discarded, ReqPayload},
  70. ok
  71. end.
  72. send_response(Msg) ->
  73. %% This function is evaluated by emqx_client itself.
  74. %% hence delegate to another temp process for the loopback gen_statem call.
  75. Client = self(),
  76. _ = spawn_link(fun() ->
  77. case emqx_client:publish(Client, Msg) of
  78. ok -> ok;
  79. {ok, _} -> ok;
  80. {error, Reason} -> exit({failed_to_publish_response, Reason})
  81. end
  82. end),
  83. ok.
  84. subscribe(Client, Topic, QoS) ->
  85. {ok, _Props, _QoS} =
  86. emqx_client:subscribe(Client, [{Topic, [{rh, 2}, {rap, false},
  87. {nl, true}, {qos, QoS}]}]),
  88. ok.