diff --git a/apps/iot/src/iot_router.erl b/apps/iot/src/iot_router.erl new file mode 100644 index 0000000..c339616 --- /dev/null +++ b/apps/iot/src/iot_router.erl @@ -0,0 +1,98 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2023, +%%% @doc +%%% +%%% @end +%%% Created : 01. 3月 2023 16:03 +%%%------------------------------------------------------------------- +-module(iot_router). +-author("licheng5"). + +-behaviour(gen_server). + +%% API +-export([start_link/0]). + +%% gen_server callbacks +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, + code_change/3]). + +-define(SERVER, ?MODULE). + +-record(iot_router_state, {}). + +%%%=================================================================== +%%% API +%%%=================================================================== + +%% @doc Spawns the server and registers the local name (unique) +-spec(start_link() -> + {ok, Pid :: pid()} | ignore | {error, Reason :: term()}). +start_link() -> + gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). + +%%%=================================================================== +%%% gen_server callbacks +%%%=================================================================== + +%% @private +%% @doc Initializes the server +-spec(init(Args :: term()) -> + {ok, State :: #iot_router_state{}} | {ok, State :: #iot_router_state{}, timeout() | hibernate} | + {stop, Reason :: term()} | ignore). +init([]) -> + {ok, #iot_router_state{}}. + +%% @private +%% @doc Handling call messages +-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()}, + State :: #iot_router_state{}) -> + {reply, Reply :: term(), NewState :: #iot_router_state{}} | + {reply, Reply :: term(), NewState :: #iot_router_state{}, timeout() | hibernate} | + {noreply, NewState :: #iot_router_state{}} | + {noreply, NewState :: #iot_router_state{}, timeout() | hibernate} | + {stop, Reason :: term(), Reply :: term(), NewState :: #iot_router_state{}} | + {stop, Reason :: term(), NewState :: #iot_router_state{}}). +handle_call(_Request, _From, State = #iot_router_state{}) -> + {reply, ok, State}. + +%% @private +%% @doc Handling cast messages +-spec(handle_cast(Request :: term(), State :: #iot_router_state{}) -> + {noreply, NewState :: #iot_router_state{}} | + {noreply, NewState :: #iot_router_state{}, timeout() | hibernate} | + {stop, Reason :: term(), NewState :: #iot_router_state{}}). +handle_cast(_Request, State = #iot_router_state{}) -> + {noreply, State}. + +%% @private +%% @doc Handling all non call/cast messages +-spec(handle_info(Info :: timeout() | term(), State :: #iot_router_state{}) -> + {noreply, NewState :: #iot_router_state{}} | + {noreply, NewState :: #iot_router_state{}, timeout() | hibernate} | + {stop, Reason :: term(), NewState :: #iot_router_state{}}). +handle_info(_Info, State = #iot_router_state{}) -> + {noreply, State}. + +%% @private +%% @doc This function is called by a gen_server when it is about to +%% terminate. It should be the opposite of Module:init/1 and do any +%% necessary cleaning up. When it returns, the gen_server terminates +%% with Reason. The return value is ignored. +-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), + State :: #iot_router_state{}) -> term()). +terminate(_Reason, _State = #iot_router_state{}) -> + ok. + +%% @private +%% @doc Convert process state when code is changed +-spec(code_change(OldVsn :: term() | {down, term()}, State :: #iot_router_state{}, + Extra :: term()) -> + {ok, NewState :: #iot_router_state{}} | {error, Reason :: term()}). +code_change(_OldVsn, State = #iot_router_state{}, _Extra) -> + {ok, State}. + +%%%=================================================================== +%%% Internal functions +%%%=================================================================== diff --git a/apps/iot/src/iot_router_sup.erl b/apps/iot/src/iot_router_sup.erl new file mode 100644 index 0000000..56848a4 --- /dev/null +++ b/apps/iot/src/iot_router_sup.erl @@ -0,0 +1,61 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2023, +%%% @doc +%%% +%%% @end +%%% Created : 01. 3月 2023 16:01 +%%%------------------------------------------------------------------- +-module(iot_router_sup). +-author("licheng5"). + +-behaviour(supervisor). + +%% API +-export([start_link/0]). + +%% Supervisor callbacks +-export([init/1]). + +-define(SERVER, ?MODULE). + +%%%=================================================================== +%%% API functions +%%%=================================================================== + +%% @doc Starts the supervisor +-spec(start_link() -> {ok, Pid :: pid()} | ignore | {error, Reason :: term()}). +start_link() -> + supervisor:start_link({local, ?SERVER}, ?MODULE, []). + +%%%=================================================================== +%%% Supervisor callbacks +%%%=================================================================== + +%% @private +%% @doc Whenever a supervisor is started using supervisor:start_link/[2,3], +%% this function is called by the new process to find out about +%% restart strategy, maximum restart frequency and child +%% specifications. +-spec(init(Args :: term()) -> + {ok, {SupFlags :: {RestartStrategy :: supervisor:strategy(), + MaxR :: non_neg_integer(), MaxT :: non_neg_integer()}, + [ChildSpec :: supervisor:child_spec()]}} + | ignore | {error, Reason :: term()}). +init([]) -> + SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, + + AChild = #{ + id => 'iot_router', + start => {'iot_router', start_link, []}, + restart => permanent, + shutdown => 2000, + type => worker, + modules => ['iot_router'] + }, + + {ok, {SupFlags, [AChild]}}. + +%%%=================================================================== +%%% Internal functions +%%%=================================================================== diff --git a/apps/iot/src/iot_sup.erl b/apps/iot/src/iot_sup.erl index 2cddf90..3cf175d 100644 --- a/apps/iot/src/iot_sup.erl +++ b/apps/iot/src/iot_sup.erl @@ -26,10 +26,17 @@ start_link() -> %% type => worker(), % optional %% modules => modules()} % optional init([]) -> - SupFlags = #{strategy => one_for_all, - intensity => 0, - period => 1}, - ChildSpecs = [], + SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, + ChildSpecs = [ + #{ + id => 'iot_router_sup', + start => {'iot_router_sup', start_link, []}, + restart => permanent, + shutdown => 2000, + type => supervisor, + modules => ['iot_router_sup'] + } + ], {ok, {SupFlags, ChildSpecs}}. %% internal functions