-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start mDNS only once per app #22
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
663d3ea
Start mDNS only once per application
mickel8 51e951f
Don't override values in mdns_queries and queries map.
mickel8 62e3ce4
Use send instead of GenServer.cast. Improve docs
mickel8 a7c4d7d
Use `handle_info` instead of `handle_cast`. Small code refactor and d…
mickel8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,16 @@ | ||
defmodule ExLibnice.App do | ||
@moduledoc false | ||
use Application | ||
|
||
@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,63 @@ | ||
defmodule ExLibnice.Mdns do | ||
@moduledoc """ | ||
Module responsible for executing mDNS queries. | ||
|
||
It can be turned off by `config :ex_libnice, mdns: false`. | ||
""" | ||
use GenServer | ||
require Logger | ||
|
||
@spec start_link(any()) :: GenServer.on_start() | ||
def start_link(opts \\ []) do | ||
GenServer.start_link(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
@spec query(String.t()) :: :ok | ||
def query(address) do | ||
send(__MODULE__, {:query, self(), 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_info({:query, from, address}, state) do | ||
state = | ||
update_in(state, [:queries, address], fn | ||
# first query for this address | ||
nil -> | ||
Logger.debug("Sending query to resolve mDNS address #{inspect(address)}") | ||
Mdns.Client.query(address) | ||
[from] | ||
|
||
pids -> | ||
[from | pids] | ||
end) | ||
|
||
{:noreply, state} | ||
end | ||
|
||
@impl true | ||
def handle_info({_namespace, %Mdns.Client.Device{} = dev} = msg, state) do | ||
Logger.debug("mDNS address resolved #{inspect(msg)}") | ||
|
||
{pids, state} = pop_in(state, [:queries, dev.domain]) | ||
|
||
if pids do | ||
for pid <- pids, do: send(pid, {: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 |
File renamed without changes.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a word of docs about this would be useful