1%%--------------------------------------------------------------------
2%%
3%% %CopyrightBegin%
4%%
5%% Copyright Ericsson AB 2001-2015. 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    : cosEventDomainApp.erl
24%% Purpose :
25%%--------------------------------------------------------------------
26
27-module(cosEventDomainApp).
28
29%%--------------- INCLUDES -----------------------------------
30-include_lib("orber/include/corba.hrl").
31-include_lib("orber/include/ifr_types.hrl").
32-include_lib("cosNotification/include/CosNotification.hrl").
33%% Application files
34-include("cosEventDomainApp.hrl").
35-include("CosEventDomainAdmin.hrl").
36
37%%--------------- EXPORTS ------------------------------------
38%% External MISC
39-export([get_option/3,
40	 create_id/1,
41         is_debug_compiled/0,
42	 install/0,
43	 uninstall/0,
44	 start_factory/0,
45	 start_factory/1,
46	 start_factory_link/0,
47	 start_factory_link/1,
48	 stop_factory/1,
49	 start/0,
50	 stop/0,
51	 create_link/3,
52	 get_qos/1,
53	 get_admin/1]).
54
55%% Application callbacks
56-export([start/2, init/1, stop/1]).
57
58%%--------------- DEFINES ------------------------------------
59
60-define(SUPERVISOR_NAME, oe_cosEventDomainSup).
61-define(SUP_FLAG,        {simple_one_for_one,50,10}).
62
63-define(SUP_SPEC(Name, Args),
64        ['CosEventDomainAdmin_EventDomain',Args,
65         [{sup_child, true}, {regname, {global, Name}}]]).
66-define(SUP_CHILD,
67        {"oe_EventDomainChild",
68         {cosEventDomainApp,create_link, []},
69	 transient,100000,worker,
70         ['CosEventDomainAdmin_EventDomain']}).
71
72
73-define(DEFAULT_OPTIONS, []).
74
75%%--------------- DEFINITIONS OF CONSTANTS -------------------
76%%--------------- EXTERNAL MISC FUNCTIONS --------------------
77%%-----------------------------------------------------------%
78%% function : install
79%% Arguments:
80%% Returns  :
81%% Exception:
82%% Effect   :
83%%------------------------------------------------------------
84install() ->
85    oe_CosEventDomainAdmin:oe_register().
86
87%%-----------------------------------------------------------%
88%% function : uninstall
89%% Arguments:
90%% Returns  :
91%% Exception:
92%% Effect   :
93%%------------------------------------------------------------
94uninstall() ->
95    oe_CosEventDomainAdmin:oe_unregister().
96
97%%-----------------------------------------------------------%
98%% function : start/stop
99%% Arguments:
100%% Returns  :
101%% Effect   : Starts or stops the cosTime application.
102%%------------------------------------------------------------
103
104start() ->
105    application:start(cosEventDomain).
106stop() ->
107    application:stop(cosEventDomain).
108
109%%-----------------------------------------------------------%
110%% function : start
111%% Arguments: Type - see module application
112%%            Arg  - see module application
113%% Returns  :
114%% Effect   : Module callback for application
115%%------------------------------------------------------------
116
117start(_, _) ->
118    supervisor:start_link({local, ?SUPERVISOR_NAME}, cosEventDomainApp, app_init).
119
120
121%%-----------------------------------------------------------%
122%% function : stop
123%% Arguments: Arg - see module application
124%% Returns  :
125%% Effect   : Module callback for application
126%%------------------------------------------------------------
127
128stop(_) ->
129    ok.
130
131%%-----------------------------------------------------------%
132%% function : start_channel
133%% Arguments: -
134%% Returns  :
135%% Effect   :
136%%------------------------------------------------------------
137start_factory() ->
138    start_factory(?DEFAULT_OPTIONS).
139
140start_factory(Options) when is_list(Options) ->
141    'CosEventDomainAdmin_EventDomainFactory':oe_create(Options);
142start_factory(Options) ->
143    orber:dbg("[~p] cosEventDomainApp:start_factory(~p);~n"
144	      "Options not correct.",
145	      [?LINE, Options], ?DEBUG_LEVEL),
146    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
147
148%%-----------------------------------------------------------%
149%% function : start_channel
150%% Arguments: -
151%% Returns  :
152%% Effect   :
153%%------------------------------------------------------------
154start_factory_link() ->
155    start_factory_link(?DEFAULT_OPTIONS).
156
157start_factory_link(Options) when is_list(Options) ->
158    'CosEventDomainAdmin_EventDomainFactory':oe_create_link(Options);
159start_factory_link(Options) ->
160    orber:dbg("[~p] cosEventDomainApp:start_factory_link(~p);~n"
161	      "Options not correct.",
162	      [?LINE, Options], ?DEBUG_LEVEL),
163    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
164
165%%-----------------------------------------------------------%
166%% function : stop_factory
167%% Arguments: ChannelObj
168%% Returns  :
169%% Effect   :
170%%------------------------------------------------------------
171stop_factory(ChannelObj) ->
172    corba:dispose(ChannelObj).
173
174%%-----------------------------------------------------------%
175%% function : init
176%% Arguments:
177%% Returns  :
178%% Effect   :
179%%------------------------------------------------------------
180
181%% Starting using create_factory/X
182init(own_init) ->
183    {ok,{?SUP_FLAG, [?SUP_CHILD]}};
184%% When starting as an application.
185init(app_init) ->
186    {ok,{?SUP_FLAG, [?SUP_CHILD]}}.
187
188
189%%------------------------------------------------------------
190%% function : create_link
191%% Arguments: Module - which Module to call
192%%            Env/ArgList - ordinary oe_create arguments.
193%% Returns  :
194%% Exception:
195%% Effect   : Necessary since we want the supervisor to be a
196%%            'simple_one_for_one'. Otherwise, using for example,
197%%            'one_for_one', we have to call supervisor:delete_child
198%%            to remove the childs startspecification from the
199%%            supervisors internal state.
200%%------------------------------------------------------------
201create_link(Module, Env, ArgList) ->
202    Module:oe_create_link(Env, ArgList).
203
204%%-----------------------------------------------------------%
205%% function : get_option
206%% Arguments:
207%% Returns  :
208%% Exception:
209%% Effect   :
210%%------------------------------------------------------------
211get_option(Key, OptionList, DefaultList) ->
212    case lists:keysearch(Key, 1, OptionList) of
213        {value,{Key,Value}} ->
214            Value;
215        _ ->
216            case lists:keysearch(Key, 1, DefaultList) of
217                {value,{Key,Value}} ->
218                    Value;
219                _->
220                    {error, "Invalid option"}
221            end
222    end.
223
224%%------------------------------------------------------------
225%% function : create_id/1
226%% Arguments: CosEventDomainAdmin::DomainID (long)
227%% Returns  : CosEventDomainAdmin::DomainID (long)
228%% Exception:
229%% Purpose  :
230%%------------------------------------------------------------
231create_id(2147483647) ->
232    -2147483648;
233create_id(OldID) ->
234    OldID+1.
235
236%%------------------------------------------------------------
237%% function : get_qos
238%% Arguments:
239%% Returns  :
240%% Exception:
241%% Effect   :
242%%------------------------------------------------------------
243get_qos([]) ->
244    [];
245get_qos(Properties) ->
246    case get_qos(Properties, [], []) of
247	{ok, Supported} ->
248	    Supported;
249	{error, Unsupported} ->
250	    corba:raise(#'CosNotification_UnsupportedQoS'{qos_err = Unsupported})
251    end.
252
253get_qos([], Supported, []) ->
254    {ok, Supported};
255get_qos([], _, Unsupported) ->
256    {error, Unsupported};
257get_qos([#'CosNotification_Property'{name = ?CycleDetection,
258				     value= #any{value = ?AuthorizeCycles}}|T],
259	Supported, Unsupported) ->
260    get_qos(T, [{?CycleDetection, ?AuthorizeCycles}|Supported], Unsupported);
261get_qos([#'CosNotification_Property'{name = ?CycleDetection,
262				     value= #any{value = ?ForbidCycles}}|T],
263	Supported, Unsupported) ->
264    get_qos(T, [{?CycleDetection, ?ForbidCycles}|Supported], Unsupported);
265get_qos([#'CosNotification_Property'{name = ?CycleDetection}|T],
266	Supported, Unsupported) ->
267    %% Illegal value supplied.
268    get_qos(T, Supported,
269	    [#'CosNotification_PropertyError'
270	     {code = 'UNSUPPORTED_VALUE',
271	      name = ?CycleDetection,
272	      available_range = #'CosNotification_PropertyRange'
273	      {low_val=any:create(orber_tc:short(), ?AuthorizeCycles),
274	       high_val=any:create(orber_tc:short(), ?ForbidCycles)}}|Unsupported]);
275get_qos([#'CosNotification_Property'{name = ?DiamondDetection,
276				     value= #any{value = ?AuthorizeDiamonds}}|T],
277	      Supported, Unsupported) ->
278    get_qos(T, [{?DiamondDetection, ?AuthorizeDiamonds}|Supported], Unsupported);
279get_qos([#'CosNotification_Property'{name = ?DiamondDetection,
280				     value= #any{value = ?ForbidDiamonds}}|T],
281	Supported, Unsupported) ->
282    get_qos(T, [{?DiamondDetection, ?ForbidDiamonds}|Supported], Unsupported);
283get_qos([#'CosNotification_Property'{name = ?DiamondDetection}|T],
284	Supported, Unsupported) ->
285    %% Illegal value supplied.
286    get_qos(T, Supported,
287	    [#'CosNotification_PropertyError'
288	     {code = 'UNSUPPORTED_VALUE',
289	      name = ?DiamondDetection,
290	      available_range = #'CosNotification_PropertyRange'
291	      {low_val=any:create(orber_tc:short(), ?AuthorizeDiamonds),
292	       high_val=any:create(orber_tc:short(), ?ForbidDiamonds)
293	      }} | Unsupported]);
294get_qos([#'CosNotification_Property'{name = Name}|T], Supported, Unsupported) ->
295    %% Unknown QoS supplied.
296    get_qos(T, Supported,
297	    [#'CosNotification_PropertyError'
298	     {code = 'BAD_PROPERTY',
299	      name = Name,
300	      available_range = #'CosNotification_PropertyRange'
301	      {low_val=any:create(orber_tc:null(), null),
302	       high_val=any:create(orber_tc:null(), null)}} | Unsupported]).
303
304%%------------------------------------------------------------
305%% function : get_admin
306%% Arguments:
307%% Returns  : {"EXCEPTION', #'CosNotification_PropertyError'{}}
308%% Exception:
309%% Effect   : No Admin supported.
310%%------------------------------------------------------------
311get_admin([]) ->
312    [];
313get_admin(Properties) ->
314    get_admin(Properties, []).
315
316get_admin([], Unsupported) ->
317    corba:raise(#'CosNotification_UnsupportedAdmin'{admin_err = Unsupported});
318get_admin([#'CosNotification_Property'{name = Name}|T], Unsupported) ->
319    %% Unknown QoS supplied.
320    get_admin(T, [#'CosNotification_PropertyError'
321		  {code = 'BAD_PROPERTY',
322		   name = Name,
323		   available_range = #'CosNotification_PropertyRange'
324		   {low_val=any:create(orber_tc:null(), null),
325		    high_val=any:create(orber_tc:null(), null)}} | Unsupported]).
326
327
328%%------------------------------------------------------------
329%% function : is_debug_compiled
330%% Arguments:
331%% Returns  :
332%% Exception:
333%% Effect   :
334%%------------------------------------------------------------
335-ifdef(debug).
336    is_debug_compiled() -> true.
337-else.
338    is_debug_compiled() -> false.
339-endif.
340
341%%--------------- END OF MODULE ------------------------------
342