-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start mDNS only once per application
- Loading branch information
Showing
4 changed files
with
87 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule ExLibnice.App do | ||
@moduledoc false | ||
use Application | ||
|
||
require Logger | ||
|
||
@impl true | ||
def start(_start_type, _start_args) do | ||
children = | ||
if Application.get_env(:ex_libnice, :mdns, true) do | ||
[ExLibnice.Mdns] | ||
else | ||
[] | ||
end | ||
|
||
Supervisor.start_link(children, strategy: :one_for_one) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule ExLibnice.Mdns do | ||
@moduledoc """ | ||
Module for executing mDNS queries. | ||
""" | ||
use GenServer | ||
require Logger | ||
|
||
@spec start_link() :: GenServer.on_start() | ||
def start_link() do | ||
GenServer.start_link(__MODULE__, [], name: __MODULE__) | ||
end | ||
|
||
@spec query(pid(), String.t()) :: :ok | ||
def query(from, address) do | ||
GenServer.cast(__MODULE__, {:query, from, address}) | ||
end | ||
|
||
@impl true | ||
def init(_opts) do | ||
Logger.debug("Initializing mDNS lookup process") | ||
:ok = Mdns.Client.start() | ||
Logger.debug("Registering for mDNS events") | ||
Mdns.EventManager.register() | ||
{:ok, %{queries: %{}}} | ||
end | ||
|
||
@impl true | ||
def handle_cast({:query, from, address}, state) do | ||
Logger.debug("Sending query to resolve mDNS address #{inspect(address)}") | ||
Mdns.Client.query(address) | ||
state = put_in(state, [:queries, address], from) | ||
{:noreply, state} | ||
end | ||
|
||
@impl true | ||
def handle_info({_namespace, %Mdns.Client.Device{} = dev} = msg, state) do | ||
Logger.debug("mDNS address resolved #{inspect(msg)}") | ||
|
||
{from, state} = pop_in(state, [:queries, dev.domain]) | ||
|
||
if from do | ||
send(from, {:mdns_response, dev.domain, dev.ip}) | ||
else | ||
Logger.debug(""" | ||
mDNS response for non existing query. | ||
We have probably already resolved address #{inspect(dev.domain)} | ||
""") | ||
end | ||
|
||
{:noreply, state} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters