141 lines
5.4 KiB
Erlang
141 lines
5.4 KiB
Erlang
%%%-------------------------------------------------------------------
|
|
%%% @author anlicheng
|
|
%%% @copyright (C) 2025, <COMPANY>
|
|
%%% @doc
|
|
%%%
|
|
%%% @end
|
|
%%% Created : 20. 4月 2025 18:47
|
|
%%%-------------------------------------------------------------------
|
|
-module(efka_agent).
|
|
-author("anlicheng").
|
|
-include("message_pb.hrl").
|
|
-include("efka.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).
|
|
|
|
-define(STATE_DENIED, denied).
|
|
-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{}) ->
|
|
case efka_transport:start_link(self()) of
|
|
{ok, TransportPid} ->
|
|
Ref = efka_transport:auth_request(TransportPid, 5000),
|
|
receive
|
|
%% 验证通过
|
|
{auth_reply, Ref, {ok, #auth_reply{code = 1, message = Message, repository_url = RepositoryUrl}}} ->
|
|
lager:debug("[efka_agent] auth result: ~p, repository_url: ~p", [Message, RepositoryUrl]),
|
|
{noreply, State#state{transport_pid = TransportPid, status = ?STATE_ACTIVATED}};
|
|
|
|
%% 主机denied状态
|
|
{auth_reply, Ref, {ok, #auth_reply{code = -1, message = Message, repository_url = RepositoryUrl}}} ->
|
|
lager:debug("[efka_agent] auth failed, message: ~p, repository_url: ~p", [Message, RepositoryUrl]),
|
|
{noreply, State};
|
|
|
|
%% 验证不通过
|
|
{auth_reply, Ref, {ok, #auth_reply{code = -2, message = Message, repository_url = RepositoryUrl}}} ->
|
|
lager:debug("[efka_agent] auth failed, message: ~p, repository_url: ~p", [Message, RepositoryUrl]),
|
|
{noreply, State};
|
|
|
|
{auth_reply, Ref, {error, Reason}} ->
|
|
lager:debug("[efka_agent] auth_request failed, error: ~p", [Reason]),
|
|
{noreply, State}
|
|
end;
|
|
{error, Reason} ->
|
|
lager:warning("[efka_agent] connect get error: ~p", [Reason]),
|
|
{noreply, State}
|
|
end;
|
|
|
|
handle_info({'EXIT', Pid, Reason}, State = #state{}) ->
|
|
lager:debug("[efka_agent] transport pid: ~p, exit with reason: ~p", [Pid, Reason]),
|
|
erlang:start_timer(5000, self(), create_transport),
|
|
{noreply, State#state{transport_pid = undefined}};
|
|
|
|
handle_info(Info, State = #state{}) ->
|
|
lager:debug("[efka_agent] get unknown info: ~p", [Info]),
|
|
{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
|
|
%%%=================================================================== |