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    : ETraP_Common.erl
24%% Purpose :
25%%----------------------------------------------------------------------
26
27-module('ETraP_Common').
28
29%%--------------- INCLUDES ----------------------------------
30-include_lib("orber/include/corba.hrl").
31-include_lib("orber/include/ifr_types.hrl").
32%% Local
33-include_lib("ETraP_Common.hrl").
34-include_lib("CosTransactions.hrl").
35
36%%--------------- EXPORTS -----------------------------------
37-export([try_timeout/1,
38	 get_option/3,
39	 create_name/2,
40	 create_name/1,
41	 is_debug_compiled/0,
42	 send_stubborn/5,
43	 create_link/3]).
44
45%%--------------- DEFINITIONS OF CONSTANTS ------------------
46%%------------------------------------------------------------
47%% function : create_link
48%% Arguments: Module - which Module to call
49%%            Env/ARgList - ordinary oe_create arguments.
50%% Returns  :
51%% Exception:
52%% Effect   : Necessary since we want the supervisor to be a
53%%            'simple_one_for_one'. Otherwise, using for example,
54%%            'one_for_one', we have to call supervisor:delete_child
55%%            to remove the childs startspecification from the
56%%            supervisors internal state.
57%%------------------------------------------------------------
58create_link(Module, Env, ArgList) ->
59    Module:oe_create_link(Env, ArgList).
60
61%%------------------------------------------------------------
62%% function : get_option
63%% Arguments:
64%% Returns  :
65%% Exception:
66%% Effect   :
67%%------------------------------------------------------------
68
69get_option(Key, OptionList, DefaultList) ->
70    case lists:keysearch(Key, 1, OptionList) of
71        {value,{Key,Value}} ->
72            Value;
73        _ ->
74            case lists:keysearch(Key, 1, DefaultList) of
75                {value,{Key,Value}} ->
76                    Value;
77                _->
78                    {error, "Invalid option"}
79            end
80    end.
81%%------------------------------------------------------------
82%% function : create_name/2
83%% Arguments:
84%% Returns  :
85%% Exception:
86%% Effect   :
87%%------------------------------------------------------------
88
89create_name(Name,Type) ->
90    Time = erlang:system_time(),
91    Unique = erlang:unique_integer([positive]),
92    lists:concat(['oe_',node(),'_',Type,'_',Name,'_',Time,'_',Unique]).
93
94%%------------------------------------------------------------
95%% function : create_name/1
96%% Arguments:
97%% Returns  :
98%% Exception:
99%% Effect   :
100%%------------------------------------------------------------
101
102create_name(Type) ->
103    Time = erlang:system_time(),
104    Unique = erlang:unique_integer([positive]),
105    lists:concat(['oe_',node(),'_',Type,'_',Time,'_',Unique]).
106
107%%------------------------------------------------------------
108%% function : try_timeout
109%% Arguments: Id - name of the timeoutSrv server.
110%% Returns  : Boolean
111%% Exception:
112%% Effect   :
113%%------------------------------------------------------------
114
115try_timeout(TimeoutAt) ->
116    case TimeoutAt of
117	infinity ->
118	    false;
119	_->
120	    TimeSec = erlang:monotonic_time(seconds),
121	    if
122		TimeSec < TimeoutAt ->
123		    false;
124		true ->
125		    true
126	    end
127    end.
128
129%%------------------------------------------------------------
130%% function : send_stubborn
131%% Arguments: M - module
132%%            F - function
133%%            A - arguments
134%%            MaxR - Maximum no retries
135%%            Wait - sleep Wait seconds before next try.
136%% Returns  : see effect
137%% Exception:
138%% Effect   : Retries repeatedly until anything else besides
139%%            'EXIT', 'COMM_FAILURE' or 'OBJECT_NOT_EXIST'
140%%------------------------------------------------------------
141
142send_stubborn(M, F, A, MaxR, Wait) when is_list(A) ->
143    send_stubborn(M, F, A, MaxR, Wait, 0);
144send_stubborn(M, F, A, MaxR, Wait) ->
145    send_stubborn(M, F, [A], MaxR, Wait, 0).
146send_stubborn(M, F, A, MaxR, _Wait, MaxR) ->
147    ?tr_error_msg("~p:~p( ~p ) failed!! Tried ~p times.~n", [M,F,A,MaxR]),
148    corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO});
149send_stubborn(M, F, A, MaxR, Wait, Times) ->
150    ?debug_print("~p:~p(~p)  # of retries: ~p~n", [M,F,A, Times]),
151    case catch apply(M,F,A) of
152	{'EXCEPTION', E} when is_record(E, 'COMM_FAILURE')->
153	    NewTimes = Times +1,
154	    timer:sleep(Wait),
155	    send_stubborn(M, F, A, MaxR, Wait, NewTimes);
156	{'EXCEPTION', E} when is_record(E, 'TRANSIENT')->
157	    NewTimes = Times +1,
158	    timer:sleep(Wait),
159	    send_stubborn(M, F, A, MaxR, Wait, NewTimes);
160	{'EXCEPTION', E} when is_record(E, 'TIMEOUT')->
161	    NewTimes = Times +1,
162	    timer:sleep(Wait),
163	    send_stubborn(M, F, A, MaxR, Wait, NewTimes);
164	{'EXIT', _} ->
165	    NewTimes = Times +1,
166	    timer:sleep(Wait),
167	    send_stubborn(M, F, A, MaxR, Wait, NewTimes);
168	Other ->
169	    ?debug_print("~p:~p(~p) Resulted in: ~p~n", [M,F,A, Other]),
170	    Other
171    end.
172
173%%------------------------------------------------------------
174%% function : is_debug_compiled
175%% Arguments:
176%% Returns  :
177%% Exception:
178%% Effect   :
179%%------------------------------------------------------------
180
181-ifdef(debug).
182    is_debug_compiled() -> true.
183-else.
184    is_debug_compiled() -> false.
185-endif.
186
187%%--------------- END OF MODULE ------------------------------
188