Skip to content

Commit

Permalink
rename CandidateBase to Candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Mar 9, 2024
1 parent f341a71 commit e71579d
Show file tree
Hide file tree
Showing 11 changed files with 624 additions and 685 deletions.
60 changes: 7 additions & 53 deletions rtc-ice/src/agent/agent_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Agent {
fn nominate_pair(&mut self) {
let result = {
if let Some(index) = &self.nominated_pair {
let pair = &self.checklist[*index];
let pair = &self.candidate_pairs[*index];
// The controlling agent MUST include the USE-CANDIDATE attribute in
// order to nominate a candidate pair (Section 8.1.1). The controlled
// agent MUST NOT include the USE-CANDIDATE attribute in a Binding
Expand Down Expand Up @@ -116,52 +116,6 @@ impl Agent {
}
}

pub(crate) fn get_selected_pair(&self) -> Option<usize> {
self.selected_pair
}

pub(crate) fn get_best_available_candidate_pair(&self) -> Option<usize> {
let mut best: Option<usize> = None;

for (index, p) in self.checklist.iter().enumerate() {
if p.state == CandidatePairState::Failed {
continue;
}

if let Some(best_index) = &mut best {
let b = &self.checklist[*best_index];
if b.priority() < p.priority() {
*best_index = index;
}
} else {
best = Some(index);
}
}

best
}

pub(crate) fn get_best_valid_candidate_pair(&self) -> Option<usize> {
let mut best: Option<usize> = None;

for (index, p) in self.checklist.iter().enumerate() {
if p.state != CandidatePairState::Succeeded {
continue;
}

if let Some(best_index) = &mut best {
let b = &self.checklist[*best_index];
if b.priority() < p.priority() {
*best_index = index;
}
} else {
best = Some(index);
}
}

best
}

pub(crate) fn start(&mut self) {
if self.is_controlling {
ControllingSelector::start(self);
Expand Down Expand Up @@ -233,15 +187,15 @@ impl ControllingSelector for Agent {
self.nominate_pair();
} else {
let has_nominated_pair = if let Some(index) = self.get_best_valid_candidate_pair() {
let p = self.checklist[index];
let p = self.candidate_pairs[index];
self.is_nominatable(p.local, true) && self.is_nominatable(p.remote, false)
} else {
false
};

if has_nominated_pair {
if let Some(index) = self.get_best_valid_candidate_pair() {
let p = &mut self.checklist[index];
let p = &mut self.candidate_pairs[index];
log::trace!(
"Nominatable pair found, nominating ({}, {})",
self.local_candidates[p.local],
Expand Down Expand Up @@ -309,7 +263,7 @@ impl ControllingSelector for Agent {
let selected_pair_is_none = self.get_selected_pair().is_none();

if let Some(index) = self.find_pair(local, remote) {
let p = &mut self.checklist[index];
let p = &mut self.candidate_pairs[index];
p.state = CandidatePairState::Succeeded;
log::trace!(
"Found valid candidate pair: {}, p.state: {}, isUseCandidate: {}, {}",
Expand Down Expand Up @@ -339,7 +293,7 @@ impl ControllingSelector for Agent {
log::trace!("controllingSelector: sendBindingSuccess");

if let Some(index) = self.find_pair(local, remote) {
let p = &self.checklist[index];
let p = &self.candidate_pairs[index];
let nominated_pair_is_none = self.nominated_pair.is_none();

log::trace!(
Expand Down Expand Up @@ -451,7 +405,7 @@ impl ControlledSelector for Agent {
);

if let Some(index) = self.find_pair(local, remote) {
let p = &mut self.checklist[index];
let p = &mut self.candidate_pairs[index];
p.state = CandidatePairState::Succeeded;
log::trace!("Found valid candidate pair: {}", *p);
} else {
Expand All @@ -473,7 +427,7 @@ impl ControlledSelector for Agent {
}

if let Some(index) = self.find_pair(local, remote) {
let p = &self.checklist[index];
let p = &self.candidate_pairs[index];
let use_candidate = m.contains(ATTR_USE_CANDIDATE);
if use_candidate {
// https://tools.ietf.org/html/rfc8445#section-7.3.1.5
Expand Down
4 changes: 2 additions & 2 deletions rtc-ice/src/agent/agent_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ impl Default for CandidateStats {
impl Agent {
/// Returns a list of candidate pair stats.
pub fn get_candidate_pairs_stats(&self) -> Vec<CandidatePairStats> {
let mut res = Vec::with_capacity(self.checklist.len());
for cp in &self.checklist {
let mut res = Vec::with_capacity(self.candidate_pairs.len());
for cp in &self.candidate_pairs {
let stat = CandidatePairStats {
timestamp: Instant::now(),
local_candidate_id: self.local_candidates[cp.local].id(),
Expand Down
90 changes: 67 additions & 23 deletions rtc-ice/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ use stun::attributes::*;
use stun::fingerprint::*;
use stun::integrity::*;
use stun::message::*;
use stun::textattrs::Username;
use stun::textattrs::*;
use stun::xoraddr::*;

use crate::candidate::candidate_pair::{CandidatePair, CandidatePairState};
use crate::candidate::*;
use crate::candidate::{candidate_pair::*, *};
use crate::rand::*;
use crate::state::*;
use crate::url::*;
Expand Down Expand Up @@ -78,17 +77,16 @@ pub struct Agent {

pub(crate) start_time: Instant,

pub(crate) nominated_pair: Option<usize>,
pub(crate) selected_pair: Option<usize>,
pub(crate) checklist: Vec<CandidatePair>,

pub(crate) connection_state: ConnectionState,

//pub(crate) started_ch_tx: Mutex<Option<broadcast::Sender<()>>>,
pub(crate) ufrag_pwd: UfragPwd,

pub(crate) local_candidates: Vec<Box<dyn Candidate>>,
pub(crate) remote_candidates: Vec<Box<dyn Candidate>>,
pub(crate) local_candidates: Vec<Candidate>,
pub(crate) remote_candidates: Vec<Candidate>,
pub(crate) candidate_pairs: Vec<CandidatePair>,
pub(crate) nominated_pair: Option<usize>,
pub(crate) selected_pair: Option<usize>,

// LRU of outbound Binding request Transaction IDs
pub(crate) pending_binding_requests: Vec<BindingRequest>,
Expand Down Expand Up @@ -146,7 +144,7 @@ impl Agent {

nominated_pair: None,
selected_pair: None,
checklist: vec![],
candidate_pairs: vec![],

connection_state: ConnectionState::New,

Expand Down Expand Up @@ -221,9 +219,9 @@ impl Agent {
}

/// Adds a new local candidate.
pub fn add_local_candidate(&mut self, c: Box<dyn Candidate>) -> Result<()> {
pub fn add_local_candidate(&mut self, c: Candidate) -> Result<()> {
for cand in &self.local_candidates {
if cand.equal(&*c) {
if cand.equal(&c) {
return Ok(());
}
}
Expand All @@ -240,7 +238,7 @@ impl Agent {
}

/// Adds a new remote candidate.
pub fn add_remote_candidate(&mut self, c: Box<dyn Candidate>) -> Result<()> {
pub fn add_remote_candidate(&mut self, c: Candidate) -> Result<()> {
// If we have a mDNS Candidate lets fully resolve it before adding it locally
if c.candidate_type() == CandidateType::Host && c.address().ends_with(".local") {
log::warn!(
Expand All @@ -251,7 +249,7 @@ impl Agent {
}

for cand in &self.remote_candidates {
if cand.equal(&*c) {
if cand.equal(&c) {
return Ok(());
}
}
Expand Down Expand Up @@ -344,7 +342,7 @@ impl Agent {

self.pending_binding_requests = vec![];

self.checklist = vec![];
self.candidate_pairs = vec![];

self.set_selected_pair(None);
self.delete_all_candidates(keep_local_candidates);
Expand All @@ -360,7 +358,7 @@ impl Agent {
}

// Returns the local candidates.
pub(crate) fn get_local_candidates(&self) -> &[Box<dyn Candidate>] {
pub(crate) fn get_local_candidates(&self) -> &[Candidate] {
&self.local_candidates
}

Expand Down Expand Up @@ -518,10 +516,10 @@ impl Agent {
log::trace!(
"[{}]: Set selected candidate pair: {:?}",
self.get_name(),
self.checklist[index]
self.candidate_pairs[index]
);

let p = &mut self.checklist[index];
let p = &mut self.candidate_pairs[index];
p.nominated = true;
self.selected_pair = Some(index);

Expand Down Expand Up @@ -552,7 +550,7 @@ impl Agent {

{
let name = self.get_name().to_string();
let checklist = &mut self.checklist;
let checklist = &mut self.candidate_pairs;
if checklist.is_empty() {
log::warn!(
"[{}]: pingAllCandidates called with no candidate pairs. Connection is not possible yet.",
Expand Down Expand Up @@ -595,11 +593,11 @@ impl Agent {
self.remote_candidates[remote].priority(),
self.is_controlling,
);
self.checklist.push(p);
self.candidate_pairs.push(p);
}

pub(crate) fn find_pair(&self, local: usize, remote: usize) -> Option<usize> {
let checklist = &self.checklist;
let checklist = &self.candidate_pairs;
for (index, p) in checklist.iter().enumerate() {
if p.local == local && p.remote == remote {
return Some(index);
Expand All @@ -615,7 +613,7 @@ impl Agent {
self.selected_pair.as_ref().map_or_else(
|| (false, Duration::from_secs(0)),
|selected_pair| {
let remote = self.checklist[*selected_pair].remote;
let remote = self.candidate_pairs[*selected_pair].remote;

let disconnected_time = Instant::now()
.duration_since(self.remote_candidates[remote].last_received());
Expand Down Expand Up @@ -654,7 +652,7 @@ impl Agent {
self.selected_pair
.as_ref()
.map_or((None, None), |selected_pair| {
let p = &self.checklist[*selected_pair];
let p = &self.candidate_pairs[*selected_pair];
(Some(p.local), Some(p.remote))
})
};
Expand Down Expand Up @@ -1164,4 +1162,50 @@ impl Agent {
"controlled"
}
}

pub(crate) fn get_selected_pair(&self) -> Option<usize> {
self.selected_pair
}

pub(crate) fn get_best_available_candidate_pair(&self) -> Option<usize> {
let mut best: Option<usize> = None;

for (index, p) in self.candidate_pairs.iter().enumerate() {
if p.state == CandidatePairState::Failed {
continue;
}

if let Some(best_index) = &mut best {
let b = &self.candidate_pairs[*best_index];
if b.priority() < p.priority() {
*best_index = index;
}
} else {
best = Some(index);
}
}

best
}

pub(crate) fn get_best_valid_candidate_pair(&self) -> Option<usize> {
let mut best: Option<usize> = None;

for (index, p) in self.candidate_pairs.iter().enumerate() {
if p.state != CandidatePairState::Succeeded {
continue;
}

if let Some(best_index) = &mut best {
let b = &self.candidate_pairs[*best_index];
if b.priority() < p.priority() {
*best_index = index;
}
} else {
best = Some(index);
}
}

best
}
}
Loading

0 comments on commit e71579d

Please sign in to comment.