From 86e28d5abbf3e02d73c46304434db795f81ed1d3 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Wed, 25 Aug 2021 16:58:56 +0800 Subject: [PATCH] chore(gw): rename rawconf to config --- apps/emqx_gateway/include/emqx_gateway.hrl | 10 +-------- apps/emqx_gateway/src/coap/emqx_coap_impl.erl | 8 +++---- apps/emqx_gateway/src/emqx_gateway.erl | 21 +++++++++++++------ apps/emqx_gateway/src/emqx_gateway_api.erl | 5 +++-- .../src/emqx_gateway_insta_sup.erl | 8 +++---- apps/emqx_gateway/src/emqx_gateway_intr.erl | 8 +++---- apps/emqx_gateway/src/emqx_gateway_schema.erl | 3 ++- apps/emqx_gateway/src/emqx_gateway_utils.erl | 6 +++--- .../src/exproto/emqx_exproto_impl.erl | 18 ++++++++-------- .../src/lwm2m/emqx_lwm2m_impl.erl | 10 ++++----- apps/emqx_gateway/src/mqttsn/emqx_sn_impl.erl | 18 ++++++++-------- .../src/stomp/emqx_stomp_impl.erl | 10 ++++----- 12 files changed, 64 insertions(+), 61 deletions(-) diff --git a/apps/emqx_gateway/include/emqx_gateway.hrl b/apps/emqx_gateway/include/emqx_gateway.hrl index baa7a1ce7..5c0893cb2 100644 --- a/apps/emqx_gateway/include/emqx_gateway.hrl +++ b/apps/emqx_gateway/include/emqx_gateway.hrl @@ -21,14 +21,6 @@ -type listener() :: #{}. -%% The RawConf got from emqx:get_config/1 --type rawconf() :: - #{ clientinfo_override => map() - , authenticator => map() - , listeners => listener() - , atom() => any() - }. - %% @doc The Gateway defination -type gateway() :: #{ name := gateway_name() @@ -40,7 +32,7 @@ %% Timestamp in millisecond , started_at => integer() %% Appears only in getting gateway info - , rawconf => rawconf() + , config => emqx_config:config() }. -endif. diff --git a/apps/emqx_gateway/src/coap/emqx_coap_impl.erl b/apps/emqx_gateway/src/coap/emqx_coap_impl.erl index f150f6f81..aacb98546 100644 --- a/apps/emqx_gateway/src/coap/emqx_coap_impl.erl +++ b/apps/emqx_gateway/src/coap/emqx_coap_impl.erl @@ -49,9 +49,9 @@ unreg() -> %%-------------------------------------------------------------------- on_gateway_load(_Gateway = #{name := GwName, - rawconf := RawConf + config := Config }, Ctx) -> - Listeners = emqx_gateway_utils:normalize_rawconf(RawConf), + Listeners = emqx_gateway_utils:normalize_config(Config), ListenerPids = lists:map(fun(Lis) -> start_listener(GwName, Ctx, Lis) end, Listeners), @@ -74,9 +74,9 @@ on_gateway_update(NewGateway, OldGateway, GwState = #{ctx := Ctx}) -> end. on_gateway_unload(_Gateway = #{ name := GwName, - rawconf := RawConf + config := Config }, _GwState) -> - Listeners = emqx_gateway_utils:normalize_rawconf(RawConf), + Listeners = emqx_gateway_utils:normalize_config(Config), lists:foreach(fun(Lis) -> stop_listener(GwName, Lis) end, Listeners). diff --git a/apps/emqx_gateway/src/emqx_gateway.erl b/apps/emqx_gateway/src/emqx_gateway.erl index 4f80bfe3b..213208f19 100644 --- a/apps/emqx_gateway/src/emqx_gateway.erl +++ b/apps/emqx_gateway/src/emqx_gateway.erl @@ -29,6 +29,8 @@ , list/0 ]). +-export([update_rawconf/2]). + -spec registered_gateway() -> [{gateway_name(), emqx_gateway_registry:descriptor()}]. registered_gateway() -> @@ -41,13 +43,13 @@ registered_gateway() -> list() -> emqx_gateway_sup:list_gateway_insta(). --spec load(gateway_name(), rawconf()) +-spec load(gateway_name(), emqx_config:config()) -> {ok, pid()} | {error, any()}. -load(Name, RawConf) -> +load(Name, Config) -> Gateway = #{ name => Name , descr => undefined - , rawconf => RawConf + , config => Config }, emqx_gateway_sup:load_gateway(Gateway). @@ -59,9 +61,9 @@ unload(Name) -> lookup(Name) -> emqx_gateway_sup:lookup_gateway(Name). --spec update(gateway_name(), rawconf()) -> ok | {error, any()}. -update(Name, RawConf) -> - NewGateway = #{name => Name, rawconf => RawConf}, +-spec update(gateway_name(), emqx_config:config()) -> ok | {error, any()}. +update(Name, Config) -> + NewGateway = #{name => Name, config => Config}, emqx_gateway_sup:update_gateway(NewGateway). -spec start(gateway_name()) -> ok | {error, any()}. @@ -72,6 +74,13 @@ start(Name) -> stop(Name) -> emqx_gateway_sup:stop_gateway_insta(Name). +-spec update_rawconf(gateway_name(), emqx_config:raw_config()) + -> ok + | {error, any()}. +update_rawconf(_Name, _RawConf) -> + %% TODO: + ok. + %%-------------------------------------------------------------------- %% Internal funcs %%-------------------------------------------------------------------- diff --git a/apps/emqx_gateway/src/emqx_gateway_api.erl b/apps/emqx_gateway/src/emqx_gateway_api.erl index 28cb45b47..19ea97e9e 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api.erl @@ -356,8 +356,9 @@ gateway_insta(delete, Request) -> gateway_insta(get, Request) -> Name = binary_to_existing_atom(cowboy_req:binding(name, Request)), case emqx_gateway:lookup(Name) of - #{rawconf := RawConf} -> - {200, RawConf}; + #{config := Config} -> + %% TODO: ??? RawConf or Config ?? + {200, Config}; undefined -> {404, <<"Not Found">>} end; diff --git a/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl b/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl index 0abcb6426..1eae5f54c 100644 --- a/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl +++ b/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl @@ -88,8 +88,8 @@ call(Pid, Req) -> init([Gateway, Ctx0, _GwDscrptr]) -> process_flag(trap_exit, true), - #{name := GwName, rawconf := RawConf} = Gateway, - Ctx = do_init_context(GwName, RawConf, Ctx0), + #{name := GwName, config := Config } = Gateway, + Ctx = do_init_context(GwName, Config, Ctx0), State = #state{ gw = Gateway, ctx = Ctx, @@ -105,8 +105,8 @@ init([Gateway, Ctx0, _GwDscrptr]) -> {ok, NState} end. -do_init_context(GwName, RawConf, Ctx) -> - Auth = case maps:get(authentication, RawConf, #{enable => false}) of +do_init_context(GwName, Config, Ctx) -> + Auth = case maps:get(authentication, Config, #{enable => false}) of #{enable := false} -> undefined; AuthCfg when is_map(AuthCfg) -> case maps:get(enable, AuthCfg, true) of diff --git a/apps/emqx_gateway/src/emqx_gateway_intr.erl b/apps/emqx_gateway/src/emqx_gateway_intr.erl index 2a0c15646..add37e1c5 100644 --- a/apps/emqx_gateway/src/emqx_gateway_intr.erl +++ b/apps/emqx_gateway/src/emqx_gateway_intr.erl @@ -39,7 +39,7 @@ gateways(Status) -> Gateways = lists:map(fun({GwName, _}) -> case emqx_gateway:lookup(GwName) of undefined -> #{name => GwName, status => unloaded}; - GwInfo = #{rawconf := RawConf} -> + GwInfo = #{config := Config} -> GwInfo0 = emqx_gateway_utils:unix_ts_to_rfc3339( [created_at, started_at, stopped_at], GwInfo), @@ -48,7 +48,7 @@ gateways(Status) -> created_at, started_at, stopped_at], GwInfo0), - GwInfo1#{listeners => get_listeners_status(GwName, RawConf)} + GwInfo1#{listeners => get_listeners_status(GwName, Config)} end end, emqx_gateway_registry:list()), @@ -59,8 +59,8 @@ gateways(Status) -> end. %% @private -get_listeners_status(GwName, RawConf) -> - Listeners = emqx_gateway_utils:normalize_rawconf(RawConf), +get_listeners_status(GwName, Config) -> + Listeners = emqx_gateway_utils:normalize_config(Config), lists:map(fun({Type, LisName, ListenOn, _, _}) -> Name0 = listener_name(GwName, Type, LisName), Name = {Name0, ListenOn}, diff --git a/apps/emqx_gateway/src/emqx_gateway_schema.erl b/apps/emqx_gateway/src/emqx_gateway_schema.erl index 0abeb1354..d161c499c 100644 --- a/apps/emqx_gateway/src/emqx_gateway_schema.erl +++ b/apps/emqx_gateway/src/emqx_gateway_schema.erl @@ -158,7 +158,8 @@ fields(dtls_listener) -> [ {"$name", t(ref(dtls_listener_settings))}]; fields(listener_settings) -> - [ {bind, t(union(ip_port(), integer()))} + [ {enable, t(boolean(), undefined, true)} + , {bind, t(union(ip_port(), integer()))} , {acceptors, t(integer(), undefined, 8)} , {max_connections, t(integer(), undefined, 1024)} , {max_conn_rate, t(integer())} diff --git a/apps/emqx_gateway/src/emqx_gateway_utils.erl b/apps/emqx_gateway/src/emqx_gateway_utils.erl index a4978ee8b..82ace4b3d 100644 --- a/apps/emqx_gateway/src/emqx_gateway_utils.erl +++ b/apps/emqx_gateway/src/emqx_gateway_utils.erl @@ -31,7 +31,7 @@ , unix_ts_to_rfc3339/2 ]). --export([ normalize_rawconf/1 +-export([ normalize_config/1 ]). %% Common Envs @@ -118,14 +118,14 @@ unix_ts_to_rfc3339(Key, Map) -> emqx_rule_funcs:unix_ts_to_rfc3339(Ts, <<"millisecond">>)} end. --spec normalize_rawconf(rawconf()) +-spec normalize_config(emqx_config:config()) -> list({ Type :: udp | tcp | ssl | dtls , Name :: atom() , ListenOn :: esockd:listen_on() , SocketOpts :: esockd:option() , Cfg :: map() }). -normalize_rawconf(RawConf) -> +normalize_config(RawConf) -> LisMap = maps:get(listeners, RawConf, #{}), Cfg0 = maps:without([listeners], RawConf), lists:append(maps:fold(fun(Type, Liss, AccIn1) -> diff --git a/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl b/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl index a3b484fad..2821e9d15 100644 --- a/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl +++ b/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl @@ -84,7 +84,7 @@ start_grpc_client_channel(GwName, Options = #{address := UriStr}) -> grpc_client_sup:create_channel_pool(GwName, SvrAddr, ClientOpts). on_gateway_load(_Gateway = #{ name := GwName, - rawconf := RawConf + config := Config }, Ctx) -> %% XXX: How to monitor it ? %% Start grpc client pool & client channel @@ -92,17 +92,17 @@ on_gateway_load(_Gateway = #{ name := GwName, PoolSize = emqx_vm:schedulers() * 2, {ok, _} = emqx_pool_sup:start_link(PoolName, hash, PoolSize, {emqx_exproto_gcli, start_link, []}), - _ = start_grpc_client_channel(GwName, maps:get(handler, RawConf, undefined)), + _ = start_grpc_client_channel(GwName, maps:get(handler, Config, undefined)), %% XXX: How to monitor it ? - _ = start_grpc_server(GwName, maps:get(server, RawConf, undefined)), + _ = start_grpc_server(GwName, maps:get(server, Config, undefined)), - NRawConf = maps:without( + NConfig = maps:without( [server, handler], - RawConf#{pool_name => PoolName} + Config#{pool_name => PoolName} ), - Listeners = emqx_gateway_utils:normalize_rawconf( - NRawConf#{handler => GwName} + Listeners = emqx_gateway_utils:normalize_config( + NConfig#{handler => GwName} ), ListenerPids = lists:map(fun(Lis) -> start_listener(GwName, Ctx, Lis) @@ -125,9 +125,9 @@ on_gateway_update(NewGateway, OldGateway, GwState = #{ctx := Ctx}) -> end. on_gateway_unload(_Gateway = #{ name := GwName, - rawconf := RawConf + config := Config }, _GwState) -> - Listeners = emqx_gateway_utils:normalize_rawconf(RawConf), + Listeners = emqx_gateway_utils:normalize_config(Config), lists:foreach(fun(Lis) -> stop_listener(GwName, Lis) end, Listeners). diff --git a/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl b/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl index 66a7bd9e2..c00f76532 100644 --- a/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl +++ b/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl @@ -48,7 +48,7 @@ unreg() -> %%-------------------------------------------------------------------- on_gateway_load(_Gateway = #{ name := GwName, - rawconf := RawConf + config := Config }, Ctx) -> %% Handler @@ -58,13 +58,13 @@ on_gateway_load(_Gateway = #{ name := GwName, emqx_lwm2m_coap_resource, undefined ), %% Xml registry - {ok, _} = emqx_lwm2m_xml_object_db:start_link(maps:get(xml_dir, RawConf)), + {ok, _} = emqx_lwm2m_xml_object_db:start_link(maps:get(xml_dir, Config)), %% XXX: Self managed table? %% TODO: Improve it later {ok, _} = emqx_lwm2m_cm:start_link(), - Listeners = emqx_gateway_utils:normalize_rawconf(RawConf), + Listeners = emqx_gateway_utils:normalize_config(Config), ListenerPids = lists:map(fun(Lis) -> start_listener(GwName, Ctx, Lis) end, Listeners), @@ -86,7 +86,7 @@ on_gateway_update(NewGateway, OldGateway, GwState = #{ctx := Ctx}) -> end. on_gateway_unload(_Gateway = #{ name := GwName, - rawconf := RawConf + config := Config }, _GwState) -> %% XXX: lwm2m_coap_server_registry:remove_handler( @@ -94,7 +94,7 @@ on_gateway_unload(_Gateway = #{ name := GwName, emqx_lwm2m_coap_resource, undefined ), - Listeners = emqx_gateway_utils:normalize_rawconf(RawConf), + Listeners = emqx_gateway_utils:normalize_config(Config), lists:foreach(fun(Lis) -> stop_listener(GwName, Lis) end, Listeners). diff --git a/apps/emqx_gateway/src/mqttsn/emqx_sn_impl.erl b/apps/emqx_gateway/src/mqttsn/emqx_sn_impl.erl index 0eeb1b952..196818478 100644 --- a/apps/emqx_gateway/src/mqttsn/emqx_sn_impl.erl +++ b/apps/emqx_gateway/src/mqttsn/emqx_sn_impl.erl @@ -48,29 +48,29 @@ unreg() -> %%-------------------------------------------------------------------- on_gateway_load(_Gateway = #{ name := GwName, - rawconf := RawConf + config := Config }, Ctx) -> %% We Also need to start `emqx_sn_broadcast` & %% `emqx_sn_registry` process - case maps:get(broadcast, RawConf, false) of + case maps:get(broadcast, Config, false) of false -> ok; true -> %% FIXME: Port = 1884, - SnGwId = maps:get(gateway_id, RawConf, undefined), + SnGwId = maps:get(gateway_id, Config, undefined), _ = emqx_sn_broadcast:start_link(SnGwId, Port), ok end, - PredefTopics = maps:get(predefined, RawConf, []), + PredefTopics = maps:get(predefined, Config, []), {ok, RegistrySvr} = emqx_sn_registry:start_link(GwName, PredefTopics), - NRawConf = maps:without( + NConfig = maps:without( [broadcast, predefined], - RawConf#{registry => emqx_sn_registry:lookup_name(RegistrySvr)} + Config#{registry => emqx_sn_registry:lookup_name(RegistrySvr)} ), - Listeners = emqx_gateway_utils:normalize_rawconf(NRawConf), + Listeners = emqx_gateway_utils:normalize_config(NConfig), ListenerPids = lists:map(fun(Lis) -> start_listener(GwName, Ctx, Lis) @@ -93,9 +93,9 @@ on_gateway_update(NewGateway = #{name := GwName}, OldGateway, end. on_gateway_unload(_Insta = #{ name := GwName, - rawconf := RawConf + config := Config }, _GwState) -> - Listeners = emqx_gateway_utils:normalize_rawconf(RawConf), + Listeners = emqx_gateway_utils:normalize_config(Config), lists:foreach(fun(Lis) -> stop_listener(GwName, Lis) end, Listeners). diff --git a/apps/emqx_gateway/src/stomp/emqx_stomp_impl.erl b/apps/emqx_gateway/src/stomp/emqx_stomp_impl.erl index 28dedac26..fd7c0427a 100644 --- a/apps/emqx_gateway/src/stomp/emqx_stomp_impl.erl +++ b/apps/emqx_gateway/src/stomp/emqx_stomp_impl.erl @@ -50,10 +50,10 @@ unreg() -> %%-------------------------------------------------------------------- on_gateway_load(_Gateway = #{ name := GwName, - rawconf := RawConf + config := Config }, Ctx) -> - %% Step1. Fold the rawconfs to listeners - Listeners = emqx_gateway_utils:normalize_rawconf(RawConf), + %% Step1. Fold the config to listeners + Listeners = emqx_gateway_utils:normalize_config(Config), %% Step2. Start listeners or escokd:specs ListenerPids = lists:map(fun(Lis) -> start_listener(GwName, Ctx, Lis) @@ -78,9 +78,9 @@ on_gateway_update(NewGateway, OldGateway, GwState = #{ctx := Ctx}) -> end. on_gateway_unload(_Gateway = #{ name := GwName, - rawconf := RawConf + config := Config }, _GwState) -> - Listeners = emqx_gateway_utils:normalize_rawconf(RawConf), + Listeners = emqx_gateway_utils:normalize_config(Config), lists:foreach(fun(Lis) -> stop_listener(GwName, Lis) end, Listeners).