This commit is contained in:
anlicheng 2025-05-06 00:13:24 +08:00
parent d7da93b7e8
commit 3f5a13aa65
2 changed files with 76 additions and 254 deletions

View File

@ -2,21 +2,21 @@
%%% @author anlicheng %%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY> %%% @copyright (C) 2025, <COMPANY>
%%% @doc %%% @doc
%%% %%%
%%% @end %%% @end
%%% Created : 29. 4 2025 17:47 %%% Created : 06. 5 2025 00:01
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(efka_agent). -module(efka_agent).
-author("anlicheng"). -author("anlicheng").
-include("message_pb.hrl"). -include("message_pb.hrl").
-behaviour(gen_statem). -behaviour(gen_server).
%% API %% API
-export([start_link/0]). -export([start_link/0]).
%% gen_statem callbacks %% gen_server callbacks
-export([init/1, format_status/2, handle_event/4, terminate/3, code_change/4, callback_mode/0]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-define(SERVER, ?MODULE). -define(SERVER, ?MODULE).
@ -30,115 +30,123 @@
-define(STATE_ACTIVATED, activated). -define(STATE_ACTIVATED, activated).
-record(state, { -record(state, {
transport_pid :: undefined | pid() transport_pid :: undefined | pid(),
status = ?STATE_DENIED
}). }).
%%%=================================================================== %%%===================================================================
%%% API %%% API
%%%=================================================================== %%%===================================================================
%% @doc Creates a gen_statem process which calls Module:init/1 to %% @doc Spawns the server and registers the local name (unique)
%% initialize. To ensure a synchronized start-up procedure, this -spec(start_link() ->
%% function does not return until Module:init/1 has returned. {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() -> start_link() ->
gen_statem:start_link({local, ?SERVER}, ?MODULE, [], []). gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%%%=================================================================== %%%===================================================================
%%% gen_statem callbacks %%% gen_server callbacks
%%%=================================================================== %%%===================================================================
%% @private %% @private
%% @doc Whenever a gen_statem is started using gen_statem:start/[3,4] or %% @doc Initializes the server
%% gen_statem:start_link/[3,4], this function is called by the new -spec(init(Args :: term()) ->
%% process to initialize. {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) -> init([]) ->
erlang:process_flag(trap_exit, true), erlang:process_flag(trap_exit, true),
erlang:start_timer(0, self(), create_transport), erlang:start_timer(0, self(), create_transport),
{ok, ?STATE_DENIED, #state{}}. {ok, #state{}}.
%% @private %% @private
%% @doc This function is called by a gen_statem when it needs to find out %% @doc Handling call messages
%% the callback mode of the callback module. -spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
callback_mode() -> State :: #state{}) ->
handle_event_function. {reply, Reply :: term(), NewState :: #state{}} |
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_call(_Request, _From, State = #state{}) ->
{reply, ok, State}.
%% @private %% @private
%% @doc Called (1) whenever sys:get_status/1,2 is called by gen_statem or %% @doc Handling cast messages
%% (2) when gen_statem terminates abnormally. -spec(handle_cast(Request :: term(), State :: #state{}) ->
%% This callback is optional. {noreply, NewState :: #state{}} |
format_status(_Opt, [_PDict, _StateName, _State]) -> {noreply, NewState :: #state{}, timeout() | hibernate} |
Status = some_term, {stop, Reason :: term(), NewState :: #state{}}).
Status. handle_cast(_Request, State = #state{}) ->
{noreply, State}.
%% @private %% @private
%% @doc If callback_mode is handle_event_function, then whenever a %% @doc Handling all non call/cast messages
%% gen_statem receives an event from call/2, cast/2, or as a normal -spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
%% process message, this function is called. {noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
%% {stop, Reason :: term(), NewState :: #state{}}).
handle_event(info, {timeout, _, create_transport}, ?STATE_DENIED, State = #state{}) -> handle_info({timeout, _, create_transport}, State = #state{status = ?STATE_DENIED}) ->
{ok, Props} = application:get_env(efka, tls_server), {ok, Props} = application:get_env(efka, tls_server),
Host = proplists:get_value(host, Props), Host = proplists:get_value(host, Props),
Port = proplists:get_value(port, Props), Port = proplists:get_value(port, Props),
{ok, TransportPid} = efka_transport:start_link(self(), Host, Port), {ok, TransportPid} = efka_transport:start_link(self(), Host, Port),
efka_transport:connect(TransportPid), efka_transport:connect(TransportPid),
{next_state, ?STATE_CONNECTING, State#state{transport_pid = TransportPid}}; {noreply, State#state{status = ?STATE_CONNECTING, transport_pid = TransportPid}};
%% handle_info({connect_reply, Reply}, State = #state{status = ?STATE_CONNECTING, transport_pid = TransportPid}) ->
handle_event(info, {connect_reply, Reply}, ?STATE_CONNECTING, State = #state{transport_pid = TransportPid}) ->
case Reply of case Reply of
ok -> ok ->
efka_transport:auth_request(TransportPid, 5000), efka_transport:auth_request(TransportPid, 5000),
{next_state, ?STATE_AUTH, State}; {noreply, State#state{status = ?STATE_AUTH}};
{error, Reason} -> {error, Reason} ->
efka_logger:debug("[efka_agent] connect failed, error: ~p, pid: ~p", [Reason, TransportPid]), efka_logger:debug("[efka_agent] connect failed, error: ~p, pid: ~p", [Reason, TransportPid]),
efka_transport:stop(TransportPid), efka_transport:stop(TransportPid),
{next_state, ?STATE_DENIED, State} {noreply, ?STATE_DENIED, State#state{status = ?STATE_DENIED}}
end; end;
%% handle_info({auth_reply, {ok, #auth_reply{code = 1, message = Message, repository_url = RepositoryUrl}}}, State = #state{status = ?STATE_AUTH}) ->
handle_event(info, {auth_reply, {ok, #auth_reply{code = 1, message = Message, repository_url = RepositoryUrl}}}, ?STATE_AUTH, State) ->
efka_logger:debug("[efka_agent] auth failed, message: ~p, repository_url: ~p", [Message, RepositoryUrl]), efka_logger:debug("[efka_agent] auth failed, message: ~p, repository_url: ~p", [Message, RepositoryUrl]),
{next_state, ?STATE_ACTIVATED, State}; {noreply, State#state{status = ?STATE_ACTIVATED}};
handle_event(info, {auth_reply, {ok, #auth_reply{code = -1, message = Message}}}, ?STATE_AUTH, State) -> handle_info({auth_reply, {ok, #auth_reply{code = -1, message = Message}}}, State = #state{status = ?STATE_AUTH}) ->
%% agent不能推送数据给云端服务器agent %% agent不能推送数据给云端服务器agent
%% socket的连接状态需要维持 %% socket的连接状态需要维持
efka_logger:debug("[efka_agent] auth denied, message: ~p", [Message]), efka_logger:debug("[efka_agent] auth denied, message: ~p", [Message]),
{next_state, ?STATE_RESTRICTED, State}; {noreply, State#state{status = ?STATE_RESTRICTED}};
handle_event(info, {auth_reply, {ok, #auth_reply{code = -2, message = Message}}}, ?STATE_AUTH, State = #state{transport_pid = TransportPid}) -> handle_info({auth_reply, {ok, #auth_reply{code = -2, message = Message}}}, State = #state{transport_pid = TransportPid, status = ?STATE_AUTH}) ->
%% %%
efka_logger:debug("[efka_agent] auth failed, message: ~p", [Message]), efka_logger:debug("[efka_agent] auth failed, message: ~p", [Message]),
efka_transport:stop(TransportPid), efka_transport:stop(TransportPid),
{next_state, ?STATE_DENIED, State#state{transport_pid = undefined}}; {noreply, State#state{transport_pid = undefined, status = ?STATE_DENIED}};
handle_event(info, {auth_reply, {error, Reason}}, ?STATE_AUTH, State = #state{transport_pid = TransportPid}) -> handle_info({auth_reply, {error, Reason}}, State = #state{transport_pid = TransportPid, status = ?STATE_AUTH}) ->
efka_logger:debug("[efka_agent] auth_request failed, error: ~p", [Reason]), efka_logger:debug("[efka_agent] auth_request failed, error: ~p", [Reason]),
efka_transport:stop(TransportPid), efka_transport:stop(TransportPid),
{next_state, ?STATE_DENIED, State#state{transport_pid = undefined}}; {noreply, State#state{transport_pid = undefined, status = ?STATE_DENIED}};
%% %%
%% %%
%% TODO %% TODO
handle_event(info, {server_push_message, <<8:8, ActivatePush>>}, StateName, State = #state{transport_pid = TransportPid}) -> handle_info({server_push_message, <<8:8, ActivatePush>>}, State = #state{transport_pid = TransportPid, status = Status}) ->
#activate_push{auth = Auth} = message_pb:decode_msg(ActivatePush, activate_push), #activate_push{auth = Auth} = message_pb:decode_msg(ActivatePush, activate_push),
case {Auth, StateName} of case {Auth, Status} of
{true, ?STATE_ACTIVATED} -> {true, ?STATE_ACTIVATED} ->
{keep_state, State}; {noreply, State};
{true, ?STATE_DENIED} -> {true, ?STATE_DENIED} ->
%% , %% ,
efka_transport:auth_request(TransportPid, 5000), efka_transport:auth_request(TransportPid, 5000),
{next_state, ?STATE_AUTH, State}; {noreply, State#state{status = ?STATE_AUTH}};
{false, _} -> {false, _} ->
%% %%
{next_state, ?STATE_RESTRICTED, State} {noreply, State#state{status = ?STATE_RESTRICTED}}
end; end;
%% %%
handle_event(info, {server_push_message, PacketId, <<16:8, Directive>>}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid}) -> handle_info({server_push_message, PacketId, <<16:8, Directive>>}, State = #state{transport_pid = TransportPid, status = ?STATE_ACTIVATED}) ->
#topic_message{topic = Topic, content = Content} = message_pb:decode_msg(Directive, directive), #topic_message{topic = Topic, content = Content} = message_pb:decode_msg(Directive, directive),
efka_logger:debug("[efka_agent] get directive with packet_id: ~p, to device_uuid: ~p, content: ~p", [PacketId, Topic, Content]), efka_logger:debug("[efka_agent] get directive with packet_id: ~p, to device_uuid: ~p, content: ~p", [PacketId, Topic, Content]),
%% %%
case PacketId > 0 of case PacketId > 0 of
true -> true ->
@ -148,30 +156,34 @@ handle_event(info, {server_push_message, PacketId, <<16:8, Directive>>}, ?STATE_
efka_subscription:publish(Topic, Content) efka_subscription:publish(Topic, Content)
end, end,
{keep_state, State}; {noreply, State};
%% transport进程退出 %% transport进程退出
handle_event(info, {'EXIT', TransportPid, Reason}, _StateName, State = #state{transport_pid = TransportPid}) -> handle_info({'EXIT', TransportPid, Reason}, State = #state{transport_pid = TransportPid}) ->
efka_logger:debug("[efka_agent] transport pid: ~p, exit with reason: ~p", [TransportPid, Reason]), efka_logger:debug("[efka_agent] transport pid: ~p, exit with reason: ~p", [TransportPid, Reason]),
erlang:start_timer(500000, self(), create_transport), erlang:start_timer(500000, self(), create_transport),
{next_state, ?STATE_DENIED, State#state{transport_pid = undefined}}; {noreply, State#state{transport_pid = undefined, status = ?STATE_DENIED}};
handle_event(_EventType, _EventContent, _StateName, State = #state{}) -> handle_info(_Info, State = #state{}) ->
NextStateName = the_next_state_name, {noreply, State}.
{next_state, NextStateName, State}.
%% @private %% @private
%% @doc This function is called by a gen_statem when it is about to %% @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 %% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_statem terminates with %% necessary cleaning up. When it returns, the gen_server terminates
%% Reason. The return value is ignored. %% with Reason. The return value is ignored.
terminate(_Reason, _StateName, _State = #state{}) -> -spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
State :: #state{}) -> term()).
terminate(_Reason, _State = #state{}) ->
ok. ok.
%% @private %% @private
%% @doc Convert process state when code is changed %% @doc Convert process state when code is changed
code_change(_OldVsn, StateName, State = #state{}, _Extra) -> -spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
{ok, StateName, State}. Extra :: term()) ->
{ok, NewState :: #state{}} | {error, Reason :: term()}).
code_change(_OldVsn, State = #state{}, _Extra) ->
{ok, State}.
%%%=================================================================== %%%===================================================================
%%% Internal functions %%% Internal functions

View File

@ -1,190 +0,0 @@
%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 06. 5 2025 00:01
%%%-------------------------------------------------------------------
-module(efka_agent1).
-author("anlicheng").
-include("message_pb.hrl").
-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).
%% agent的状态 activated
-define(STATE_DENIED, denied).
-define(STATE_CONNECTING, connecting).
-define(STATE_AUTH, auth).
%%
-define(STATE_RESTRICTED, restricted).
%%
-define(STATE_ACTIVATED, activated).
-record(state, {
transport_pid :: undefined | pid(),
status = ?STATE_DENIED
}).
%%%===================================================================
%%% 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 :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) ->
erlang:process_flag(trap_exit, true),
erlang:start_timer(0, self(), create_transport),
{ok, #state{}}.
%% @private
%% @doc Handling call messages
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
State :: #state{}) ->
{reply, Reply :: term(), NewState :: #state{}} |
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_call(_Request, _From, State = #state{}) ->
{reply, ok, State}.
%% @private
%% @doc Handling cast messages
-spec(handle_cast(Request :: term(), State :: #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_cast(_Request, State = #state{}) ->
{noreply, State}.
%% @private
%% @doc Handling all non call/cast messages
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_info({timeout, _, create_transport}, State = #state{status = ?STATE_DENIED}) ->
{ok, Props} = application:get_env(efka, tls_server),
Host = proplists:get_value(host, Props),
Port = proplists:get_value(port, Props),
{ok, TransportPid} = efka_transport:start_link(self(), Host, Port),
efka_transport:connect(TransportPid),
{noreply, State#state{status = ?STATE_CONNECTING, transport_pid = TransportPid}};
handle_info({connect_reply, Reply}, State = #state{status = ?STATE_CONNECTING, transport_pid = TransportPid}) ->
case Reply of
ok ->
efka_transport:auth_request(TransportPid, 5000),
{noreply, State#state{status = ?STATE_AUTH}};
{error, Reason} ->
efka_logger:debug("[efka_agent] connect failed, error: ~p, pid: ~p", [Reason, TransportPid]),
efka_transport:stop(TransportPid),
{noreply, ?STATE_DENIED, State#state{status = ?STATE_DENIED}}
end;
handle_info({auth_reply, {ok, #auth_reply{code = 1, message = Message, repository_url = RepositoryUrl}}}, State = #state{status = ?STATE_AUTH}) ->
efka_logger:debug("[efka_agent] auth failed, message: ~p, repository_url: ~p", [Message, RepositoryUrl]),
{noreply, State#state{status = ?STATE_ACTIVATED}};
handle_info({auth_reply, {ok, #auth_reply{code = -1, message = Message}}}, State = #state{status = ?STATE_AUTH}) ->
%% agent不能推送数据给云端服务器agent
%% socket的连接状态需要维持
efka_logger:debug("[efka_agent] auth denied, message: ~p", [Message]),
{noreply, State#state{status = ?STATE_RESTRICTED}};
handle_info({auth_reply, {ok, #auth_reply{code = -2, message = Message}}}, State = #state{transport_pid = TransportPid, status = ?STATE_AUTH}) ->
%%
efka_logger:debug("[efka_agent] auth failed, message: ~p", [Message]),
efka_transport:stop(TransportPid),
{noreply, State#state{transport_pid = undefined, status = ?STATE_DENIED}};
handle_info({auth_reply, {error, Reason}}, State = #state{transport_pid = TransportPid, status = ?STATE_AUTH}) ->
efka_logger:debug("[efka_agent] auth_request failed, error: ~p", [Reason]),
efka_transport:stop(TransportPid),
{noreply, State#state{transport_pid = undefined, status = ?STATE_DENIED}};
%%
%%
%% TODO
handle_info({server_push_message, <<8:8, ActivatePush>>}, State = #state{transport_pid = TransportPid, status = Status}) ->
#activate_push{auth = Auth} = message_pb:decode_msg(ActivatePush, activate_push),
case {Auth, Status} of
{true, ?STATE_ACTIVATED} ->
{noreply, State};
{true, ?STATE_DENIED} ->
%% ,
efka_transport:auth_request(TransportPid, 5000),
{noreply, State#state{status = ?STATE_AUTH}};
{false, _} ->
%%
{noreply, State#state{status = ?STATE_RESTRICTED}}
end;
%%
handle_info({server_push_message, PacketId, <<16:8, Directive>>}, State = #state{transport_pid = TransportPid, status = ?STATE_ACTIVATED}) ->
#topic_message{topic = Topic, content = Content} = message_pb:decode_msg(Directive, directive),
efka_logger:debug("[efka_agent] get directive with packet_id: ~p, to device_uuid: ~p, content: ~p", [PacketId, Topic, Content]),
%%
case PacketId > 0 of
true ->
CallbackFun = fun(Response) -> is_process_alive(TransportPid) andalso efka_transport:response(TransportPid, PacketId, Response) end,
efka_subscription:publish(PacketId, Topic, Content, CallbackFun);
false ->
efka_subscription:publish(Topic, Content)
end,
{noreply, State};
%% transport进程退出
handle_info({'EXIT', TransportPid, Reason}, State = #state{transport_pid = TransportPid}) ->
efka_logger:debug("[efka_agent] transport pid: ~p, exit with reason: ~p", [TransportPid, Reason]),
erlang:start_timer(500000, self(), create_transport),
{noreply, State#state{transport_pid = undefined, status = ?STATE_DENIED}};
handle_info(_Info, State = #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 :: #state{}) -> term()).
terminate(_Reason, _State = #state{}) ->
ok.
%% @private
%% @doc Convert process state when code is changed
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
Extra :: term()) ->
{ok, NewState :: #state{}} | {error, Reason :: term()}).
code_change(_OldVsn, State = #state{}, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================