diff --git a/src/host/iot_host.erl b/src/host/iot_host.erl index d10254d..4682f04 100644 --- a/src/host/iot_host.erl +++ b/src/host/iot_host.erl @@ -95,44 +95,37 @@ attach_channel(Pid, ChannelPid) when is_pid(Pid), is_pid(ChannelPid) -> -spec get_containers(Pid :: pid()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. get_containers(Pid) when is_pid(Pid) -> - Request = #'JsonRpcRequest'{method = <<"get_containers">>, params = erlang:term_to_binary(#{} )}, - gen_statem:call(Pid, {jsonrpc_call, self(), Request}). + gen_statem:call(Pid, {jsonrpc_call, self(), {<<"get_containers">>, #{}}}). -spec config_container(Pid :: pid(), ContainerName :: binary(), ConfigJson :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. config_container(Pid, ContainerName, ConfigJson) when is_pid(Pid), is_binary(ContainerName), is_binary(ConfigJson) -> - Request = #'JsonRpcRequest'{method = <<"config_container">>, - params = erlang:term_to_binary(#{<<"container_name">> => ContainerName, <<"config">> => ConfigJson})}, - gen_statem:call(Pid, {jsonrpc_call, self(), Request}). + Params = #{<<"container_name">> => ContainerName, <<"config">> => ConfigJson}, + gen_statem:call(Pid, {jsonrpc_call, self(), {<<"config_container">>, Params}}). -spec deploy_container(Pid :: pid(), TaskId :: integer(), Config :: map()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. deploy_container(Pid, TaskId, Config) when is_pid(Pid), is_integer(TaskId), is_map(Config) -> - Request = #'JsonRpcRequest'{method = <<"deploy">>, - params = erlang:term_to_binary(#{<<"task_id">> => TaskId, <<"config">> => Config})}, - gen_statem:call(Pid, {jsonrpc_call, self(), Request}). + Params = #{<<"task_id">> => TaskId, <<"config">> => Config}, + gen_statem:call(Pid, {jsonrpc_call, self(), {<<"deploy">>, Params}}). -spec start_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. start_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) -> - Request = #'JsonRpcRequest'{method = <<"start_container">>, - params = erlang:term_to_binary(#{<<"container_name">> => ContainerName})}, - gen_statem:call(Pid, {jsonrpc_call, self(), Request}). + Params = #{<<"container_name">> => ContainerName}, + gen_statem:call(Pid, {jsonrpc_call, self(), {<<"start_container">>, Params}}). -spec stop_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. stop_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) -> - Request = #'JsonRpcRequest'{method = <<"stop_container">>, - params = erlang:term_to_binary(#{<<"container_name">> => ContainerName})}, - gen_statem:call(Pid, {jsonrpc_call, self(), Request}). + Params = #{<<"container_name">> => ContainerName}, + gen_statem:call(Pid, {jsonrpc_call, self(), {<<"stop_container">>, Params}}). -spec kill_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. kill_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) -> - Request = #'JsonRpcRequest'{method = <<"kill_container">>, - params = erlang:term_to_binary(#{<<"container_name">> => ContainerName})}, - gen_statem:call(Pid, {jsonrpc_call, self(), Request}). + Params = #{<<"container_name">> => ContainerName}, + gen_statem:call(Pid, {jsonrpc_call, self(), {<<"kill_container">>, Params}}). -spec remove_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. remove_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) -> - Request = #'JsonRpcRequest'{method = <<"remove_container">>, - params = erlang:term_to_binary(#{<<"container_name">> => ContainerName})}, - gen_statem:call(Pid, {jsonrpc_call, self(), Request}). + Params = #{<<"container_name">> => ContainerName}, + gen_statem:call(Pid, {jsonrpc_call, self(), {<<"remove_container">>, Params}}). -spec await_reply(Ref :: reference(), Timeout :: integer()) -> {ok, Result :: binary()} | {error, Reason :: binary()}. await_reply(Ref, Timeout) when is_reference(Ref), is_integer(Timeout) -> diff --git a/src/transport/tcp/tcp_channel.erl b/src/transport/tcp/tcp_channel.erl index 2c0c41f..b7a5513 100644 --- a/src/transport/tcp/tcp_channel.erl +++ b/src/transport/tcp/tcp_channel.erl @@ -44,8 +44,8 @@ command(Pid, CommandType, Command) when is_pid(Pid), is_integer(CommandType), is gen_server:cast(Pid, {command, CommandType, Command}). %% 向通道中写入消息 --spec jsonrpc_call(Pid :: pid(), ReceiverPid :: pid(), Request :: #'JsonRpcRequest'{}) -> Ref :: reference(). -jsonrpc_call(Pid, ReceiverPid, Request = #'JsonRpcRequest'{}) when is_pid(Pid), is_pid(ReceiverPid) -> +-spec jsonrpc_call(Pid :: pid(), ReceiverPid :: pid(), Request :: {Method :: binary(), Params :: any()}) -> Ref :: reference(). +jsonrpc_call(Pid, ReceiverPid, Request = {Method, _Params}) when is_pid(Pid), is_pid(ReceiverPid), is_binary(Method) -> Ref = make_ref(), gen_server:cast(Pid, {jsonrpc_call, ReceiverPid, Ref, Request}), Ref. @@ -90,7 +90,9 @@ handle_cast({command, CommandType, Command}, State = #state{transport = Transpor {noreply, State}; %% 推送消息 -handle_cast({jsonrpc_call, ReceiverPid, Ref, Request = #'JsonRpcRequest'{}}, State = #state{transport = Transport, socket = Socket, packet_id = PacketId, inflight = Inflight}) -> +handle_cast({jsonrpc_call, ReceiverPid, Ref, {Method, Params}}, State = #state{transport = Transport, socket = Socket, packet_id = PacketId, inflight = Inflight}) + when is_binary(Method) -> + Request = #'JsonRpcRequest'{method = Method, params = erlang:term_to_binary(Params)}, Encoded = message_pb:encode_msg(Request, 'JsonRpcRequest'), EncRequest = <>, Transport:send(Socket, <>),