megaco_mib_test.erl
来自「OTP是开放电信平台的简称」· ERL 代码 · 共 1,492 行 · 第 1/4 页
ERL
1,492 行
{ok, Handle, ControlPid} -> MgcMid = preliminary_mid, SendHandle = megaco_udp:create_send_handle(Handle, LocalHost, MgcPort), {ok, ConnHandle} = megaco:connect(RH, MgcMid, SendHandle, ControlPid), ConnHandle; {error, Reason} -> {error, {megaco_udp_open, Reason}} end; {error, Reason} -> {error, {megaco_udp_start_transport, Reason}} end.mg_service_change(ConnHandle) -> mg_service_change(ConnHandle, restart, ?megaco_cold_boot).mg_service_change(ConnHandle, Method, Reason) -> SCP = #'ServiceChangeParm'{serviceChangeMethod = Method, serviceChangeReason = [Reason]}, TermId = [?megaco_root_termination_id], SCR = #'ServiceChangeRequest'{terminationID = TermId, serviceChangeParms = SCP}, CR = #'CommandRequest'{command = {serviceChangeReq, SCR}}, AR = #'ActionRequest'{contextId = ?megaco_null_context_id, commandRequests = [CR]}, megaco:cast(ConnHandle, [AR], []).mg_notify_request(CH) -> TimeStamp = cre_timeNotation("19990729", "22000000"), Event = cre_observedEvent("al/of",TimeStamp), Desc = cre_observedEventsDesc(2222,[Event]), NotifyReq = cre_notifyReq([#megaco_term_id{id = ?A4444}],Desc), CmdReq = cre_commandReq({notifyReq, NotifyReq}), ActReq = cre_actionReq(?megaco_null_context_id, [CmdReq]), megaco:cast(CH, [ActReq], [{reply_data, Desc}]). mg_apply_load(#mg{conn_handle = CH}) -> mg_notify_request(CH).cre_actionReq(Cid, Cmds) -> #'ActionRequest'{contextId = Cid, commandRequests = Cmds}.cre_commandReq(Cmd) -> #'CommandRequest'{command = Cmd}.cre_notifyReq(Tid, EvsDesc) -> #'NotifyRequest'{terminationID = Tid, observedEventsDescriptor = EvsDesc}.cre_observedEventsDesc(Id, EvList) -> #'ObservedEventsDescriptor'{requestId = Id, observedEventLst = EvList}.cre_observedEvent(Name, Not) -> #'ObservedEvent'{eventName = Name, timeNotation = Not}.cre_timeNotation(D,T) -> #'TimeNotation'{date = D, time = T}.%% -----------------------%% Handle megaco callbacks%%mg_handle_request({handle_connect, CH, _PV}, #mg{state = connecting} = S) -> {ok, S#mg{conn_handle = CH}};mg_handle_request({handle_disconnect, CH, _PV, _R}, S) -> {ok, S#mg{conn_handle = CH}};mg_handle_request({handle_syntax_error, _RH, _PV, _ED}, S) -> {reply, S};mg_handle_request({handle_message_error, CH, _PV, _ED}, S) -> {no_reply, S#mg{conn_handle = CH}};mg_handle_request({handle_trans_request, CH, _PV, _AR}, S) -> ED = #'ErrorDescriptor'{errorCode = ?megaco_not_implemented, errorText = "Transaction requests not handled"}, {{discard_ack, ED}, S#mg{conn_handle = CH}};mg_handle_request({handle_trans_long_request, CH, _PV, _RD}, S) -> ED = #'ErrorDescriptor'{errorCode = ?megaco_not_implemented, errorText = "Long transaction requests not handled"}, {{discard_ack, ED}, S#mg{conn_handle = CH}};mg_handle_request({handle_trans_reply, CH, _PV, _AR, _RD}, #mg{parent = Pid, state = connecting} = S) -> %% Should really check this... Pid ! {service_change_reply, ok, self()}, {ok, S#mg{conn_handle = CH, state = connected}};mg_handle_request({handle_trans_reply, _CH, _PV, {error, ED}, RD}, #mg{parent = Pid, load_counter = 0} = S) when record(ED, 'ErrorDescriptor'), record(RD, 'ObservedEventsDescriptor') -> Pid ! {load_complete, self()}, {ok, S};mg_handle_request({handle_trans_reply, _CH, _PV, {error, ED}, RD}, #mg{load_counter = N} = S) when record(ED, 'ErrorDescriptor'), record(RD, 'ObservedEventsDescriptor') -> apply_load_timer(), {ok, S#mg{load_counter = N-1}};mg_handle_request({handle_trans_ack, _CH, _PV, _AS, _AD}, S) -> {ok, S}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%await_started(Pid) -> receive {started, Pid} -> d("await_started ~p: ok", [Pid]), {ok, Pid}; {'EXIT', Pid, Reason} -> i("await_started ~p: received exit signal: ~p", [Pid, Reason]), exit({failed_starting, Pid, Reason}) after 10000 -> i("await_started ~p: timeout", [Pid]), exit({error, timeout}) end.stop(Pid) -> d("stop ~p", [Pid]), Pid ! {stop, self()}, receive {stopped, Pid} -> d("stop -> received stopped from ~p", [Pid]), ok; {'EXIT', Pid, Reason} -> i("stop ~p: received exit signal: ~p", [Pid, Reason]), exit({failed_stopping, Pid, Reason}) after 10000 -> exit({error, timeout}) end.get_stats(Pid, No) -> d("get_stats ~p", [Pid]), Pid ! {statistics, No, self()}, receive {statistics, No, Stats, Pid} -> {ok, Stats}; {'EXIT', Pid, Reason} -> i("get_stats ~p: received exit signal: ~p", [Pid, Reason]), exit({failed_getting_stats, Pid, Reason}) after 10000 -> exit({error, timeout}) end.service_change(Pid) -> d("service_change ~p", [Pid]), Pid ! {service_change, self()}, receive {service_change_reply, Res, Pid} -> {ok, Res}; {'EXIT', Pid, Reason} -> i("service_change ~p: received exit signal: ~p", [Pid, Reason]), exit({failed_service_change, Pid, Reason}) after 10000 -> exit({error, timeout}) end.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%notify_started(Parent) -> Parent ! {started, self()}.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The megaco user callback interface handle_connect(CH, PV, Pid) ->% i("handle_connect -> entry with"% "~n CH: ~p"% "~n PV: ~p"% "~n Pid: ~p", [CH, PV, Pid]), case CH#megaco_conn_handle.remote_mid of preliminary_mid -> %% Avoids deadlock ok; _ -> Reply = request(Pid, {handle_connect, CH, PV}),% d("handle_connect -> Reply:~n~p", [Reply]), Reply end.handle_disconnect(_CH, _PV, {user_disconnect, {Pid, ignore}}, Pid) ->% i("handle_disconnect(ignore) -> entry with"% "~n CH: ~p"% "~n PV: ~p", [CH, PV]), %% Avoids deadlock ok;handle_disconnect(CH, PV, R, Pid) ->% i("handle_disconnect -> entry with"% "~n CH: ~p"% "~n PV: ~p"% "~n R: ~p", [CH, PV, R]), request(Pid, {handle_disconnect, CH, PV, R}).handle_syntax_error(ReceiveHandle, ProtocolVersion, ErrorDescriptor, Pid) ->% i("handle_syntax_error -> entry with"% "~n ReceiveHandle: ~p"% "~n ProtocolVersion: ~p"% "~n ErrorDescriptor: ~p", % [ReceiveHandle, ProtocolVersion, ErrorDescriptor]), Req = {handle_syntax_error, ReceiveHandle, ProtocolVersion, ErrorDescriptor}, request(Pid, Req). handle_message_error(ConnHandle, ProtocolVersion, ErrorDescriptor, Pid) ->% i("handle_message_error -> entry with"% "~n ConnHandle: ~p"% "~n ProtocolVersion: ~p"% "~n ErrorDescriptor: ~p", % [ConnHandle, ProtocolVersion, ErrorDescriptor]), Req = {handle_message_error, ConnHandle, ProtocolVersion, ErrorDescriptor}, request(Pid, Req).handle_trans_request(CH, PV, AR, Pid) ->% i("handle_trans_request -> entry with" % "~n CH: ~p"% "~n PV: ~p"% "~n AR: ~p"% "~n Pid: ~p", [CH, PV, AR, Pid]), Reply = request(Pid, {handle_trans_request, CH, PV, AR}),% i("handle_trans_request -> Reply:~n~p", [Reply]), Reply.handle_trans_long_request(ConnHandle, ProtocolVersion, ReqData, Pid) ->% i("handle_trans_long_request -> entry with"% "~n ConnHandle: ~p"% "~n ProtocolVersion: ~p"% "~n ReqData: ~p", [ConnHandle, ProtocolVersion, ReqData]), Req = {handle_trans_long_request, ConnHandle, ProtocolVersion, ReqData}, request(Pid, Req).handle_trans_reply(ConnHandle, ProtocolVersion, ActualReply, ReplyData, Pid) ->% i("handle_trans_reply -> entry with"% "~n ConnHandle: ~p"% "~n ProtocolVersion: ~p"% "~n ActualReply: ~p"% "~n ReplyData: ~p", % [ConnHandle, ProtocolVersion, ActualReply, ReplyData]), Req = {handle_trans_reply, ConnHandle, ProtocolVersion, ActualReply, ReplyData}, request(Pid, Req).handle_trans_ack(ConnHandle, ProtocolVersion, AckStatus, AckData, Pid) ->% i("handle_trans_ack -> entry with"% "~n ConnHandle: ~p"% "~n ProtocolVersion: ~p"% "~n AckStatus: ~p"% "~n AckData: ~p", % [ConnHandle, ProtocolVersion, AckStatus, AckData]), Req = {handle_trans_ack, ConnHandle, ProtocolVersion, AckStatus, AckData}, request(Pid, Req).request(Pid, Request) -> Pid ! {request, Request, self()}, receive {reply, Reply, Pid} -> Reply end.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%sleep(X) -> receive after X -> ok end.error_msg(F,A) -> error_logger:error_msg(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.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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~p:~s: " ++ F ++ "~n", [P, self(), get(sname) | A]);print(_, _, _, _) -> ok.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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 + =
减小字号Ctrl + -
显示快捷键?