fix agent
This commit is contained in:
parent
2bfc195c56
commit
e886dce82d
@ -20,7 +20,9 @@
|
|||||||
-define(SERVER, ?MODULE).
|
-define(SERVER, ?MODULE).
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
|
host :: string(),
|
||||||
|
port :: integer(),
|
||||||
|
socket
|
||||||
}).
|
}).
|
||||||
|
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
@ -43,7 +45,13 @@ start_link() ->
|
|||||||
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
||||||
{stop, Reason :: term()} | ignore).
|
{stop, Reason :: term()} | ignore).
|
||||||
init([]) ->
|
init([]) ->
|
||||||
{ok, #state{}}.
|
{ok, Props} = application:get_env(efka, tls_server),
|
||||||
|
Host = proplists:get_value(host, Props),
|
||||||
|
Port = proplists:get_value(port, Props),
|
||||||
|
|
||||||
|
erlang:start_timer(0, self(), create_connection),
|
||||||
|
|
||||||
|
{ok, #state{host = Host, port = Port}}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc Handling call messages
|
%% @doc Handling call messages
|
||||||
@ -73,6 +81,30 @@ handle_cast(_Request, State = #state{}) ->
|
|||||||
{noreply, NewState :: #state{}} |
|
{noreply, NewState :: #state{}} |
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
|
handle_info({timeout, _, create_connection}, State = #state{host = Host, port = Port}) ->
|
||||||
|
case connect(Host, Port) of
|
||||||
|
{ok, Socket} ->
|
||||||
|
{noreply, State#state{socket = Socket}};
|
||||||
|
{error, Reason} ->
|
||||||
|
lager:debug("[efka_agent] create_connection get error: ~p", [Reason]),
|
||||||
|
retry_connect(),
|
||||||
|
{noreply, State#state{socket = undefined}}
|
||||||
|
end;
|
||||||
|
|
||||||
|
handle_info({ssl, Socket, Data}, State = #state{socket = Socket}) ->
|
||||||
|
lager:debug("[efka_agent] socket get message: ~p", [Data]),
|
||||||
|
retry_connect(),
|
||||||
|
{noreply, State#state{socket = undefined}};
|
||||||
|
|
||||||
|
handle_info({ssl_error, Socket, Reason}, State = #state{socket = Socket}) ->
|
||||||
|
lager:warning("[efka_agent] socket close with reason: ~p", [Reason]),
|
||||||
|
retry_connect(),
|
||||||
|
{noreply, State#state{socket = undefined}};
|
||||||
|
handle_info({ssl_closed, Socket}, State = #state{socket = Socket}) ->
|
||||||
|
lager:warning("[efka_agent] socket closed"),
|
||||||
|
retry_connect(),
|
||||||
|
{noreply, State#state{socket = undefined}};
|
||||||
|
|
||||||
handle_info(_Info, State = #state{}) ->
|
handle_info(_Info, State = #state{}) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
@ -97,3 +129,13 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
|
|||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
||||||
|
retry_connect() ->
|
||||||
|
erlang:start_timer(5000, self(), create_connection).
|
||||||
|
|
||||||
|
connect(Host, Port) when is_list(Host), is_integer(Port) ->
|
||||||
|
SslOptions = [
|
||||||
|
{verify, verify_none},
|
||||||
|
{active, true}
|
||||||
|
],
|
||||||
|
ssl:connect(Host, Port, SslOptions, 5000).
|
||||||
@ -2,7 +2,7 @@
|
|||||||
{efka, [
|
{efka, [
|
||||||
{root_dir, "/tmp/efka/"},
|
{root_dir, "/tmp/efka/"},
|
||||||
|
|
||||||
{wss_server, [
|
{tls_server, [
|
||||||
{host, "localhost"},
|
{host, "localhost"},
|
||||||
{port, 18080}
|
{port, 18080}
|
||||||
]}
|
]}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user