move cmd off of grpc
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use crate::command::command_handle::CommandHandle;
|
||||
use crate::command::service::CommandManagementService;
|
||||
use crate::telemetry::management_service::TelemetryManagementService;
|
||||
use actix_ws::{AggregatedMessage, ProtocolError, Session};
|
||||
use anyhow::bail;
|
||||
@@ -10,18 +12,26 @@ use uuid::Uuid;
|
||||
pub(super) struct BackendConnection {
|
||||
session: Session,
|
||||
tlm_management: Arc<TelemetryManagementService>,
|
||||
cmd_management: Arc<CommandManagementService>,
|
||||
tx: Sender<ResponseMessage>,
|
||||
commands: Vec<CommandHandle>,
|
||||
pub rx: Receiver<ResponseMessage>,
|
||||
pub should_close: bool,
|
||||
}
|
||||
|
||||
impl BackendConnection {
|
||||
pub fn new(session: Session, tlm_management: Arc<TelemetryManagementService>) -> Self {
|
||||
pub fn new(
|
||||
session: Session,
|
||||
tlm_management: Arc<TelemetryManagementService>,
|
||||
cmd_management: Arc<CommandManagementService>,
|
||||
) -> Self {
|
||||
let (tx, rx) = tokio::sync::mpsc::channel::<ResponseMessage>(128);
|
||||
Self {
|
||||
session,
|
||||
tlm_management,
|
||||
cmd_management,
|
||||
tx,
|
||||
commands: vec![],
|
||||
rx,
|
||||
should_close: false,
|
||||
}
|
||||
@@ -41,6 +51,21 @@ impl BackendConnection {
|
||||
RequestMessagePayload::TelemetryEntry(tlm_entry) => {
|
||||
self.tlm_management.add_tlm_item(tlm_entry)?;
|
||||
}
|
||||
RequestMessagePayload::GenericCallbackError(_) => todo!(),
|
||||
RequestMessagePayload::CommandDefinition(def) => {
|
||||
let cmd = self
|
||||
.cmd_management
|
||||
.register_command(msg.uuid, def, self.tx.clone())?;
|
||||
self.commands.push(cmd);
|
||||
}
|
||||
RequestMessagePayload::CommandResponse(response) => match msg.response {
|
||||
None => bail!("Command Response Payload Must Respond to a Command"),
|
||||
Some(uuid) => {
|
||||
self.cmd_management
|
||||
.handle_command_response(uuid, response)
|
||||
.await?;
|
||||
}
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -78,6 +103,15 @@ impl BackendConnection {
|
||||
|
||||
pub async fn cleanup(mut self) {
|
||||
self.rx.close();
|
||||
let _ = self.session.close(None).await;
|
||||
// Clone here to prevent conflict with the Drop trait
|
||||
let _ = self.session.clone().close(None).await;
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BackendConnection {
|
||||
fn drop(&mut self) {
|
||||
for command in self.commands.drain(..) {
|
||||
self.cmd_management.unregister(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user