1%%--------------------------------------------------------------------
2%%
3%% %CopyrightBegin%
4%%
5%% Copyright Ericsson AB 2001-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        : CosEventDomainAdmin_EventDomainFactory_impl.erl
24%% Description :
25%%
26%%----------------------------------------------------------------------
27-module('CosEventDomainAdmin_EventDomainFactory_impl').
28
29%%----------------------------------------------------------------------
30%% Include files
31%%----------------------------------------------------------------------
32-include_lib("orber/include/corba.hrl").
33-include_lib("orber/include/ifr_types.hrl").
34-include_lib("cosNotification/include/CosNotification.hrl").
35
36-include("CosEventDomainAdmin.hrl").
37-include("cosEventDomainApp.hrl").
38
39%%----------------------------------------------------------------------
40%% External exports
41%%----------------------------------------------------------------------
42-export([init/1,
43	 terminate/2,
44	 code_change/3,
45	 handle_info/2]).
46
47-export([create_event_domain/4,
48	 get_all_domains/2,
49	 get_event_domain/3]).
50
51%%----------------------------------------------------------------------
52%% Internal exports
53%%----------------------------------------------------------------------
54-export([]).
55
56%%----------------------------------------------------------------------
57%% Records
58%%----------------------------------------------------------------------
59-record(state, {current_id = -1, domains = []}).
60
61%%----------------------------------------------------------------------
62%% Macros
63%%----------------------------------------------------------------------
64
65%%======================================================================
66%% External functions
67%%======================================================================
68%%----------------------------------------------------------------------
69%% Function   : init/1
70%% Returns    : {ok, State}          |
71%%              {ok, State, Timeout} |
72%%              ignore               |
73%%              {stop, Reason}
74%% Description: Initiates the server
75%%----------------------------------------------------------------------
76init([]) ->
77    process_flag(trap_exit, true),
78    {ok, #state{}}.
79
80%%----------------------------------------------------------------------
81%% Function   : terminate/2
82%% Returns    : any (ignored by gen_server)
83%% Description: Shutdown the server
84%%----------------------------------------------------------------------
85terminate(_Reason, _State) ->
86    ok.
87
88%%----------------------------------------------------------------------
89%% Function   : code_change/3
90%% Returns    : {ok, NewState}
91%% Description: Convert process state when code is changed
92%%----------------------------------------------------------------------
93code_change(_OldVsn, State, _Extra) ->
94    {ok, State}.
95
96%%----------------------------------------------------------------------
97%% Function   : handle_info/2
98%% Returns    : {noreply, State}   |
99%%              {stop, Reason, State}
100%% Description: Handle, for example, exit signals.
101%%----------------------------------------------------------------------
102handle_info({'EXIT', Pid, _Reason}, State) ->
103    {noreply, State#state{domains=delete_domain(State#state.domains, Pid, [])}};
104handle_info(_Info, State) ->
105    {noreply, State}.
106
107
108%%----------------------------------------------------------------------
109%% Function   : create_event_domain
110%% Arguments  : InitialQoS - CosNotification::QoSProperties
111%%              InitialAdmin - CosNotification::AdminProperties
112%% Returns    : CosEventDomainAdmin::EventDomain |
113%%              {'EXCEPTION', #'CosNotification_UnsupportedQoS'{}} |
114%%              {'EXCEPTION', #'CosNotification_UnsupportedAdmin'{}} |
115%% Description:
116%%----------------------------------------------------------------------
117create_event_domain(_OE_This, State, InitialQoS, InitialAdmin) ->
118    Id = cosEventDomainApp:create_id(State#state.current_id),
119    Admin = cosEventDomainApp:get_admin(InitialAdmin),
120    QoS = cosEventDomainApp:get_qos(InitialQoS),
121    case catch 'CosEventDomainAdmin_EventDomain':oe_create_link([self(), Id,
122								 QoS, Admin],
123								[{sup_child, true}]) of
124	{ok, Pid, ED} ->
125	    {reply, {ED, Id}, State#state{current_id = Id,
126					  domains = [{Id, ED, Pid}|State#state.domains]}};
127	What ->
128	    orber:dbg("[~p] CosEventDomainAdmin_EventDomainFactory:"
129		      "create_event_domain();~n"
130		      "Failed creatin a new EventDomain due to: ~p",
131		      [?LINE, What], ?DEBUG_LEVEL),
132	    corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO})
133    end.
134
135%%----------------------------------------------------------------------
136%% Function   : get_all_domains
137%% Arguments  : -
138%% Returns    : CosEventDomainAdmin::DomainIDSeq - [long()]
139%% Description:
140%%----------------------------------------------------------------------
141get_all_domains(_OE_This, State) ->
142    {reply, get_all_domains_helper(State#state.domains, []), State}.
143
144get_all_domains_helper([], Acc) ->
145    Acc;
146get_all_domains_helper([{Id, _, _}|T], Acc) ->
147    get_all_domains_helper(T, [Id|Acc]).
148
149
150%%----------------------------------------------------------------------
151%% Function   : get_event_domain
152%% Arguments  : CosEventDomainAdmin::DomainID - long()
153%% Returns    : CosEventDomainAdmin::EventDomain |
154%%              {'EXCEPTION', #'CosEventDomainAdmin_DomainNotFound'{}} |
155%% Description:
156%%----------------------------------------------------------------------
157get_event_domain(_OE_This, State, DomainID) ->
158    {reply, get_event_domain_helper(State#state.domains, DomainID), State}.
159
160get_event_domain_helper([], _) ->
161    corba:raise(#'CosEventDomainAdmin_DomainNotFound'{});
162get_event_domain_helper([{Id, ED, _}|_], Id) ->
163    ED;
164get_event_domain_helper([_|T], Id) ->
165    get_event_domain_helper(T, Id).
166
167%%======================================================================
168%% Internal functions
169%%======================================================================
170delete_domain([], _, Acc) ->
171    %% The domain didn't exist.
172    Acc;
173delete_domain([{_Id, _, Pid}], Pid, Acc) ->
174    Acc;
175delete_domain([{_Id, _, Pid}|T], Pid, Acc) ->
176    T++Acc;
177delete_domain([H|T], Pid, Acc) ->
178    delete_domain(T, Pid, [H|Acc]).
179
180%%======================================================================
181%% END OF MODULE
182%%======================================================================
183
184