mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-01 07:24:56 -10:00
112 lines
3.9 KiB
Rust
112 lines
3.9 KiB
Rust
//! A memory-backed document in a collaborative session. The caller spawns the transport driver.
|
|
|
|
use document_container::AnyContainer;
|
|
use document_container::backends::memory::MemoryBackend;
|
|
use document_format::{GddV1, GddV1Layout};
|
|
use document_graph_storage::{PeerId, UserId};
|
|
use graph_craft::application_io::resource::ResourceHash;
|
|
use graph_craft::document::{NodeId, NodeNetwork};
|
|
use peer_transport::{Room, SyncTarget};
|
|
|
|
pub use document_format::Error as FormatError;
|
|
pub use document_graph_storage::to_runtime::ConversionError;
|
|
pub use peer_transport::{DEFAULT_SIGNALING_SERVER, Event, MessageLoopFuture, SessionToken};
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum LiveError {
|
|
#[error(transparent)]
|
|
Format(#[from] FormatError),
|
|
#[error(transparent)]
|
|
Conversion(#[from] ConversionError),
|
|
}
|
|
|
|
pub struct LiveDocument {
|
|
gdd: GddV1,
|
|
token: SessionToken,
|
|
}
|
|
|
|
impl LiveDocument {
|
|
/// Host an empty document under `token`. `spawn` must drive the returned future for the whole session.
|
|
pub async fn host(signaling_server: &str, token: SessionToken, peer: PeerId, user: UserId, spawn: impl FnOnce(MessageLoopFuture)) -> Result<Self, LiveError> {
|
|
let gdd = Self::empty_document(peer, user).await?;
|
|
Self::host_document(gdd, signaling_server, token, user, spawn)
|
|
}
|
|
|
|
pub fn host_document(mut gdd: GddV1, signaling_server: &str, token: SessionToken, user: UserId, spawn: impl FnOnce(MessageLoopFuture)) -> Result<Self, LiveError> {
|
|
gdd.share(Self::connect(signaling_server, token, spawn), user)?;
|
|
Ok(Self { gdd, token })
|
|
}
|
|
|
|
pub async fn join(signaling_server: &str, token: SessionToken, peer: PeerId, user: UserId, spawn: impl FnOnce(MessageLoopFuture)) -> Result<Self, LiveError> {
|
|
let mut gdd = Self::empty_document(peer, user).await?;
|
|
gdd.connect(Self::connect(signaling_server, token, spawn), user)?;
|
|
Ok(Self { gdd, token })
|
|
}
|
|
|
|
fn connect(signaling_server: &str, token: SessionToken, spawn: impl FnOnce(MessageLoopFuture)) -> Room {
|
|
let (room, driver) = Room::connect(&token.signaling_url(signaling_server));
|
|
spawn(driver);
|
|
room
|
|
}
|
|
|
|
async fn empty_document(peer: PeerId, user: UserId) -> Result<GddV1, FormatError> {
|
|
let version = env!("CARGO_PKG_VERSION").to_string();
|
|
GddV1::create_in(AnyContainer::Memory(MemoryBackend::new()), GddV1Layout, peer, user, peer.0, version.clone(), version)
|
|
}
|
|
|
|
pub fn token(&self) -> SessionToken {
|
|
self.token
|
|
}
|
|
|
|
pub fn is_synced(&self) -> bool {
|
|
self.gdd.is_synced()
|
|
}
|
|
|
|
pub fn is_ready(&self) -> bool {
|
|
self.is_synced() && SyncTarget::missing_resources(&self.gdd).is_empty()
|
|
}
|
|
|
|
pub fn missing_resources(&self) -> Vec<ResourceHash> {
|
|
let mut missing: Vec<ResourceHash> = SyncTarget::missing_resources(&self.gdd).into_iter().collect();
|
|
missing.sort_unstable();
|
|
missing
|
|
}
|
|
|
|
/// Apply what peers sent and answer their requests. Call it regularly.
|
|
pub async fn poll(&mut self) -> Vec<Event> {
|
|
let events = self.gdd.poll_peers();
|
|
for event in &events {
|
|
let Event::ResourceRequested { from, hash } = event else { continue };
|
|
match self.gdd.read_resource(hash).await {
|
|
Ok(bytes) => {
|
|
if let Err(error) = self.gdd.send_resource(*from, *hash, bytes.as_slice().to_vec()) {
|
|
log::warn!("Failed to send resource {hash}: {error}");
|
|
}
|
|
}
|
|
Err(error) => log::warn!("Peer asked for resource {hash} which is not in this document: {error}"),
|
|
}
|
|
}
|
|
events
|
|
}
|
|
|
|
pub fn document(&self) -> &GddV1 {
|
|
&self.gdd
|
|
}
|
|
|
|
pub fn document_mut(&mut self) -> &mut GddV1 {
|
|
&mut self.gdd
|
|
}
|
|
|
|
/// The document's runtime network, with the path of every layer in it.
|
|
pub async fn network_with_layers(&self) -> Result<(NodeNetwork, Vec<Vec<NodeId>>), LiveError> {
|
|
let declarations = self.gdd.declarations(&self.gdd).await;
|
|
let (network, node_metadata) = self.gdd.registry().to_runtime_with_metadata(&declarations, self.gdd.blobs())?;
|
|
let layers = document_format::layers::layer_paths(&network, &node_metadata);
|
|
Ok((network, layers))
|
|
}
|
|
|
|
pub fn leave(&mut self) {
|
|
self.gdd.leave();
|
|
}
|
|
}
|