%% Copyright -module(socket_utilites). -author("cheese"). -include("../../include/types_network.hrl"). -include_lib("kernel/include/logger.hrl"). %% API -export([get_name/1, get_name/2, timeout_seconds/1, prepare_l2l1_messages_from_bytes/2, our_list/1, parse_ip_from_bytes/1, parse_ip_from_list/4, parse_socket_info/1, send_message/3,get_gurrent_ip/0, ip_to_string/1]). -export([parse_socket_info/2, get_first_privat_ip/1]). %%--------------------------------------------------------- %% Prepare #socket_info from application settings %%--------------------------------------------------------- %%-spec(parseSocket(_) -> #socket_info{}). %%parseSocket([{type, ServerType}, {ip, ServerIP}, {port, ServerPort}, {name, Name}]) -> %% #socket_info{ip = ServerIP, port = ServerPort, type = ServerType, name = Name}. parse_socket_info(Arr) -> Info = parse_socket_info(Arr, #socket_info{}), case Info#socket_info.log_file of undefined -> LogFile="log/" ++ atom_to_list(Info#socket_info.name) ++ ".log", Info#socket_info{log_file = LogFile}; _ -> Info end. parse_socket_info([], Info) -> Info; parse_socket_info([{Key, Value} | Tail], Info) -> case Key of ip -> parse_socket_info(Tail, Info#socket_info{ip = Value}); port -> parse_socket_info(Tail, Info#socket_info{port = Value}); name -> parse_socket_info(Tail, Info#socket_info{name = Value}); log_file -> parse_socket_info(Tail, Info#socket_info{log_file = Value}); valid_ip -> parse_socket_info(Tail, Info#socket_info{valid_ip = Value}); _Other -> parse_socket_info(Tail, Info) end. %%--------------------------------------------------------- %% get name param from application setting %%--------------------------------------------------------- -spec(get_name([term()]) -> atom()). get_name([]) -> unknown; get_name([{Key, Val} | Tail]) -> case Key of name -> Val; _ -> get_name(Tail) end. %%--------------------------------------------------------- %% Prepare #socket_info from application settings %% and add prefix %%--------------------------------------------------------- -spec(get_name([term()], [term()]) -> atom()). get_name(List, Prefix) -> Name = get_name(List), Suffix = atom_to_list(Name), list_to_atom(Prefix ++ Suffix). timeout_seconds(Seconds) -> receive illegal_message -> ok after Seconds -> ok end. %% ------------------------------------------------------------------ %% Подготовить сообщения из "пакета" %% ------------------------------------------------------------------ prepare_l2l1_messages_from_bytes([], Res) -> Res; prepare_l2l1_messages_from_bytes([L1 | [L2 | Bytes]], Res) -> HeaderLen = L1 * 256 + L2, DataLen = erlang:length(Bytes), if HeaderLen > DataLen -> ?LOG_INFO("Header len: ~w, Data len: ~w. Message ignored: ~w", [HeaderLen, DataLen, Bytes]), Res; true -> Message = lists:sublist(Bytes, HeaderLen), Tail = lists:sublist(Bytes, HeaderLen + 1, DataLen - HeaderLen), prepare_l2l1_messages_from_bytes(Tail, [{erlang:list_to_binary([L1 | [L2 | Message]])} | Res]) end; prepare_l2l1_messages_from_bytes(Bytes, Res) -> ?LOG_ERROR("Message ignored [invalid size]: ~w", [Bytes]), Res. our_list(Pos) -> fun() -> {Pos, our_list(Pos + 1)} end. %% ------------------------------------------------------------------ %% %% ------------------------------------------------------------------ parse_ip_from_bytes(IP) -> [IP_1 | [IP_2 | [IP_3 | [IP_4 | _Other]]]] = parse_ip_from_list(lists:reverse(binary_to_list(IP)), [], 1, 0), {IP_1, IP_2, IP_3, IP_4}. %%<<49,48,46,53,53,46,49,46,56>> parse_ip_from_list([], Res, _Coef, IP) -> [IP | Res]; parse_ip_from_list([46 | Tail], Res, _Coef, IP) -> parse_ip_from_list(Tail, [IP | Res], 1, 0); parse_ip_from_list([Num | Tail], Res, Coef, IP) -> parse_ip_from_list(Tail, Res, Coef * 10, Coef * (Num - 48) + IP). send_message(Message, SocketClientPid, LogFile) -> case gen_tcp:send(SocketClientPid, Message) of ok -> ?LOG_INFO("Send [ok] to ~w ~w bytes: ~s~n", [SocketClientPid, erlang:byte_size(Message), bytes_extension:bin_to_hexstr(Message)], LogFile), ok; {error, Reason} -> ?LOG_INFO("Send [error: ~w] to ~w ~w bytes: ~s~n", [Reason, SocketClientPid, erlang:byte_size(Message), bytes_extension:bin_to_hexstr(Message)], LogFile), {error, Reason} end. get_gurrent_ip() -> {ok, IPArr} = inet:getif(), get_first_privat_ip(IPArr). get_first_privat_ip([]) -> undefined; get_first_privat_ip([{{10, _, _, _}= IP,_,_} | _Tail]) -> IP; get_first_privat_ip([_IP | Tail]) -> get_first_privat_ip(Tail). ip_to_string({IP1, IP2, IP3, IP4}) -> list_to_binary(io_lib:fwrite("~w.~w.~w.~w", [IP1, IP2, IP3, IP4])).