use resource_timeline::envelope::{Envelope, Flexible, Precedence, Verdict};
use resource_timeline::share::split;
use resource_timeline::{ActivityId, Bound, Error, Impact, Level, Tick, Timeline, Window};
const LINK: Level = 16;
const PASS: Tick = 180;
const MARGIN: Level = 1;
const CONTACT: Tick = 90;
const OTHERS: ActivityId = ActivityId(0);
const fn downlink(start: Tick, end: Tick, rate: Level) -> Impact {
Impact::Claim {
start,
end,
amount: rate,
}
}
fn share_line(share: Level) -> Result<Timeline, Error> {
let mut line = Timeline::claimable(0, LINK, 16)?;
reserve(&mut line, 0, PASS, share)?;
Ok(line)
}
fn reserve(line: &mut Timeline, from: Tick, until: Tick, share: Level) -> Result<(), Error> {
if share < LINK && from < until {
line.add(OTHERS, downlink(from, until, LINK - share))?;
}
Ok(())
}
fn reshare(line: &mut Timeline, at: Tick, old: Level, new: Level) -> Result<(), Error> {
line.remove(OTHERS);
reserve(line, 0, at, old)?;
reserve(line, at, PASS, new)
}
const C_BURST: [Impact; 1] = [downlink(0, 20, 4)];
const C_ORDER: [Precedence; 1] = [Precedence::after_end(ActivityId(21), ActivityId(22), 0)];
fn need(line: &Timeline, share: Level, from: Tick) -> Result<Level, Error> {
Ok(share - line.lowest_from(from)?)
}
fn c_downlinks() -> Result<[Flexible<'static>; 2], Error> {
let window = Window::new(0, 60)?;
let burst = |id| Flexible {
activity: ActivityId(id),
window,
impacts: &C_BURST,
};
Ok([burst(21), burst(22)])
}
fn fixed_rovers() -> Result<(Timeline, Timeline), Error> {
let mut a = share_line(LINK)?;
a.add(ActivityId(1), downlink(10, 40, 4))?;
a.add(ActivityId(2), downlink(100, 130, 3))?;
let mut b = share_line(LINK)?;
b.add(ActivityId(11), downlink(0, PASS, 2))?;
b.add(ActivityId(12), downlink(50, 80, 2))?;
Ok((a, b))
}
fn ground_split(a: &Timeline, b: &Timeline) -> Result<(Vec<Level>, Level), Error> {
let c_open = share_line(LINK)?;
let c = c_downlinks()?;
let free = Envelope::new(&c_open, &c)?;
let needs = [
need(a, LINK, 0)?,
need(b, LINK, 0)?,
LINK - free.lowest_from(0)?,
];
println!("Needs with C's downlinks free to overlap: {needs:?}");
assert_eq!(needs, [4, 4, 8]);
let refused = split(LINK, MARGIN, &needs).err();
println!(" split: {refused:?}");
assert_eq!(
refused,
Some(Error::Oversubscribed {
needed: 19,
pool: LINK
})
);
let c_need = LINK - free.with_precedences(&C_ORDER)?.lowest_from(0)?;
let needs = [needs[0], needs[1], c_need];
let shares: Vec<Level> = split(LINK, MARGIN, &needs)?.collect();
println!("Needs with C's downlinks ordered: {needs:?}, shares {shares:?}");
assert_eq!(shares, [6, 5, 5]);
Ok((shares, c_need))
}
fn before_contact(a: &mut Timeline, b: &Timeline, c_share: Level) -> Result<(), Error> {
assert_eq!(a.first_violation()?, None);
assert_eq!(b.first_violation()?, None);
let c = share_line(c_share)?;
let c_flexible = c_downlinks()?;
let verdict = Envelope::new(&c, &c_flexible)?
.with_precedences(&C_ORDER)?
.check()?;
println!(" C's envelope on its share of {c_share}: {verdict:?}");
assert_eq!(verdict, Verdict::Safe);
a.add(ActivityId(3), downlink(60, 90, 2))?;
assert_eq!(a.first_violation()?, None);
println!("A adds a 2 Mbit/s dump at minute 60 within its share: safe.");
Ok(())
}
fn contact(a: &mut Timeline, b: &mut Timeline, shares: &[Level]) -> Result<Vec<Level>, Error> {
let panorama = (ActivityId(13), downlink(120, 150, 6));
let mut trial = b.clone();
trial.add(panorama.0, panorama.1)?;
let violation = trial.first_violation()?.expect("the share is too small");
println!(
"B's panorama alone: {} Mbit/s short at minute {}, held by {:?}",
-violation.level, violation.at, violation.contributors
);
assert_eq!(violation.bound, Bound::BelowMin(0));
assert!(violation.contributors.contains(&OTHERS));
let needs = [
need(a, shares[0], CONTACT)?,
need(&trial, shares[1], CONTACT)?,
];
let pool = shares[0] + shares[1];
let resplit: Vec<Level> = split(pool, 0, &needs)?.collect();
println!(
"Contact at minute {CONTACT}: A and B need {needs:?} of their {pool}, new shares {resplit:?}"
);
assert_eq!(resplit, [3, 8]);
reshare(a, CONTACT, shares[0], resplit[0])?;
reshare(b, CONTACT, shares[1], resplit[1])?;
b.add(panorama.0, panorama.1)?;
assert_eq!(a.first_violation()?, None);
assert_eq!(b.first_violation()?, None);
println!(
" B's panorama fits its new share; C still holds {}.",
shares[2]
);
Ok(resplit)
}
fn main() -> Result<(), Error> {
println!("Relay pass: {LINK} Mbit/s for {PASS} minutes, three rovers, margin {MARGIN}.");
let (mut a, mut b) = fixed_rovers()?;
let (shares, c_need) = ground_split(&a, &b)?;
reshare(&mut a, 0, LINK, shares[0])?;
reshare(&mut b, 0, LINK, shares[1])?;
before_contact(&mut a, &b, shares[2])?;
let resplit = contact(&mut a, &mut b, &shares)?;
let used = |line: &Timeline, share: Level, t| share - line.level_at(t).unwrap_or(share);
let peak = (0..PASS)
.map(|t| {
let (a_share, b_share) = if t < CONTACT {
(shares[0], shares[1])
} else {
(resplit[0], resplit[1])
};
used(&a, a_share, t) + used(&b, b_share, t) + c_need
})
.max()
.unwrap_or(0);
println!("Peak link use over the pass, C at its worst: {peak} of {LINK} Mbit/s.");
assert!(peak <= LINK);
Ok(())
}