Skip to content

Commit

Permalink
feat(kitsune2): add top-level crate kitsune2
Browse files Browse the repository at this point in the history
  • Loading branch information
jost-s committed Jan 30, 2025
1 parent f203f61 commit dd92ea0
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 185 deletions.
19 changes: 17 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"crates/core",
"crates/dht",
"crates/gossip",
"crates/kitsune2",
"crates/test_utils",
"crates/tool_proto_build",
"crates/transport_tx5",
Expand Down
1 change: 1 addition & 0 deletions crates/api/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub struct Builder {
/// The [gossip::GossipFactory] to be used for creating
/// [gossip::Gossip] instances.
pub gossip: gossip::DynGossipFactory,

/// The [local_agent_store::LocalAgentStoreFactory] to be used for creating
/// [local_agent_store::LocalAgentStore] instances.
pub local_agent_store: Arc<dyn LocalAgentStoreFactory>,
Expand Down
11 changes: 11 additions & 0 deletions crates/api/src/space.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Kitsune2 space related types.
use crate::agent::DynLocalAgent;
use crate::fetch::DynFetch;
use crate::*;
use std::sync::Arc;

Expand Down Expand Up @@ -47,6 +48,16 @@ pub trait Space: 'static + Send + Sync + std::fmt::Debug {
/// Get a reference to the local agent store being used by this space.
fn local_agent_store(&self) -> &DynLocalAgentStore;

/// Get a reference to the op store of this space. Ops can be injected and
/// retrieved from the op store.
fn op_store(&self) -> &DynOpStore;

/// Get a reference to the fetch module of this space.
fn fetch(&self) -> &DynFetch;

/// Get a reference to the gossip module of this space.
fn gossip(&self) -> &DynGossip;

/// Indicate that an agent is now online, and should begin receiving
/// messages and exchanging dht information.
fn local_agent_join(
Expand Down
6 changes: 3 additions & 3 deletions crates/core/src/factories/core_kitsune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ impl Kitsune for CoreKitsune {

#[cfg(test)]
mod test {
use kitsune2_test_utils::space::TEST_SPACE_ID;

#[tokio::test(flavor = "multi_thread")]
async fn happy_space_construct() {
use kitsune2_api::{kitsune::*, space::*, *};
Expand Down Expand Up @@ -186,8 +188,6 @@ mod test {
.await
.unwrap();

k.space(bytes::Bytes::from_static(b"space1").into())
.await
.unwrap();
k.space(TEST_SPACE_ID).await.unwrap();
}
}
3 changes: 2 additions & 1 deletion crates/core/src/factories/core_local_agent_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ impl LocalAgentStore for CoreLocalAgentStore {
pub struct CoreLocalAgentStoreFactory;

impl CoreLocalAgentStoreFactory {
pub(crate) fn create() -> Arc<dyn LocalAgentStoreFactory> {
/// Construct a new CoreLocalAgentStoreFactory.
pub fn create() -> Arc<dyn LocalAgentStoreFactory> {
Arc::new(CoreLocalAgentStoreFactory)
}
}
Expand Down
56 changes: 55 additions & 1 deletion crates/core/src/factories/core_space.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The core space implementation provided by Kitsune2.
use kitsune2_api::{config::*, space::*, *};
use kitsune2_api::{config::*, fetch::DynFetch, space::*, *};
use std::sync::{Arc, Mutex, Weak};

mod protocol;
Expand Down Expand Up @@ -101,6 +101,35 @@ impl SpaceFactory for CoreSpaceFactory {
let local_agent_store =
builder.local_agent_store.create(builder.clone()).await?;
let inner = Arc::new(Mutex::new(InnerData { current_url: None }));
let op_store = builder
.op_store
.create(builder.clone(), space.clone())
.await?;
let fetch = builder
.fetch
.create(
builder.clone(),
space.clone(),
op_store.clone(),
tx.clone(),
)
.await?;
let peer_meta_store =
builder.peer_meta_store.create(builder.clone()).await?;
let gossip = builder
.gossip
.create(
builder.clone(),
space.clone(),
peer_store.clone(),
local_agent_store.clone(),
peer_meta_store,
op_store.clone(),
tx.clone(),
fetch.clone(),
)
.await?;

let out: DynSpace = Arc::new_cyclic(move |this| {
let current_url = tx.register_space_handler(
space.clone(),
Expand All @@ -115,6 +144,9 @@ impl SpaceFactory for CoreSpaceFactory {
bootstrap,
local_agent_store,
inner,
op_store,
fetch,
gossip,
)
});
Ok(out)
Expand Down Expand Up @@ -168,6 +200,9 @@ struct CoreSpace {
peer_store: peer_store::DynPeerStore,
bootstrap: bootstrap::DynBootstrap,
local_agent_store: DynLocalAgentStore,
op_store: DynOpStore,
fetch: DynFetch,
gossip: DynGossip,
inner: Arc<Mutex<InnerData>>,
task_check_agent_infos: tokio::task::JoinHandle<()>,
}
Expand All @@ -187,6 +222,7 @@ impl std::fmt::Debug for CoreSpace {
}

impl CoreSpace {
#[allow(clippy::too_many_arguments)]
pub fn new(
config: CoreSpaceConfig,
space: SpaceId,
Expand All @@ -195,6 +231,9 @@ impl CoreSpace {
bootstrap: bootstrap::DynBootstrap,
local_agent_store: DynLocalAgentStore,
inner: Arc<Mutex<InnerData>>,
op_store: DynOpStore,
fetch: DynFetch,
gossip: DynGossip,
) -> Self {
let task_check_agent_infos = tokio::task::spawn(check_agent_infos(
config,
Expand All @@ -208,7 +247,10 @@ impl CoreSpace {
bootstrap,
local_agent_store,
inner,
op_store,
task_check_agent_infos,
fetch,
gossip,
}
}

Expand All @@ -235,6 +277,18 @@ impl Space for CoreSpace {
&self.local_agent_store
}

fn op_store(&self) -> &DynOpStore {
&self.op_store
}

fn fetch(&self) -> &DynFetch {
&self.fetch
}

fn gossip(&self) -> &DynGossip {
&self.gossip
}

fn local_agent_join(
&self,
local_agent: agent::DynLocalAgent,
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/factories/core_space/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async fn space_local_agent_join_leave() {
let bob = Arc::new(TestLocalAgent::default()) as agent::DynLocalAgent;
let ned = Arc::new(TestLocalAgent::default()) as agent::DynLocalAgent;

let s1 = k1.space(TEST_SPACE_ID.clone()).await.unwrap();
let s1 = k1.space(TEST_SPACE_ID).await.unwrap();

s1.local_agent_join(bob.clone()).await.unwrap();
s1.local_agent_join(ned.clone()).await.unwrap();
Expand Down
Loading

0 comments on commit dd92ea0

Please sign in to comment.