1%%--------------------------------------------------------------------
2%%
3%% %CopyrightBegin%
4%%
5%% Copyright Ericsson AB 1999-2016. All Rights Reserved.
6%%
7%% Licensed under the Apache License, Version 2.0 (the "License");
8%% you may not use this file except in compliance with the License.
9%% You may obtain a copy of the License at
10%%
11%%     http://www.apache.org/licenses/LICENSE-2.0
12%%
13%% Unless required by applicable law or agreed to in writing, software
14%% distributed under the License is distributed on an "AS IS" BASIS,
15%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16%% See the License for the specific language governing permissions and
17%% limitations under the License.
18%%
19%% %CopyrightEnd%
20%%
21%%
22%%-------------------------------------------------------------------
23%% File    : CosNotifyChannelAdmin_EventChannelFactory_impl.erl
24%% Purpose :
25%%-------------------------------------------------------------------
26
27-module('CosNotifyChannelAdmin_EventChannelFactory_impl').
28
29%%--------------- INCLUDES -----------------------------------
30%% Application files
31-include_lib("orber/include/corba.hrl").
32-include_lib("orber/include/ifr_types.hrl").
33%% Application files
34-include("CosNotification.hrl").
35-include("CosNotifyChannelAdmin.hrl").
36-include("CosNotifyComm.hrl").
37-include("CosNotifyFilter.hrl").
38-include("CosNotification_Definitions.hrl").
39
40%%--------------- IMPORTS ------------------------------------
41
42%%--------------- EXPORTS ------------------------------------
43%% External
44-export([create_channel/5,
45	 get_all_channels/3,
46	 get_event_channel/4]).
47
48%%--------------- gen_server specific exports ----------------
49-export([handle_info/2, code_change/3]).
50-export([init/1, terminate/2]).
51
52%%--------------- LOCAL DEFINITIONS --------------------------
53%% Data structures
54-record(state, {adminProp,
55		idCounter = 0,
56		options,
57		etsR,
58		server_options}).
59
60%%-----------------------------------------------------------%
61%% function : handle_info, code_change
62%% Arguments: See gen_server documentation.
63%% Effect   : Functions demanded by the gen_server module.
64%%------------------------------------------------------------
65
66code_change(_OldVsn, State, _Extra) ->
67    {ok, State}.
68
69handle_info(Info, State) ->
70    ?debug_print("INFO: ~p~n", [Info]),
71    case Info of
72        {'EXIT', Pid, normal} ->
73	    ets:match_delete(State#state.etsR, {'_','_',Pid}),
74            {noreply, State};
75        _Other ->
76            ?debug_print("TERMINATED: ~p~n",[_Other]),
77            {noreply, State}
78    end.
79
80%%----------------------------------------------------------%
81%% function : init, terminate
82%% Arguments:
83%%-----------------------------------------------------------
84
85init(Options) ->
86    process_flag(trap_exit, true),
87    SO = 'CosNotification_Common':get_option(server_options, Options, ?not_DEFAULT_SETTINGS),
88    {ok, #state{options = Options,
89		etsR    = ets:new(oe_ets, [set, protected]),
90		server_options = SO}}.
91
92terminate(_Reason, _State) ->
93    ok.
94
95%%-----------------------------------------------------------
96%%------- Exported external functions -----------------------
97%%-----------------------------------------------------------
98%%----------------------------------------------------------%
99%% function : create_channel
100%% Arguments: InitQoS
101%%            InitAdmin
102%% Returns  : Ch - Channel obj ref
103%%            Id - Channel Id (out-type)
104%%-----------------------------------------------------------
105create_channel(OE_THIS, _OE_FROM, State, InitQoS, InitAdmin) ->
106    {QoS, LQoS} = 'CosNotification_Common':init_qos(InitQoS),
107    {IAdm, LAdm} = 'CosNotification_Common':init_adm(InitAdmin),
108    Id = 'CosNotification_Common':create_id(State#state.idCounter),
109    case 'CosNotifyChannelAdmin_EventChannel':oe_create_link([OE_THIS, QoS, IAdm,
110							      LQoS, LAdm,
111							      State#state.options],
112							     [{sup_child, true}|State#state.server_options]) of
113	{ok, Pid, Ch} ->
114	    ets:insert(State#state.etsR, {Id,Ch,Pid}),
115	    {reply, {Ch, Id}, State#state{idCounter=Id}};
116	_ ->
117	    corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO})
118    end.
119
120%%----------------------------------------------------------%
121%% function : get_all_channels
122%% Arguments: -
123%% Returns  : ChannelIDSeq - List of alive channels created
124%%            by this factory.
125%%-----------------------------------------------------------
126get_all_channels(_OE_THIS, _OE_FROM, State) ->
127    {reply, lists:flatten(ets:match(State#state.etsR, {'$1','_','_'})), State}.
128
129%%----------------------------------------------------------%
130%% function : get_event_channel
131%% Arguments: ChannelId
132%% Returns  : ChannelRef | 'CosNotifyChannelAdmin_ChannelNotFound'
133%%-----------------------------------------------------------
134get_event_channel(_OE_THIS, _OE_FROM, State, Id) ->
135    {reply, find_obj(ets:lookup(State#state.etsR, Id)), State}.
136
137%%--------------- LOCAL FUNCTIONS ----------------------------
138find_obj([]) -> {'EXCEPTION', #'CosNotifyChannelAdmin_ChannelNotFound'{}};
139find_obj([{_, Obj,_}]) -> Obj;
140find_obj(_) -> {'EXCEPTION', #'CosNotifyChannelAdmin_ChannelNotFound'{}}.
141
142%%--------------- MISC FUNCTIONS, E.G. DEBUGGING -------------
143%%--------------- END OF MODULE ------------------------------
144