-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherproxy_trans.erl
199 lines (172 loc) · 5.99 KB
/
erproxy_trans.erl
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
%%
%% Copyright (c) 2018 Dmitry Poroh
%% All rights reserved.
%% Distributed under the terms of the MIT License. See the LICENSE file.
%%
%% SIP Transaction
%%
-module(erproxy_trans).
-behaviour(gen_server).
-export([start_link/3,
find_server_trans/1,
find_server_cancel_trans/1,
find_client_trans/2,
recv_request/2,
recv_response/2,
send_response/2,
cancel_request/1
]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, {trans :: ersip_trans:trans(),
tu_callback :: fun((ersip_sipmsg:sipmsg()) -> any())
}).
%%%===================================================================
%%% API
%%%===================================================================
start_link(Type, TUCallback, Args) ->
{Trans, SE} = create_transaction(Type, Args),
State = #state{trans = Trans,
tu_callback = TUCallback
},
Id = ersip_trans:id(Trans),
lager:info("Starting ~p transaction with id: ~p", [Type, Id]),
gen_server:start_link({global, {?MODULE, Id}}, ?MODULE, [State, SE], []).
recv_request(InSipMsg, Pid) ->
gen_server:cast(Pid, {received, InSipMsg}).
recv_response(SipMsg, Pid) ->
gen_server:cast(Pid, {received, SipMsg}).
send_response(SipMsg, Pid) ->
gen_server:cast(Pid, {send, SipMsg}).
-spec cancel_request(pid()) -> ok | {error, no_request}.
cancel_request(Pid) ->
try
gen_server:call(Pid, cancel_request)
catch
exit:{noproc, _} ->
{error, no_request}
end.
find_server_trans(InSipMsg) ->
TransId = ersip_trans:server_id(InSipMsg),
case global:whereis_name({?MODULE, TransId}) of
undefined ->
error;
Pid when is_pid(Pid) ->
{ok, Pid}
end.
find_server_cancel_trans(CancelSipMsg) ->
TransId = ersip_trans:server_cancel_id(CancelSipMsg),
case global:whereis_name({?MODULE, TransId}) of
undefined ->
error;
Pid when is_pid(Pid) ->
{ok, Pid}
end.
find_client_trans(RecvVia, SipMsg) ->
TransId = ersip_trans:client_id(RecvVia, SipMsg),
case global:whereis_name({?MODULE, TransId}) of
undefined ->
error;
Pid when is_pid(Pid) ->
{ok, Pid}
end.
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init([State, SE]) ->
case process_se_list(SE, State) of
{noreply, State1} ->
{ok, State1};
{stop, _State1} ->
{stop, normal}
end.
handle_call(cancel_request, _From, #state{tu_callback = CB} = State) ->
case call_callback(CB, [cancel]) of
ok ->
{reply, ok, State};
{error, noproc} ->
{reply, {error, no_request}, State}
end;
handle_call(Request, _From, State) ->
lager:error("Unexpected call ~p", [Request]),
Reply = ok,
{reply, Reply, State}.
handle_cast({received, _SipMsg} = Ev, #state{trans = Trans} = State) ->
{NewTrans, SE} = ersip_trans:event(Ev, Trans),
cast_se(SE),
{noreply, State#state{trans = NewTrans}};
handle_cast({send, _SipMsg} = Ev, #state{trans = Trans} = State) ->
{NewTrans, SE} = ersip_trans:event(Ev, Trans),
case process_se_list(SE, State#state{trans = NewTrans}) of
{noreply, State1} ->
{noreply, State1};
{stop, State1} ->
{stop, normal, ok, State1}
end;
handle_cast({process_se, SEList}, State) ->
process_se_list(SEList, State);
handle_cast(Request, State) ->
lager:error("Unexpected cast ~p", [Request]),
{noreply, State}.
handle_info({event, TimerEvent}, #state{trans = Trans} = State) ->
{NewTrans, SE} = ersip_trans:event(TimerEvent, Trans),
process_se_list(SE, State#state{trans = NewTrans});
handle_info(Info, State) ->
lager:error("Unexpected info ~p", [Info]),
{noreply, State}.
terminate(Reason, _State) ->
lager:info("Terminated with reason ~p", [Reason]),
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
process_se_list([], State) ->
{noreply, State};
process_se_list([SE | Rest], State) ->
case process_se(SE, State) of
{stop, NewState} ->
{stop, normal, NewState};
{continue, NewState} ->
process_se_list(Rest, NewState)
end.
process_se({tu_result, SipMsg}, #state{tu_callback = CB} = State) ->
call_callback(CB, [SipMsg]),
{continue, State};
process_se({set_timer, {Timeout, TimerEvent}}, State) ->
erlang:send_after(Timeout, self(), {event, TimerEvent}),
{continue, State};
process_se({clear_trans, timeout}, #state{tu_callback = CB} = State) ->
call_callback(CB, [timeout]),
{stop, State};
process_se({clear_trans, no_ack}, #state{tu_callback = CB} = State) ->
call_callback(CB, [no_ack]),
{stop, State};
process_se({clear_trans, Reason}, State) ->
lager:info("Transaction is cleared: ~p", [Reason]),
{stop, State};
process_se({send_request, OutReq}, State) ->
erproxy_conn:send_request(OutReq),
{continue, State};
process_se({send_response, SipMsg}, State) ->
erproxy_conn:send_response(SipMsg),
{continue, State}.
cast_se(SE) ->
gen_server:cast(self(), {process_se, SE}).
create_transaction(client, {OutReq, Options}) ->
ersip_trans:new_client(OutReq, Options);
create_transaction(server, {SipMsg, Options}) ->
ersip_trans:new_server(SipMsg, Options).
call_callback(CB, Args) ->
try
case CB of
{M, F, A} ->
erlang:apply(M, F, Args ++ A);
F when is_function(F) ->
erlang:apply(F, Args)
end
catch
Type:Error ->
lager:error("Transaction user error: ~p:~p", [Type, {Error, erlang:get_stacktrace()}])
end.