emqttd.erl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2012-2016 Feng Lee <feng@emqtt.io>.
  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(emqttd).
  17. -include("emqttd.hrl").
  18. -include("emqttd_protocol.hrl").
  19. -export([start/0, env/1, env/2, is_running/1]).
  20. %% PubSub API
  21. -export([create/2, lookup/2, publish/1, subscribe/1, subscribe/3,
  22. unsubscribe/1, unsubscribe/3]).
  23. %% Hooks API
  24. -export([hook/4, hook/3, unhook/2, run_hooks/3]).
  25. -define(APP, ?MODULE).
  26. %%--------------------------------------------------------------------
  27. %% Bootstrap, environment, is_running...
  28. %%--------------------------------------------------------------------
  29. %% @doc Start emqttd application.
  30. -spec(start() -> ok | {error, any()}).
  31. start() -> application:start(?APP).
  32. %% @doc Group environment
  33. -spec(env(Group :: atom()) -> list()).
  34. env(Group) -> application:get_env(?APP, Group, []).
  35. %% @doc Get environment
  36. -spec(env(Group :: atom(), Name :: atom()) -> undefined | any()).
  37. env(Group, Name) -> proplists:get_value(Name, env(Group)).
  38. %% @doc Is running?
  39. -spec(is_running(node()) -> boolean()).
  40. is_running(Node) ->
  41. case rpc:call(Node, erlang, whereis, [?APP]) of
  42. {badrpc, _} -> false;
  43. undefined -> false;
  44. Pid when is_pid(Pid) -> true
  45. end.
  46. %%--------------------------------------------------------------------
  47. %% PubSub APIs that wrap emqttd_server, emqttd_pubsub
  48. %%--------------------------------------------------------------------
  49. %% @doc Lookup Topic or Subscription
  50. -spec(lookup(topic, binary()) -> [mqtt_topic()];
  51. (subscription, binary()) -> [mqtt_subscription()]).
  52. lookup(topic, Topic) when is_binary(Topic) ->
  53. emqttd_pubsub:lookup_topic(Topic);
  54. lookup(subscription, ClientId) when is_binary(ClientId) ->
  55. emqttd_server:lookup_subscription(ClientId).
  56. %% @doc Create a Topic or Subscription
  57. -spec(create(topic | subscription, binary()) -> ok | {error, any()}).
  58. create(topic, Topic) when is_binary(Topic) ->
  59. emqttd_pubsub:create_topic(Topic);
  60. create(subscription, {ClientId, Topic, Qos}) ->
  61. Subscription = #mqtt_subscription{subid = ClientId, topic = Topic, qos = ?QOS_I(Qos)},
  62. emqttd_backend:add_subscription(Subscription).
  63. %% @doc Publish MQTT Message
  64. -spec(publish(mqtt_message()) -> ok).
  65. publish(Msg) when is_record(Msg, mqtt_message) ->
  66. emqttd_server:publish(Msg), ok.
  67. %% @doc Subscribe
  68. -spec(subscribe(binary()) -> ok;
  69. ({binary(), binary(), mqtt_qos()}) -> ok).
  70. subscribe(Topic) when is_binary(Topic) ->
  71. emqttd_server:subscribe(Topic);
  72. subscribe({ClientId, Topic, Qos}) ->
  73. subscribe(ClientId, Topic, Qos).
  74. -spec(subscribe(binary(), binary(), mqtt_qos()) -> {ok, mqtt_qos()}).
  75. subscribe(ClientId, Topic, Qos) ->
  76. emqttd_server:subscribe(ClientId, Topic, Qos).
  77. %% @doc Unsubscribe
  78. -spec(unsubscribe(binary()) -> ok).
  79. unsubscribe(Topic) when is_binary(Topic) ->
  80. emqttd_server:unsubscribe(Topic).
  81. -spec(unsubscribe(binary(), binary(), mqtt_qos()) -> ok).
  82. unsubscribe(ClientId, Topic, Qos) ->
  83. emqttd_server:unsubscribe(ClientId, Topic, Qos).
  84. %%--------------------------------------------------------------------
  85. %% Hooks API
  86. %%--------------------------------------------------------------------
  87. -spec(hook(atom(), function(), list(any())) -> ok | {error, any()}).
  88. hook(Hook, Function, InitArgs) ->
  89. emqttd_hook:add(Hook, Function, InitArgs).
  90. -spec(hook(atom(), function(), list(any()), integer()) -> ok | {error, any()}).
  91. hook(Hook, Function, InitArgs, Priority) ->
  92. emqttd_hook:add(Hook, Function, InitArgs, Priority).
  93. -spec(unhook(atom(), function()) -> ok | {error, any()}).
  94. unhook(Hook, Function) ->
  95. emqttd_hook:delete(Hook, Function).
  96. -spec(run_hooks(atom(), list(any()), any()) -> {ok | stop, any()}).
  97. run_hooks(Hook, Args, Acc) ->
  98. emqttd_hook:run(Hook, Args, Acc).