Skip to content

Commit

Permalink
Encapsulate all backend logic in new Node struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sandreae committed Dec 27, 2024
1 parent f113be1 commit fb7b3f5
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 161 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions aardvark-app/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ mod imp {
let window = self.obj().clone();
let dialog = self.open_document_dialog.clone();
self.open_document_button.connect_clicked(move |_| {
// @TODO: Send `FromApp::Subscribe` message to network containing the share code
// and wait for the document to be joined.
dialog.present(Some(&window));
});
}
Expand Down
5 changes: 4 additions & 1 deletion aardvark-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ edition = "2021"
anyhow = "1.0.94"
async-trait = "0.1.83"
ciborium = "0.2.2"
futures-io = "0.3.31"
futures-sink = "0.3.31"
futures-util = "0.3.31"
iroh-gossip = "0.25.0"
p2panda-core = { git = "https://github.com/p2panda/p2panda", rev = "a174565f25c40603cfa3eb48fcde97918f23dbc8" }
p2panda-discovery = { git = "https://github.com/p2panda/p2panda", rev = "a174565f25c40603cfa3eb48fcde97918f23dbc8", features = [
Expand All @@ -19,5 +22,5 @@ p2panda-sync = { git = "https://github.com/p2panda/p2panda", rev = "a174565f25c4
"log-sync",
] }
serde = { version = "1.0.215", features = ["derive"] }
tokio = { version = "1.42.0", features = ["full"] }
tokio = { version = "1.42.0", features = ["rt"] }
tokio-stream = "0.1.17"
26 changes: 18 additions & 8 deletions aardvark-node/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use p2panda_core::PublicKey;
use p2panda_sync::log_sync::TopicLogMap;

use crate::operation::LogId;
use crate::topics::TextDocument;
use crate::topics::{AardvarkTopics, TextDocument};

pub type ShortCode = [char; 6];

Expand All @@ -15,15 +15,17 @@ pub struct TextDocumentStore {
inner: Arc<RwLock<TextDocumentStoreInner>>,
}

impl TextDocumentStore {
pub fn new() -> Self {
impl Default for TextDocumentStore {
fn default() -> Self {
Self {
inner: Arc::new(RwLock::new(TextDocumentStoreInner {
authors: HashMap::new(),
})),
}
}
}

impl TextDocumentStore {
pub fn write(&self) -> RwLockWriteGuard<TextDocumentStoreInner> {
self.inner.write().expect("acquire write lock")
}
Expand All @@ -35,17 +37,25 @@ pub struct TextDocumentStoreInner {
}

#[async_trait]
impl TopicLogMap<TextDocument, LogId> for TextDocumentStore {
async fn get(&self, topic: &TextDocument) -> Option<HashMap<PublicKey, Vec<LogId>>> {
impl TopicLogMap<AardvarkTopics, LogId> for TextDocumentStore {
async fn get(&self, topic: &AardvarkTopics) -> Option<HashMap<PublicKey, Vec<LogId>>> {
let text_document = match topic {
// When discovering documents we don't want any sync sessions to occur, this is a
// little hack to make sure that is the case, as if both peers resolve a topic to
// "None" then the sync session will naturally end.
AardvarkTopics::DiscoveryCode(_) => return None,
AardvarkTopics::TextDocument(text_document) => text_document,
};

let authors = &self.inner.read().unwrap().authors;
let mut result = HashMap::<PublicKey, Vec<LogId>>::new();

for (public_key, documents) in authors {
if documents.contains(&topic) {
if documents.contains(text_document) {
result
.entry(*public_key)
.and_modify(|logs| logs.push(topic.hash()))
.or_insert(vec![topic.hash()]);
.and_modify(|logs| logs.push(text_document.hash()))
.or_insert(vec![text_document.hash()]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions aardvark-node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod document;
pub mod network;
pub mod operation;
pub mod document;
pub mod topics;
pub mod topics;
Loading

0 comments on commit fb7b3f5

Please sign in to comment.