task_allocation/cbba/message.rs
1//! What CBBA agents exchange.
2
3use alloc::vec::Vec;
4
5use super::table::Claim;
6use crate::id::{AgentId, TaskId};
7
8/// One agent's broadcast: its belief about every task's winner and winning bid, and the
9/// latest clock value it has seen of every agent.
10///
11/// A message is a snapshot of the sender's state, so a receiver can merge it whenever it
12/// arrives, in any order, any number of times.
13#[derive(Clone, Debug, PartialEq, Eq)]
14pub struct Message {
15 /// The agent that sent the message.
16 pub(super) sender: AgentId,
17
18 /// The sender's winner and winning bid for each task, indexed by task.
19 pub(super) claims: Vec<Claim>,
20
21 /// For each agent, the latest clock value of it the sender had seen; the sender's own
22 /// entry is its clock when it sent the message.
23 pub(super) seen: Vec<u64>,
24}
25
26impl Message {
27 /// The agent that sent the message.
28 #[must_use]
29 pub const fn sender(&self) -> AgentId {
30 self.sender
31 }
32
33 /// The sender's clock when it sent the message: how many messages it had sent,
34 /// this one included.
35 #[must_use]
36 pub fn clock(&self) -> u64 {
37 self.seen[self.sender.index()]
38 }
39
40 /// The winner the sender believed in for `task`; `None` when it knew of no bid.
41 ///
42 /// # Panics
43 ///
44 /// Panics when `task` is not one of the message's tasks.
45 #[must_use]
46 pub fn winner(&self, task: TaskId) -> Option<AgentId> {
47 self.claims[task.index()].winner
48 }
49
50 /// The winning bid the sender believed in for `task`; zero when it knew of no bid.
51 ///
52 /// # Panics
53 ///
54 /// Panics when `task` is not one of the message's tasks.
55 #[must_use]
56 pub fn winning_bid(&self, task: TaskId) -> u64 {
57 self.claims[task.index()].bid
58 }
59
60 /// The sender's winner and winning bid for each task, in task order.
61 pub(crate) fn claims(&self) -> &[Claim] {
62 &self.claims
63 }
64
65 /// The number of tasks the message covers.
66 #[must_use]
67 pub fn tasks(&self) -> usize {
68 self.claims.len()
69 }
70
71 /// The message's nominal size on the wire, in bytes: the sender, then every task's
72 /// winner and bid, then every agent's clock, each list after its length.
73 pub(crate) fn bytes(&self) -> u64 {
74 let claims = u64::try_from(self.claims.len()).unwrap_or(u64::MAX);
75 let seen = u64::try_from(self.seen.len()).unwrap_or(u64::MAX);
76
77 4 + 4 + 12 * claims + 4 + 8 * seen
78 }
79}