diff --git a/src/api/erotik.erl b/src/api/erotik.erl index 990cc3a..4934acf 100644 --- a/src/api/erotik.erl +++ b/src/api/erotik.erl @@ -16,13 +16,17 @@ -spec api_connect(string(), string(), integer(), string(), string()) -> {ok, pid()}. api_connect(Name, Host, Port, Login, Password) -> Config = [{host, Host}, {port, Port}, {login, Login}, {password, Password}], - me_connector:start_link(Name, {api, Config}). + {ok, Pid} = me_connector:start_link(Name, []), + ok = gen_server:call(Pid, {connect, {api, Config}}, 10 * 1000), + {ok, Pid}. %% same as api_connect, but with timeout in milliseconds -spec api_connect(string(), string(), integer(), string(), string(), integer()) -> {ok, pid()}. api_connect(Name, Host, Port, Login, Password, Timeout) -> Config = [{host, Host}, {port, Port}, {login, Login}, {password, Password}, {timeout, Timeout}], - me_connector:start_link(Name, {api, Config}). + {ok, Pid} = me_connector:start_link(Name, []), + ok = gen_server:call(Pid, {connect, {api, Config}}, 10 * 1000), + {ok, Pid}. -spec ssh_connect(string(), string(), integer(), string(), string()) -> {ok, pid()}. ssh_connect(Name, Host, Port, Login, Password) -> diff --git a/src/core/me_connector.erl b/src/core/me_connector.erl index 69891bc..f68f8e5 100644 --- a/src/core/me_connector.erl +++ b/src/core/me_connector.erl @@ -28,7 +28,7 @@ { socket :: port(), ssh_ref :: pid(), - channel_ids :: dict:dict() + channel_ids :: dict() }). %%%=================================================================== @@ -96,6 +96,11 @@ handle_call({command, List}, _From, State = #state{socket = S}) when S /= undefi handle_call({command, List}, From, State = #state{ssh_ref = SSHRef, channel_ids = Dict}) -> %working through ssh ChannelId = me_logic:send_command(SSHRef, List), {noreply, State#state{channel_ids = dict:append(ChannelId, From, Dict)}}; +handle_call({connect, {api, Config}}, _From, State) -> + {Host, Port, Login, Password, Timeout} = parse_params(Config), + % {ok, Socket} = gen_tcp:connect(Host, Port, [{active, false}], Timeout), %TODO change to active true and stream dencoding + {ok, Socket} = login(Login, Password, Host, Port, Timeout), + {reply, ok, State#state{socket = Socket}}; handle_call(disconnect, _From, State = #state{socket = Socket, ssh_ref = SShRef}) -> me_ssh:close(SShRef), me_api:close(Socket), @@ -118,11 +123,6 @@ handle_cast({connect, {ssh, Config}}, State) -> {Host, Port, Login, Password, Timeout} = parse_params(Config), {ok, SSHRef} = me_ssh:connect(Host, Port, Login, Password, Timeout), {noreply, State#state{ssh_ref = SSHRef, channel_ids = dict:new()}}; -handle_cast({connect, {api, Config}}, State) -> - {Host, Port, Login, Password, Timeout} = parse_params(Config), - {ok, Socket} = gen_tcp:connect(Host, Port, [{active, false}], Timeout), %TODO change to active true and stream dencoding - ok = me_logic:do_login(Socket, Login, Password), - {noreply, State#state{socket = Socket}}; handle_cast(halt, State) -> {stop, normal, State}; handle_cast(_Request, State) -> @@ -196,4 +196,17 @@ parse_params(Config) -> Login = proplists:get_value(login, Config), Password = proplists:get_value(password, Config), Timeout = proplists:get_value(timeout, Config, infinity), - {Host, Port, Login, Password, Timeout}. \ No newline at end of file + {Host, Port, Login, Password, Timeout}. + +login(Login, Password, Host, Port, Timeout) -> + {ok, Socket} = gen_tcp:connect(Host, Port, [{active, false}], Timeout), %TODO change to active true and stream dencoding + case me_logic:do_login(Socket, Login, Password) of + ok -> + io:format("Connected~n"), + {ok, Socket}; + err -> + ok = gen_tcp:close(Socket), + timer:sleep(1000), + io:format("Reconnecting~n"), + login(Login, Password, Host, Port, Timeout) + end. \ No newline at end of file diff --git a/src/core/me_logic.erl b/src/core/me_logic.erl index e8f1c66..f872da7 100644 --- a/src/core/me_logic.erl +++ b/src/core/me_logic.erl @@ -26,8 +26,12 @@ do_login(Socket, Login, Password) -> Hash = count_hash(Password, Salt), LoginRequest = form_login_sentence(Login, Hash), me_api:write_sentence(Socket, LoginRequest), - {done, ["!done"]} = me_api:read_sentence(Socket), - ok. + case me_api:read_sentence(Socket) of + {done, ["!done"]} -> + ok; + {trap, ["!trap", "=message=cannot log in"]} -> + err + end. %% @private get_salt({done, LoginGreeting}) ->