1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2003-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21%%
22%%----------------------------------------------------------------------
23%% Purpose: Used when performing testing appup tests
24%%
25%% {ok, P} = megaco_appup_mg:start().
26%% megaco_appup_mg:stop(P).
27%% megaco_appup_mg:verbosity(P,silence).
28%% megaco_appup_mg:verbosity(P,debug).
29%% megaco_appup_mg:timeout(P,100).
30%% megaco_appup_mg:aam(P,10).
31%% megaco_appup_mg:aam(P,15).
32%% megaco_appup_mg:aat(P,2000).
33%% megaco_appup_mg:aat(P,10000).
34%%
35%%----------------------------------------------------------------------
36
37-module(megaco_appup_mg).
38
39%% API
40-export([start1/0, start2/0, start3/0, start4/0]).
41-export([start/0,  start/1,  start/2]).
42-export([verbosity/2, timeout/2]).
43-export([aat/2, aam/2]).
44
45%% Internal
46-export([main/4]).
47
48%% Constants:
49-define(TIMEOUT,1000).
50-define(MAXCOUNT,1).
51
52%% Wrapper macros
53-define(START(Mid, Enc, Transp, Conf, Verb),
54	megaco_test_mg:start(node(), Mid, Enc, Transp, Conf, Verb)).
55-define(STOP(Pid),        megaco_test_mg:stop(Pid)).
56-define(SERV_CHANGE(Pid), megaco_test_mg:service_change(Pid)).
57-define(NOTIF_RAR(Pid),   megaco_test_mg:notify_request_and_reply(Pid)).
58-define(GRP_REQ(Pid,N),   megaco_test_mg:group_requests(Pid,N)).
59-define(VERBOSITY(Pid,V), megaco_test_mg:verbosity(Pid,V)).
60
61
62%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63
64start1() -> start(text,   tcp).
65start2() -> start(text,   udp).
66start3() -> start(binary, tcp).
67start4() -> start(binary, udp).
68
69start() ->
70    start(text, tcp).
71
72start(tcp) ->
73    start(text, tcp);
74start(udp) ->
75    start(text, udp).
76
77start(Encoding, Transport) ->
78    proc_lib:start_link(?MODULE, main, [self(), Encoding, Transport, 1]).
79
80stop(Pid) ->
81    Pid ! stop.
82
83verbosity(Pid, V) ->
84    Pid ! {verbosity, V}.
85
86timeout(Pid, T) ->
87    Pid ! {timeout, T}.
88
89aat(Pid, Val) ->
90    uci(Pid, accu_ack_timer, Val).
91
92aam(Pid, Val) ->
93    uci(Pid, accu_ack_maxcount, Val).
94
95uci(Pid, Item, Val) ->
96    Pid ! {update_conn_info, Item, Val}.
97
98
99%% -------------------------------------------------------------------------
100
101%% - start function -
102main(Parent, Encoding, Transport, MaxCount) ->
103    Mg = init(Encoding, Transport, MaxCount),
104    proc_lib:init_ack(Parent, {ok, self()}),
105    loop(Mg, ?TIMEOUT, ?TIMEOUT).
106
107%% - init -
108init(Encoding, Transport, MaxCount) ->
109    verify_encoding(Encoding),
110    verify_transport(Transport),
111
112    Mid      = {deviceName, "mg"},
113    Config   = [{auto_ack,          true} %,
114		%{trans_timer,       2000},
115		%{trans_ack_maxcount, MaxCount}
116	       ],
117
118    d("start MG"),
119    {ok, Mg} = ?START(Mid, Encoding, Transport, Config, debug),
120
121    d("service change"),
122    ok = ?SERV_CHANGE(Mg),
123
124    ?GRP_REQ(Mg, MaxCount),
125
126    Mg.
127
128%% - main loop -
129loop(Mg, Timeout, To) when To =< 0 ->
130    ?NOTIF_RAR(Mg),
131    loop(Mg, Timeout, Timeout);
132loop(Mg, Timeout, To) ->
133    Start = t(),
134    receive
135	stop ->
136	    d("stop"),
137	    megaco_test_mg:stop(Mg),
138	    exit(normal);
139
140	{update_conn_info, Item, Val} ->
141	    d("update_conn_info -> ~p:~p", [Item, Val]),
142	    megaco_test_mg:update_conn_info(Mg, Item, Val),
143	    loop(Mg, Timeout, To - (t() - Start));
144
145	{request_group_size, Size} when Size > 1 ->
146	    d("request_group_size -> ~p", [Size]),
147	    ?GRP_REQ(Mg, Size),
148	    loop(Mg, Timeout, To - (t() - Start));
149
150	{verbosity, V} ->
151	    d("verbosity: ~p", [V]),
152	    ?VERBOSITY(Mg, V),
153	    loop(Mg, Timeout, To - (t() - Start));
154
155	{timeout, T} ->
156	    d("timeout: ~p", [T]),
157	    T1 = T - Timeout,
158	    loop(Mg, T, To - (t() - Start) + T1);
159
160	Any ->
161	    error("received unknown request: ~n~p", [Any]),
162	    loop(Mg, Timeout, To - (t() - Start))
163
164    after To ->
165	    ?NOTIF_RAR(Mg),
166	    loop(Mg, Timeout, Timeout)
167    end.
168
169
170
171%% -------------------------------------------------------------------------
172
173verify_encoding(text)   -> ok;
174verify_encoding(binary) -> ok;
175verify_encoding(Encoding) -> exit({invalid_encoding, Encoding}).
176
177verify_transport(tcp)       -> ok;
178verify_transport(udp)       -> ok;
179verify_transport(Transport) -> exit({invalid_transport, Transport}).
180
181%% -
182
183sleep(X) -> receive after X -> ok end.
184
185t() ->
186    {A,B,C} = erlang:now(),
187    A*1000000000+B*1000+(C div 1000).
188
189
190%% -
191
192error(F, A) ->
193    d("ERROR: " ++ F, A).
194
195d(F) ->
196    d(F, []).
197
198d(F, A) ->
199    io:format("~pAMG-" ++ F ++ "~n", [self()|A]).
200