📄 megaco_test_mg.erl
字号:
end.start_notify_request_handler(EAF, CH, N) -> d("start_notify_request_handler -> entry with" "~n EAF: ~p" "~n CH: ~p" "~n N: ~p", [EAF, CH, N]), Env = get(), spawn_link(?MODULE, notify_request_handler_main, [self(), Env, EAF, CH, N]).notify_request_handler_main(Parent, Env, EAF, CH, N) -> F = fun({Tag, Val}) -> put(Tag, Val) end, lists:foreach(F, Env), d("notify_request_handler_main -> entry with" "~n Parent: ~p" "~n EAF: ~p" "~n CH: ~p" "~n N: ~p", [Parent, EAF, CH, N]), Res = do_notify_request(EAF, CH, N), d("notify_request_handler_main -> notify complete:" "~n Res: ~p", [Res]), Parent ! {notify_request_complete, {ok, Res}, self()}, unlink(Parent), exit(normal).do_notify_request(_EAF, _CH, N) when N =< 0 -> d("do_notify_request(~p) -> ignoring", [N]), ignore;do_notify_request(EAF, CH, 1) -> d("do_notify_request(1) -> entry with"), {Action, _} = make_notify_request(), send_sync(EAF, CH, Action, []);do_notify_request(EAF, CH, N) -> d("do_notify_request(~p) -> entry with", [N]), {N, Actions, _} = make_notify_request(N,N), send_sync(EAF, CH, Actions, []).make_notify_request(N, Sz) when N >= Sz, Sz > 0 -> {Req, ReplyData} = make_notify_request(N, Sz, [], []), {Sz, Req, ReplyData};make_notify_request(N, _Sz) when N > 0 -> {Req, ReplyData} = make_notify_request(N, N, [], []), {N, Req, ReplyData}. make_notify_request(_Offset, 0, Actions, ReplyDatas) -> {lists:reverse(Actions), lists:reverse(ReplyDatas)};make_notify_request(Offset, N, Actions, ReplyDatas) when N > 0 -> TimeStamp = cre_timeNotation(), Event = cre_observedEvent("al/of", TimeStamp), Desc = cre_observedEventsDesc(2220 + N, [Event]), NotifyReq = cre_notifyReq([#megaco_term_id{id = tid(100+Offset-N)}],Desc), CmdReq = cre_commandReq({notifyReq, NotifyReq}), ActReq = cre_actionReq(?megaco_null_context_id, [CmdReq]), make_notify_request(Offset, N-1, [[ActReq]|Actions], [Desc|ReplyDatas]). make_notify_request() -> TimeStamp = cre_timeNotation("19990729", "22000000"), Event = cre_observedEvent("al/of", TimeStamp), Desc1 = cre_observedEventsDesc(2221, [Event]), Desc2 = cre_observedEventsDesc(2222, [Event]), Desc3 = cre_observedEventsDesc(2223, [Event]), Desc4 = cre_observedEventsDesc(2224, [Event]), NotifyReq1 = cre_notifyReq([#megaco_term_id{id = ?A4444}], Desc1), NotifyReq2 = cre_notifyReq([#megaco_term_id{id = ?A4445}], Desc2), CmdReq1 = cre_commandReq({notifyReq, NotifyReq1}), CmdReq2 = cre_commandReq({notifyReq, NotifyReq2}), ActReq = cre_actionReq(?megaco_null_context_id, [CmdReq1,CmdReq2]), {[ActReq], [Desc3,Desc4]}.cre_actionReq(Cid, Cmds) -> #'ActionRequest'{contextId = Cid, commandRequests = Cmds}.cre_commandReq(Cmd) -> #'CommandRequest'{command = Cmd}.cre_serviceChangeReq(TermId, Parms) -> #'ServiceChangeRequest'{terminationID = TermId, serviceChangeParms = Parms}.cre_serviceChangeParm(Method, Reason) -> #'ServiceChangeParm'{serviceChangeMethod = Method, serviceChangeReason = Reason}.cre_notifyReq(Tid, EvsDesc) -> #'NotifyRequest'{terminationID = Tid, observedEventsDescriptor = EvsDesc}.% cre_notifyRep(Tid) ->% #'NotifyReply'{terminationID = [Tid]}.% cre_notifyRep(Tid,Err) ->% #'NotifyReply'{terminationID = [Tid], errorDescriptor = Err}.cre_observedEventsDesc(Id, EvList) -> #'ObservedEventsDescriptor'{requestId = Id, observedEventLst = EvList}.cre_observedEvent(Name, Not) -> #'ObservedEvent'{eventName = Name, timeNotation = Not}.cre_timeNotation() -> {{Year,Month,Day},{Hour,Min,Sec}} = calendar:universal_time(), D = lists:flatten(io_lib:format("~4..0w~2..0w~2..0w", [Year,Month,Day])), T = lists:flatten(io_lib:format("~2..0w~2..0w~4..0w", [Hour,Min,Sec])), cre_timeNotation(D, T). cre_timeNotation(D,T) -> #'TimeNotation'{date = D, time = T}.cre_error_descr(Code,Text) -> #'ErrorDescriptor'{errorCode = Code, errorText = Text}.% cre_error_descr(Code,FormatString,Args) ->% Text = lists:flatten(io_lib:format(FormatString,Args)),% cre_error_descr(Code,Text).%% -----------------------%% Handle megaco callbacks%%handle_megaco_request(#mg{state = connecting} = MG, {handle_connect, _CH, _PV}) -> d("handle_megaco_request(handle_connect,connecting) -> entry"), {ok, MG};handle_megaco_request(#mg{state = S} = MG, {handle_connect, _CH, _PV}) -> d("handle_megaco_request(handle_connect) -> entry"), Desc = lists:flatten(io_lib:format("not ready for connect in state ~p", [S])), ED = cre_error_descr(?megaco_internal_gateway_error, Desc), {{discard_ack, ED}, MG};handle_megaco_request(#mg{req_handler = Pid} = MG, {handle_disconnect, _CH, _PV, R}) when pid(Pid) -> d("handle_megaco_request(handle_disconnect) -> entry with" "~n Pid: ~p", [Pid]), Error = {error, {disconnected, R}}, self() ! {notify_request_complete, Error, Pid}, unlink(Pid), exit(Pid, kill), {ok, MG#mg{req_handler = undefined, state = initiated}};handle_megaco_request(MG, {handle_disconnect, _CH, _PV, _R}) -> d("handle_megaco_request(handle_disconnect) -> entry"), {ok, MG#mg{state = initiated}};handle_megaco_request(MG, {handle_syntax_error, _RH, _PV, _ED}) -> {reply, MG};handle_megaco_request(#mg{req_handler = Pid} = MG, {handle_message_error, CH, PV, ED}) when pid(Pid) -> d("handle_megaco_request(handle_message_error) -> entry with" "~n Pid: ~p" "~n CH: ~p" "~n PV: ~p" "~n ED: ~p", [Pid, CH, PV, ED]), self() ! {notify_request_complete, ED, Pid}, unlink(Pid), exit(Pid, kill), {no_reply, MG#mg{req_handler = undefined}};handle_megaco_request(MG, {handle_message_error, CH, PV, ED}) -> d("handle_megaco_request(handle_message_error) -> entry with" "~n CH: ~p" "~n PV: ~p" "~n ED: ~p", [CH, PV, ED]), {no_reply, MG};handle_megaco_request(MG, {handle_trans_request, _CH, _PV, _AR}) -> ED = cre_error_descr(?megaco_not_implemented, "Transaction requests not handled"), {{discard_ack, ED}, MG};handle_megaco_request(MG, {handle_trans_long_request, _CH, _PV, _RD}) -> ED = cre_error_descr(?megaco_not_implemented, "Long transaction requests not handled"), {{discard_ack, ED}, MG};handle_megaco_request(#mg{rep_info = P} = MG, {handle_trans_reply, CH, PV, AR, RD}) when pid(P) -> P ! {rep_received, self(), AR}, do_handle_trans_reply(MG, CH, PV, AR, RD);handle_megaco_request(MG, {handle_trans_reply, CH, PV, AR, RD}) -> do_handle_trans_reply(MG, CH, PV, AR, RD);handle_megaco_request(#mg{ack_info = P} = MG, {handle_trans_ack, _CH, _PV, AS, _AD}) when pid(P) -> d("handle_megaco_request(handle_trans_ack,~p) -> entry",[P]), P ! {ack_received, self(), AS}, {ok, MG};handle_megaco_request(MG, {handle_trans_ack, _CH, _PV, _AS, _AD}) -> d("handle_megaco_request(handle_trans_ack) -> entry"), {ok, MG}.do_handle_trans_reply(#mg{parent = Parent, state = connecting} = MG, CH, _PV, {ok, Rep}, _RD) -> d("do_handle_trans_reply(connecting) -> entry with" "~n CH: ~p" "~n Rep: ~p", [CH, Rep]), server_reply(Parent, service_change_reply, ok), {ok, MG#mg{state = connected}};do_handle_trans_reply(#mg{parent = Parent, load_counter = 0} = MG, CH, _PV, {ok, Rep}, _RD) -> d("do_handle_trans_reply(load_counter = 0) -> entry with" "~n CH: ~p" "~n Rep: ~p", [CH, Rep, Parent]), handle_trans_reply_verify_act(Rep), server_reply(Parent, load_complete, ok), {ok, MG#mg{reply_counter = 0}};do_handle_trans_reply(#mg{reply_counter = 0} = MG, CH, _PV, {ok, Rep}, _RD) -> d("do_handle_trans_reply(reply_counter = 0) -> entry with" "~n CH: ~p" "~n Rep: ~p", [CH, Rep]), handle_trans_reply_verify_act(Rep), apply_load_timer(), {ok, MG};do_handle_trans_reply(#mg{reply_counter = N} = MG, CH, _PV, {ok, Rep}, _RD) -> d("do_handle_trans_reply(reply_counter = ~p) -> entry with" "~n CH: ~p" "~n Rep: ~p", [N, CH, Rep]), handle_trans_reply_verify_act(Rep), apply_load_timer(), {ok, MG#mg{reply_counter = N-1}};do_handle_trans_reply(MG, _CH, _PV, {error, ED}, _RD) -> i("unexpected error transaction: ~p", [ED]), {ok, MG}.handle_trans_reply_verify_act([]) -> ok;handle_trans_reply_verify_act([#'ActionReply'{commandReply = Rep}|Reps]) -> handle_trans_reply_verify_cmd(Rep), handle_trans_reply_verify_act(Reps);handle_trans_reply_verify_act([Rep|Reps]) -> i("received 'propably' unexpected reply: ~n~p", [Rep]), handle_trans_reply_verify_act(Reps).handle_trans_reply_verify_cmd([]) -> ok;handle_trans_reply_verify_cmd([Cmd|Cmds]) -> case Cmd of {notifyReply, #'NotifyReply'{terminationID = [Tid]}} -> d("received expected notification reply from ~n ~p", [Tid]); Else -> i("received unexpected notification reply ~n~p", [Else]) end, handle_trans_reply_verify_cmd(Cmds). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%notify_started(Parent) -> Parent ! {started, self()}.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The megaco user callback interface handle_connect(CH, PV, Pid, Mid) -> case CH#megaco_conn_handle.remote_mid of preliminary_mid -> %% Avoids deadlock ok; _ -> Reply = request(Pid, {handle_connect, CH, PV}, Mid), Reply end.handle_disconnect(_CH, _PV, {user_disconnect, {stopped_by_user, Pid}}, Pid, _Mid) -> ok;handle_disconnect(CH, PV, R, Pid, Mid) -> request(Pid, {handle_disconnect, CH, PV, R}, Mid).handle_syntax_error(ReceiveHandle, ProtocolVersion, ErrorDescriptor, Pid, Mid) -> Req = {handle_syntax_error, ReceiveHandle, ProtocolVersion, ErrorDescriptor}, request(Pid, Req, Mid). handle_message_error(ConnHandle, ProtocolVersion, ErrorDescriptor, Pid, Mid) -> Req = {handle_message_error, ConnHandle, ProtocolVersion, ErrorDescriptor}, request(Pid, Req, Mid).handle_trans_request(CH, PV, AR, Pid, Mid) -> Reply = request(Pid, {handle_trans_request, CH, PV, AR}, Mid), Reply.handle_trans_long_request(ConnHandle, ProtocolVersion, ReqData, Pid, Mid) -> Req = {handle_trans_long_request, ConnHandle, ProtocolVersion, ReqData}, request(Pid, Req, Mid).handle_trans_reply(ConnHandle, ProtocolVersion, ActualReply, ReplyData, Pid, Mid) -> Req = {handle_trans_reply, ConnHandle, ProtocolVersion, ActualReply, ReplyData}, request(Pid, Req, Mid).handle_trans_ack(ConnHandle, ProtocolVersion, AckStatus, AckData, Pid, Mid) -> Req = {handle_trans_ack, ConnHandle, ProtocolVersion, AckStatus, AckData}, request(Pid, Req, Mid).request(Pid, Request, Mid) -> Pid ! {request, Request, Mid, self()}, receive {reply, {delay, To, ED}, Pid} -> sleep(To), {discard_ack, ED}; {reply, Reply, Pid} -> Reply end.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%send_async(true, CH, Actions, Opts) -> d("send_async(true) -> encode actions first"), case megaco:encode_actions(CH, Actions, Opts) of {ok, BinOrBins} -> d("send_async(true) -> send message"), megaco:cast(CH, BinOrBins, Opts); Error -> d("send_async(true) -> encode failed: ~n~p", [Error]), Error end;send_async(_, CH, Actions, Opts) -> d("send_async(true) -> send message"), megaco:cast(CH, Actions, Opts).send_sync(true, CH, Actions, Opts) -> d("send_sync(true) -> encode actions first"), case megaco:encode_actions(CH, Actions, Opts) of {ok, BinOrBins} -> d("send_sync(true) -> send message"), megaco:call(CH, BinOrBins, Opts); Error -> d("send_sync(true) -> encode failed: ~n~p", [Error]), Error end;send_sync(_, CH, Actions, Opts) -> d("send_sync(false) -> send message"), megaco:call(CH, Actions, Opts).%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%sleep(X) -> receive after X -> ok end.error_msg(F,A) -> error_logger:error_msg("MG: " ++ F ++ "~n",A).get_encoding_module(RI) -> case (catch get_conf(encoding_module, RI)) of {error, _} -> undefined; Val -> Val end.get_encoding_config(RI, EM) -> case text_codec(EM) of true -> case megaco:system_info(text_config) of [Conf] when list(Conf) -> Conf; _ -> [] end; false -> get_conf(encoding_config, RI) end.text_codec(megaco_compact_text_encoder) -> true;text_codec(megaco_pretty_text_encoder) -> true;text_codec(_) -> false.get_transport_module(RI) -> get_conf(transport_module, RI).get_transport_port(RI) -> get_conf(port, RI).get_conf(Key, Config) -> case lists:keysearch(Key, 1, Config) of {value, {Key, Val}} -> Val; _ -> exit({error, {not_found, Key, Config}}) end.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%tid(N) when N >= 0 -> {Rem1, Val1} = num2str(N), {Rem2, Val2} = num2str(Rem1), {0, Val3} = num2str(Rem2), [Val3, Val2, Val1].num2str(N) when N >= 0 -> num2str(N, []).num2str(Rem, Val) when length(Val) == 8 -> {Rem, Val};num2str(N, Val) -> D = N div 2, case N rem 2 of 1 -> num2str(D, [$1|Val]); 0 -> num2str(D, [$0|Val]) end.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%i(F) -> i(F, []).i(F, A) -> print(info, get(verbosity), "", F, A).d(F) -> d(F, []).d(F, A) -> print(debug, get(verbosity), "DBG", F, A).printable(_, debug) -> true;printable(info, info) -> true;printable(_,_) -> false.print(Severity, Verbosity, P, F, A) -> print(printable(Severity,Verbosity), P, F, A).print(true, P, F, A) -> io:format("*** [~s] ~s ~p ~s ***" "~n " ++ F ++ "~n~n", [format_timestamp(now()), P, self(), get(sname) | A]);print(_, _, _, _) -> ok.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).%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%random_init() -> {A,B,C} = now(), random:seed(A,B,C).random() -> 10 * random:uniform(50).apply_load_timer() -> erlang:send_after(random(), self(), apply_load_timeout).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -