2023-08-24 15:34:51 -07:00
|
|
|
//! Functions for setting up our WebSocket and WebRTC connections for communications with the
|
|
|
|
//! engine.
|
|
|
|
|
2025-02-18 13:50:13 -08:00
|
|
|
use std::{collections::HashMap, sync::Arc};
|
2024-03-23 15:45:55 -07:00
|
|
|
|
2023-08-24 15:34:51 -07:00
|
|
|
use anyhow::Result;
|
2024-09-19 14:06:29 -07:00
|
|
|
use indexmap::IndexMap;
|
|
|
|
use kcmc::{
|
|
|
|
ok_response::OkModelingCmdResponse,
|
|
|
|
websocket::{
|
|
|
|
BatchResponse, ModelingBatch, OkWebSocketResponseData, SuccessWebSocketResponse, WebSocketRequest,
|
|
|
|
WebSocketResponse,
|
|
|
|
},
|
2024-09-18 17:04:04 -05:00
|
|
|
};
|
2025-02-18 13:50:13 -08:00
|
|
|
use kittycad_modeling_cmds::{self as kcmc};
|
|
|
|
use tokio::sync::RwLock;
|
2025-01-08 20:02:30 -05:00
|
|
|
use uuid::Uuid;
|
2023-08-24 15:34:51 -07:00
|
|
|
|
2025-04-05 09:44:18 -07:00
|
|
|
use super::EngineStats;
|
2024-10-09 19:38:40 -04:00
|
|
|
use crate::{
|
|
|
|
errors::KclError,
|
2025-01-08 20:02:30 -05:00
|
|
|
exec::DefaultPlanes,
|
|
|
|
execution::{ArtifactCommand, IdGenerator},
|
2024-12-03 16:39:51 +13:00
|
|
|
SourceRange,
|
2024-10-09 19:38:40 -04:00
|
|
|
};
|
2023-08-24 15:34:51 -07:00
|
|
|
|
2023-09-20 18:22:47 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2024-03-23 15:45:55 -07:00
|
|
|
pub struct EngineConnection {
|
2025-02-18 13:50:13 -08:00
|
|
|
batch: Arc<RwLock<Vec<(WebSocketRequest, SourceRange)>>>,
|
|
|
|
batch_end: Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>>,
|
|
|
|
artifact_commands: Arc<RwLock<Vec<ArtifactCommand>>>,
|
2025-03-15 10:08:39 -07:00
|
|
|
/// The default planes for the scene.
|
|
|
|
default_planes: Arc<RwLock<Option<DefaultPlanes>>>,
|
2025-03-18 16:19:24 +13:00
|
|
|
stats: EngineStats,
|
2024-03-23 15:45:55 -07:00
|
|
|
}
|
2023-08-24 15:34:51 -07:00
|
|
|
|
|
|
|
impl EngineConnection {
|
2023-08-28 14:58:24 -07:00
|
|
|
pub async fn new() -> Result<EngineConnection> {
|
2024-03-23 15:45:55 -07:00
|
|
|
Ok(EngineConnection {
|
2025-02-18 13:50:13 -08:00
|
|
|
batch: Arc::new(RwLock::new(Vec::new())),
|
|
|
|
batch_end: Arc::new(RwLock::new(IndexMap::new())),
|
|
|
|
artifact_commands: Arc::new(RwLock::new(Vec::new())),
|
2025-03-15 10:08:39 -07:00
|
|
|
default_planes: Default::default(),
|
2025-03-18 16:19:24 +13:00
|
|
|
stats: Default::default(),
|
2024-03-23 15:45:55 -07:00
|
|
|
})
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
2023-09-17 21:57:43 -07:00
|
|
|
}
|
2023-08-24 15:34:51 -07:00
|
|
|
|
2024-03-12 13:37:47 -07:00
|
|
|
#[async_trait::async_trait]
|
2023-09-17 21:57:43 -07:00
|
|
|
impl crate::engine::EngineManager for EngineConnection {
|
2025-02-18 13:50:13 -08:00
|
|
|
fn batch(&self) -> Arc<RwLock<Vec<(WebSocketRequest, SourceRange)>>> {
|
2024-03-23 15:45:55 -07:00
|
|
|
self.batch.clone()
|
|
|
|
}
|
|
|
|
|
2025-02-18 13:50:13 -08:00
|
|
|
fn batch_end(&self) -> Arc<RwLock<IndexMap<uuid::Uuid, (WebSocketRequest, SourceRange)>>> {
|
2024-06-22 14:31:37 -07:00
|
|
|
self.batch_end.clone()
|
|
|
|
}
|
|
|
|
|
2025-02-18 13:50:13 -08:00
|
|
|
fn responses(&self) -> Arc<RwLock<IndexMap<Uuid, WebSocketResponse>>> {
|
|
|
|
Arc::new(RwLock::new(IndexMap::new()))
|
2025-01-17 14:34:36 -05:00
|
|
|
}
|
|
|
|
|
2025-03-18 16:19:24 +13:00
|
|
|
fn stats(&self) -> &EngineStats {
|
|
|
|
&self.stats
|
|
|
|
}
|
|
|
|
|
2025-02-18 13:50:13 -08:00
|
|
|
fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>> {
|
|
|
|
self.artifact_commands.clone()
|
2025-01-08 20:02:30 -05:00
|
|
|
}
|
|
|
|
|
2025-03-15 10:08:39 -07:00
|
|
|
fn get_default_planes(&self) -> Arc<RwLock<Option<DefaultPlanes>>> {
|
|
|
|
self.default_planes.clone()
|
2024-04-15 17:18:32 -07:00
|
|
|
}
|
|
|
|
|
2024-10-09 19:38:40 -04:00
|
|
|
async fn clear_scene_post_hook(
|
|
|
|
&self,
|
|
|
|
_id_generator: &mut IdGenerator,
|
2024-12-03 16:39:51 +13:00
|
|
|
_source_range: SourceRange,
|
2024-10-09 19:38:40 -04:00
|
|
|
) -> Result<(), KclError> {
|
2024-04-15 17:18:32 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-03-23 15:45:55 -07:00
|
|
|
async fn inner_send_modeling_cmd(
|
2023-09-20 16:59:03 -05:00
|
|
|
&self,
|
2024-06-19 13:57:50 -07:00
|
|
|
id: uuid::Uuid,
|
2024-12-03 16:39:51 +13:00
|
|
|
_source_range: SourceRange,
|
2024-09-18 17:04:04 -05:00
|
|
|
cmd: WebSocketRequest,
|
2025-02-18 13:50:13 -08:00
|
|
|
_id_to_source_range: HashMap<Uuid, SourceRange>,
|
2024-06-19 13:57:50 -07:00
|
|
|
) -> Result<WebSocketResponse, KclError> {
|
|
|
|
match cmd {
|
2024-09-18 17:04:04 -05:00
|
|
|
WebSocketRequest::ModelingCmdBatchReq(ModelingBatch {
|
2024-06-19 13:57:50 -07:00
|
|
|
ref requests,
|
|
|
|
batch_id: _,
|
|
|
|
responses: _,
|
2024-09-18 17:04:04 -05:00
|
|
|
}) => {
|
2024-06-19 13:57:50 -07:00
|
|
|
// Create the empty responses.
|
2025-02-12 22:37:34 -05:00
|
|
|
let mut responses = HashMap::with_capacity(requests.len());
|
2024-06-19 13:57:50 -07:00
|
|
|
for request in requests {
|
|
|
|
responses.insert(
|
2024-09-18 17:04:04 -05:00
|
|
|
request.cmd_id,
|
|
|
|
BatchResponse::Success {
|
|
|
|
response: OkModelingCmdResponse::Empty {},
|
2024-06-19 13:57:50 -07:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2024-09-18 17:04:04 -05:00
|
|
|
Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
|
2024-06-19 13:57:50 -07:00
|
|
|
request_id: Some(id),
|
2024-09-18 17:04:04 -05:00
|
|
|
resp: OkWebSocketResponseData::ModelingBatch { responses },
|
|
|
|
success: true,
|
|
|
|
}))
|
2024-06-19 13:57:50 -07:00
|
|
|
}
|
2025-02-18 13:50:13 -08:00
|
|
|
WebSocketRequest::ModelingCmdReq(_) => Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
|
|
|
|
request_id: Some(id),
|
|
|
|
resp: OkWebSocketResponseData::Modeling {
|
|
|
|
modeling_response: OkModelingCmdResponse::Empty {},
|
|
|
|
},
|
|
|
|
success: true,
|
|
|
|
})),
|
2024-09-18 17:04:04 -05:00
|
|
|
_ => Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
|
2024-06-19 13:57:50 -07:00
|
|
|
request_id: Some(id),
|
2024-09-18 17:04:04 -05:00
|
|
|
resp: OkWebSocketResponseData::Modeling {
|
|
|
|
modeling_response: OkModelingCmdResponse::Empty {},
|
|
|
|
},
|
|
|
|
success: true,
|
|
|
|
})),
|
2024-06-19 13:57:50 -07:00
|
|
|
}
|
2023-09-17 21:57:43 -07:00
|
|
|
}
|
2025-01-10 20:05:27 -05:00
|
|
|
|
|
|
|
async fn close(&self) {}
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|