fix 消息格式

This commit is contained in:
anlicheng 2026-04-27 11:26:01 +08:00
parent bc0f906f66
commit 94d20f88f4
3 changed files with 22 additions and 4 deletions

View File

@ -125,9 +125,11 @@ remove_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName)
container_call(Pid, docker_container_builder:remove_request(ContainerName)).
-spec await_reply(Pid :: pid(), Ref :: reference(), Timeout :: integer()) ->
{ok, Result :: term()} | {error, Reason :: term()}.
ok | {ok, Result :: term()} | {error, Reason :: term()}.
await_reply(Pid, Ref, Timeout) when is_pid(Pid), is_reference(Ref), is_integer(Timeout) ->
receive
{request_reply, Ref, ok} ->
ok;
{request_reply, Ref, {ok, Result}} ->
{ok, Result};
{request_reply, Ref, {error, Reason}} ->

View File

@ -24,6 +24,8 @@ handle_request("GET", "/container/get_all", #{<<"uuid">> := UUID}, _) when is_bi
case iot_host:get_containers(Pid) of
{ok, Ref} ->
case iot_host:await_reply(Pid, Ref, ?REQ_TIMEOUT) of
ok ->
{ok, 200, request_success_response(<<"ok">>)};
{ok, Result} ->
{ok, 200, request_success_response(Result)};
{error, Reason} ->
@ -49,6 +51,8 @@ handle_request("POST", "/container/push_config", _,
case iot_host:config_container(Pid, ContainerName, Config) of
{ok, Ref} ->
case iot_host:await_reply(Pid, Ref, Timeout) of
ok ->
{ok, 200, request_success_response(<<"ok">>)};
{ok, Result} ->
{ok, 200, request_success_response(Result)};
{error, Reason} ->
@ -69,6 +73,8 @@ handle_request("POST", "/container/deploy", _, #{<<"uuid">> := UUID, <<"task_id"
case iot_host:deploy_container(Pid, TaskId, Config) of
{ok, Ref} ->
case iot_host:await_reply(Pid, Ref, ?REQ_TIMEOUT) of
ok ->
{ok, 200, request_success_response(<<"ok">>)};
{ok, Result} ->
{ok, 200, request_success_response(Result)};
{error, Reason} ->
@ -88,6 +94,8 @@ handle_request("POST", "/container/start", _, #{<<"uuid">> := UUID, <<"container
case iot_host:start_container(Pid, ContainerName) of
{ok, Ref} ->
case iot_host:await_reply(Pid, Ref, ?REQ_TIMEOUT) of
ok ->
{ok, 200, request_success_response(<<"ok">>)};
{ok, Result} ->
{ok, 200, request_success_response(Result)};
{error, Reason} ->
@ -107,6 +115,8 @@ handle_request("POST", "/container/stop", _, #{<<"uuid">> := UUID, <<"container_
case iot_host:stop_container(Pid, ContainerName) of
{ok, Ref} ->
case iot_host:await_reply(Pid, Ref, ?REQ_TIMEOUT) of
ok ->
{ok, 200, request_success_response(<<"ok">>)};
{ok, Result} ->
{ok, 200, request_success_response(Result)};
{error, Reason} ->
@ -125,6 +135,8 @@ handle_request("POST", "/container/kill", _, #{<<"uuid">> := UUID, <<"container_
case iot_host:kill_container(Pid, ContainerName) of
{ok, Ref} ->
case iot_host:await_reply(Pid, Ref, ?REQ_TIMEOUT) of
ok ->
{ok, 200, request_success_response(<<"ok">>)};
{ok, Result} ->
{ok, 200, request_success_response(Result)};
{error, Reason} ->
@ -144,6 +156,8 @@ handle_request("POST", "/container/remove", _, #{<<"uuid">> := UUID, <<"containe
case iot_host:remove_container(Pid, ContainerName) of
{ok, Ref} ->
case iot_host:await_reply(Pid, Ref, ?REQ_TIMEOUT) of
ok ->
{ok, 200, request_success_response(<<"ok">>)};
{ok, Result} ->
{ok, 200, request_success_response(Result)};
{error, Reason} ->

View File

@ -196,7 +196,7 @@ handle_request_frame(Ref,
case iot_host:attach_channel(HostPid, self()) of
ok ->
erlang:monitor(process, HostPid),
send_reply_frame(Transport, Socket, Ref, {auth_response, {ok, <<"ok">>}}),
send_reply_frame(Transport, Socket, Ref, {auth_response, ok}),
{noreply, State#state{uuid = UUID, host_pid = HostPid}};
{denied, Reason} when is_binary(Reason) ->
erlang:monitor(process, HostPid),
@ -265,8 +265,10 @@ send_reply_frame(Transport, Socket, Ref, Reply) ->
Packet = term_to_binary({response, Ref, Reply}),
Transport:send(Socket, Packet).
-spec decode_reply({container_response, {ok, term()} | {error, term()}} | tuple()) ->
{ok, term()} | {error, term()}.
-spec decode_reply({container_response, ok | {ok, term()} | {error, term()}} | tuple()) ->
ok | {ok, term()} | {error, term()}.
decode_reply({container_response, ok}) ->
ok;
decode_reply({container_response, {ok, Result}}) ->
{ok, Result};
decode_reply({container_response, {error, Reason}}) ->