Skip to content

Commit ce379b9

Browse files
authored
Make distinction between valid and succeeded pair (#4)
1 parent 7c93c4d commit ce379b9

File tree

5 files changed

+112
-73
lines changed

5 files changed

+112
-73
lines changed

lib/candidate_pair.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ defmodule ExICE.CandidatePair do
1717
priority: non_neg_integer(),
1818
remote_cand: Candidate.t(),
1919
state: state(),
20-
valid?: boolean
20+
valid?: boolean,
21+
succeeded_pair_id: integer() | nil,
22+
discovered_pair_id: integer() | nil
2123
}
2224

2325
@enforce_keys [:id, :local_cand, :remote_cand, :priority]
@@ -26,7 +28,9 @@ defmodule ExICE.CandidatePair do
2628
nominate?: false,
2729
nominated?: false,
2830
state: :frozen,
29-
valid?: false
31+
valid?: false,
32+
succeeded_pair_id: nil,
33+
discovered_pair_id: nil
3034
]
3135

3236
@doc false

lib/conn_check_handler.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ defmodule ExICE.ConnCheckHandler do
2020
2121
Its implementation is based on sec. 7.2.5.3.4.
2222
23+
`pair_id` is the id of valid pair, not the succeeded one.
24+
2325
The meaning of `nominate?` flag depends on the ice agent role.
2426
2527
In case of controlled, it means that either we received nomination

lib/controlled_handler.ex

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ defmodule ExICE.ControlledHandler do
1818
Logger.debug("Adding new candidate pair: #{inspect(pair)}")
1919
put_in(state, [:checklist, pair.id], pair)
2020

21-
%CandidatePair{} = pair when pair == state.selected_pair ->
21+
%CandidatePair{} = pair
22+
when state.selected_pair != nil and pair.discovered_pair_id == state.selected_pair.id ->
2223
# to be honest this might also be a retransmission
23-
Logger.debug("Keepalive on selected pair: #{pair.id}")
24+
Logger.debug("Keepalive on selected pair: #{pair.discovered_pair_id}")
2425
state
2526

2627
%CandidatePair{} ->
@@ -44,19 +45,15 @@ defmodule ExICE.ControlledHandler do
4445
pair = %CandidatePair{pair | nominate?: true}
4546
put_in(state, [:checklist, pair.id], pair)
4647

47-
%CandidatePair{} = pair when pair == state.selected_pair ->
48+
%CandidatePair{} = pair
49+
when state.selected_pair != nil and pair.discovered_pair_id == state.selected_pair.id ->
4850
Logger.debug("Keepalive on selected pair: #{pair.id}")
4951
state
5052

5153
%CandidatePair{} = pair ->
5254
if pair.state == :succeeded do
53-
# FIXME we should check against valid? flag
54-
# it's possible to have to pairs: one with state succeeded
55-
# and flag valid? set to false, and the other one with state
56-
# succeeded and flag valid? set to true
57-
# TODO should we call this selected or nominated pair
58-
Logger.debug("Nomination request on valid pair: #{pair.id}.")
59-
update_nominated_flag(state, pair.id, true)
55+
Logger.debug("Nomination request on pair: #{pair.id}.")
56+
update_nominated_flag(state, pair.discovered_pair_id, true)
6057
else
6158
# TODO should we check if this pair is not in failed?
6259
Logger.debug("""
@@ -79,31 +76,26 @@ defmodule ExICE.ControlledHandler do
7976
Logger.debug("Nomination succeeded, pair: #{pair_id}")
8077

8178
pair = Map.fetch!(state.checklist, pair_id)
79+
pair = %CandidatePair{pair | nominate?: false, nominated?: true}
8280

83-
state =
84-
cond do
85-
state.selected_pair == nil ->
86-
Logger.debug("Selecting pair: #{pair_id}")
87-
%{state | selected_pair: pair}
88-
89-
state.selected_pair != nil and pair.priority >= state.selected_pair.priority ->
90-
Logger.debug("""
91-
Selecting new pair with higher priority. \
92-
New pair: #{pair_id}, old pair: #{state.selected_pair.id}.\
93-
""")
81+
state = put_in(state, [:checklist, pair.id], pair)
9482

95-
%{state | selected_pair: pair}
83+
cond do
84+
state.selected_pair == nil ->
85+
Logger.debug("Selecting pair: #{pair_id}")
86+
%{state | selected_pair: pair}
9687

97-
true ->
98-
Logger.debug("Not selecting a new pair as it has lower priority")
99-
state
100-
end
88+
state.selected_pair != nil and pair.priority >= state.selected_pair.priority ->
89+
Logger.debug("""
90+
Selecting new pair with higher priority. \
91+
New pair: #{pair_id}, old pair: #{state.selected_pair.id}.\
92+
""")
10193

102-
checklist =
103-
Map.update!(state.checklist, pair_id, fn pair ->
104-
%CandidatePair{pair | nominate?: false, nominated?: true}
105-
end)
94+
%{state | selected_pair: pair}
10695

107-
%{state | checklist: checklist, selected_pair: Map.fetch!(checklist, pair_id)}
96+
true ->
97+
Logger.debug("Not selecting a new pair as it has lower priority")
98+
state
99+
end
108100
end
109101
end

lib/controlling_handler.ex

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ defmodule ExICE.ControllingHandler do
2929
Logger.debug("Adding new candidate pair: #{inspect(pair)}")
3030
put_in(state, [:checklist, pair.id], pair)
3131

32-
%CandidatePair{} = pair when pair == state.selected_pair ->
32+
%CandidatePair{} = pair
33+
when state.selected_pair != nil and pair.discovered_pair_id == state.selected_pair.id ->
3334
# to be honest this might also be a retransmission
34-
Logger.debug("Keepalive on selected pair: #{pair.id}")
35+
Logger.debug("Keepalive on selected pair: #{pair.discovered_pair_id}")
3536
state
3637

3738
%CandidatePair{} ->
@@ -48,21 +49,17 @@ defmodule ExICE.ControllingHandler do
4849
Logger.debug("Nomination succeeded. Selecting pair: #{inspect(pair_id)}")
4950
state = ICEAgent.change_connection_state(:completed, state)
5051

51-
checklist =
52-
Map.update!(state.checklist, pair_id, fn pair ->
53-
%CandidatePair{pair | nominate?: false, nominated?: true}
54-
end)
52+
pair = Map.fetch!(state.checklist, pair_id)
53+
pair = %CandidatePair{pair | nominate?: false, nominated?: true}
54+
55+
state = put_in(state, [:checklist, pair.id], pair)
5556

5657
# the controlling agent could nominate only when eoc was set
5758
# and checklist finished
58-
# important: we have to check on a new checklist
59-
false = Checklist.in_progress?(checklist) or Checklist.waiting?(checklist)
59+
unless Checklist.finished?(state.checklist) do
60+
Logger.warning("Nomination succeeded but checklist hasn't finished.")
61+
end
6062

61-
%{
62-
state
63-
| checklist: checklist,
64-
nominating?: {false, nil},
65-
selected_pair: Map.fetch!(checklist, pair_id)
66-
}
63+
%{state | nominating?: {false, nil}, selected_pair: pair}
6764
end
6865
end

0 commit comments

Comments
 (0)