⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 megaco_session_impl.erl

📁 一个Megaco实现源代码
💻 ERL
📖 第 1 页 / 共 3 页
字号:
%% ``The contents of this file are subject to the Erlang Public License,%% Version 1.1, (the "License"); you may not use this file except in%% compliance with the License. You should have received a copy of the%% Erlang Public License along with this software. If not, it can be%% retrieved via the world wide web at http://www.erlang.org/.%%%% Software distributed under the License is distributed on an "AS IS"%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See%% the License for the specific language governing rights and limitations%% under the License.%%%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings%% AB. All Rights Reserved.''%%%%     $Id$%%%%----------------------------------------------------------------------%% Purpose : Actual implementation of the Megaco Session interface.%%-----------------------------------------------------------------------module('Megaco_Session_impl').-behaviour(megaco_user).%% Megaco user callbacks:-export([         handle_connect/3,         handle_disconnect/4,         handle_syntax_error/4,         handle_message_error/4,         handle_trans_request/4,         handle_trans_long_request/4,         handle_trans_reply/5,         handle_trans_ack/5,	 handle_unexpected_trans/4        ]).%% Genserver callbacks:-export([init/1, terminate/2, code_change/3, handle_info/2]).%% Requests from the (C-) user:-export([	 systemInfo/4,	 startUser/5,	 stopUser/4,	 userInfo/5,	 updateUserInfo/5,	 connInfo/5,	 updateConnInfo/5,	 connect/7,	 disconnect/5,	 sendTransRequest/6,	 cancel/5	]).%% Reponses from the (C-) user:%% -export([%% 	    handleConnectResponse/3,%% 	    handleDisconnectResponse/3,%% 	    handleSyntaxErrorResponse/5,%% 	    handleMessageErrorResponse/3,%% 	    handleTransRequestPending/2,%% 	    handleTransRequestResponse/5%% 	   ]).-include_lib("megaco/include/megaco.hrl").-include_lib("megaco/include/megaco_message_v1.hrl").-include_lib("megaco_session/include/Megaco.hrl").-include_lib("megaco_session/include/MegacoMessage.hrl").%% Records:-record(state, {parent_pid, mid, auto_stop_user = true}).-record(reply_data, {request_ref, reply_to}).-record(pending_data, {local_pid, remote_pid, request_ref, mon_ref}).-record(ack_data, {local_pid, remote_pid, request_ref}).-define(EXT(Fun, Value), megaco_session_externalizer:Fun(Value, undefined)).-define(INT(Fun, Value), megaco_session_internalizer:Fun(Value, undefined)).-define(d(F,A),dbg(F,A)).%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Gen-server callbacks:%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%init(Env) ->    process_flag(trap_exit, true),    put(debug,get_debug()),    ?d("init -> entry", []),    case (catch do_init(Env)) of	{ok, State} ->	    {ok, State};	{error, Reason} ->	    {stop, Reason}    end.    do_init(Env) ->    ParentPid    = get_parent_pid(Env),    AutoStopUser = get_auto_stop_user(Env),    {ok, #state{parent_pid = ParentPid, auto_stop_user = AutoStopUser}}.terminate(normal, State) ->    ?d("terminate -> entry with"       "~n   Reason: ~p", [normal]),    ok;terminate(Reason, #state{mid = undefined} = State) ->    ?d("terminate(undefined mid) -> entry with"       "~n   Reason: ~p", [Reason]),    ok;terminate(Reason, #state{mid = Mid, auto_stop_user = true} = State) ->    ?d("terminate -> entry with"       "~n   Mid:    ~p"       "~n   Reason: ~p", [Mid, Reason]),    Res = stop_user(Mid, Reason),    ?d("terminate -> stop user result: ~p", [Res]),    ok;terminate(Reason, State) ->    ?d("terminate -> entry with"       "~n   Reason: ~p", [Reason]),    ok.stop_user(Mid, Reason) ->    disconnect(megaco:user_info(Mid, connections), Reason),    megaco:stop_user(Mid).disconnect([], _) ->    ok;disconnect([CH|CHs], Reason) ->    ?d("disconnecting ~p", [CH]),    megaco:disconnect(CH, Reason),    disconnect(CHs, Reason).     code_change({down, Vsn}, State, Extra) ->    {ok, State};code_change(Vsn, State, Extra) ->    {ok, State}.handle_info({'EXIT', Pid, Reason}, State) when Pid == State#state.parent_pid ->    ?d("handle_info(EXIT) -> entry when parent (~p) dies"       "~n   Reason: ~p", [Pid, Reason]),    {stop, Reason, State};handle_info(Info, State) ->    ok = error_logger:format("~p(~p): handle_info(~p, ~p)~n",                             [?MODULE, self(), Info, State]),    {noreply, State}.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Requests from the (C-) user:%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%systemInfo(State, Ref, ReplyTo, Item) ->    ?d("systemInfo -> entry with"       "~n   Ref:     ~w"       "~n   ReplyTo: ~w"       "~n   Item:    ~w", [Ref, ReplyTo, Item]),    case catch system_info_wrapper(Item) of	{'EXIT', Reason} ->	    Status = ?EXT(tr_Status, {'EXIT', Reason}),	    	    Dummy = #'Megaco_SystemInfoValue'{label = 'Megaco_SystemInfo_connections', value = []},	    'Megaco_SessionUser':systemInfoResponse(ReplyTo, Ref, Status, 						    Dummy),	    {noreply, State};	Value ->	    Status = ?EXT(tr_Status, ok),	    V = #'Megaco_SystemInfoValue'{label = Item, value = Value},	    'Megaco_SessionUser':systemInfoResponse(ReplyTo, Ref, Status, V),	    {noreply, State}    end.system_info_wrapper(Item) ->    %% Will exit at error    case Item of	'SystemInfo_connections' ->	    [?EXT(tr_conn_handle, CH) || CH <- megaco:system_info(connections)];	'SystemInfo_users' ->	    [?EXT(tr_MId, Mid) || Mid <- megaco:system_info(users)];	'SystemInfo_NActiveRequests' ->	    megaco:system_info(n_active_requests);	'SystemInfo_NActiveReplies' ->	    megaco:system_info(n_active_replies);	'SystemInfo_NActiveConnections' ->	    megaco:system_info(n_active_connections)    end.%% ----------------------------------------------------------------------startUser(State, Ref, ReplyTo, Mid, Config) ->    ?d("startUser -> entry with"       "~n   Ref:     ~w"       "~n   ReplyTo: ~w"       "~n   Mid:     ~w"       "~n   Config:  ~w", [Ref, ReplyTo, Mid, Config]),    M = ?INT(tr_MId, Mid),    ?d("startUser -> M: ~p", [M]),    C = [?INT(tr_user_info_value, V) || V <- Config],    ?d("startUser -> start megaco user", []),    UserArgs = [{user_mod, ?MODULE}, {user_args, [self()]} | C],    Res = megaco:start_user(M, UserArgs),    ?d("startUser -> user registration result: ~w", [Res]),    Status = ?EXT(tr_Status, Res),	        'Megaco_SessionUser':startUserResponse(ReplyTo, Ref, Status),    ?d("startUser -> done", []),    case Res of	ok ->	    {noreply, State#state{mid = M}};	_ ->	    {noreply, State}    end.	%% ----------------------------------------------------------------------stopUser(State, Ref, ReplyTo, Mid) ->    ?d("stopUser -> entry with"       "~n   Ref:     ~w"       "~n   ReplyTo: ~w"       "~n   Mid:     ~w", [Ref, ReplyTo, Mid]),    M      = ?INT(tr_MId, Mid),    Res    = megaco:stop_user(M),    ?d("stopUser -> Res: ~p", [Res]),    Status = ?EXT(tr_Status, Res),	        'Megaco_SessionUser':stopUserResponse(ReplyTo, Ref, Status),    {noreply, State}.%% ----------------------------------------------------------------------userInfo(State, Ref, ReplyTo, Mid, Item) ->    ?d("userInfo -> entry with"       "~n   Ref:     ~w"       "~n   ReplyTo: ~w"       "~n   Mid:     ~w"       "~n   Item:    ~w", [Ref, ReplyTo, Mid, Item]),    case catch user_info_wrapper(Mid, Item) of	{'EXIT', Reason} ->	    Status = ?EXT(tr_Status, {'EXIT', Reason}),	    	    Dummy = #'Megaco_UserInfoValue'{label = 'UserInfo_connections',					    value = []},	    'Megaco_SessionUser':userInfoResponse(ReplyTo, Ref, Status, Dummy),	    {noreply, State};	Value ->	    Status = ?EXT(tr_Status, ok),	    V = #'Megaco_UserInfoValue'{label = Item, value = Value},	    'Megaco_SessionUser':userInfoResponse(ReplyTo, Ref, Status, V),	    {noreply, State}    end.user_info_wrapper(ExternalMid, Item) ->    Mid = ?INT(tr_MId, ExternalMid),    %% Will exit at error    case Item of        'UserInfo_connections' ->	    [?EXT(tr_conn_handle, CH) || CH <- megaco:user_info(Mid, connections)];        'UserInfo_receiveHandle' ->	    ?EXT(tr_receive_handle, megaco:user_info(Mid, receive_handle));        'UserInfo_minTransId' ->	    megaco:user_info(Mid, min_trans_id);         'UserInfo_maxTransId' ->	    megaco:user_info(Mid, max_trans_id);         'UserInfo_requestTimer' ->	    ?EXT(tr_timer, megaco:user_info(Mid, request_timer));         'UserInfo_longRequestTimer' ->	    ?EXT(tr_timer, megaco:user_info(Mid, long_request_timer));         'UserInfo_autoAck' ->	    megaco:user_info(Mid, auto_ack);         'UserInfo_replyTimer' ->	    ?EXT(tr_timer, megaco:user_info(Mid, reply_timer));         'UserInfo_pendingTimer' ->	    ?EXT(tr_timer, megaco:user_info(Mid, pending_timer));         'UserInfo_sendMod' ->	    ?EXT(tr_atom, megaco:user_info(Mid, send_mod));         'UserInfo_encodingMod' ->	    ?EXT(tr_atom, megaco:user_info(Mid, encoding_mod));         'UserInfo_encodingConfig' ->	    ?EXT(tr_term, megaco:user_info(Mid, encoding_config));         'UserInfo_protocolVersion' ->	    megaco:user_info(Mid, protocol_version);         'UserInfo_replyData' ->	    ?EXT(tr_term, megaco:user_info(Mid, reply_data));         'UserInfo_userMod' ->	    ?EXT(tr_atom, megaco:user_info(Mid, user_mod));         'UserInfo_userArgs' ->	    ?EXT(tr_term, megaco:user_info(Mid, user_args))    end.%% ----------------------------------------------------------------------updateUserInfo(State, Ref, ReplyTo, Mid, ItemValue) ->    ?d("updateUserInfo -> entry with"       "~n   Ref:       ~w"       "~n   ReplyTo:   ~w"       "~n   Mid:       ~w"       "~n   ItemValue: ~w", [Ref, ReplyTo, Mid, ItemValue]),    M = ?INT(tr_MId, Mid),

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -