From c9a4a0bdd3e962a815b5c4914b50f43805968502 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Thu, 8 May 2025 17:40:48 +0800 Subject: [PATCH] fix --- apps/efka/include/efka.hrl | 16 ++- apps/efka/include/message_pb.hrl | 15 +- apps/efka/src/efka_agent.erl | 8 +- apps/efka/src/efka_subscription.erl | 68 +-------- apps/efka/src/efka_transport.erl | 17 ++- apps/efka/src/efka_util.erl | 11 +- apps/efka/src/proto/message_pb.erl | 215 +++++++++++++++------------- message_pb.proto | 11 +- 8 files changed, 170 insertions(+), 191 deletions(-) diff --git a/apps/efka/include/efka.hrl b/apps/efka/include/efka.hrl index 176d777..8d58238 100644 --- a/apps/efka/include/efka.hrl +++ b/apps/efka/include/efka.hrl @@ -8,13 +8,21 @@ %%%------------------------------------------------------------------- -author("anlicheng"). -%% 消息体类型 +%% efka主动发起的消息体类型 -define(PACKET_REQUEST, 16#01). -define(PACKET_RESPONSE, 16#02). +%% 服务器基于pub/sub的消息 +-define(PACKET_PUB, 16#03). +%% push调用不需要返回 +-define(PACKET_COMMAND, 16#04). + %% 服务器端推送消息 --define(PACKET_PUBLISH, 16#03). --define(PACKET_PUBLISH_RESPONSE, 16#04). +-define(PACKET_ASYNC_REQUEST, 16#5). +-define(PACKET_ASYNC_RESPONSE, 16#6). + +%% 授权 +-define(COMMAND_AUTH, 16#8). %% 主机端上报数据类型标识 %% 建立到websocket的register关系 @@ -27,12 +35,10 @@ -define(METHOD_FEEDBACK_STEP, 16#05). -define(METHOD_EVENT, 16#07). - -define(METHOD_PHASE, 16#09). %% 部署微服务 -define(METHOD_DEPLOY, 16#10). --define(METHOD_CONFIG, 16#11). %% 事件类型 -define(EVENT_DEVICE, 16#01). diff --git a/apps/efka/include/message_pb.hrl b/apps/efka/include/message_pb.hrl index facbc2f..7de3eaf 100644 --- a/apps/efka/include/message_pb.hrl +++ b/apps/efka/include/message_pb.hrl @@ -27,13 +27,6 @@ }). -endif. --ifndef('ACTIVATE_PUSH_PB_H'). --define('ACTIVATE_PUSH_PB_H', true). --record(activate_push, - {auth = false :: boolean() | 0 | 1 | undefined % = 1, optional - }). --endif. - -ifndef('DEPLOY_PB_H'). -define('DEPLOY_PB_H', true). -record(deploy, @@ -60,6 +53,14 @@ }). -endif. +-ifndef('COMMAND_PB_H'). +-define('COMMAND_PB_H', true). +-record(command, + {topic = <<>> :: unicode:chardata() | undefined, % = 1, optional + content = <<>> :: unicode:chardata() | undefined % = 2, optional + }). +-endif. + -ifndef('ASYNC_REQUEST_PB_H'). -define('ASYNC_REQUEST_PB_H', true). -record(async_request, diff --git a/apps/efka/src/efka_agent.erl b/apps/efka/src/efka_agent.erl index 227ade9..5c79bc6 100644 --- a/apps/efka/src/efka_agent.erl +++ b/apps/efka/src/efka_agent.erl @@ -297,7 +297,7 @@ handle_info({timeout, _, {request_timeout, Ref}}, State = #state{inflight = Infl end; %% TODO -handle_info({server_push_message, <<8:8, ActivatePush/binary>>}, State = #state{transport_pid = TransportPid, status = Status}) -> +handle_info({command, <<8:8, ActivatePush/binary>>}, State = #state{transport_pid = TransportPid, status = Status}) -> #activate_push{auth = Auth} = message_pb:decode_msg(ActivatePush, activate_push), case {Auth, Status} of {true, ?STATE_ACTIVATED} -> @@ -312,9 +312,9 @@ handle_info({server_push_message, <<8:8, ActivatePush/binary>>}, State = #state{ end; %% 收到需要回复的指令 -handle_info({server_push_message, PacketId, <<16:8, Directive/binary>>}, State = #state{transport_pid = TransportPid, status = ?STATE_ACTIVATED}) -> - #topic_message{topic = Topic, content = Content} = message_pb:decode_msg(Directive, topic_message), - efka_logger:debug("[efka_agent] get directive with packet_id: ~p, to device_uuid: ~p, content: ~p", [PacketId, Topic, Content]), +handle_info({pub, PubBin}, State = #state{transport_pid = TransportPid, status = ?STATE_ACTIVATED}) -> + #pub{topic = Topic, content = Content} = message_pb:decode_msg(PubBin, pub), + efka_logger:debug("[efka_agent] get pub topic: ~p, content: ~p", [Topic, Content]), %% 消息发送到订阅系统 case PacketId > 0 of true -> diff --git a/apps/efka/src/efka_subscription.erl b/apps/efka/src/efka_subscription.erl index c9f9e76..d43b3cb 100644 --- a/apps/efka/src/efka_subscription.erl +++ b/apps/efka/src/efka_subscription.erl @@ -13,7 +13,7 @@ %% API -export([start_link/0]). --export([subscribe/2, publish/4, publish/2, response/2]). +-export([subscribe/2, publish/2]). -export([match_components/2, is_valid_components/1, of_components/1]). %% gen_server callbacks @@ -34,10 +34,7 @@ }). -record(state, { - subscribers = [], - %% #{sha_uuid => fun(Response)}; - %% 为什么不采用packet_id来作为映射关系是因为 efka_agent建立新的efka_transport进程的时候packet_id会重置; 会导致映射关系错误 - callbacks = #{} + subscribers = [] }). %%%=================================================================== @@ -52,14 +49,6 @@ subscribe(Topic, SubscriberPid) when is_binary(Topic), is_pid(SubscriberPid) -> publish(Topic, Content) when is_binary(Topic), is_binary(Content) -> gen_server:cast(?SERVER, {publish, Topic, Content}). --spec publish(PacketId :: integer(), Topic :: binary(), Content :: binary(), CallbackFun :: fun((A :: binary()) -> term()) ) -> no_return(). -publish(PacketId, Topic, Content, CallbackFun) when is_integer(PacketId), is_binary(Topic), is_binary(Content) -> - gen_server:cast(?SERVER, {publish, PacketId, Topic, Content, CallbackFun}). - --spec response(ShaUUID :: binary(), Response :: binary()) -> no_return(). -response(ShaUUID, Response) when is_binary(ShaUUID), is_binary(Response) -> - gen_server:cast(?SERVER, {response, ShaUUID, Response}). - %% @doc Spawns the server and registers the local name (unique) -spec(start_link() -> {ok, Pid :: pid()} | ignore | {error, Reason :: term()}). @@ -116,38 +105,11 @@ handle_cast({publish, Topic, Content}, State = #state{subscribers = Subscribers} %% 不需要回复的消息体采用广播的信息 MatchedSubscribers = match_subscribers(Subscribers, Topic), lists:foreach(fun(#subscriber{subscriber_pid = SubscriberPid}) -> - erlang:send(SubscriberPid, {topic_broadcast, Content}) + SubscriberPid ! {topic_broadcast, Content} end, MatchedSubscribers), efka_logger:debug("[efka_subscription] topic: ~p, content: ~p, match subscribers: ~p", [Topic, Content, MatchedSubscribers]), - {noreply, State}; - -%% 需要响应的推送 -handle_cast({publish, PacketId, Topic, Content, CallbackFun}, State = #state{subscribers = Subscribers, callbacks = Callbacks}) when PacketId > 0 -> - ShaUUID = sha_uuid(), - - %% 基于优先级 - MatchedSubscribers = match_subscribers(Subscribers, Topic), - SortedSubscribers = lists:sort(fun(#subscriber{order = Order0}, #subscriber{order = Order1}) -> Order0 < Order1 end, MatchedSubscribers), - case SortedSubscribers of - [] -> - ok; - [S = #subscriber{subscriber_pid = SubscriberPid} | _] -> - efka_logger:debug("[efka_subscription] topic_sink topic: ~p, content: ~p, match subscriber: ~p", [Topic, Content, S]), - erlang:send(SubscriberPid, {topic_sink, Content}) - end, - - {noreply, State#state{callbacks = maps:put(ShaUUID, CallbackFun, Callbacks)}}; - -%% 消息回复 -handle_cast({response, ShaUUID, Response}, State = #state{callbacks = Callbacks}) -> - case maps:take(ShaUUID, Callbacks) of - {F, NCallbacks} -> - F(Response), - {noreply, State#state{callbacks = NCallbacks}}; - error -> - {noreply, State} - end. + {noreply, State}. %% @private %% @doc Handling all non call/cast messages @@ -186,19 +148,6 @@ code_change(_OldVsn, State = #state{}, _Extra) -> %%% Internal functions %%%=================================================================== --spec dispatch(Subscribers :: list(), ShaUUID :: binary(), Topic :: binary(), Content :: binary()) -> [pid()]. -dispatch(Subscribers, ShaUUID, Topic, Content) when is_list(Subscribers), is_binary(Topic), is_binary(Content) -> - Components = of_components(Topic), - lists:foldl(fun(#subscriber{components = Components0, subscriber_pid = SubscriberPid}, AccPids) -> - case match_components(Components0, Components) andalso not lists:member(SubscriberPid, AccPids) of - true -> - erlang:send(SubscriberPid, {topic_sink, ShaUUID, Content}), - [SubscriberPid|AccPids]; - false -> - AccPids - end - end, [], Subscribers). - %% 查找满足条件订阅者 -spec match_subscribers(Subscribers :: [#subscriber{}], Topic :: binary()) -> [#subscriber{}]. match_subscribers(Subscribers, Topic) when is_list(Subscribers), is_binary(Topic) -> @@ -253,11 +202,4 @@ order_num([<<$*>>|_]) -> order_num([<<$+>>|_]) -> 3; order_num([_|Tail]) -> - order_num(Tail). - -%% 生产唯一的uuid --spec sha_uuid() -> binary(). -sha_uuid() -> - Salt = crypto:strong_rand_bytes(32), - Str = string:lowercase(binary:encode_hex(crypto:hash(sha256, Salt))), - binary:part(Str, 1, 32). \ No newline at end of file + order_num(Tail). \ No newline at end of file diff --git a/apps/efka/src/efka_transport.erl b/apps/efka/src/efka_transport.erl index f6aeb62..e16a974 100644 --- a/apps/efka/src/efka_transport.erl +++ b/apps/efka/src/efka_transport.erl @@ -139,7 +139,7 @@ handle_cast({send, Method, Packet}, State = #state{socket = Socket}) -> {noreply, State}; handle_cast({response, PacketId, Response}, State = #state{socket = Socket}) -> - ok = ssl:send(Socket, <>), + ok = ssl:send(Socket, <>), {noreply, State}. %% @private @@ -149,15 +149,20 @@ handle_cast({response, PacketId, Response}, State = #state{socket = Socket}) -> {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). %% 服务器主动推送的数据,有packetId的是要求返回的;为0的表示不需要返回值 -handle_info({ssl, Socket, <>}, State = #state{socket = Socket, parent_pid = ParentPid}) -> - efka_logger:debug("[efka_agent] socket get message: ~p", [Msg]), - ParentPid ! {server_push_message, Msg}, +handle_info({ssl, Socket, <>}, State = #state{socket = Socket, parent_pid = ParentPid}) -> + efka_logger:debug("[efka_agent] socket get command: ~p", [Command]), + ParentPid ! {command, Command}, + {noreply, State}; + +handle_info({ssl, Socket, <>}, State = #state{socket = Socket, parent_pid = ParentPid}) -> + efka_logger:debug("[efka_agent] socket get pub: ~p", [PubBin]), + ParentPid ! {pub, PubBin}, {noreply, State}; %% 目前推送的消息包括: <>, <<16:8, Directive/binary>> -handle_info({ssl, Socket, <>}, State = #state{socket = Socket, parent_pid = ParentPid}) -> +handle_info({ssl, Socket, <>}, State = #state{socket = Socket, parent_pid = ParentPid}) -> efka_logger:debug("[efka_agent] socket get message: ~p", [Msg]), - ParentPid ! {server_push_message, PacketId, Msg}, + ParentPid ! {async_request, PacketId, Msg}, {noreply, State#state{}}; handle_info({ssl_error, Socket, _Reason}, State = #state{socket = Socket}) -> diff --git a/apps/efka/src/efka_util.erl b/apps/efka/src/efka_util.erl index 8e5578b..fbe0e54 100644 --- a/apps/efka/src/efka_util.erl +++ b/apps/efka/src/efka_util.erl @@ -12,7 +12,7 @@ %% API -export([get_file_md5/1]). -export([timestamp/0, number_format/2, timestamp_ms/0, float_to_binary/2, int_format/2]). --export([chunks/2, rand_bytes/1, uuid/0, md5/1]). +-export([chunks/2, rand_bytes/1, uuid/0, md5/1, sha_uuid/0]). -export([json_data/1, json_error/2]). get_file_md5(FilePath) when is_list(FilePath) -> @@ -95,4 +95,11 @@ float_to_binary(V, _) when is_integer(V) -> integer_to_binary(V); float_to_binary(V, Decimals) when is_float(V), is_integer(Decimals) -> S = float_to_list(V, [{decimals, Decimals}, compact]), - list_to_binary(S). \ No newline at end of file + list_to_binary(S). + +%% 生产唯一的uuid +-spec sha_uuid() -> binary(). +sha_uuid() -> + Salt = crypto:strong_rand_bytes(32), + Str = string:lowercase(binary:encode_hex(crypto:hash(sha256, Salt))), + binary:part(Str, 1, 32). \ No newline at end of file diff --git a/apps/efka/src/proto/message_pb.erl b/apps/efka/src/proto/message_pb.erl index 09155b9..0bbc22e 100644 --- a/apps/efka/src/proto/message_pb.erl +++ b/apps/efka/src/proto/message_pb.erl @@ -59,14 +59,14 @@ -type auth_reply() :: #auth_reply{}. --type activate_push() :: #activate_push{}. - -type deploy() :: #deploy{}. -type efka_response() :: #efka_response{}. -type pub() :: #pub{}. +-type command() :: #command{}. + -type async_request() :: #async_request{}. -type async_response() :: #async_response{}. @@ -83,9 +83,9 @@ -type event() :: #event{}. --export_type(['auth_request'/0, 'auth_reply'/0, 'activate_push'/0, 'deploy'/0, 'efka_response'/0, 'pub'/0, 'async_request'/0, 'async_response'/0, 'service_config'/0, 'data'/0, 'ping'/0, 'service_inform'/0, 'feedback_phase'/0, 'event'/0]). --type '$msg_name'() :: auth_request | auth_reply | activate_push | deploy | efka_response | pub | async_request | async_response | service_config | data | ping | service_inform | feedback_phase | event. --type '$msg'() :: auth_request() | auth_reply() | activate_push() | deploy() | efka_response() | pub() | async_request() | async_response() | service_config() | data() | ping() | service_inform() | feedback_phase() | event(). +-export_type(['auth_request'/0, 'auth_reply'/0, 'deploy'/0, 'efka_response'/0, 'pub'/0, 'command'/0, 'async_request'/0, 'async_response'/0, 'service_config'/0, 'data'/0, 'ping'/0, 'service_inform'/0, 'feedback_phase'/0, 'event'/0]). +-type '$msg_name'() :: auth_request | auth_reply | deploy | efka_response | pub | command | async_request | async_response | service_config | data | ping | service_inform | feedback_phase | event. +-type '$msg'() :: auth_request() | auth_reply() | deploy() | efka_response() | pub() | command() | async_request() | async_response() | service_config() | data() | ping() | service_inform() | feedback_phase() | event(). -export_type(['$msg_name'/0, '$msg'/0]). -if(?OTP_RELEASE >= 24). @@ -114,10 +114,10 @@ encode_msg(Msg, MsgName, Opts) -> case MsgName of auth_request -> encode_msg_auth_request(id(Msg, TrUserData), TrUserData); auth_reply -> encode_msg_auth_reply(id(Msg, TrUserData), TrUserData); - activate_push -> encode_msg_activate_push(id(Msg, TrUserData), TrUserData); deploy -> encode_msg_deploy(id(Msg, TrUserData), TrUserData); efka_response -> encode_msg_efka_response(id(Msg, TrUserData), TrUserData); pub -> encode_msg_pub(id(Msg, TrUserData), TrUserData); + command -> encode_msg_command(id(Msg, TrUserData), TrUserData); async_request -> encode_msg_async_request(id(Msg, TrUserData), TrUserData); async_response -> encode_msg_async_response(id(Msg, TrUserData), TrUserData); service_config -> encode_msg_service_config(id(Msg, TrUserData), TrUserData); @@ -207,20 +207,6 @@ encode_msg_auth_reply(#auth_reply{code = F1, message = F2}, Bin, TrUserData) -> end end. -encode_msg_activate_push(Msg, TrUserData) -> encode_msg_activate_push(Msg, <<>>, TrUserData). - - -encode_msg_activate_push(#activate_push{auth = F1}, Bin, TrUserData) -> - if F1 == undefined -> Bin; - true -> - begin - TrF1 = id(F1, TrUserData), - if TrF1 =:= false -> Bin; - true -> e_type_bool(TrF1, <>, TrUserData) - end - end - end. - encode_msg_deploy(Msg, TrUserData) -> encode_msg_deploy(Msg, <<>>, TrUserData). @@ -314,6 +300,31 @@ encode_msg_pub(#pub{topic = F1, content = F2}, Bin, TrUserData) -> end end. +encode_msg_command(Msg, TrUserData) -> encode_msg_command(Msg, <<>>, TrUserData). + + +encode_msg_command(#command{topic = F1, content = F2}, Bin, TrUserData) -> + B1 = if F1 == undefined -> Bin; + true -> + begin + TrF1 = id(F1, TrUserData), + case is_empty_string(TrF1) of + true -> Bin; + false -> e_type_string(TrF1, <>, TrUserData) + end + end + end, + if F2 == undefined -> B1; + true -> + begin + TrF2 = id(F2, TrUserData), + case is_empty_string(TrF2) of + true -> B1; + false -> e_type_string(TrF2, <>, TrUserData) + end + end + end. + encode_msg_async_request(Msg, TrUserData) -> encode_msg_async_request(Msg, <<>>, TrUserData). @@ -843,10 +854,10 @@ decode_msg_1_catch(Bin, MsgName, TrUserData) -> decode_msg_2_doit(auth_request, Bin, TrUserData) -> id(decode_msg_auth_request(Bin, TrUserData), TrUserData); decode_msg_2_doit(auth_reply, Bin, TrUserData) -> id(decode_msg_auth_reply(Bin, TrUserData), TrUserData); -decode_msg_2_doit(activate_push, Bin, TrUserData) -> id(decode_msg_activate_push(Bin, TrUserData), TrUserData); decode_msg_2_doit(deploy, Bin, TrUserData) -> id(decode_msg_deploy(Bin, TrUserData), TrUserData); decode_msg_2_doit(efka_response, Bin, TrUserData) -> id(decode_msg_efka_response(Bin, TrUserData), TrUserData); decode_msg_2_doit(pub, Bin, TrUserData) -> id(decode_msg_pub(Bin, TrUserData), TrUserData); +decode_msg_2_doit(command, Bin, TrUserData) -> id(decode_msg_command(Bin, TrUserData), TrUserData); decode_msg_2_doit(async_request, Bin, TrUserData) -> id(decode_msg_async_request(Bin, TrUserData), TrUserData); decode_msg_2_doit(async_response, Bin, TrUserData) -> id(decode_msg_async_response(Bin, TrUserData), TrUserData); decode_msg_2_doit(service_config, Bin, TrUserData) -> id(decode_msg_service_config(Bin, TrUserData), TrUserData); @@ -981,50 +992,6 @@ skip_32_auth_reply(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> skip_64_auth_reply(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dfp_read_field_def_auth_reply(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData). -decode_msg_activate_push(Bin, TrUserData) -> dfp_read_field_def_activate_push(Bin, 0, 0, 0, id(false, TrUserData), TrUserData). - -dfp_read_field_def_activate_push(<<8, Rest/binary>>, Z1, Z2, F, F@_1, TrUserData) -> d_field_activate_push_auth(Rest, Z1, Z2, F, F@_1, TrUserData); -dfp_read_field_def_activate_push(<<>>, 0, 0, _, F@_1, _) -> #activate_push{auth = F@_1}; -dfp_read_field_def_activate_push(Other, Z1, Z2, F, F@_1, TrUserData) -> dg_read_field_def_activate_push(Other, Z1, Z2, F, F@_1, TrUserData). - -dg_read_field_def_activate_push(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, TrUserData) when N < 32 - 7 -> dg_read_field_def_activate_push(Rest, N + 7, X bsl N + Acc, F, F@_1, TrUserData); -dg_read_field_def_activate_push(<<0:1, X:7, Rest/binary>>, N, Acc, _, F@_1, TrUserData) -> - Key = X bsl N + Acc, - case Key of - 8 -> d_field_activate_push_auth(Rest, 0, 0, 0, F@_1, TrUserData); - _ -> - case Key band 7 of - 0 -> skip_varint_activate_push(Rest, 0, 0, Key bsr 3, F@_1, TrUserData); - 1 -> skip_64_activate_push(Rest, 0, 0, Key bsr 3, F@_1, TrUserData); - 2 -> skip_length_delimited_activate_push(Rest, 0, 0, Key bsr 3, F@_1, TrUserData); - 3 -> skip_group_activate_push(Rest, 0, 0, Key bsr 3, F@_1, TrUserData); - 5 -> skip_32_activate_push(Rest, 0, 0, Key bsr 3, F@_1, TrUserData) - end - end; -dg_read_field_def_activate_push(<<>>, 0, 0, _, F@_1, _) -> #activate_push{auth = F@_1}. - -d_field_activate_push_auth(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, TrUserData) when N < 57 -> d_field_activate_push_auth(Rest, N + 7, X bsl N + Acc, F, F@_1, TrUserData); -d_field_activate_push_auth(<<0:1, X:7, Rest/binary>>, N, Acc, F, _, TrUserData) -> - {NewFValue, RestF} = {id(X bsl N + Acc =/= 0, TrUserData), Rest}, - dfp_read_field_def_activate_push(RestF, 0, 0, F, NewFValue, TrUserData). - -skip_varint_activate_push(<<1:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, TrUserData) -> skip_varint_activate_push(Rest, Z1, Z2, F, F@_1, TrUserData); -skip_varint_activate_push(<<0:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, TrUserData) -> dfp_read_field_def_activate_push(Rest, Z1, Z2, F, F@_1, TrUserData). - -skip_length_delimited_activate_push(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, TrUserData) when N < 57 -> skip_length_delimited_activate_push(Rest, N + 7, X bsl N + Acc, F, F@_1, TrUserData); -skip_length_delimited_activate_push(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, TrUserData) -> - Length = X bsl N + Acc, - <<_:Length/binary, Rest2/binary>> = Rest, - dfp_read_field_def_activate_push(Rest2, 0, 0, F, F@_1, TrUserData). - -skip_group_activate_push(Bin, _, Z2, FNum, F@_1, TrUserData) -> - {_, Rest} = read_group(Bin, FNum), - dfp_read_field_def_activate_push(Rest, 0, Z2, FNum, F@_1, TrUserData). - -skip_32_activate_push(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, TrUserData) -> dfp_read_field_def_activate_push(Rest, Z1, Z2, F, F@_1, TrUserData). - -skip_64_activate_push(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, TrUserData) -> dfp_read_field_def_activate_push(Rest, Z1, Z2, F, F@_1, TrUserData). - decode_msg_deploy(Bin, TrUserData) -> dfp_read_field_def_deploy(Bin, 0, 0, 0, id(0, TrUserData), id(<<>>, TrUserData), id(<<>>, TrUserData), TrUserData). dfp_read_field_def_deploy(<<8, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData) -> d_field_deploy_task_id(Rest, Z1, Z2, F, F@_1, F@_2, F@_3, TrUserData); @@ -1192,6 +1159,57 @@ skip_32_pub(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dfp_rea skip_64_pub(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dfp_read_field_def_pub(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData). +decode_msg_command(Bin, TrUserData) -> dfp_read_field_def_command(Bin, 0, 0, 0, id(<<>>, TrUserData), id(<<>>, TrUserData), TrUserData). + +dfp_read_field_def_command(<<10, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> d_field_command_topic(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData); +dfp_read_field_def_command(<<18, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> d_field_command_content(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData); +dfp_read_field_def_command(<<>>, 0, 0, _, F@_1, F@_2, _) -> #command{topic = F@_1, content = F@_2}; +dfp_read_field_def_command(Other, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dg_read_field_def_command(Other, Z1, Z2, F, F@_1, F@_2, TrUserData). + +dg_read_field_def_command(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, TrUserData) when N < 32 - 7 -> dg_read_field_def_command(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, TrUserData); +dg_read_field_def_command(<<0:1, X:7, Rest/binary>>, N, Acc, _, F@_1, F@_2, TrUserData) -> + Key = X bsl N + Acc, + case Key of + 10 -> d_field_command_topic(Rest, 0, 0, 0, F@_1, F@_2, TrUserData); + 18 -> d_field_command_content(Rest, 0, 0, 0, F@_1, F@_2, TrUserData); + _ -> + case Key band 7 of + 0 -> skip_varint_command(Rest, 0, 0, Key bsr 3, F@_1, F@_2, TrUserData); + 1 -> skip_64_command(Rest, 0, 0, Key bsr 3, F@_1, F@_2, TrUserData); + 2 -> skip_length_delimited_command(Rest, 0, 0, Key bsr 3, F@_1, F@_2, TrUserData); + 3 -> skip_group_command(Rest, 0, 0, Key bsr 3, F@_1, F@_2, TrUserData); + 5 -> skip_32_command(Rest, 0, 0, Key bsr 3, F@_1, F@_2, TrUserData) + end + end; +dg_read_field_def_command(<<>>, 0, 0, _, F@_1, F@_2, _) -> #command{topic = F@_1, content = F@_2}. + +d_field_command_topic(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, TrUserData) when N < 57 -> d_field_command_topic(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, TrUserData); +d_field_command_topic(<<0:1, X:7, Rest/binary>>, N, Acc, F, _, F@_2, TrUserData) -> + {NewFValue, RestF} = begin Len = X bsl N + Acc, <> = Rest, Bytes2 = binary:copy(Bytes), {id(Bytes2, TrUserData), Rest2} end, + dfp_read_field_def_command(RestF, 0, 0, F, NewFValue, F@_2, TrUserData). + +d_field_command_content(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, TrUserData) when N < 57 -> d_field_command_content(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, TrUserData); +d_field_command_content(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, _, TrUserData) -> + {NewFValue, RestF} = begin Len = X bsl N + Acc, <> = Rest, Bytes2 = binary:copy(Bytes), {id(Bytes2, TrUserData), Rest2} end, + dfp_read_field_def_command(RestF, 0, 0, F, F@_1, NewFValue, TrUserData). + +skip_varint_command(<<1:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> skip_varint_command(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData); +skip_varint_command(<<0:1, _:7, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dfp_read_field_def_command(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData). + +skip_length_delimited_command(<<1:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, TrUserData) when N < 57 -> skip_length_delimited_command(Rest, N + 7, X bsl N + Acc, F, F@_1, F@_2, TrUserData); +skip_length_delimited_command(<<0:1, X:7, Rest/binary>>, N, Acc, F, F@_1, F@_2, TrUserData) -> + Length = X bsl N + Acc, + <<_:Length/binary, Rest2/binary>> = Rest, + dfp_read_field_def_command(Rest2, 0, 0, F, F@_1, F@_2, TrUserData). + +skip_group_command(Bin, _, Z2, FNum, F@_1, F@_2, TrUserData) -> + {_, Rest} = read_group(Bin, FNum), + dfp_read_field_def_command(Rest, 0, Z2, FNum, F@_1, F@_2, TrUserData). + +skip_32_command(<<_:32, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dfp_read_field_def_command(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData). + +skip_64_command(<<_:64, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> dfp_read_field_def_command(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData). + decode_msg_async_request(Bin, TrUserData) -> dfp_read_field_def_async_request(Bin, 0, 0, 0, id(<<>>, TrUserData), id(<<>>, TrUserData), TrUserData). dfp_read_field_def_async_request(<<10, Rest/binary>>, Z1, Z2, F, F@_1, F@_2, TrUserData) -> d_field_async_request_method(Rest, Z1, Z2, F, F@_1, F@_2, TrUserData); @@ -1886,10 +1904,10 @@ merge_msgs(Prev, New, MsgName, Opts) -> case MsgName of auth_request -> merge_msg_auth_request(Prev, New, TrUserData); auth_reply -> merge_msg_auth_reply(Prev, New, TrUserData); - activate_push -> merge_msg_activate_push(Prev, New, TrUserData); deploy -> merge_msg_deploy(Prev, New, TrUserData); efka_response -> merge_msg_efka_response(Prev, New, TrUserData); pub -> merge_msg_pub(Prev, New, TrUserData); + command -> merge_msg_command(Prev, New, TrUserData); async_request -> merge_msg_async_request(Prev, New, TrUserData); async_response -> merge_msg_async_response(Prev, New, TrUserData); service_config -> merge_msg_service_config(Prev, New, TrUserData); @@ -1934,13 +1952,6 @@ merge_msg_auth_reply(#auth_reply{code = PFcode, message = PFmessage}, #auth_repl true -> NFmessage end}. --compile({nowarn_unused_function,merge_msg_activate_push/3}). -merge_msg_activate_push(#activate_push{auth = PFauth}, #activate_push{auth = NFauth}, _) -> - #activate_push{auth = - if NFauth =:= undefined -> PFauth; - true -> NFauth - end}. - -compile({nowarn_unused_function,merge_msg_deploy/3}). merge_msg_deploy(#deploy{task_id = PFtask_id, service_id = PFservice_id, tar_url = PFtar_url}, #deploy{task_id = NFtask_id, service_id = NFservice_id, tar_url = NFtar_url}, _) -> #deploy{task_id = @@ -1982,6 +1993,17 @@ merge_msg_pub(#pub{topic = PFtopic, content = PFcontent}, #pub{topic = NFtopic, true -> NFcontent end}. +-compile({nowarn_unused_function,merge_msg_command/3}). +merge_msg_command(#command{topic = PFtopic, content = PFcontent}, #command{topic = NFtopic, content = NFcontent}, _) -> + #command{topic = + if NFtopic =:= undefined -> PFtopic; + true -> NFtopic + end, + content = + if NFcontent =:= undefined -> PFcontent; + true -> NFcontent + end}. + -compile({nowarn_unused_function,merge_msg_async_request/3}). merge_msg_async_request(#async_request{method = PFmethod, params = PFparams}, #async_request{method = NFmethod, params = NFparams}, _) -> #async_request{method = @@ -2162,10 +2184,10 @@ verify_msg(Msg, MsgName, Opts) -> case MsgName of auth_request -> v_msg_auth_request(Msg, [MsgName], TrUserData); auth_reply -> v_msg_auth_reply(Msg, [MsgName], TrUserData); - activate_push -> v_msg_activate_push(Msg, [MsgName], TrUserData); deploy -> v_msg_deploy(Msg, [MsgName], TrUserData); efka_response -> v_msg_efka_response(Msg, [MsgName], TrUserData); pub -> v_msg_pub(Msg, [MsgName], TrUserData); + command -> v_msg_command(Msg, [MsgName], TrUserData); async_request -> v_msg_async_request(Msg, [MsgName], TrUserData); async_response -> v_msg_async_response(Msg, [MsgName], TrUserData); service_config -> v_msg_service_config(Msg, [MsgName], TrUserData); @@ -2211,15 +2233,6 @@ v_msg_auth_reply(#auth_reply{code = F1, message = F2}, Path, TrUserData) -> ok; v_msg_auth_reply(X, Path, _TrUserData) -> mk_type_error({expected_msg, auth_reply}, X, Path). --compile({nowarn_unused_function,v_msg_activate_push/3}). --dialyzer({nowarn_function,v_msg_activate_push/3}). -v_msg_activate_push(#activate_push{auth = F1}, Path, TrUserData) -> - if F1 == undefined -> ok; - true -> v_type_bool(F1, [auth | Path], TrUserData) - end, - ok; -v_msg_activate_push(X, Path, _TrUserData) -> mk_type_error({expected_msg, activate_push}, X, Path). - -compile({nowarn_unused_function,v_msg_deploy/3}). -dialyzer({nowarn_function,v_msg_deploy/3}). v_msg_deploy(#deploy{task_id = F1, service_id = F2, tar_url = F3}, Path, TrUserData) -> @@ -2262,6 +2275,18 @@ v_msg_pub(#pub{topic = F1, content = F2}, Path, TrUserData) -> ok; v_msg_pub(X, Path, _TrUserData) -> mk_type_error({expected_msg, pub}, X, Path). +-compile({nowarn_unused_function,v_msg_command/3}). +-dialyzer({nowarn_function,v_msg_command/3}). +v_msg_command(#command{topic = F1, content = F2}, Path, TrUserData) -> + if F1 == undefined -> ok; + true -> v_type_string(F1, [topic | Path], TrUserData) + end, + if F2 == undefined -> ok; + true -> v_type_string(F2, [content | Path], TrUserData) + end, + ok; +v_msg_command(X, Path, _TrUserData) -> mk_type_error({expected_msg, command}, X, Path). + -compile({nowarn_unused_function,v_msg_async_request/3}). -dialyzer({nowarn_function,v_msg_async_request/3}). v_msg_async_request(#async_request{method = F1, params = F2}, Path, TrUserData) -> @@ -2430,14 +2455,6 @@ v_type_uint32(N, _Path, _TrUserData) when is_integer(N), 0 =< N, N =< 4294967295 v_type_uint32(N, Path, _TrUserData) when is_integer(N) -> mk_type_error({value_out_of_range, uint32, unsigned, 32}, N, Path); v_type_uint32(X, Path, _TrUserData) -> mk_type_error({bad_integer, uint32, unsigned, 32}, X, Path). --compile({nowarn_unused_function,v_type_bool/3}). --dialyzer({nowarn_function,v_type_bool/3}). -v_type_bool(false, _Path, _TrUserData) -> ok; -v_type_bool(true, _Path, _TrUserData) -> ok; -v_type_bool(0, _Path, _TrUserData) -> ok; -v_type_bool(1, _Path, _TrUserData) -> ok; -v_type_bool(X, Path, _TrUserData) -> mk_type_error(bad_boolean_value, X, Path). - -compile({nowarn_unused_function,v_type_float/3}). -dialyzer({nowarn_function,v_type_float/3}). v_type_float(N, _Path, _TrUserData) when is_float(N) -> ok; @@ -2503,7 +2520,6 @@ get_msg_defs() -> #field{name = token, fnum = 5, rnum = 5, type = string, occurrence = optional, opts = []}, #field{name = timestamp, fnum = 6, rnum = 6, type = uint32, occurrence = optional, opts = []}]}, {{msg, auth_reply}, [#field{name = code, fnum = 1, rnum = 2, type = uint32, occurrence = optional, opts = []}, #field{name = message, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]}, - {{msg, activate_push}, [#field{name = auth, fnum = 1, rnum = 2, type = bool, occurrence = optional, opts = []}]}, {{msg, deploy}, [#field{name = task_id, fnum = 1, rnum = 2, type = uint32, occurrence = optional, opts = []}, #field{name = service_id, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}, @@ -2513,6 +2529,7 @@ get_msg_defs() -> #field{name = result, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}, #field{name = message, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}]}, {{msg, pub}, [#field{name = topic, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = content, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]}, + {{msg, command}, [#field{name = topic, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = content, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]}, {{msg, async_request}, [#field{name = method, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = params, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]}, {{msg, async_response}, [#field{name = code, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, @@ -2555,13 +2572,13 @@ get_msg_defs() -> #field{name = params, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}]}]. -get_msg_names() -> [auth_request, auth_reply, activate_push, deploy, efka_response, pub, async_request, async_response, service_config, data, ping, service_inform, feedback_phase, event]. +get_msg_names() -> [auth_request, auth_reply, deploy, efka_response, pub, command, async_request, async_response, service_config, data, ping, service_inform, feedback_phase, event]. get_group_names() -> []. -get_msg_or_group_names() -> [auth_request, auth_reply, activate_push, deploy, efka_response, pub, async_request, async_response, service_config, data, ping, service_inform, feedback_phase, event]. +get_msg_or_group_names() -> [auth_request, auth_reply, deploy, efka_response, pub, command, async_request, async_response, service_config, data, ping, service_inform, feedback_phase, event]. get_enum_names() -> []. @@ -2585,7 +2602,6 @@ find_msg_def(auth_request) -> #field{name = token, fnum = 5, rnum = 5, type = string, occurrence = optional, opts = []}, #field{name = timestamp, fnum = 6, rnum = 6, type = uint32, occurrence = optional, opts = []}]; find_msg_def(auth_reply) -> [#field{name = code, fnum = 1, rnum = 2, type = uint32, occurrence = optional, opts = []}, #field{name = message, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]; -find_msg_def(activate_push) -> [#field{name = auth, fnum = 1, rnum = 2, type = bool, occurrence = optional, opts = []}]; find_msg_def(deploy) -> [#field{name = task_id, fnum = 1, rnum = 2, type = uint32, occurrence = optional, opts = []}, #field{name = service_id, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}, @@ -2595,6 +2611,7 @@ find_msg_def(efka_response) -> #field{name = result, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}, #field{name = message, fnum = 3, rnum = 4, type = string, occurrence = optional, opts = []}]; find_msg_def(pub) -> [#field{name = topic, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = content, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]; +find_msg_def(command) -> [#field{name = topic, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = content, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]; find_msg_def(async_request) -> [#field{name = method, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, #field{name = params, fnum = 2, rnum = 3, type = string, occurrence = optional, opts = []}]; find_msg_def(async_response) -> [#field{name = code, fnum = 1, rnum = 2, type = string, occurrence = optional, opts = []}, @@ -2695,10 +2712,10 @@ service_and_rpc_name_to_fqbins(S, R) -> error({gpb_error, {badservice_or_rpc, {S fqbin_to_msg_name(<<"AuthRequest">>) -> auth_request; fqbin_to_msg_name(<<"AuthReply">>) -> auth_reply; -fqbin_to_msg_name(<<"ActivatePush">>) -> activate_push; fqbin_to_msg_name(<<"Deploy">>) -> deploy; fqbin_to_msg_name(<<"EfkaResponse">>) -> efka_response; fqbin_to_msg_name(<<"Pub">>) -> pub; +fqbin_to_msg_name(<<"Command">>) -> command; fqbin_to_msg_name(<<"AsyncRequest">>) -> async_request; fqbin_to_msg_name(<<"AsyncResponse">>) -> async_response; fqbin_to_msg_name(<<"ServiceConfig">>) -> service_config; @@ -2712,10 +2729,10 @@ fqbin_to_msg_name(E) -> error({gpb_error, {badmsg, E}}). msg_name_to_fqbin(auth_request) -> <<"AuthRequest">>; msg_name_to_fqbin(auth_reply) -> <<"AuthReply">>; -msg_name_to_fqbin(activate_push) -> <<"ActivatePush">>; msg_name_to_fqbin(deploy) -> <<"Deploy">>; msg_name_to_fqbin(efka_response) -> <<"EfkaResponse">>; msg_name_to_fqbin(pub) -> <<"Pub">>; +msg_name_to_fqbin(command) -> <<"Command">>; msg_name_to_fqbin(async_request) -> <<"AsyncRequest">>; msg_name_to_fqbin(async_response) -> <<"AsyncResponse">>; msg_name_to_fqbin(service_config) -> <<"ServiceConfig">>; @@ -2762,7 +2779,7 @@ get_all_source_basenames() -> ["message_pb.proto"]. get_all_proto_names() -> ["message_pb"]. -get_msg_containment("message_pb") -> [activate_push, async_request, async_response, auth_reply, auth_request, data, deploy, efka_response, event, feedback_phase, ping, pub, service_config, service_inform]; +get_msg_containment("message_pb") -> [async_request, async_response, auth_reply, auth_request, command, data, deploy, efka_response, event, feedback_phase, ping, pub, service_config, service_inform]; get_msg_containment(P) -> error({gpb_error, {badproto, P}}). @@ -2785,6 +2802,7 @@ get_enum_containment(P) -> error({gpb_error, {badproto, P}}). get_proto_by_msg_name_as_fqbin(<<"Data">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"Pub">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"Event">>) -> "message_pb"; +get_proto_by_msg_name_as_fqbin(<<"Command">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"AuthRequest">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"AsyncRequest">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"FeedbackPhase">>) -> "message_pb"; @@ -2792,7 +2810,6 @@ get_proto_by_msg_name_as_fqbin(<<"EfkaResponse">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"AsyncResponse">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"ServiceConfig">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"Ping">>) -> "message_pb"; -get_proto_by_msg_name_as_fqbin(<<"ActivatePush">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"Deploy">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"AuthReply">>) -> "message_pb"; get_proto_by_msg_name_as_fqbin(<<"ServiceInform">>) -> "message_pb"; diff --git a/message_pb.proto b/message_pb.proto index 1c104f4..abe79c7 100644 --- a/message_pb.proto +++ b/message_pb.proto @@ -15,11 +15,6 @@ message AuthReply { string message = 2; } -// 激活信息 -message ActivatePush { - bool auth = 1; -} - // 部署逻辑 message Deploy { uint32 task_id = 1; @@ -40,6 +35,12 @@ message Pub { string content = 2; } +// 命令,不需要返回 +message Command { + string topic = 1; + string content = 2; +} + // 需要响应; 系统内部的调用采用: $sys_前缀 message AsyncRequest { string method = 1;