megaco_test_generator.erl
来自「OTP是开放电信平台的简称」· ERL 代码 · 共 1,523 行 · 第 1/4 页
ERL
1,523 行
parse_megaco([{debug, Debug}|Instrs], RevInstrs) when Debug == true; Debug == false -> parse_megaco(Instrs, [{debug, Debug}|RevInstrs]);parse_megaco([{expect_nothing, To}|Instrs], RevInstrs) when is_integer(To) and (To > 0) -> parse_megaco(Instrs, [{expect_nothing, To}|RevInstrs]);parse_megaco([{megaco_trace, Level}|Instrs], RevInstrs) when Level == disable; Level == max; Level == min -> Trace = {megaco_trace, Level}, parse_megaco(Instrs, [Trace|RevInstrs]);parse_megaco([{megaco_trace, Level}|Instrs], RevInstrs) when is_integer(Level) -> Trace = {megaco_trace, Level}, parse_megaco(Instrs, [Trace|RevInstrs]);parse_megaco([{sleep, To}|Instrs], RevInstrs) when is_integer(To) and (To > 0) -> parse_megaco(Instrs, [{sleep, To}|RevInstrs]);parse_megaco([megaco_start|Instrs], RevInstrs) -> parse_megaco(Instrs, [megaco_start|RevInstrs]);parse_megaco([megaco_stop|Instrs], RevInstrs) -> parse_megaco(Instrs, [megaco_stop|RevInstrs]);parse_megaco([{megaco_start_user, Mid, RecvInfo, Conf}|Instrs], RevInstrs) when is_list(Conf) -> Start = {megaco_start_user, Mid, RecvInfo, Conf}, parse_megaco(Instrs, [Start|RevInstrs]);parse_megaco([megaco_stop_user|Instrs], RevInstrs) -> parse_megaco(Instrs, [megaco_stop_user|RevInstrs]);parse_megaco([{megaco_system_info, Tag}|Instrs], RevInstrs) when is_atom(Tag) -> Info = {megaco_system_info, Tag}, parse_megaco(Instrs, [Info|RevInstrs]);parse_megaco([{megaco_user_info, Tag}|Instrs], RevInstrs) when is_atom(Tag) -> Info = {megaco_user_info, Tag}, parse_megaco(Instrs, [Info|RevInstrs]);parse_megaco([{megaco_update_user_info, Tag, Val}|Instrs], RevInstrs) when is_atom(Tag) -> Update = {megaco_update_user_info, Tag, Val}, parse_megaco(Instrs, [Update|RevInstrs]);parse_megaco([{megaco_conn_info, Tag}|Instrs], RevInstrs) when is_atom(Tag) -> Info = {megaco_conn_info, Tag}, parse_megaco(Instrs, [Info|RevInstrs]);parse_megaco([{megaco_update_conn_info, Tag, Val}|Instrs], RevInstrs) when is_atom(Tag) -> Update = {megaco_update_conn_info, Tag, Val}, parse_megaco(Instrs, [Update|RevInstrs]);parse_megaco([start_transport|Instrs], RevInstrs) -> parse_megaco(Instrs, [start_transport|RevInstrs]);parse_megaco([listen|Instrs], RevInstrs) -> Listen = {listen, []}, parse_megaco(Instrs, [Listen|RevInstrs]);parse_megaco([{listen, Opts}|Instrs], RevInstrs) when is_list(Opts) -> Listen = {listen, Opts}, parse_megaco(Instrs, [Listen|RevInstrs]);parse_megaco([connect|Instrs], RevInstrs) -> {ok, LocalHost} = inet:gethostname(), Conn = {connect, LocalHost, []}, parse_megaco(Instrs, [Conn|RevInstrs]);parse_megaco([{connect, [{Key, _Val}|_] = Opts}|Instrs], RevInstrs) when is_atom(Key) -> {ok, LocalHost} = inet:gethostname(), Conn = {connect, LocalHost, Opts}, parse_megaco(Instrs, [Conn|RevInstrs]);parse_megaco([{connect, Host}|Instrs], RevInstrs) -> Conn = {connect, Host, []}, parse_megaco(Instrs, [Conn|RevInstrs]);parse_megaco([{connect, Host, Opts}|Instrs], RevInstrs) -> Conn = {connect, Host, Opts}, parse_megaco(Instrs, [Conn|RevInstrs]);parse_megaco([disconnect|Instrs], RevInstrs) -> Disco = disconnect, parse_megaco(Instrs, [Disco|RevInstrs]);parse_megaco([megaco_connect|Instrs], RevInstrs) -> Conn = megaco_connect, parse_megaco(Instrs, [Conn|RevInstrs]);parse_megaco([megaco_disconnect|Instrs], RevInstrs) -> Disco = {megaco_disconnect, ignore}, parse_megaco(Instrs, [Disco|RevInstrs]);parse_megaco([{megaco_disconnect, Reason}|Instrs], RevInstrs) -> Disco = {megaco_disconnect, Reason}, parse_megaco(Instrs, [Disco|RevInstrs]);parse_megaco([{megaco_call, ARs, Opts}|Instrs], RevInstrs) when is_list(ARs) and is_list(Opts) -> Call = {megaco_call, ARs, Opts}, parse_megaco(Instrs, [Call|RevInstrs]);parse_megaco([{megaco_call, Mid, ARs, Opts}|Instrs], RevInstrs) when is_list(ARs) and is_list(Opts) -> Call = {megaco_call, Mid, ARs, Opts}, parse_megaco(Instrs, [Call|RevInstrs]);parse_megaco([{megaco_call, Mid, ARs, Opts}|Instrs], RevInstrs) when is_binary(ARs) and is_list(Opts) -> Call = {megaco_call, Mid, ARs, Opts}, parse_megaco(Instrs, [Call|RevInstrs]);parse_megaco([{megaco_call, ARs, Opts}|Instrs], RevInstrs) when is_binary(ARs) and is_list(Opts) -> Call = {megaco_call, ARs, Opts}, parse_megaco(Instrs, [Call|RevInstrs]);parse_megaco([{megaco_cast, ARs, Opts}|Instrs], RevInstrs) when is_list(ARs) and is_list(Opts) -> Cast = {megaco_cast, ARs, Opts}, parse_megaco(Instrs, [Cast|RevInstrs]);parse_megaco([{megaco_cast, Mid, ARs, Opts}|Instrs], RevInstrs) when is_list(ARs) and is_list(Opts) -> Cast = {megaco_cast, Mid, ARs, Opts}, parse_megaco(Instrs, [Cast|RevInstrs]);parse_megaco([{megaco_cast, ARs, Opts}|Instrs], RevInstrs) when is_binary(ARs) and is_list(Opts) -> Cast = {megaco_cast, ARs, Opts}, parse_megaco(Instrs, [Cast|RevInstrs]);parse_megaco([{megaco_cast, Mid, ARs, Opts}|Instrs], RevInstrs) when is_binary(ARs) and is_list(Opts) -> Cast = {megaco_cast, Mid, ARs, Opts}, parse_megaco(Instrs, [Cast|RevInstrs]);parse_megaco([{megaco_cancel, Reason}|Instrs], RevInstrs) -> Cancel = {megaco_cancel, Reason}, parse_megaco(Instrs, [Cancel|RevInstrs]);parse_megaco([{megaco_callback, nocall = Tag, Timeout}|Instrs], RevInstrs) when is_integer(Timeout) -> C = {megaco_callback, Tag, Timeout}, parse_megaco(Instrs, [C|RevInstrs]);parse_megaco([{megaco_callback, Tag, Verify}|Instrs], RevInstrs) when is_atom(Tag) and is_function(Verify) -> C = {megaco_callback, Tag, Verify}, parse_megaco(Instrs, [C|RevInstrs]);parse_megaco([{megaco_callback, Tag, {VMod, VFunc, VArgs}}|Instrs], RevInstrs) when is_atom(Tag) and (is_atom(VMod) and is_atom(VFunc) and is_list(VArgs)) -> Verify = fun(X) -> io:format("[megaco_callback ~w] calling ~w:~w with" "~n X: ~p" "~n VArgs: ~w" "~n", [Tag, VMod, VFunc, X, VArgs]), (catch apply(VMod, VFunc, [X|VArgs])) end, C = {megaco_callback, Tag, Verify}, parse_megaco(Instrs, [C|RevInstrs]);parse_megaco([{megaco_callback, Verifiers0}|Instrs], RevInstrs) when is_list(Verifiers0) -> Verifiers = [make_verifier(Verifier) || Verifier <- Verifiers0], C = {megaco_callback, Verifiers}, parse_megaco(Instrs, [C|RevInstrs]);parse_megaco([{trigger, Trigger}|Instrs], RevInstrs) when is_function(Trigger) -> C = {trigger, Trigger}, parse_megaco(Instrs, [C|RevInstrs]);parse_megaco([Instr|_], _RevInstrs) -> throw({error, {invalid_instruction, Instr}}).make_verifier({Tag, No, VerifyFunc} = Verify) when is_atom(Tag) and is_integer(No) and is_function(VerifyFunc) -> Verify;make_verifier({Tag, No, {VMod, VFunc, VArgs}}) when is_atom(Tag) and is_integer(No) and (is_atom(VMod) and is_atom(VFunc) and is_list(VArgs)) -> VerifyFunc = fun(X) -> io:format("[megaco_callback ~w] calling ~w:~w with" "~n X: ~p" "~n VArgs: ~w" "~n", [Tag, VMod, VFunc, X, VArgs]), (catch apply(VMod, VFunc, [X|VArgs])) end, Verify = {Tag, No, VerifyFunc}, Verify;make_verifier(BadVerifier) -> throw({error, {bad_verifier, BadVerifier}}). %%% ----------------------------------------------------------------%% -- Megaco user callback interface --handle_connect(CH, PV, P) -> Req = {handle_connect, CH, PV}, handle_megaco_callback_call(P, Req). handle_disconnect(CH, PV, R, P) -> Msg = {handle_disconnect, CH, PV, R}, Reply = ok, handle_megaco_callback_cast(P, Msg, Reply).handle_syntax_error(RH, PV, ED, P) -> Req = {handle_syntax_error, RH, PV, ED}, handle_megaco_callback_call(P, Req).handle_message_error(CH, PV, ED, P) -> Msg = {handle_message_error, CH, PV, ED}, Reply = ok, handle_megaco_callback_cast(P, Msg, Reply).handle_trans_request(CH, PV, AR, P) -> Req = {handle_trans_request, CH, PV, AR}, handle_megaco_callback_call(P, Req).handle_trans_long_request(CH, PV, RD, P) -> Req = {handle_trans_long_request, CH, PV, RD}, handle_megaco_callback_call(P, Req).handle_trans_reply(CH, PV, AR, RD, P) -> Msg = {handle_trans_reply, CH, PV, AR, RD}, Reply = ok, handle_megaco_callback_cast(P, Msg, Reply).handle_trans_ack(CH, PV, AS, AD, P) -> Msg = {handle_trans_ack, CH, PV, AS, AD}, Reply = ok, handle_megaco_callback_cast(P, Msg, Reply).handle_unexpected_trans(CH, PV, T, P) -> Msg = {handle_unexpected_trans, CH, PV, T}, Reply = ok, handle_megaco_callback_cast(P, Msg, Reply).handle_trans_request_abort(RH, PV, TransNo, Pid, P) -> Msg = {handle_trans_request_abort, RH, PV, TransNo, Pid}, Reply = ok, handle_megaco_callback_cast(P, Msg, Reply).handle_megaco_callback_cast(P, Msg, Reply) -> p("handle_megaco_callback_cast -> entry with Msg: ~n~p", [Msg]), P ! {handle_megaco_callback, cast, Msg, self()}, Reply.handle_megaco_callback_call(P, Msg) -> p("handle_megaco_callback_call -> entry with Msg: ~n~p", [Msg]), P ! {handle_megaco_callback, call, Msg, self()}, receive {handle_megaco_callback_reply, Reply} -> p("handle_megaco_callback_call -> received reply: ~n~p", [Reply]), Reply; {handle_megaco_callback_reply, Delay, Reply} when is_integer(Delay) -> p("handle_megaco_callback_call -> " "received reply [~w]: " "~n ~p", [Delay, Reply]), sleep(Delay), p("handle_megaco_callback_call -> deliver reply after delay [~w]", [Delay]), Reply; {'EXIT', SomePid, SomeReason} -> p("handle_megaco_callback_call -> " "received unexpected EXIT signal: " "~n SomePid: ~p" "~n SomeReason: ~p", [SomePid, SomeReason]), exit({unexpected_EXIT_signal, SomePid, SomeReason}) end.handle_megaco_callback_reply(P, call, Reply) -> P ! {handle_megaco_callback_reply, Reply};handle_megaco_callback_reply(_, _, _) -> ok.handle_megaco_callback_reply(P, call, Delay, Reply) -> P ! {handle_megaco_callback_reply, Delay, Reply};handle_megaco_callback_reply(_, _, _, _) -> ok. %%% ----------------------------------------------------------------close(#tcp{connection = Sock}) -> close(Sock);close(undefined) -> ok;close(Sock) -> (catch gen_tcp:close(Sock)).add_tpkt_header(Bin) when is_binary(Bin) -> L = size(Bin) + 4, SZ1 = ((L) bsr 8) band 16#ff, SZ2 = (L) band 16#ff, <<3, 0, SZ1, SZ2, Bin/binary>>;add_tpkt_header(IOList) when is_list(IOList) -> add_tpkt_header(list_to_binary(IOList)).sleep(X) when is_integer(X), X =< 0 -> ok;sleep(X) -> receive after X -> ok end.sz(Bin) when is_binary(Bin) -> size(Bin);sz(L) when is_list(L) -> lists:length(L);sz(_) -> -1.%%% ----------------------------------------------------------------get_config(Key, Opts) -> {value, {Key, Val}} = lists:keysearch(Key, 1, Opts), Val.%%% ----------------------------------------------------------------d(F) -> d(F, []).d(F, A) -> d(true, F, A).d(true, F, A) -> p(" DBG", F, A);d(_, _, _) -> ok.e(F, A) -> p(" ERROR", F, A).p(F) -> p("", F, []).p(F, A) -> p("", F, A).p(P, F, A) -> p(P, get(name), F, A).p([], undefined, F, A) -> io:format("*** [~s] ~p *** " ++ "~n " ++ F ++ "~n", [format_timestamp(now()),self()|A]);p(P, undefined, F, A) -> io:format("*** [~s] ~p ~s *** " ++ "~n " ++ F ++ "~n", [format_timestamp(now()),self(),P|A]);p(P, N, F, A) -> io:format("*** [~s] ~p ~s~s *** " ++ "~n " ++ F ++ "~n", [format_timestamp(now()),self(),N,P|A]).format_timestamp({_N1, _N2, N3} = Now) -> {Date, Time} = calendar:now_to_datetime(Now), {YYYY,MM,DD} = Date, {Hour,Min,Sec} = Time, FormatDate = io_lib:format("~.4w-~.2.0w-~.2.0w ~.2.0w:~.2.0w:~.2.0w 4~w", [YYYY,MM,DD,Hour,Min,Sec,round(N3/1000)]), lists:flatten(FormatDate).
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?