|
22 | 22 | , stop/0
|
23 | 23 | ]).
|
24 | 24 |
|
| 25 | +%%-------------------------------------------------------------------- |
| 26 | +%% APIs |
| 27 | +%%-------------------------------------------------------------------- |
| 28 | + |
25 | 29 | start() ->
|
26 | 30 | {ok, _} = application:ensure_all_started(gen_coap),
|
27 |
| - start_udp(), |
28 |
| - start_dtls(), |
| 31 | + start_listeners(), |
29 | 32 | coap_server_registry:add_handler([<<"mqtt">>], emqx_coap_resource, undefined),
|
30 | 33 | coap_server_registry:add_handler([<<"ps">>], emqx_coap_ps_resource, undefined),
|
31 | 34 | emqx_coap_ps_topics:start_link().
|
32 | 35 |
|
33 | 36 | stop() ->
|
34 |
| - stop_udp(), |
35 |
| - stop_dtls(). |
| 37 | + stop_listeners(). |
| 38 | + |
| 39 | +%%-------------------------------------------------------------------- |
| 40 | +%% Internal funcs |
| 41 | +%%-------------------------------------------------------------------- |
36 | 42 |
|
37 |
| -start_udp() -> |
38 |
| - BindUdps = application:get_env(?APP, bind_udp, [{5683, []}]), |
39 |
| - lists:foreach(fun({Port, InetOpt}) -> |
40 |
| - Name = process_name(coap_udp_socket, Port), |
41 |
| - coap_server:start_udp(Name, Port, InetOpt) |
42 |
| - end, BindUdps). |
| 43 | +start_listeners() -> |
| 44 | + lists:foreach(fun start_listener/1, listeners_confs()). |
43 | 45 |
|
44 |
| -start_dtls() -> |
| 46 | +stop_listeners() -> |
| 47 | + lists:foreach(fun stop_listener/1, listeners_confs()). |
| 48 | + |
| 49 | +start_listener({Proto, ListenOn, Opts}) -> |
| 50 | + case start_listener(Proto, ListenOn, Opts) of |
| 51 | + {ok, _Pid} -> |
| 52 | + io:format("Start coap:~s listener on ~s successfully.~n", |
| 53 | + [Proto, format(ListenOn)]); |
| 54 | + {error, Reason} -> |
| 55 | + io:format(standard_error, "Failed to start coap:~s listener on ~s - ~0p~n!", |
| 56 | + [Proto, format(ListenOn), Reason]), |
| 57 | + error(Reason) |
| 58 | + end. |
| 59 | + |
| 60 | +start_listener(udp, ListenOn, Opts) -> |
| 61 | + coap_server:start_udp('coap:udp', ListenOn, Opts); |
| 62 | +start_listener(dtls, ListenOn, Opts) -> |
| 63 | + coap_server:start_dtls('coap:dtls', ListenOn, Opts). |
| 64 | + |
| 65 | +stop_listener({Proto, ListenOn, _Opts}) -> |
| 66 | + Ret = stop_listener(Proto, ListenOn), |
| 67 | + case Ret of |
| 68 | + ok -> io:format("Stop coap:~s listener on ~s successfully.~n", |
| 69 | + [Proto, format(ListenOn)]); |
| 70 | + {error, Reason} -> |
| 71 | + io:format(standard_error, "Failed to stop coap:~s listener on ~s - ~p~n.", |
| 72 | + [Proto, format(ListenOn), Reason]) |
| 73 | + end, |
| 74 | + Ret. |
| 75 | + |
| 76 | +stop_listener(udp, ListenOn) -> |
| 77 | + coap_server:stop_udp('coap:udp', ListenOn); |
| 78 | +stop_listener(dtls, ListenOn) -> |
| 79 | + coap_server:stop_dtls('coap:dtls', ListenOn). |
| 80 | + |
| 81 | +%% XXX: It is a temporary func to convert conf format for esockd |
| 82 | +listeners_confs() -> |
| 83 | + listeners_confs(udp) ++ listeners_confs(dtls). |
| 84 | + |
| 85 | +listeners_confs(udp) -> |
| 86 | + Udps = application:get_env(?APP, bind_udp, []), |
| 87 | + [{udp, Port, [{udp_options, InetOpts}]} || {Port, InetOpts} <- Udps]; |
| 88 | + |
| 89 | +listeners_confs(dtls) -> |
45 | 90 | case application:get_env(?APP, dtls_opts, []) of
|
46 |
| - [] -> ok; |
| 91 | + [] -> []; |
47 | 92 | DtlsOpts ->
|
48 |
| - BindDtls = application:get_env(?APP, bind_dtls, [{5684, []}]), |
49 |
| - lists:foreach(fun({DtlsPort, InetOpt}) -> |
50 |
| - Name = process_name(coap_dtls_socket, DtlsPort), |
51 |
| - coap_server:start_dtls(Name, DtlsPort, InetOpt ++ DtlsOpts) |
52 |
| - end, BindDtls) |
| 93 | + BindDtls = application:get_env(?APP, bind_dtls, []), |
| 94 | + [{dtls, Port, [{dtls_options, InetOpts ++ DtlsOpts}]} || {Port, InetOpts} <- BindDtls] |
53 | 95 | end.
|
54 | 96 |
|
55 |
| -stop_udp() -> |
56 |
| - BindUdps = application:get_env(?APP, bind_udp, [{5683, []}]), |
57 |
| - lists:foreach(fun({Port, _}) -> |
58 |
| - Name = process_name(coap_udp_socket, Port), |
59 |
| - coap_server:stop_udp(Name) |
60 |
| - end, BindUdps). |
61 |
| - |
62 |
| -stop_dtls() -> |
63 |
| - BindDtls = application:get_env(?APP, bind_dtls, [{5684, []}]), |
64 |
| - lists:foreach(fun({Port, _}) -> |
65 |
| - Name = process_name(coap_dtls_socket, Port), |
66 |
| - coap_server:stop_dtls(Name) |
67 |
| - end, BindDtls). |
68 |
| - |
69 |
| -process_name(Mod, Port) -> |
70 |
| - list_to_atom(atom_to_list(Mod) ++ "_" ++ integer_to_list(Port)). |
| 97 | +format(Port) when is_integer(Port) -> |
| 98 | + io_lib:format("0.0.0.0:~w", [Port]); |
| 99 | +format({Addr, Port}) when is_list(Addr) -> |
| 100 | + io_lib:format("~s:~w", [Addr, Port]); |
| 101 | +format({Addr, Port}) when is_tuple(Addr) -> |
| 102 | + io_lib:format("~s:~w", [inet:ntoa(Addr), Port]). |
| 103 | + |
0 commit comments