Skip to content

Commit

Permalink
Included app id SipMsg and Dialog ids
Browse files Browse the repository at this point in the history
  • Loading branch information
kalta committed Apr 4, 2014
1 parent bb95f1a commit 309249d
Show file tree
Hide file tree
Showing 40 changed files with 784 additions and 753 deletions.
3 changes: 1 addition & 2 deletions include/nksip.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@


-record(sipmsg, {
id1 :: nksip_sipmsg:id(),
id :: nksip_sipmsg:id(),
class :: {req, nksip:method()} | {resp, nksip:response_code(), binary()},
app_id :: nksip:app_id(),
dialog_id :: nksip_dialog:id(),
Expand Down Expand Up @@ -247,7 +247,6 @@

-record(subscription, {
id :: nksip_subscription:id(),
app_id :: nksip:app_id(),
event :: nksip:token(),
expires :: pos_integer(),
status :: nksip_subscription:status(),
Expand Down
2 changes: 1 addition & 1 deletion include/nksip_call.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@


-record(provisional_event, {
id :: {Type::binary(), Id::binary(), Tag::binary()},
id :: {Id::nksip_subscription:id(), Tag::binary()},
timer_n :: reference()
}).

Expand Down
6 changes: 3 additions & 3 deletions samples/nksip_pbx/src/nksip_pbx_sipapp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ get_user_pass(_User, _Realm, State) ->
%% a challenge to the user.</li>
%% </ul>
authorize(ReqId, Auth, _From, State) ->
Method = nksip_request:method(pbx, ReqId),
Method = nksip_request:method(ReqId),
lager:notice("Request ~p auth data: ~p", [Method, Auth]),
case lists:member(dialog, Auth) orelse lists:member(register, Auth) of
true ->
Expand Down Expand Up @@ -172,7 +172,7 @@ route(ReqId, _Scheme, <<>>, Domain, _From, State) ->
true ->
process;
false ->
case nksip_request:is_local_route(pbx, ReqId) of
case nksip_request:is_local_route(ReqId) of
true -> process;
false -> proxy
end
Expand Down Expand Up @@ -270,7 +270,7 @@ find_all() ->

%% @doc Gets all registered contacts, excluding the one in `Request'
find_all_except_me(ReqId) ->
[From] = nksip_request:header(pbx, ReqId, <<"from">>),
[From] = nksip_request:header(ReqId, <<"from">>),
[{Scheme, User, Domain}] = nksip_parse:aors(From),
AOR = {Scheme, User, Domain},
All = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ invite(ReqId, Meta, From, #state{id=AppId}=State) ->
case nksip_sdp:is_sdp(SDP) of
true ->
Fun = fun() ->
nksip_request:reply(AppId, ReqId, ringing),
nksip_request:reply(ReqId, ringing),
timer:sleep(2000),
nksip:reply(From, {answer, SDP})
end,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ route(ReqId, _Scheme, <<>>, Domain, _From, #state{id=AppId}=State) ->
<<"nksip">> ->
process;
_ ->
case nksip_request:is_local_route(AppId, ReqId) of
case nksip_request:is_local_route(ReqId) of
true -> process;
false -> proxy
end
Expand Down
4 changes: 2 additions & 2 deletions src/nksip_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ realms(#sipmsg{headers=Headers}) ->
[Realm::binary()].

realms(AppId, RespId) ->
Hd1 = case nksip_response:header(AppId, RespId, ?RESP_WWW) of
Hd1 = case nksip_response:header(RespId, ?RESP_WWW) of
WWW when is_list(WWW) -> [{?RESP_WWW, Data} || Data <- WWW];
_ -> []
end,
Hd2 = case nksip_response:header(AppId, RespId, ?RESP_PROXY) of
Hd2 = case nksip_response:header(RespId, ?RESP_PROXY) of
Proxy when is_list(Proxy) -> [{?RESP_PROXY, Data} || Data <- Proxy];
_ -> []
end,
Expand Down
28 changes: 14 additions & 14 deletions src/nksip_call.erl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ send(AppId, Method, Uri, Opts) ->
nksip_uac:result() | nksip_uac:ack_result() | {error, nksip_uac:error()}.

send_dialog(Id, Method, Opts) ->
[_, AppId, CallId, DialogId] = nksip_sipmsg:id_parts(Id),
{AppId, DialogId, CallId} = nksip_dialog:parse_id(Id),
send_work_sync(AppId, CallId, {send_dialog, DialogId, Method, Opts}).


Expand All @@ -110,7 +110,7 @@ send_dialog(Id, Method, Opts) ->
ok | {error, nksip_uac:cancel_error()}.

cancel(Id) ->
[$R, AppId, CallId, ReqId] = nksip_sipmsg:id_parts(Id),
{req, AppId, ReqId, CallId} = nksip_sipmsg:parse_id(Id),
send_work_sync(AppId, CallId, {cancel, ReqId}).


Expand All @@ -119,10 +119,10 @@ cancel(Id) ->
{ok, nksip:id()} | {error, Error}
when Error :: unknown_dialog | invalid_request | nksip_call_router:sync_error().

dialog_id(MsgId) ->
case nksip_sipmsg:id_parts2(MsgId) of
[Class, AppId, CallId, SipMsgId] when Class==$R; Class==$S ->
send_work_sync(AppId, CallId, {dialog_id, SipMsgId})
dialog_id(Id) ->
case nksip_sipmsg:parse_id(Id) of
{Class, AppId, MsgId, CallId} when Class==$R; Class==$S ->
send_work_sync(AppId, CallId, {dialog_id, MsgId})
end.


Expand All @@ -140,7 +140,7 @@ app_reply(Fun, TransId, Pid, Reply) ->
when Error :: invalid_call | invalid_request | nksip_call_router:sync_error().

send_reply(Id, Reply) ->
[$R, AppId, CallId, ReqId] = nksip_sipmsg:id_parts(Id),
{req, AppId, ReqId, CallId} = nksip_sipmsg:parse_id(Id),
send_work_sync(AppId, CallId, {send_reply, ReqId, Reply}).


Expand All @@ -149,7 +149,7 @@ send_reply(Id, Reply) ->
[{nksip:protocol(), inet:ip_address(), inet:port_number()}].

get_authorized_list(Id) ->
[$D, AppId, CallId, DialogId] = nksip_sipmsg:id_parts(Id),
{AppId, DialogId, CallId} = nksip_dialog:parse_id(Id),
case send_work_sync(AppId, CallId, {get_authorized_list, DialogId}) of
{ok, List} -> List;
_ -> []
Expand All @@ -161,7 +161,7 @@ get_authorized_list(Id) ->
ok | error.

clear_authorized_list(Id) ->
[$D, AppId, CallId, DialogId] = nksip_sipmsg:id_parts(Id),
{AppId, DialogId, CallId} = nksip_dialog:parse_id(Id),
case send_work_sync(AppId, CallId, {clear_authorized_list, DialogId}) of
ok -> ok;
_ -> error
Expand All @@ -173,7 +173,7 @@ clear_authorized_list(Id) ->
ok | {error, unknown_dialog}.

stop_dialog(Id) ->
[$D, AppId, CallId, DialogId] = nksip_sipmsg:id_parts(Id),
{AppId, DialogId, CallId} = nksip_dialog:parse_id(Id),
send_work_sync(AppId, CallId, {stop_dialog, DialogId}).


Expand Down Expand Up @@ -290,8 +290,8 @@ work({apply_dialog, DialogId, Fun}, From, Call) ->
Call
end;

work(get_all_dialogs, From, #call{app_id=AppId, dialogs=Dialogs}=Call) ->
Ids = [{AppId, DialogId} || #dialog{id=DialogId} <- Dialogs],
work(get_all_dialogs, From, #call{dialogs=Dialogs}=Call) ->
Ids = [nksip_dialog:get_id(Dialog) || Dialog <- Dialogs],
gen_server:reply(From, {ok, Ids}),
Call;

Expand Down Expand Up @@ -579,8 +579,8 @@ get_trans(SipMsgId, #call{msgs=Msgs, trans=AllTrans}) ->

get_sipmsg(SipMsgId, Call) ->
case get_trans(SipMsgId, Call) of
{ok, #trans{request=#sipmsg{id1=SipMsgId}=Req}} -> {ok, Req};
{ok, #trans{response=#sipmsg{id1=SipMsgId}=Resp}} -> {ok, Resp};
{ok, #trans{request=#sipmsg{id=SipMsgId}=Req}} -> {ok, Req};
{ok, #trans{response=#sipmsg{id=SipMsgId}=Resp}} -> {ok, Resp};
_ -> not_found
end.

Expand Down
2 changes: 1 addition & 1 deletion src/nksip_call_dialog.erl
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ cast(Fun, Arg, Dialog, Call) ->
#dialog{id=DialogId} = Dialog,
#call{app_id=AppId} = Call,
Args1 = [Dialog, Arg],
Args2 = [DialogId, Arg],
Args2 = [nksip_dialog:get_id(Dialog), Arg],
?call_debug("called dialog ~s ~p: ~p", [DialogId, Fun, Arg]),
nksip_sipapp_srv:sipapp_cast(AppId, Fun, Args1, Args2),
ok.
Expand Down
59 changes: 27 additions & 32 deletions src/nksip_call_event.erl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ uac_response(#sipmsg{class={req, Method}}=Req, Resp, Dialog, Call)
uac_do_response(Method, Code, Req1, Resp, Subs, Dialog, Call);
not_found when Code>=200 andalso Code<300 andalso
(Method=='SUBSCRIBE' orelse Method=='REFER') ->
Subs = #subscription{id=Id} = Subs = create(uac, Req1, Dialog, Call),
Subs = #subscription{id=Id} = create(uac, Req1, Dialog, Call),
?call_debug("Subscription ~s UAC response ~p ~p", [Id, Method, Code]),
uac_do_response(Method, Code, Req1, Resp, Subs, Dialog, Call);
_ ->
Expand Down Expand Up @@ -151,7 +151,7 @@ uas_request(#sipmsg{class={req, Method}}=Req, Dialog, Call)
?call_debug("Subscription ~s UAS request ~p", [Id, Method]),
{ok, Dialog};
not_found when Method=='SUBSCRIBE' andalso
element(1, Req1#sipmsg.event) == <<"refer">> ->
element(1, Req1#sipmsg.event) == <<"refer">> ->
{error, forbidden};
not_found when Method=='SUBSCRIBE'; Method=='REFER' ->
{ok, Dialog};
Expand Down Expand Up @@ -191,7 +191,7 @@ uas_response(#sipmsg{class={req, Method}}=Req, Resp, Dialog, Call)
'REFER' -> uas;
'NOTIFY' -> uac
end,
#subscription{id=Id} = Subs = create(Class, Req1, Dialog, Call),
Subs = #subscription{id=Id} = create(Class, Req1, Dialog, Call),
?call_debug("Subscription ~s UAS response ~p, ~p",
[Id, Method, Code]),
uas_do_response(Method, Code, Req1, Resp, Subs, Dialog, Call);
Expand Down Expand Up @@ -376,14 +376,13 @@ update(Status, Subs, Dialog, Call) ->
nksip_call:call().

create_event(Req, Call) ->
#sipmsg{event={EvType, EvOpts}, from={_, FromTag}} = maybe_add_refer_event(Req, Call),
EvId = nksip_lib:get_binary(<<"id">>, EvOpts),
?call_debug("Event ~s_~s_~s UAC created", [EvType, EvId, FromTag]),
#call{events=Events} = Call,
Id = {EvType, EvId, FromTag},
#sipmsg{event=Event, from={_, FromTag}} = maybe_add_refer_event(Req, Call),
Id = nksip_subscription:make_id(Event),
?call_debug("Event ~s (~s) UAC created", [Id, FromTag]),
#call{timers={T1, _, _, _, _}} = Call,
Timer = erlang:start_timer(64*T1, self(), {remove_event, Id}),
Event = #provisional_event{id=Id, timer_n=Timer},
Event = #provisional_event{id={Id, FromTag}, timer_n=Timer},
#call{events=Events} = Call,
Call#call{events=[Event|Events]}.


Expand All @@ -395,15 +394,15 @@ create_event(Req, Call) ->
nksip_call:call().

remove_event(#sipmsg{}=Req, Call) ->
#sipmsg{event={EvType, EvOpts}, from={_, FromTag}} = maybe_add_refer_event(Req, Call),
EvId = nksip_lib:get_binary(<<"id">>, EvOpts),
remove_event({EvType, EvId, FromTag}, Call);
#sipmsg{event=Event, from={_, FromTag}} = maybe_add_refer_event(Req, Call),
Id = nksip_subscription:make_id(Event),
remove_event({Id, FromTag}, Call);

remove_event({EvType, EvId, Tag}=Id, #call{events=Events}=Call) ->
case lists:keytake(Id, #provisional_event.id, Events) of
remove_event({Id, FromTag}, #call{events=Events}=Call) ->
case lists:keytake({Id, FromTag}, #provisional_event.id, Events) of
{value, #provisional_event{timer_n=Timer}, Rest} ->
cancel_timer(Timer),
?call_debug("Provisional event ~s_~s_~s destroyed", [EvType, EvId, Tag]),
?call_debug("Provisional event ~s (~s) destroyed", [Id, FromTag]),
Call#call{events=Rest};
false ->
Call
Expand All @@ -427,7 +426,8 @@ request_uac_opts(Method, Opts, #dialog{}=Dialog) ->
case lists:keytake(subscription_id, 1, Opts) of
false ->
{ok, Opts};
{value, {_, SubsId}, Opts1} ->
{value, {_, Id}, Opts1} ->
{_AppId, SubsId, _DialogId, _CallId} = nksip_subscription:parse_id(Id),
case find(SubsId, Dialog) of
#subscription{} = Subs ->
{ok, request_uac_opts(Method, Opts1, Subs)};
Expand Down Expand Up @@ -497,12 +497,12 @@ timer({Type, Id}, Dialog, Call) ->
nksip:subscription().

create(Class, Req, Dialog, Call) ->
#sipmsg{event=Event, app_id=AppId} = Req,
Id = nksip_subscription:subscription_id(Event, Dialog#dialog.id),
#sipmsg{event=Event} = Req,
Id = nksip_subscription:make_id(Event),
#call{timers={T1, _, _, _, _}} = Call,
Subs = #subscription{
id = Id,
app_id = AppId,
% app_id = AppId,
event = Event,
status = init,
class = Class,
Expand All @@ -514,19 +514,14 @@ create(Class, Req, Dialog, Call) ->


%% @private Finds a event.
-spec find(nksip:request()|nksip:response()|nksip_subscription:id(), nksip:dialog()) ->
-spec find(nksip:request()|nksip:response(), nksip:dialog()) ->
nksip:subscription() | not_found.

find(#sipmsg{event=Event}, #dialog{id=DialogId, subscriptions=Subs}) ->
Id = nksip_subscription:subscription_id(Event, DialogId),
do_find(Id, Subs);
find(<<"U_", _/binary>>=Id, #dialog{subscriptions=Subs}) ->
do_find(Id, Subs);
find(_, _) ->
not_found.
find(#sipmsg{event=Event}, #dialog{subscriptions=Subs}) ->
do_find(nksip_subscription:make_id(Event), Subs).

%% @private
do_find(_Id, []) -> not_found;
do_find(_, []) -> not_found;
do_find(Id, [#subscription{id=Id}=Subs|_]) -> Subs;
do_find(Id, [_|Rest]) -> do_find(Id, Rest).

Expand All @@ -538,15 +533,15 @@ do_find(Id, [_|Rest]) -> do_find(Id, Rest).

is_event(#sipmsg{event=Event, to={_, ToTag}}, #call{events=Events}) ->
case Event of
{Name, Opts} when is_list(Opts) ->
Id = nksip_lib:get_value(<<"id">>, Opts, <<>>),
do_is_event({Name, Id, ToTag}, Events);
{_, Opts} when is_list(Opts) ->
Id = nksip_subscription:make_id(Event),
do_is_event({Id, ToTag}, Events);
_ ->
false
end.

%% @private.
do_is_event(_Id, []) -> false;
do_is_event(_, []) -> false;
do_is_event(Id, [#provisional_event{id=Id}|_]) -> true;
do_is_event(Id, [_|Rest]) -> do_is_event(Id, Rest).

Expand Down
14 changes: 7 additions & 7 deletions src/nksip_call_router.erl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ incoming_sync(#sipmsg{app_id=AppId, call_id=CallId}=SipMsg) ->
when Error :: unknown_dialog | sync_error().

apply_dialog(Id, Fun) ->
[$D, AppId, CallId, DialogId] = nksip_sipmsg:id_parts(Id),
{AppId, DialogId, CallId} = nksip_dialog:parse_id(Id),
send_work_sync(AppId, CallId, {apply_dialog, DialogId, Fun}).

%% @doc Get all dialog ids for all calls.
Expand Down Expand Up @@ -115,9 +115,9 @@ get_all_dialogs(AppId, CallId) ->
when Error :: unknown_sipmsg | invalid_id | sync_error().

apply_sipmsg(Id, Fun) ->
case nksip_sipmsg:id_parts(Id) of
[Class, AppId, CallId, SipMsgId] when Class==$R; Class==$S->
send_work_sync(AppId, CallId, {apply_sipmsg, SipMsgId, Fun})
case nksip_sipmsg:parse_id(Id) of
{Class, AppId, MsgId, CallId} when Class==req; Class==resp->
send_work_sync(AppId, CallId, {apply_sipmsg, MsgId, Fun})
end.


Expand All @@ -127,9 +127,9 @@ apply_sipmsg(Id, Fun) ->
when Error :: unknown_transaction | sync_error().

apply_transaction(Id, Fun) ->
case nksip_sipmsg:id_parts(Id) of
[Class, AppId, CallId, SipMsgId] when Class==$R; Class==$S->
send_work_sync(AppId, CallId, {apply_transaction, SipMsgId, Fun})
case nksip_sipmsg:parse_id(Id) of
{Class, AppId, MsgId, CallId} when Class==req; Class==resp->
send_work_sync(AppId, CallId, {apply_transaction, MsgId, Fun})
end.


Expand Down
6 changes: 3 additions & 3 deletions src/nksip_call_uac_dialog.erl
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,12 @@ new_local_seq(#sipmsg{dialog_id=DialogId}, Call) ->
end.


%% @private
%% @private Helps generating the transaction id
-spec uac_id(nksip:request()|nksip:response(), boolean(), nksip_call:call()) ->
nksip_dialog:id().

uac_id(SipMsg, IsProxy, #call{dialogs=Dialogs}) ->
case nksip_dialog:class_id(uac, SipMsg) of
case nksip_dialog:make_id(uac, SipMsg) of
<<>> ->
<<>>;
DlgIdA when not IsProxy ->
Expand All @@ -489,7 +489,7 @@ uac_id(SipMsg, IsProxy, #call{dialogs=Dialogs}) ->
true ->
DlgIdA;
false ->
DlgIdB = nksip_dialog:class_id(uas, SipMsg),
DlgIdB = nksip_dialog:make_id(uas, SipMsg),
case lists:keymember(DlgIdB, #dialog.id, Dialogs) of
true -> DlgIdB;
false -> DlgIdA
Expand Down
Loading

0 comments on commit 309249d

Please sign in to comment.