1%%--------------------------------------------------------------------
2%%
3%% %CopyrightBegin%
4%%
5%% Copyright Ericsson AB 1999-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    : cosNotificationApp.erl
24%% Purpose :
25%%----------------------------------------------------------------------
26
27-module(cosNotificationApp).
28
29%%--------------- INCLUDES -----------------------------------
30%% Local
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
39-include("CosNotification_Definitions.hrl").
40%%--------------- EXPORTS-------------------------------------
41%% cosNotification API external
42-export([start/0, stop/0,
43	 start_factory/1, start_factory/0, stop_factory/1,
44	 start_global_factory/0, start_global_factory/1,
45	 start_filter_factory/1, start_filter_factory/0, stop_filter_factory/1,
46	 install/0, install/1, uninstall/0, uninstall/1,
47	 install_event/0, install_event/1, uninstall_event/0, uninstall_event/1,
48	 install_typed/0, install_typed/1, uninstall_typed/0, uninstall_typed/1,
49	 create_structured_event/6, type_check/0, notify/0, max_events/0,
50	 timeout_events/0, interval_events/0]).
51
52%% Application callbacks
53-export([start/2, init/1, stop/1]).
54
55%%--------------- DEFINES ------------------------------------
56-define(IDL_MODULES, ['oe_CosNotification',
57		      'oe_cosNotificationAppComm',
58		      'oe_CosNotifyComm',
59		      'oe_CosNotifyFilter',
60		      'oe_CosNotifyChannelAdmin']).
61-define(EVENT_IDL_MODULES, ['oe_CosEventComm',
62			    'oe_CosEventChannelAdmin']).
63-define(TYPED_IDL_MODULES, ['oe_CosTypedEvent',
64			    'oe_CosTypedNotification']).
65
66-define(FACTORY_NAME,    oe_cosNotificationFactory).
67-define(SUPERVISOR_NAME, cosNotificationSup).
68
69
70%%------------------------------------------------------------
71%% function : install/X
72%% Arguments: - | Time (seconds)
73%% Returns  : ok | EXIT | EXCEPTION
74%% Effect   : Install necessary data in the IFR DB
75%%------------------------------------------------------------
76
77install() ->
78    install(0).
79
80install(Time) when is_integer(Time) ->
81    install_loop(?IDL_MODULES, timer:seconds(Time));
82install(_Time) ->
83    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
84
85%%------------------------------------------------------------
86%% function : install_event/X
87%% Arguments: - | Time (seconds)
88%% Returns  : ok | EXIT | EXCEPTION
89%% Effect   : Install necessary data in the IFR DB
90%%------------------------------------------------------------
91
92install_event() ->
93    install_event(0).
94
95install_event(Time) when is_integer(Time) ->
96    install_loop(?EVENT_IDL_MODULES, timer:seconds(Time));
97install_event(_Time) ->
98    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
99
100%%------------------------------------------------------------
101%% function : install_typed/X
102%% Arguments: - | Time (seconds)
103%% Returns  : ok | EXIT | EXCEPTION
104%% Effect   : Install necessary data in the IFR DB
105%%------------------------------------------------------------
106
107install_typed() ->
108    install_typed(0).
109
110install_typed(Time) when is_integer(Time) ->
111    install_loop(?TYPED_IDL_MODULES, timer:seconds(Time));
112install_typed(_Time) ->
113    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
114
115install_loop([], _) ->
116    ok;
117install_loop([H|T], Time) ->
118    H:'oe_register'(),
119    timer:sleep(Time),
120    install_loop(T, Time).
121
122%%------------------------------------------------------------
123%% function : uninstall/X
124%% Arguments: - | Time (seconds)
125%% Returns  : ok | EXIT | EXCEPTION
126%% Effect   : Remove data related to cosNotificationin from the IFR DB
127%%------------------------------------------------------------
128
129uninstall() ->
130    uninstall(0).
131
132uninstall(Time) when is_integer(Time) ->
133    uninstall_loop(lists:reverse(?IDL_MODULES), timer:seconds(Time));
134uninstall(_Time) ->
135    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
136
137%%------------------------------------------------------------
138%% function : uninstall_event/X
139%% Arguments: - | Time (seconds)
140%% Returns  : ok | EXIT | EXCEPTION
141%% Effect   : Remove data related to cosNotificationin from the IFR DB
142%%------------------------------------------------------------
143
144uninstall_event() ->
145    uninstall_event(0).
146
147uninstall_event(Time) when is_integer(Time) ->
148    uninstall_loop(lists:reverse(?EVENT_IDL_MODULES), timer:seconds(Time));
149uninstall_event(_Time) ->
150    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
151
152%%------------------------------------------------------------
153%% function : uninstall_typed/X
154%% Arguments: - | Time (seconds)
155%% Returns  : ok | EXIT | EXCEPTION
156%% Effect   : Remove data related to cosNotificationin from the IFR DB
157%%------------------------------------------------------------
158
159uninstall_typed() ->
160    uninstall_typed(0).
161
162uninstall_typed(Time) when is_integer(Time) ->
163    uninstall_loop(lists:reverse(?TYPED_IDL_MODULES), timer:seconds(Time));
164uninstall_typed(_Time) ->
165    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
166
167uninstall_loop([], _) ->
168    ok;
169uninstall_loop([H|T], Time) ->
170    H:'oe_unregister'(),
171    timer:sleep(Time),
172    uninstall_loop(T, Time).
173
174
175%%------------------------------------------------------------
176%% function : start/stop
177%% Arguments:
178%% Returns  :
179%% Effect   : Starts or stops the cosTRansaction application.
180%%------------------------------------------------------------
181
182start() ->
183    application:start(cosNotification).
184stop() ->
185    application:stop(cosNotification).
186
187%%------------------------------------------------------------
188%% function : start_factory
189%% Arguments: none or an argumentlist whith default values.
190%% Returns  : ObjectRef | {'EXCEPTION', _} | {'EXIT', Reason}
191%% Effect   : Starts a CosNotifyChannelAdmin_EventChannelFactory
192%%------------------------------------------------------------
193start_factory() ->
194    start_factory(?not_DEFAULT_SETTINGS).
195
196start_factory(Args) when is_list(Args) ->
197    SO = 'CosNotification_Common':get_option(server_options, Args, ?not_DEFAULT_SETTINGS),
198    SPEC = ['CosNotifyChannelAdmin_EventChannelFactory',Args,
199	    [{sup_child, true},
200	     {regname, {local, oe_cosNotificationFactory}}|SO]],
201    case supervisor:start_child(?SUPERVISOR_NAME, SPEC) of
202	{ok, Pid, Obj} when is_pid(Pid) ->
203	    Obj;
204	Other->
205	    orber:dbg("[~p] cosNotificationApp:start_factory( ~p ).~n"
206		      "Reason: ~p~n", [?LINE, Args, Other], ?DEBUG_LEVEL),
207	    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO})
208    end;
209start_factory(Args) ->
210    orber:dbg("[~p] cosNotificationApp:start_factory( ~p ).~n"
211	      "Bad parameters~n", [?LINE, Args], ?DEBUG_LEVEL),
212    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
213
214%%------------------------------------------------------------
215%% function : start_global_factory
216%% Arguments: none or an argumentlist whith default values.
217%% Returns  : ObjectRef | {'EXCEPTION', _} | {'EXIT', Reason}
218%% Effect   : Starts a CosNotifyChannelAdmin_EventChannelFactory
219%%------------------------------------------------------------
220start_global_factory() ->
221    start_global_factory(?not_DEFAULT_SETTINGS).
222
223start_global_factory(Args) when is_list(Args) ->
224    SO = 'CosNotification_Common':get_option(server_options, Args, ?not_DEFAULT_SETTINGS),
225    Name = 'CosNotification_Common':create_name(),
226    SPEC = ['CosNotifyChannelAdmin_EventChannelFactory',Args,
227	    [{sup_child, true},
228	     {regname, {global, Name}}|SO]],
229    case supervisor:start_child(?SUPERVISOR_NAME, SPEC) of
230	{ok, Pid, Obj} when is_pid(Pid) ->
231	    Obj;
232	Other->
233	    orber:dbg("[~p] cosNotificationApp:start_global_factory( ~p ).~n"
234		      "Reason: ~p~n", [?LINE, Args, Other], ?DEBUG_LEVEL),
235	    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO})
236    end;
237start_global_factory(Args) ->
238    orber:dbg("[~p] cosNotificationApp:start_global_factory( ~p ).~n"
239	      "Bad parameters~n", [?LINE, Args], ?DEBUG_LEVEL),
240    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
241
242
243%%------------------------------------------------------------
244%% function : stop_factory
245%% Arguments: Factory Object Reference
246%% Returns  : ok | {'EXCEPTION', _}
247%% Effect   :
248%%------------------------------------------------------------
249
250stop_factory(Fac)->
251    corba:dispose(Fac).
252
253%%------------------------------------------------------------
254%% function : start_filter_factory
255%% Arguments: none or an argumentlist which by default is defined
256%%            in CosNotification_Definitions.hrl, i.e., '?not_FILTERFAC_DEF'
257%% Returns  : ObjectRef | {'EXCEPTION', _} | {'EXIT', Reason}
258%% Effect   : Starts a CosNotifyChannelAdmin_EventChannelFactory
259%%------------------------------------------------------------
260start_filter_factory() ->
261    start_filter_factory([{typecheck, true},
262			  {tty, false},
263			  {logfile, false},
264			  {server_options, []}]).
265start_filter_factory(Args) when is_list(Args) ->
266    SO = 'CosNotification_Common':get_option(server_options, Args,
267					     ?not_DEFAULT_SETTINGS),
268    SPEC = ['CosNotifyFilter_FilterFactory',Args, [{sup_child, true}|SO]],
269    case supervisor:start_child(?SUPERVISOR_NAME, SPEC) of
270	{ok, Pid, Obj} when is_pid(Pid) ->
271	    Obj;
272	Other->
273	    orber:dbg("[~p] cosNotificationApp:start_filter_factory( ~p ).~n"
274		      "Reason: ~p~n", [?LINE, Args, Other], ?DEBUG_LEVEL),
275	    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO})
276    end;
277start_filter_factory(Args) ->
278	    orber:dbg("[~p] cosNotificationApp:start_filter_factory( ~p ).~n"
279		      "Bad parameters~n", [?LINE, Args], ?DEBUG_LEVEL),
280    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
281
282
283%%------------------------------------------------------------
284%% function : stop_filter_factory
285%% Arguments: FilterFactory Object Reference
286%% Returns  : ok | {'EXCEPTION', _}
287%% Effect   :
288%%------------------------------------------------------------
289
290stop_filter_factory(Fac)->
291    corba:dispose(Fac).
292
293
294%%------------------------------------------------------------
295%% function : create_structured_event
296%% Arguments:
297%% Returns  :
298%% Effect   :
299%%------------------------------------------------------------
300create_structured_event(StrD,StrT,StrE,PSeqV,PSeqF,AnyR)
301  when is_list(StrD) andalso is_list(StrT) andalso is_list(StrE)
302       andalso is_list(PSeqV) andalso is_list(PSeqF) andalso
303       is_record(AnyR, any) ->
304#'CosNotification_StructuredEvent'{header =
305   #'CosNotification_EventHeader'{fixed_header =
306		  #'CosNotification_FixedEventHeader'{event_type =
307				      #'CosNotification_EventType'{domain_name=StrD,
308						   type_name=StrT},
309				      event_name = StrE},
310		  variable_header = PSeqV},
311   filterable_data = PSeqF,
312   remainder_of_body = AnyR};
313create_structured_event(_StrD,_StrT,_StrE,_PSeqV,_PSeqF,_AnyR) ->
314    corba:raise(#'BAD_PARAM'{completion_status=?COMPLETED_NO}).
315
316
317%%------------------------------------------------------------
318%% function : type_check
319%% Arguments:
320%% Returns  :
321%% Effect   :
322%%------------------------------------------------------------
323type_check() ->
324    case application:get_env(cosNotification, type_check) of
325	{ok, Boolean} when is_atom(Boolean) ->
326	    Boolean;
327	_ ->
328	    true
329    end.
330
331%%------------------------------------------------------------
332%% function : notify
333%% Arguments:
334%% Returns  :
335%% Effect   :
336%%------------------------------------------------------------
337notify() ->
338    case application:get_env(cosNotification, notify) of
339	{ok, Module} when is_atom(Module) ->
340	    Module;
341	_ ->
342	    false
343    end.
344
345%%------------------------------------------------------------
346%% function : max_events
347%% Arguments:
348%% Returns  :
349%% Effect   :
350%%------------------------------------------------------------
351max_events() ->
352    case application:get_env(cosNotification, max_events) of
353	{ok, Max} when is_integer(Max) ->
354	    Max;
355	_ ->
356	    50
357    end.
358
359%%------------------------------------------------------------
360%% function : timeout_events
361%% Arguments:
362%% Returns  :
363%% Effect   :
364%%------------------------------------------------------------
365timeout_events() ->
366    case application:get_env(cosNotification, timeout_events) of
367	{ok, Max} when is_integer(Max) ->
368	    Max;
369	_ ->
370	    3000000 %% 5 minutes
371    end.
372
373
374%%------------------------------------------------------------
375%% function : interval_events
376%% Arguments:
377%% Returns  :
378%% Effect   :
379%%------------------------------------------------------------
380interval_events() ->
381    case application:get_env(cosNotification, interval_events) of
382	{ok, Max} when is_integer(Max) ->
383	    Max;
384	_ ->
385	    10000 %% 10 seconds
386    end.
387
388
389%%------------------------------------------------------------
390%% function : start
391%% Arguments: Type - see module application
392%%            Arg  - see module application
393%% Returns  :
394%% Effect   : Module callback for application
395%%------------------------------------------------------------
396
397start(_, _) ->
398    supervisor:start_link({local, ?SUPERVISOR_NAME}, cosNotificationApp, app_init).
399
400
401%%------------------------------------------------------------
402%% function : stop
403%% Arguments: Arg - see module application
404%% Returns  :
405%% Effect   : Module callback for application
406%%------------------------------------------------------------
407
408stop(_) ->
409    ok.
410
411%%------------------------------------------------------------
412%% function : init
413%% Arguments:
414%% Returns  :
415%% Effect   :
416%%------------------------------------------------------------
417
418%% Starting using create_factory/X
419init(own_init) ->
420    {ok,{{simple_one_for_one,50,10},
421	 [{"oe_NotChild",
422	   {'CosNotification_Common',create_link, []},
423	   transient,100000,worker,
424	   ['CosNotifyChannelAdmin_EventChannel',
425	    'CosNotifyChannelAdmin_EventChannel_impl']}]}};
426%% When starting as an application.
427init(app_init) ->
428    {ok,{{simple_one_for_one,50,10},
429	 [{"oe_NotChild",
430	   {'CosNotification_Common',create_link, []},
431	   transient,100000,worker,
432	   ['CosNotifyChannelAdmin_EventChannel',
433	    'CosNotifyChannelAdmin_EventChannel_impl']}]}}.
434
435
436%%--------------- END OF MODULE ------------------------------
437