forked from NetComposer/nksip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnksip_tutorial.erl
126 lines (97 loc) · 4.35 KB
/
nksip_tutorial.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
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2015 Carlos Gonzalez Florido. All Rights Reserved.
%%
%% This file is provided to you 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.
%%
%% -------------------------------------------------------------------
%% @doc Companion code for NkSIP Tutorial.
-module(nksip_tutorial).
-author('Carlos Gonzalez <[email protected]>').
-export([launch/0, trace/1, loglevel/1]).
%% @doc Launches the full tutorial.
launch() ->
{ok, _} = nksip:start(server, #{
sip_local_host => "localhost",
callback => nksip_tutorial_server_callbacks,
plugins => [nksip_registrar],
sip_listen => "sip:all:5060, <sip:all:5061;transport=tls>"
}),
{ok, _} = nksip:start(client1, #{
sip_local_host => "localhost",
sip_from => "sip:client1@nksip",
callback => nksip_tutorial_client_callbacks,
plugins => [nksip_uac_auto_auth],
sip_listen => "sip:127.0.0.1:5070, sips:127.0.0.1:5071"
}),
{ok, _} = nksip:start(client2, #{
sip_local_host => "localhost",
sip_from => "sips:client2@nksip",
callback => nksip_tutorial_client_callbacks,
plugins => [nksip_uac_auto_auth],
sip_listen => "sip:all, sips:all"
}),
nksip_registrar_util:clear(),
{ok,200,[]} = nksip_uac:options(client2, "sip:127.0.0.1:5070", []),
{ok,407,[{reason_phrase, <<"Proxy Authentication Required">>}]} =
nksip_uac:options(client1, "sip:127.0.0.1", [{meta,[reason_phrase]}]),
{ok,200,[]} = nksip_uac:options(client1, "sip:127.0.0.1", [{sip_pass, "1234"}]),
{ok,200,[]} = nksip_uac:options(client2, "<sip:127.0.0.1;transport=tls>", [{sip_pass, "1234"}]),
{ok,200,[{<<"contact">>, [<<"<sip:client1@localhost:5070>", _/binary>>]}]} =
nksip_uac:register(client1, "sip:127.0.0.1",
[{sip_pass, "1234"}, contact, {meta, [<<"contact">>]}]),
{ok,200,[]} = nksip_uac:register(client2, "sips:127.0.0.1", [{sip_pass, "1234"}, contact]),
{ok,200,[{all_headers, _}]} =
nksip_uac:register(client2, "sips:127.0.0.1", [{sip_pass, "1234"}, {meta, [all_headers]}]),
{ok,200,[]} = nksip_uac:options(client1, "sip:127.0.0.1", []),
{ok,200,[]} = nksip_uac:options(client2, "sips:127.0.0.1", []),
{ok,407,[]} = nksip_uac:options(client1, "sips:client2@nksip", [{route, "<sip:127.0.0.1;lr>"}]),
{ok,200,[{<<"x-nk-id">>, [<<"client2">>]}]} =
nksip_uac:options(client1, "sips:client2@nksip",
[{route, "<sip:127.0.0.1;lr>"}, {sip_pass, "1234"},
{meta, [<<"x-nk-id">>]}]),
{ok,488,[]} =
nksip_uac:invite(client2, "sip:client1@nksip", [{route, "<sips:127.0.0.1;lr>"}]),
{ok,200,[{dialog, DlgId}]}=
nksip_uac:invite(client2, "sip:client1@nksip",
[{route, "<sips:127.0.0.1;lr>"}, {body, nksip_sdp:new()},
auto_2xx_ack]),
{ok, confirmed} = nksip_dialog:meta(invite_status, DlgId),
[_, _, _] = nksip_dialog:get_all_data(),
timer:sleep(1000),
{ok,200,[]} = nksip_uac:bye(DlgId, []),
ok = nksip:stop_all().
%% ===================================================================
%% Utilities
%% ===================================================================
%% @doc Enables SIP trace messages to console.
-spec trace(Start::boolean()) ->
ok.
trace(true) ->
nksip_trace:start(),
ok;
trace(false) ->
nksip_trace:stop(),
ok.
%% @doc Changes console log level.
%% Availanle options are `debug' (maximum), `info' (medium) and `notice' (minimum).
-spec loglevel(debug|info|notice) ->
ok.
loglevel(Level) ->
nklib_log:console_loglevel(Level),
ok = nksip:update(server, [{log_level, Level}]),
ok = nksip:update(client1, [{log_level, Level}]),
ok = nksip:update(client2, [{log_level, Level}]),
ok.