1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
%%--------------------------------------------------------------------
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2001-2009. All Rights Reserved.
%%
%% 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 online 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.
%%
%% %CopyrightEnd%
%%
%%
%%----------------------------------------------------------------------
%% File : oe_CosEventComm_PullerS_impl.erl
%% Description :
%%
%%----------------------------------------------------------------------
-module(oe_CosEventComm_PullerS_impl).
%%----------------------------------------------------------------------
%% Include files
%%----------------------------------------------------------------------
-include_lib("orber/include/corba.hrl").
-include("CosEventChannelAdmin.hrl").
-include("CosEventComm.hrl").
-include("cosEventApp.hrl").
%%----------------------------------------------------------------------
%% External exports
%%----------------------------------------------------------------------
-export([init/1,
terminate/2,
code_change/3,
handle_info/2]).
%% Exports from "CosEventChannelAdmin::ProxyPullSupplier"
-export([connect_pull_consumer/4]).
%% Exports from "CosEventComm::PullSupplier"
-export([pull/3,
try_pull/3,
disconnect_pull_supplier/3]).
%%----------------------------------------------------------------------
%% Internal exports
%%----------------------------------------------------------------------
%% Exports from "oe_CosEventComm::Event
-export([send/3, send_sync/4]).
%%----------------------------------------------------------------------
%% Records
%%----------------------------------------------------------------------
-record(state, {admin_pid, client, db, respond_to, typecheck, maxevents}).
%%----------------------------------------------------------------------
%% Macros
%%----------------------------------------------------------------------
%%======================================================================
%% External functions
%%======================================================================
%%---------------------------------------------------------------------%
%% Function : init/1
%% Returns : {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% Description: Initiates the server
%%----------------------------------------------------------------------
init([AdminPid, TypeCheck, MaxEvents]) ->
process_flag(trap_exit, true),
{ok, #state{admin_pid = AdminPid,
db = ets:new(oe_ets, [set, private, ordered_set]),
typecheck = TypeCheck, maxevents = MaxEvents}}.
%%---------------------------------------------------------------------%
%% function : handle_info
%% Arguments:
%% Returns : {noreply, State} |
%% {stop, Reason, State}
%% Effect : Functions demanded by the gen_server module.
%% The CosEvent specification states:
%% "A nil object reference may be passed to the connect_pull_consumer operation;
%% if so a channel cannot invoke a disconnect_pull_consumer operation on the
%% consumer; the consumer may be disconnected from the channel without being
%% informed."
%% If we would invoke the disconnect_pull_consumer operation
%% at the same time as the client tries to pull an event it
%% would cause a dead-lock. We can solve this by spawning a process
%% but as is the client will discover that the object no longer exists
%% the next time it tries to pull an event.
%%----------------------------------------------------------------------
handle_info({'EXIT', Pid, Reason}, #state{admin_pid = Pid} = State) ->
orber:dbg("[~p] oe_CosEventComm_PullerS_impl:handle_info(~p);~n"
"My Admin terminated and so will I.",
[?LINE, Reason], ?DEBUG_LEVEL),
{stop, Reason, State};
handle_info(_Info, State) ->
?DBG("Unknown Info ~p~n", [_Info]),
{noreply, State}.
%%---------------------------------------------------------------------%
%% Function : terminate/2
%% Returns : any (ignored by gen_server)
%% Description: Shutdown the server
%%----------------------------------------------------------------------
terminate(_Reason, #state{client = undefined, respond_to = undefined, db = DB}) ->
?DBG("Terminating ~p; no client connected and no pending pull's.~n", [_Reason]),
ets:delete(DB),
ok;
terminate(_Reason, #state{client = undefined, respond_to = ReplyTo, db = DB}) ->
?DBG("Terminating ~p; no client connected but a pending pull.~n", [_Reason]),
corba:reply(ReplyTo, {'EXCEPTION', #'CosEventComm_Disconnected'{}}),
ets:delete(DB),
ok;
terminate(_Reason, #state{client = Client, respond_to = undefined, db = DB}) ->
?DBG("Terminating ~p; no pending pull~n", [_Reason]),
cosEventApp:disconnect('CosEventComm_PullConsumer',
disconnect_pull_consumer, Client),
ets:delete(DB),
ok;
terminate(_Reason, #state{client = Client, respond_to = ReplyTo, db = DB}) ->
?DBG("Terminating ~p; pending pull~n", [_Reason]),
corba:reply(ReplyTo, {'EXCEPTION', #'CosEventComm_Disconnected'{}}),
cosEventApp:disconnect('CosEventComm_PullConsumer',
disconnect_pull_consumer, Client),
ets:delete(DB),
ok.
%%---------------------------------------------------------------------%
%% Function : code_change/3
%% Returns : {ok, NewState}
%% Description: Convert process state when code is changed
%%----------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%---------------------------------------------------------------------%
%% Function : connect_pull_consumer
%% Arguments :
%% Returns :
%% Description:
%%----------------------------------------------------------------------
connect_pull_consumer(_OE_This, _OE_From, #state{client = undefined,
typecheck = TypeCheck} = State,
NewClient) ->
case corba_object:is_nil(NewClient) of
true ->
?DBG("A NIL client supplied.~n", []),
{reply, ok, State};
false ->
cosEventApp:type_check(NewClient, 'CosEventComm_PullConsumer', TypeCheck),
?DBG("Connected to client.~n", []),
{reply, ok, State#state{client = NewClient}}
end;
connect_pull_consumer(_, _, _, _) ->
corba:raise(#'CosEventChannelAdmin_AlreadyConnected'{}).
%%---------------------------------------------------------------------%
%% Function : pull
%% Arguments :
%% Returns :
%% Description:
%%----------------------------------------------------------------------
pull(_OE_This, OE_From, State) ->
case get_event(State#state.db) of
false ->
?DBG("pull invoked but no event stored; put the client on hold.~n", []),
{noreply, State#state{respond_to = OE_From}};
Event ->
?DBG("pull invoked and returned: ~p~n", [Event]),
{reply, Event, State}
end.
%%---------------------------------------------------------------------%
%% Function : try_pull
%% Arguments :
%% Returns :
%% Description:
%%----------------------------------------------------------------------
try_pull(_OE_This, _OE_From, State) ->
case get_event(State#state.db) of
false ->
?DBG("try_pull invoked but no event stored.~n", []),
{reply, {any:create(orber_tc:long(), 0), false}, State};
Event ->
?DBG("try_pull invoked and returned: ~p~n", [Event]),
{reply, {Event, true}, State}
end.
%%---------------------------------------------------------------------%
%% Function : disconnect_pull_supplier
%% Arguments :
%% Returns :
%% Description:
%%----------------------------------------------------------------------
disconnect_pull_supplier(_OE_This, _OE_From, State) ->
?DBG("Disconnect invoked ~p ~n", [State]),
{stop, normal, ok, State#state{client = undefined}}.
%%======================================================================
%% Internal functions
%%======================================================================
%%---------------------------------------------------------------------%
%% Function : send
%% Arguments :
%% Returns :
%% Description:
%%----------------------------------------------------------------------
send(_OE_This, #state{respond_to = undefined} = State, Any) ->
?DBG("Received event ~p and stored it.~n", [Any]),
store_event(State#state.db, State#state.maxevents, Any),
{noreply, State};
send(_OE_This, State, Any) ->
?DBG("Received event ~p and sent it to pending client.~n", [Any]),
corba:reply(State#state.respond_to, Any),
{noreply, State#state{respond_to = undefined}}.
%%---------------------------------------------------------------------%
%% Function : send_sync
%% Arguments :
%% Returns :
%% Description:
%%----------------------------------------------------------------------
send_sync(_OE_This, _OE_From, #state{respond_to = undefined} = State, Any) ->
?DBG("Received event ~p and stored it (sync).~n", [Any]),
store_event(State#state.db, State#state.maxevents, Any),
{reply, ok, State};
send_sync(_OE_This, _OE_From, State, Any) ->
?DBG("Received event ~p and sent it to pending client (sync).~n", [Any]),
corba:reply(State#state.respond_to, Any),
{reply, ok, State#state{respond_to = undefined}}.
%%---------------------------------------------------------------------%
%% Function : store_event
%% Arguments : DB - ets reference
%% Event - CORBA::Any
%% Returns : true
%% Description: Insert the event in FIFO order.
%%----------------------------------------------------------------------
store_event(DB, Max, Event) ->
case ets:info(DB, size) of
CurrentSize when CurrentSize < Max ->
ets:insert(DB, {now(), Event});
_ ->
orber:dbg("[~p] oe_CosEventComm_PullerS:store_event(~p); DB full drop event.",
[?LINE, Event], ?DEBUG_LEVEL),
true
end.
%%---------------------------------------------------------------------%
%% Function : get_event
%% Arguments : DB - ets reference
%% Event - CORBA::Any
%% Returns : false | Event (CORBA::Any)
%% Description: Lookup event in FIFO order; return false if no event exists.
%%----------------------------------------------------------------------
get_event(DB) ->
case ets:first(DB) of
'$end_of_table' ->
false;
Key ->
[{_, Event}] = ets:lookup(DB, Key),
ets:delete(DB, Key),
Event
end.
%%======================================================================
%% END OF MODULE
%%======================================================================
|