From 5d45d40db5175fae99d52e3a074de3a79d37af68 Mon Sep 17 00:00:00 2001 From: Feng Lee Date: Tue, 12 Jun 2018 10:04:01 +0800 Subject: [PATCH] Rename emqx_mqtt module to emqx_listeners --- src/{emqx_mqtt.erl => emqx_listeners.erl} | 70 ++++++++++------------- 1 file changed, 29 insertions(+), 41 deletions(-) rename src/{emqx_mqtt.erl => emqx_listeners.erl} (65%) diff --git a/src/emqx_mqtt.erl b/src/emqx_listeners.erl similarity index 65% rename from src/emqx_mqtt.erl rename to src/emqx_listeners.erl index d7a7281cf..d63a6d620 100644 --- a/src/emqx_mqtt.erl +++ b/src/emqx_listeners.erl @@ -1,45 +1,36 @@ -%%-------------------------------------------------------------------- -%% Copyright (c) 2013-2018 EMQ Inc. All rights reserved. -%% -%% Licensed under the Apache License, Version 2.0 (the "License"); -%% you may not use this file except in compliance with the License. -%% You may obtain a copy of the License at -%% -%% http://www.apache.org/licenses/LICENSE-2.0 -%% -%% Unless required by applicable law or agreed to in writing, software -%% distributed under the License is distributed on an "AS IS" BASIS, -%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -%% See the License for the specific language governing permissions and -%% limitations under the License. -%%-------------------------------------------------------------------- +%%%=================================================================== +%%% Copyright (c) 2013-2018 EMQ Inc. All rights reserved. +%%% +%%% Licensed under the Apache License, Version 2.0 (the "License"); +%%% you may not use this file except in compliance with the License. +%%% You may obtain a copy of the License at +%%% +%%% http://www.apache.org/licenses/LICENSE-2.0 +%%% +%%% Unless required by applicable law or agreed to in writing, software +%%% distributed under the License is distributed on an "AS IS" BASIS, +%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%%% See the License for the specific language governing permissions and +%%% limitations under the License. +%%%=================================================================== --module(emqx_mqtt). +-module(emqx_listeners). -include("emqx_mqtt.hrl"). --export([bootstrap/0, shutdown/0]). - --export([start_listeners/0, start_listener/1]). --export([stop_listeners/0, stop_listener/1]). --export([restart_listeners/0, restart_listener/1]). --export([listeners/0]). +-export([start/0, restart/0, stop/0]). +-export([start_listener/1, stop_listener/1, restart_listener/1]). +-export([all/0]). -type(listener() :: {atom(), esockd:listen_on(), [esockd:option()]}). -bootstrap() -> - start_listeners(). - -shutdown() -> - stop_listeners(). - %%-------------------------------------------------------------------- %% Start/Stop Listeners %%-------------------------------------------------------------------- %% @doc Start Listeners. --spec(start_listeners() -> ok). -start_listeners() -> +-spec(start() -> ok). +start() -> lists:foreach(fun start_listener/1, emqx_config:get_env(listeners, [])). %% Start mqtt listener @@ -65,7 +56,7 @@ start_listener(Proto, ListenOn, Opts) -> MFArgs = {emqx_connection, start_link, [Env]}, {ok, _} = esockd:open(Proto, ListenOn, merge_sockopts(Opts), MFArgs). -listeners() -> +all() -> [Listener || Listener = {{Proto, _}, _Pid} <- esockd:listeners(), is_mqtt(Proto)]. is_mqtt('mqtt:tcp') -> true; @@ -75,8 +66,8 @@ is_mqtt('mqtt:wss') -> true; is_mqtt(_Proto) -> false. %% @doc Stop Listeners --spec(stop_listeners() -> ok). -stop_listeners() -> +-spec(stop() -> ok). +stop() -> lists:foreach(fun stop_listener/1, emqx_config:get_env(listeners, [])). -spec(stop_listener(listener()) -> ok | {error, any()}). @@ -88,16 +79,13 @@ stop_listener({Proto, ListenOn, _Opts}) when Proto == http; Proto == ws -> mochiweb:stop_http('mqtt:ws', ListenOn); stop_listener({Proto, ListenOn, _Opts}) when Proto == https; Proto == wss -> mochiweb:stop_http('mqtt:wss', ListenOn); -% stop_listener({Proto, ListenOn, _Opts}) when Proto == api -> -% mochiweb:stop_http('mqtt:api', ListenOn); stop_listener({Proto, ListenOn, _Opts}) -> esockd:close(Proto, ListenOn). %% @doc Restart Listeners --spec(restart_listeners() -> ok). -restart_listeners() -> - lists:foreach(fun restart_listener/1, - emqx_config:get_env(listeners, [])). +-spec(restart() -> ok). +restart() -> + lists:foreach(fun restart_listener/1, emqx_config:get_env(listeners, [])). -spec(restart_listener(listener()) -> any()). restart_listener({tcp, ListenOn, _Opts}) -> @@ -108,13 +96,13 @@ restart_listener({Proto, ListenOn, _Opts}) when Proto == http; Proto == ws -> mochiweb:restart_http('mqtt:ws', ListenOn); restart_listener({Proto, ListenOn, _Opts}) when Proto == https; Proto == wss -> mochiweb:restart_http('mqtt:wss', ListenOn); -restart_listener({Proto, ListenOn, _Opts}) when Proto == api -> - mochiweb:restart_http('mqtt:api', ListenOn); restart_listener({Proto, ListenOn, _Opts}) -> esockd:reopen(Proto, ListenOn). merge_sockopts(Options) -> + %%TODO: tcp_options? SockOpts = emqx_misc:merge_opts( ?MQTT_SOCKOPTS, proplists:get_value(sockopts, Options, [])), emqx_misc:merge_opts(Options, [{sockopts, SockOpts}]). +