This commit is contained in:
anlicheng 2025-09-18 15:58:04 +08:00
parent 0fc6653e3b
commit 0c07deef0e
5 changed files with 33 additions and 47 deletions

View File

@ -178,8 +178,6 @@ retry_connect() ->
check_produce_result(ok) ->
true;
check_produce_result({ok, _}) ->
true;
check_produce_result({ok, _}) ->
false.

View File

@ -15,17 +15,17 @@
%% config.json,
handle_request("POST", "/container/push_config", _,
#{<<"uuid">> := UUID, <<"container_name">> := ContainerName, <<"config_json">> := ConfigJson, <<"timeout">> := Timeout0})
when is_binary(UUID), is_binary(ContainerName), is_binary(ConfigJson), is_integer(Timeout0) ->
#{<<"uuid">> := UUID, <<"container_name">> := ContainerName, <<"config">> := Config, <<"timeout">> := Timeout0})
when is_binary(UUID), is_binary(ContainerName), is_map(Config), is_integer(Timeout0) ->
%% ConfigJson是否是合法的json字符串
true = iot_util:is_json(ConfigJson),
true = iot_util:is_json(Config),
case iot_host:get_pid(UUID) of
undefined ->
{ok, 200, iot_util:json_error(-1, <<"host not found">>)};
Pid when is_pid(Pid) ->
Timeout = Timeout0 * 1000,
case iot_host:config_container(Pid, ContainerName, ConfigJson) of
case iot_host:config_container(Pid, ContainerName, Config) of
{ok, Ref} ->
case iot_host:await_reply(Ref, Timeout) of
{ok, Result} ->
@ -40,13 +40,14 @@ handle_request("POST", "/container/push_config", _,
%%
handle_request("POST", "/container/deploy", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId, <<"config">> := Config})
when is_binary(UUID), is_integer(TaskId), is_binary(Config) ->
when is_binary(UUID), is_integer(TaskId), is_map(Config) ->
case iot_host:get_pid(UUID) of
undefined ->
{ok, 200, iot_util:json_error(404, <<"host not found">>)};
Pid when is_pid(Pid) ->
case iot_host:deploy_container(Pid, TaskId, Config) of
ConfigBin = jiffy:encode(Config, [force_utf8]),
case iot_host:deploy_container(Pid, TaskId, ConfigBin) of
{ok, Ref} ->
case iot_host:await_reply(Ref, 5000) of
{ok, Result} ->

View File

@ -19,7 +19,7 @@ start(_StartType, _StartArgs) ->
start_http_server(),
%% tcp服务
tcp_server:start(),
start_tcp_server(),
iot_sup:start_link().
@ -63,6 +63,30 @@ start_http_server() ->
lager:debug("[http_server] the http server start at: ~p, pid is: ~p", [Port, Pid]).
%% tcp服务
start_tcp_server() ->
{ok, Props} = application:get_env(iot, tcp_server),
Acceptors = proplists:get_value(acceptors, Props, 50),
MaxConnections = proplists:get_value(max_connections, Props, 10240),
Backlog = proplists:get_value(backlog, Props, 1024),
Port = proplists:get_value(port, Props),
TransOpts = [
{tcp_options, [
binary,
{reuseaddr, true},
{active, false},
{packet, 4},
{nodelay, false},
{backlog, Backlog}
]},
{acceptors, Acceptors},
{max_connections, MaxConnections}
],
{ok, _} = esockd:open('iot/tcp_server', Port, TransOpts, {tcp_channel, start_link, []}),
lager:debug("[iot_app] the tcp server start at: ~p", [Port]).
-spec ensure_mnesia_schema() -> any().
ensure_mnesia_schema() ->
case mnesia:system_info(use_dir) of

View File

@ -97,7 +97,7 @@ config_container(Pid, ContainerName, ConfigJson) when is_pid(Pid), is_binary(Con
-spec deploy_container(Pid :: pid(), TaskId :: integer(), Config :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
deploy_container(Pid, TaskId, Config) when is_pid(Pid), is_integer(TaskId), is_binary(Config) ->
EncDeployBin = message_codec:encode(?MESSAGE_RPC_DEPLOY, #rpc_deploy{task_id = TaskId, config = Config}),
gen_statem:call(Pid, {rpc_call, self(), ?PUSH_DEPLOY, EncDeployBin}).
gen_statem:call(Pid, {rpc_call, self(), EncDeployBin}).
-spec start_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}.
start_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) ->

View File

@ -1,37 +0,0 @@
%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 08. 5 2025 12:58
%%%-------------------------------------------------------------------
-module(tcp_server).
-author("anlicheng").
%% API
-export([start/0]).
%% tcp服务
start() ->
{ok, Props} = application:get_env(iot, tcp_server),
Acceptors = proplists:get_value(acceptors, Props, 50),
MaxConnections = proplists:get_value(max_connections, Props, 10240),
Backlog = proplists:get_value(backlog, Props, 1024),
Port = proplists:get_value(port, Props),
TransOpts = [
{tcp_options, [
binary,
{reuseaddr, true},
{active, false},
{packet, 4},
{nodelay, false},
{backlog, Backlog}
]},
{acceptors, Acceptors},
{max_connections, MaxConnections}
],
{ok, _} = esockd:open('iot/tcp_server', Port, TransOpts, {tcp_channel, start_link, []}),
lager:debug("[iot_app] the tcp server start at: ~p", [Port]).