Skip to content

Commit

Permalink
Make dnssd optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
surik committed Sep 30, 2015
1 parent 5d4d2c6 commit 104f47a
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 14 deletions.
7 changes: 7 additions & 0 deletions doc/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@
* ___Explanation___: Problems in hello application initialization.
* ___Level___: info

#### 4001

* ___Messages___:
* [cde9a95372554e5fa65c76a9c62bf9d1] Application dnssd is not started.
* ___Explanation___: Problems during starting dnssd
* ___Level___: error

#### 4102

* ___Messages___:
Expand Down
3 changes: 3 additions & 0 deletions include/hello_log_ids.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@
-define(LOGID61, {ab65930009164e7fb6419b615b4ae4b5, 4102}).
-define(LOGID62, {bd81f08a8fc94502bcbd07975e952016, 4310}).
-define(LOGID63, {c796ed5e01f0455fb09c42e0f04198c1, 4310}).

%MESSAGE Ids and SATUS CODEs for optional starting dnssd
-define(LOGID64, {cde9a95372554e5fa65c76a9c62bf9d1, 4001}).
7 changes: 4 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ defmodule Hello.Mixfile do
end

def application do
[applications: [:lager, :exometer_core, :cowboy, :ex_uri, :ezmq, :dnssd, :hackney, :jsx],
env: [{:metrics, [:packets, :request, :response, :service, :handler, :binding, :listener, :client]},
[applications: [:lager, :exometer_core, :cowboy, :ex_uri, :ezmq, :hackney, :jsx],
env: [{:dnssd, false},
{:metrics, [:packets, :request, :response, :service, :handler, :binding, :listener, :client]},
{:default_protocol, :hello_proto_jsonrpc},
{:transports, []},
{:server_timeout, 10000},
Expand All @@ -29,7 +30,7 @@ defmodule Hello.Mixfile do
{:ezmq, github: "RoadRunnr/ezmq", branch: "fix-socket-crash"},
{:jsx, "~> 2.6.2"},
{:msgpack, github: "msgpack/msgpack-erlang", branch: "master"},
{:dnssd, github: "benoitc/dnssd_erlang", branch: "master"},
{:dnssd, github: "benoitc/dnssd_erlang", branch: "master", optional: true},
{:meck, "~> 0.8.2", override: true},
{:exometer_core, github: "Feuerlabs/exometer_core", branch: "master"},
{:edown, github: "uwiger/edown", branch: "master", override: true}]
Expand Down
3 changes: 2 additions & 1 deletion src/hello.app.src
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
% vim: filetype=erlang
{application, hello,
[{description, "JSON-RPC API toolkit"},
{applications, [kernel, stdlib, lager, exometer_core, cowboy, ex_uri, ezmq, dnssd, hackney, jsx]},
{applications, [kernel, stdlib, lager, exometer_core, cowboy, ex_uri, ezmq, hackney, jsx]},
{registered, [hello_supervisor, hello_stateless_zmq_supervisor]},
{vsn, git},
{mod, {hello, []}},
{env, [
%% metrics is list of packets | request | response | service | handler | binding | listener | client
{dnssd, false},
{metrics, [packets, request, response, service, handler, binding, listener, client]},
{default_protocol, hello_proto_jsonrpc},
{transports, []},
Expand Down
15 changes: 15 additions & 0 deletions src/hello.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
-export([bind_handler/3, bind/2, bind/3, bind/7, unbind/2]).

-include("hello.hrl").
-include("hello_log.hrl").
-include_lib("ex_uri/include/ex_uri.hrl").

-type decoded_url() :: #ex_uri{}.
Expand All @@ -50,6 +51,7 @@ start() ->

% @doc Callback for application behaviour.
start(_Type, _StartArgs) ->
ok = start_dnssd(),
{ok, Supervisor} = hello_supervisor:start_link(),
{ok, Metrics} = application:get_env(hello, metrics),
hello_metrics:start_subscriptions(Metrics),
Expand Down Expand Up @@ -175,3 +177,16 @@ on_ex_uri(URL, Fun) ->
Other ->
error(badarg, [URL, Other])
end.

start_dnssd() ->
Reason = case application:get_env(hello, dnssd) of
{ok, true} ->
case application:ensure_all_started(dnssd) of
{ok, _} -> ok;
Error -> Error
end;
_ -> ok
end,
Reason /= ok andalso ?LOG_ERROR("Application dnssd is not started.", [],
[{hello_dnssd_starting_reason, Reason}], ?LOGID64),
Reason.
5 changes: 4 additions & 1 deletion src/hello_lib.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-module(hello_lib).
-export([get_in/2, to_binary/1, wrap/1, get/2, get/3]).
-export([get_in/2, to_binary/1, wrap/1, get/2, get/3, is_dnssd_started/0]).

get_in(Map, Path) when is_list(Path) == false -> get_in(Map, Path);
get_in(Value, []) -> Value;
Expand All @@ -23,3 +23,6 @@ to_binary(Term) -> unicode:characters_to_binary(io_lib:format("~p", [Term])).

wrap(Requests) when is_list(Requests) -> Requests;
wrap(Request) -> [Request].

is_dnssd_started() ->
lists:member(dnssd, [App || {App, _, _} <- application:which_applications()]).
14 changes: 10 additions & 4 deletions src/hello_registry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,16 @@ do_dnss_register(App, Name, Port) ->
_ -> ok
end.

dnss_register(App, Name, Port)
when is_list(App), is_list(Name), is_integer(Port) ->
do_dnss_register(list_to_binary(App), list_to_binary(Name), Port);
dnss_register(App, Name, Port) when is_list(App), is_list(Name), is_integer(Port) ->
case hello_lib:is_dnssd_started() of
true -> do_dnss_register(list_to_binary(App), list_to_binary(Name), Port);
false -> ok
end;
dnss_register(_, _, _) -> ok.

dnssd_clean(Ref) when is_reference(Ref) -> dnssd:stop(Ref);
dnssd_clean(Ref) when is_reference(Ref) ->
case hello_lib:is_dnssd_started() of
true -> dnssd:stop(Ref);
false -> ok
end;
dnssd_clean(_) -> ok.
9 changes: 6 additions & 3 deletions src/transports/hello_http_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@ validate_options([_ | R], Opts) ->
validate_options([], Opts) ->
{ok, Opts}.

http_connect_url(#ex_uri{authority = #ex_uri_authority{host = Host}, path = [$/|Path]}) ->
dnssd:resolve(list_to_binary(Path), <<"_", (list_to_binary(Host))/binary, "._tcp.">>, <<"local.">>),
ok;
http_connect_url(#ex_uri{authority = #ex_uri_authority{host = Host}, path = [$/|Path]} = URI) ->
case hello_lib:is_dnssd_started() of
true ->
dnssd:resolve(list_to_binary(Path), <<"_", (list_to_binary(Host))/binary, "._tcp.">>, <<"local.">>);
false -> URI
end;
http_connect_url(URI) ->
URI.

Expand Down
8 changes: 6 additions & 2 deletions src/transports/hello_zmq_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ zmq_protocol(#ex_uri{scheme = "zmq-tcp6"}) -> inet6.
%% use dnssd to resolve port AND host
%% map host to Type and Path to Name
ezmq_connect_url(_Socket, #ex_uri{authority = #ex_uri_authority{host = Host, port = undefined}, path = [$/|Path]}) ->
dnssd:resolve(list_to_binary(Path), <<"_", (list_to_binary(Host))/binary, "._tcp.">>, <<"local.">>),
ok;
case hello_lib:is_dnssd_started() of
true ->
dnssd:resolve(list_to_binary(Path), <<"_", (list_to_binary(Host))/binary, "._tcp.">>, <<"local.">>),
ok;
false -> {error, port_undefined}
end;

ezmq_connect_url(Socket, URI = #ex_uri{authority = #ex_uri_authority{host = Host, port = Port}}) ->
Protocol = zmq_protocol(URI),
Expand Down

0 comments on commit 104f47a

Please sign in to comment.