Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove CyclerOutput from Communication Client and changes the communication protocol to only use the String type for paths #709

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions crates/communication/src/client/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use super::{
id_tracker::id_tracker,
output_subscription_manager::{self, output_subscription_manager},
responder::responder,
CyclerOutput,
};

#[derive(Clone)]
Expand Down Expand Up @@ -113,14 +112,14 @@ impl Communication {

pub async fn subscribe_output(
&self,
output: CyclerOutput,
path: Path,
format: Format,
) -> (Uuid, Receiver<SubscriberMessage>) {
let (subscriber_sender, subscriber_receiver) = mpsc::channel(10);
let (response_sender, response_receiver) = oneshot::channel();
self.output_subscription_manager
.send(output_subscription_manager::Message::Subscribe {
output,
path,
format,
subscriber: subscriber_sender,
response_sender,
Expand Down
2 changes: 1 addition & 1 deletion crates/communication/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ mod types;

pub use crate::client::communication::Communication;
pub use connector::ConnectionStatus;
pub use types::{Cycler, CyclerOutput, HierarchyType, Output, OutputHierarchy, SubscriberMessage};
pub use types::{HierarchyType, OutputHierarchy, SubscriberMessage};
34 changes: 14 additions & 20 deletions crates/communication/src/client/output_subscription_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ use uuid::Uuid;
use crate::{
client::{
id_tracker::{self, get_message_id},
responder, Output, SubscriberMessage,
responder, SubscriberMessage,
},
messages::{
Fields, Format, OutputsRequest, Request,
TextualDataOrBinaryReference::{self, BinaryReference, TextualData},
Fields, Format, OutputsRequest, Path, Request, TextualDataOrBinaryReference::{self, BinaryReference, TextualData}
},
};

use super::{responder::Response, CyclerOutput};
use super::responder::Response;

#[derive(Debug)]
pub enum Message {
Expand All @@ -28,7 +27,7 @@ pub enum Message {
},
Disconnect,
Subscribe {
output: CyclerOutput,
path: Path,
format: Format,
subscriber: mpsc::Sender<SubscriberMessage>,
response_sender: oneshot::Sender<Uuid>,
Expand All @@ -52,9 +51,9 @@ pub enum Message {

#[derive(Default)]
struct SubscriptionManager {
ids_to_outputs: HashMap<usize, (CyclerOutput, Format)>,
ids_to_outputs: HashMap<usize, (Path, Format)>,
outputs_to_subscribers:
HashMap<(CyclerOutput, Format), HashMap<Uuid, mpsc::Sender<SubscriberMessage>>>,
HashMap<(Path, Format), HashMap<Uuid, mpsc::Sender<SubscriberMessage>>>,
}

pub async fn output_subscription_manager(
Expand All @@ -68,7 +67,7 @@ pub async fn output_subscription_manager(
let mut requester = None;
let mut fields = None;
let mut binary_data_waiting_for_references: HashMap<usize, Vec<u8>> = HashMap::new();
let mut binary_references_waiting_for_data: HashMap<usize, CyclerOutput> = HashMap::new();
let mut binary_references_waiting_for_data: HashMap<usize, Path> = HashMap::new();

while let Some(message) = receiver.recv().await {
match message {
Expand Down Expand Up @@ -107,7 +106,7 @@ pub async fn output_subscription_manager(
manager.ids_to_outputs.clear();
}
Message::Subscribe {
output,
path,
format,
subscriber: output_sender,
response_sender,
Expand All @@ -118,7 +117,7 @@ pub async fn output_subscription_manager(
add_subscription(
&mut manager,
uuid,
output,
path,
format,
output_sender,
&id_tracker,
Expand Down Expand Up @@ -274,7 +273,7 @@ async fn query_output_fields(
async fn add_subscription(
manager: &mut SubscriptionManager,
uuid: Uuid,
output: CyclerOutput,
path: Path,
format: Format,
output_sender: mpsc::Sender<SubscriberMessage>,
id_tracker: &mpsc::Sender<id_tracker::Message>,
Expand All @@ -283,15 +282,15 @@ async fn add_subscription(
) {
match manager
.outputs_to_subscribers
.entry((output.clone(), format))
.entry((path.clone(), format))
{
Entry::Occupied(mut entry) => {
entry.get_mut().insert(uuid, output_sender);
}
Entry::Vacant(entry) => {
if let Some(requester) = requester {
if let Some(subscription_id) = subscribe(
output.clone(),
path.clone(),
format,
vec![output_sender.clone()],
id_tracker,
Expand All @@ -302,7 +301,7 @@ async fn add_subscription(
{
manager
.ids_to_outputs
.insert(subscription_id, (output, format));
.insert(subscription_id, (path, format));
}
};
entry.insert(HashMap::new()).insert(uuid, output_sender);
Expand All @@ -311,7 +310,7 @@ async fn add_subscription(
}

async fn subscribe(
output: CyclerOutput,
path: Path,
format: Format,
subscribers: Vec<mpsc::Sender<SubscriberMessage>>,
id_tracker: &mpsc::Sender<id_tracker::Message>,
Expand All @@ -330,13 +329,8 @@ async fn subscribe(
error!("{error}");
return None;
}
let path = match output.output {
Output::Main { path } => format!("main_outputs.{path}"),
Output::Additional { path } => format!("additional_outputs.{path}"),
};
let request = Request::Outputs(OutputsRequest::Subscribe {
id: message_id,
cycler_instance: output.cycler.to_string(),
path,
format,
});
Expand Down
84 changes: 4 additions & 80 deletions crates/communication/src/client/types.rs
Original file line number Diff line number Diff line change
@@ -1,85 +1,9 @@
use std::{
collections::BTreeMap,
fmt::{self, Display, Formatter},
str::FromStr,
};
use std::collections::BTreeMap;

use color_eyre::{
eyre::{bail, eyre},
Report, Result,
};
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use serde_json::Value;

#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct CyclerOutput {
pub cycler: Cycler,
pub output: Output,
}

impl FromStr for CyclerOutput {
type Err = Report;

fn from_str(string: &str) -> Result<Self, Self::Err> {
let (cycler_str, output_str) = string.split_once('.').ok_or_else(|| {
eyre!("expected '.' in subscription path (e.g. 'control.main.foo_bar')")
})?;
let cycler = Cycler::from_str(cycler_str)?;
let (output_str, path) = output_str.split_once('.').ok_or_else(|| {
eyre!("expected '.' after output source (e.g. 'control.main.foo_bar')")
})?;
let output = match output_str {
"main" | "main_outputs" => Output::Main {
path: path.to_string(),
},
"additional" | "additional_outputs" => Output::Additional {
path: path.to_string(),
},
_ => bail!("unknown output '{output_str}'"),
};
Ok(CyclerOutput { cycler, output })
}
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum Cycler {
Control,
VisionTop,
VisionBottom,
BehaviorSimulator,
}

impl Display for Cycler {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Cycler::Control => f.write_str("Control"),
Cycler::VisionTop => f.write_str("VisionTop"),
Cycler::VisionBottom => f.write_str("VisionBottom"),
Cycler::BehaviorSimulator => f.write_str("BehaviorSimulator"),
}
}
}

impl FromStr for Cycler {
type Err = Report;

fn from_str(string: &str) -> Result<Self, Self::Err> {
Ok(match string {
"Control" => Cycler::Control,
"VisionTop" => Cycler::VisionTop,
"VisionBottom" => Cycler::VisionBottom,
"BehaviorSimulator" => Cycler::BehaviorSimulator,
_ => bail!("unknown cycler '{string}'"),
})
}
}

#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(tag = "type")]
pub enum Output {
Main { path: String },
Additional { path: String },
}
use crate::messages::Path;

#[derive(Debug, Clone)]
pub enum SubscriberMessage {
Expand Down Expand Up @@ -123,6 +47,6 @@ pub struct OutputHierarchy {

#[derive(Debug, Deserialize)]
pub struct SubscribedOutput {
pub output: Output,
pub output: Path,
pub data: Value,
}
2 changes: 0 additions & 2 deletions crates/communication/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ pub enum OutputsRequest {
},
GetNext {
id: usize,
cycler_instance: CyclerInstance,
path: Path,
format: Format,
},
Subscribe {
id: usize,
cycler_instance: CyclerInstance,
path: Path,
format: Format,
},
Expand Down
Loading
Loading