forked from carlosedp/diameter-dcca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.erl
executable file
·98 lines (77 loc) · 2.8 KB
/
server.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
%%
%% An example Diameter server that can respond to the base protocol
%% sent by the client example.
%%
%% The simplest example to start a server listening on the loopback
%% address (which will serve the example usage given in client.erl) is
%% like this assuming diameter is already started (eg. diameter:start()):
%%
%% server:start().
%% server:listen(tcp).
%%
%% The first call starts a service, the second adds a transport listening
%% on the default port.
%%
-module(server).
-include_lib("diameter/include/diameter.hrl").
-include_lib("diameter/include/diameter_gen_base_rfc3588.hrl").
-include_lib("diameter_settings.hrl").
-export([start/1, %% start a service
listen/2, %% add a listening transport
stop/1]). %% stop a service
%% Convenience functions using the default service name, ?SVC_NAME.
-export([start/0,
listen/1,
stop/0,
restart/0]).
-define(DIA_STATS_TAB, dcca_stats).
-define(DIA_STATS_COUNTERS, [event_OK, event_ERR]).
%% Server parameters
-define(SVC_NAME, ?MODULE).
-define(APP_ALIAS, ?MODULE).
-define(CALLBACK_MOD, server_cb).
-define(DIAMETER_DICT_CCRA, rfc4006_cc_Gy).
%% The service configuration. In a server supporting multiple Diameter
%% applications each application may have its own, although they could all
%% be configured with a common callback module.
-define(SERVICE(Name), [{'Origin-Host', ?ORIGIN_HOST},
{'Origin-Realm', ?ORIGIN_REALM},
{'Vendor-Id', ?VENDOR_ID},
{'Product-Name', "Server"},
{'Auth-Application-Id', [?DCCA_APPLICATION_ID]},
{application,
[{alias, ?APP_ALIAS},
{dictionary, ?DIAMETER_DICT_CCRA},
{module, ?CALLBACK_MOD}]
}]).
%% start/1
start(Name)
when is_atom(Name) ->
diameter:start(),
OCS = ocs:start(ocs_intm),
register(ocs, OCS),
common_stats:init(?DIA_STATS_TAB, ?DIA_STATS_COUNTERS),
diameter:start_service(Name, ?SERVICE(Name)),
listen({address, ?DIAMETER_PROTO, ?DIAMETER_IP, ?DIAMETER_PORT}).
start() ->
start(?SVC_NAME).
%% listen/2
listen(Name, {address, Protocol, IPAddr, Port}) ->
{ok, IP} = inet_parse:address(IPAddr),
TransportOpts = [{transport_module, tmod(Protocol)},
{transport_config, [{reuseaddr, true},
{ip, IP}, {port, Port}]}],
diameter:add_transport(Name, {listen, TransportOpts}).
listen(Address) ->
listen(?SVC_NAME, Address).
%% stop/1
stop(Name) ->
common_stats:terminate(?DIA_STATS_TAB),
diameter:stop_service(Name).
stop() ->
stop(?SVC_NAME).
restart() ->
stop(),
start().
tmod(tcp) -> diameter_tcp;
tmod(sctp) -> diameter_sctp.