Skip to content

Commit 9918674

Browse files
committed
Select a new pair only if it has the same or greater priority
1 parent 0efc5ad commit 9918674

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

lib/ex_ice/candidate.ex

+7-7
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ defmodule ExICE.Candidate do
9494
def new(type, config) when type in [:host, :srflx, :prflx, :relay] do
9595
transport = Keyword.get(config, :transport, :udp)
9696

97-
priority =
98-
if config[:priority] do
99-
config[:priority]
100-
else
101-
base_address = Keyword.fetch!(config, :base_address)
102-
ExICE.Priv.Candidate.priority(base_address, type)
103-
end
97+
priority = Keyword.fetch!(config, :priority)
98+
# if config[:priority] do
99+
# config[:priority]
100+
# else
101+
# base_address = config[:base_address] || config[:address] || raise "no address"
102+
# ExICE.Priv.Candidate.priority(base_address, type)
103+
# end
104104

105105
address = Keyword.fetch!(config, :address)
106106

lib/ex_ice/priv/conn_check_handler/controlling.ex

+27-1
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,39 @@ defmodule ExICE.Priv.ConnCheckHandler.Controlling do
109109
pair = %CandidatePair{pair | nominate?: false, nominated?: true}
110110
ice_agent = put_in(ice_agent.checklist[pair.id], pair)
111111

112+
ice_agent =
113+
cond do
114+
ice_agent.selected_pair_id == nil ->
115+
Logger.debug("Selecting pair: #{pair_id}")
116+
%ICEAgent{ice_agent | selected_pair_id: pair.id}
117+
118+
ice_agent.selected_pair_id != nil and pair.id != ice_agent.selected_pair_id ->
119+
selected_pair = Map.fetch!(ice_agent.checklist, ice_agent.selected_pair_id)
120+
121+
if pair.priority >= selected_pair.priority do
122+
Logger.debug("""
123+
Selecting new pair with higher priority. \
124+
New pair: #{pair_id}, old pair: #{ice_agent.selected_pair_id}.\
125+
""")
126+
127+
%ICEAgent{ice_agent | selected_pair_id: pair.id}
128+
else
129+
Logger.debug("Not selecting a new pair as it has lower priority.")
130+
ice_agent
131+
end
132+
133+
true ->
134+
Logger.debug("Not selecting a new pair as it has the same id")
135+
ice_agent
136+
end
137+
112138
# the controlling agent could nominate only when eoc was set
113139
# and checklist finished
114140
if not ice_agent.aggressive_nomination and not Checklist.finished?(ice_agent.checklist) do
115141
Logger.warning("Nomination succeeded but checklist hasn't finished.")
116142
end
117143

118-
%ICEAgent{ice_agent | nominating?: {false, nil}, selected_pair_id: pair.id}
144+
%ICEAgent{ice_agent | nominating?: {false, nil}}
119145
end
120146

121147
defp resolve_pair(ice_agent, pair) do

lib/ex_ice/priv/ice_agent.ex

+8-2
Original file line numberDiff line numberDiff line change
@@ -2079,11 +2079,17 @@ defmodule ExICE.Priv.ICEAgent do
20792079
end
20802080
end
20812081

2082-
defp get_or_create_remote_cand(ice_agent, src_ip, src_port, _prio_attr) do
2082+
defp get_or_create_remote_cand(ice_agent, src_ip, src_port, prio_attr) do
20832083
case find_remote_cand(Map.values(ice_agent.remote_cands), src_ip, src_port) do
20842084
nil ->
20852085
# TODO calculate correct prio using prio_attr
2086-
cand = ExICE.Candidate.new(:prflx, address: src_ip, port: src_port)
2086+
cand =
2087+
ExICE.Candidate.new(:prflx,
2088+
address: src_ip,
2089+
port: src_port,
2090+
priority: prio_attr.priority
2091+
)
2092+
20872093
Logger.debug("Adding new remote prflx candidate: #{inspect(cand)}")
20882094
ice_agent = put_in(ice_agent.remote_cands[cand.id], cand)
20892095
{cand, ice_agent}

0 commit comments

Comments
 (0)