aboutsummaryrefslogtreecommitdiffstats
path: root/lib/megaco/test/megaco_test_deliver.erl
blob: 307caad73b73a96fb6a26a9aad681b8b15f4c2aa (plain) (blame)
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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2007-2010. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%

%%
%%----------------------------------------------------------------------
%% Purpose: This module takes the role of the main megaco module for
%%          the transport module. It is used when  delivering 
%%          received messages. The purpose is to be able to do
%%          various forms of filtering before passing the message 
%%          the the megaco stack (by calling the megaco module).
%%          It can be controlled with the following flags:
%%          allow_recv_message - Shall the received message be 
%%                               delivered.
%%          extra_transport_info - Provide extra info from the transport
%%                                 module.
%%----------------------------------------------------------------------
-module(megaco_test_deliver).


%%----------------------------------------------------------------------
%% Include files
%%----------------------------------------------------------------------
-include_lib("megaco/src/udp/megaco_udp.hrl").
-include("megaco_test_lib.hrl").


%%----------------------------------------------------------------------
%% External exports
%%----------------------------------------------------------------------
-export([
	 process_received_message/4, process_received_message/5,
	 receive_message/4, receive_message/5
	]).



%%======================================================================
%% External functions
%%======================================================================

process_received_message(ReceiveHandle, ControlPid, SendHandle, BinMsg) ->
    i("process_received_message -> entry with"
      "~n   ReceiveHandle: ~p"
      "~n   ControlPid:    ~p"
      "~n   SendHandle:    ~p", 
      [ReceiveHandle, ControlPid, SendHandle]),
    case allow_recv_message() of
	true ->
	    i("process_received_message -> allowed recv msg"),
	    case extra_transport_info() of
		{value, Extra} ->
		    i("process_received_message -> extra_transport_info: "
		      "~n   Extra: ~p", [Extra]),
		    megaco:process_received_message(ReceiveHandle, ControlPid, 
						    SendHandle, BinMsg,
						    Extra);
		_ ->
		    i("process_received_message -> no extra_transport_info"),
		    megaco:process_received_message(ReceiveHandle, ControlPid, 
						    SendHandle, BinMsg)
	    end;
	false ->
	    i("process_received_message -> recv msg not allowed"),
	    ok;
	{false, Reason} ->
	    i("process_received_message -> recv msg not allowed"
	      "~n   Reason: ~p", [Reason]),
	    exit(Reason)
    end.
	    

process_received_message(ReceiveHandle, ControlPid, SendHandle, BinMsg, Extra) ->
    i("process_received_message -> entry with"
      "~n   ReceiveHandle: ~p"
      "~n   ControlPid:    ~p"
      "~n   SendHandle:    ~p"
      "~n   Extra:         ~p", 
      [ReceiveHandle, ControlPid, SendHandle, Extra]),
    case allow_recv_message() of
	true ->
	    i("process_received_message -> allowed recv msg"),
	    megaco:process_received_message(ReceiveHandle, ControlPid, 
					    SendHandle, BinMsg,
					    Extra);
	false ->
	    i("process_received_message -> recv msg not allowed"),
	    ok;
	{false, Reason} ->
	    i("process_received_message -> recv msg not allowed"
	      "~n   Reason: ~p", [Reason]),
	    exit(Reason)
    end.

receive_message(ReceiveHandle, ControlPid, SendHandle, BinMsg) ->
    i("receive_message -> entry with"
      "~n   ReceiveHandle: ~p"
      "~n   ControlPid:    ~p"
      "~n   SendHandle:    ~p", 
      [ReceiveHandle, ControlPid, SendHandle]),
    case allow_recv_message() of
	true ->
	    i("receive_message -> allowed recv msg"),
	    case extra_transport_info() of
		{value, Extra} ->
		    i("receive_message -> extra_transport_info: "
		      "~n   Extra: ~p", [Extra]),
		    megaco:receive_message(ReceiveHandle, ControlPid,
					   SendHandle, BinMsg, Extra);
		_ ->
		    i("receive_message -> no extra_transport_info"),
		    megaco:receive_message(ReceiveHandle, ControlPid, 
					   SendHandle, BinMsg)
	    end;
	false ->
	    i("receive_message -> recv msg not allowed"),
	    ok;
	{false, Reason} ->
	    i("receive_message -> recv msg not allowed"
	      "~n   Reason: ~p", [Reason]),
	    exit(Reason)
    end.

receive_message(ReceiveHandle, ControlPid, SendHandle, BinMsg, Extra) ->
    i("receive_message -> entry with"
      "~n   ReceiveHandle: ~p"
      "~n   ControlPid:    ~p"
      "~n   SendHandle:    ~p"
      "~n   Extra:         ~p", 
      [ReceiveHandle, ControlPid, SendHandle, Extra]),
    case allow_recv_message() of
	true ->
	    i("receive_message -> allowed recv msg"),
	    megaco:receive_message(ReceiveHandle, ControlPid,
				   SendHandle, BinMsg,
				   Extra);
	false ->
	    i("receive_message -> recv msg not allowed"),
	    ok;
	{false, Reason} ->
	    i("receive_message -> recv msg not allowed"
	      "~n   Reason: ~p", [Reason]),
	    exit(Reason)
    end.


%%======================================================================
%% Internal functions
%%======================================================================

allow_recv_message() ->
    case megaco_tc_controller:lookup(allow_recv_message) of
	{error, _} ->
	    true;
	{value, Else} ->
	    Else;
	false ->
	    true
    end.

extra_transport_info() ->
    case megaco_tc_controller:lookup(extra_transport_info) of
	{error, _} ->
	    false;
	Else ->
	    Else
    end.
    
i(F) ->
    i(F, []).

i(F, A) ->
    io:format("~p ~w:" ++ F ++ "~n", [self(), ?MODULE | A]).