Skip to content

Commit 58e5c4e

Browse files
committed
much progress has been made
1 parent aec5588 commit 58e5c4e

File tree

13 files changed

+2459
-336
lines changed

13 files changed

+2459
-336
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ tracing = "0.1"
7676
# rpc
7777
irpc = { version = "0.5.0" }
7878
n0-future = "0.1.3"
79+
serde-error = "0.1.3"
80+
quinn = { package = "iroh-quinn", version = "0.14.0" }
7981

8082
[dev-dependencies]
8183
rand_chacha = "0.3.1"
@@ -92,7 +94,7 @@ testdir = "0.7"
9294
data-encoding = "2.6.0"
9395

9496
[features]
95-
default = ["net", "metrics", "engine", "test-utils"]
97+
default = ["net", "metrics", "engine", "test-utils", "rpc"]
9698
net = ["dep:iroh", "tokio/io-util", "dep:tokio-stream", "dep:tokio-util"]
9799
metrics = ["iroh-metrics/metrics", "iroh/metrics"]
98100
engine = ["net", "dep:iroh-gossip"]

src/actor.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,24 @@ use anyhow::{anyhow, Context, Result};
1212
use bytes::Bytes;
1313
use futures_util::FutureExt;
1414
use iroh_blobs::Hash;
15+
use irpc::channel::mpsc;
1516
use serde::{Deserialize, Serialize};
1617
use tokio::{sync::oneshot, task::JoinSet};
1718
use tracing::{debug, error, error_span, trace, warn};
1819

1920
use crate::{
2021
metrics::Metrics,
2122
ranger::Message,
23+
rpc2::{
24+
protocol::{AuthorListResponse, ListResponse},
25+
RpcError, RpcResult,
26+
},
2227
store::{
2328
fs::{ContentHashesIterator, StoreInstance},
2429
DownloadPolicy, ImportNamespaceOutcome, Query, Store,
2530
},
26-
Author, AuthorHeads, AuthorId, Capability, CapabilityKind, ContentStatus,
27-
ContentStatusCallback, Event, NamespaceId, NamespaceSecret, PeerIdBytes, Replica, ReplicaInfo,
28-
SignedEntry, SyncOutcome,
31+
Author, AuthorHeads, AuthorId, Capability, ContentStatus, ContentStatusCallback, Event,
32+
NamespaceId, NamespaceSecret, PeerIdBytes, Replica, ReplicaInfo, SignedEntry, SyncOutcome,
2933
};
3034

3135
const ACTION_CAP: usize = 1024;
@@ -60,12 +64,12 @@ enum Action {
6064
#[display("ListAuthors")]
6165
ListAuthors {
6266
#[debug("reply")]
63-
reply: async_channel::Sender<Result<AuthorId>>,
67+
reply: mpsc::Sender<RpcResult<AuthorListResponse>>,
6468
},
6569
#[display("ListReplicas")]
6670
ListReplicas {
6771
#[debug("reply")]
68-
reply: async_channel::Sender<Result<(NamespaceId, CapabilityKind)>>,
72+
reply: mpsc::Sender<RpcResult<ListResponse>>,
6973
},
7074
#[display("ContentHashes")]
7175
ContentHashes {
@@ -165,7 +169,7 @@ enum ReplicaAction {
165169
},
166170
GetMany {
167171
query: Query,
168-
reply: async_channel::Sender<Result<SignedEntry>>,
172+
reply: mpsc::Sender<RpcResult<SignedEntry>>,
169173
},
170174
DropReplica {
171175
reply: oneshot::Sender<Result<()>>,
@@ -290,6 +294,7 @@ impl SyncHandle {
290294
}
291295

292296
pub async fn open(&self, namespace: NamespaceId, opts: OpenOpts) -> Result<()> {
297+
tracing::debug!("SyncHandle::open called");
293298
let (reply, rx) = oneshot::channel();
294299
let action = ReplicaAction::Open { reply, opts };
295300
self.send_replica(namespace, action).await?;
@@ -443,7 +448,7 @@ impl SyncHandle {
443448
&self,
444449
namespace: NamespaceId,
445450
query: Query,
446-
reply: async_channel::Sender<Result<SignedEntry>>,
451+
reply: mpsc::Sender<RpcResult<SignedEntry>>,
447452
) -> Result<()> {
448453
let action = ReplicaAction::GetMany { query, reply };
449454
self.send_replica(namespace, action).await?;
@@ -497,14 +502,14 @@ impl SyncHandle {
497502
Ok(store)
498503
}
499504

500-
pub async fn list_authors(&self, reply: async_channel::Sender<Result<AuthorId>>) -> Result<()> {
505+
pub async fn list_authors(
506+
&self,
507+
reply: mpsc::Sender<RpcResult<AuthorListResponse>>,
508+
) -> Result<()> {
501509
self.send(Action::ListAuthors { reply }).await
502510
}
503511

504-
pub async fn list_replicas(
505-
&self,
506-
reply: async_channel::Sender<Result<(NamespaceId, CapabilityKind)>>,
507-
) -> Result<()> {
512+
pub async fn list_replicas(&self, reply: mpsc::Sender<RpcResult<ListResponse>>) -> Result<()> {
508513
self.send(Action::ListReplicas { reply }).await
509514
}
510515

@@ -696,15 +701,18 @@ impl Actor {
696701
let iter = self
697702
.store
698703
.list_authors()
699-
.map(|a| a.map(|a| a.map(|a| a.id())));
704+
.map(|a| a.map(|a| a.map(|a| AuthorListResponse { author_id: a.id() })));
700705
self.tasks
701-
.spawn_local(iter_to_channel_async(reply, iter).map(|_| ()));
706+
.spawn_local(iter_to_irpc(reply, iter).map(|_| ()));
702707
Ok(())
703708
}
704709
Action::ListReplicas { reply } => {
705710
let iter = self.store.list_namespaces();
711+
let iter = iter.map(|inner| {
712+
inner.map(|res| res.map(|(id, capability)| ListResponse { id, capability }))
713+
});
706714
self.tasks
707-
.spawn_local(iter_to_channel_async(reply, iter).map(|_| ()));
715+
.spawn_local(iter_to_irpc(reply, iter).map(|_| ()));
708716
Ok(())
709717
}
710718
Action::ContentHashes { reply } => {
@@ -838,7 +846,7 @@ impl Actor {
838846
.ensure_open(&namespace)
839847
.and_then(|_| self.store.get_many(namespace, query));
840848
self.tasks
841-
.spawn_local(iter_to_channel_async(reply, iter).map(|_| ()));
849+
.spawn_local(iter_to_irpc(reply, iter).map(|_| ()));
842850
Ok(())
843851
}
844852
ReplicaAction::DropReplica { reply } => send_reply_with(reply, self, |this| {
@@ -984,6 +992,7 @@ impl OpenReplicas {
984992
}
985993
hash_map::Entry::Occupied(mut e) => {
986994
let state = e.get_mut();
995+
tracing::debug!("STATE {state:?}");
987996
state.handles = state.handles.wrapping_sub(1);
988997
if state.handles == 0 {
989998
let _ = e.remove_entry();
@@ -1001,14 +1010,18 @@ impl OpenReplicas {
10011010
}
10021011
}
10031012

1004-
async fn iter_to_channel_async<T: Send + 'static>(
1005-
channel: async_channel::Sender<Result<T>>,
1013+
async fn iter_to_irpc<T: irpc::RpcMessage>(
1014+
channel: mpsc::Sender<RpcResult<T>>,
10061015
iter: Result<impl Iterator<Item = Result<T>>>,
10071016
) -> Result<(), SendReplyError> {
10081017
match iter {
1009-
Err(err) => channel.send(Err(err)).await.map_err(send_reply_error)?,
1018+
Err(err) => channel
1019+
.send(Err(RpcError::new(&*err)))
1020+
.await
1021+
.map_err(send_reply_error)?,
10101022
Ok(iter) => {
10111023
for item in iter {
1024+
let item = item.map_err(|err| RpcError::new(&*err));
10121025
channel.send(item).await.map_err(send_reply_error)?;
10131026
}
10141027
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ pub mod actor;
5252
pub mod store;
5353
pub mod sync;
5454

55+
pub mod rpc2;
56+
5557
mod heads;
5658
mod keys;
5759
mod ranger;

src/protocol.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use iroh_gossip::net::Gossip;
99

1010
use crate::{
1111
engine::{DefaultAuthorStorage, Engine},
12+
rpc2::api::DocsApi,
1213
store::Store,
1314
};
1415

@@ -32,8 +33,8 @@ impl ProtocolHandler for Docs {
3233
#[derive(Debug, Clone)]
3334
pub struct Docs {
3435
engine: Arc<Engine>,
35-
// #[cfg(feature = "rpc")]
36-
// pub(crate) rpc_handler: Arc<std::sync::OnceLock<crate::rpc::RpcHandler>>,
36+
api: DocsApi, // #[cfg(feature = "rpc")]
37+
// pub(crate) rpc_handler: Arc<std::sync::OnceLock<crate::rpc::RpcHandler>>,
3738
}
3839

3940
impl Docs {
@@ -47,6 +48,11 @@ impl Docs {
4748
pub fn persistent(path: PathBuf) -> Builder {
4849
Builder { path: Some(path) }
4950
}
51+
52+
/// Returns the API for this docs instance.
53+
pub fn api(&self) -> &DocsApi {
54+
&self.api
55+
}
5056
}
5157

5258
impl Docs {
@@ -63,10 +69,12 @@ impl Docs {
6369
///
6470
/// Note that usually you would use the [`Builder`] to create a new docs protocol.
6571
pub fn new(engine: Engine) -> Self {
72+
let engine = Arc::new(engine);
73+
let api = DocsApi::spawn(engine.clone());
6674
Self {
67-
engine: Arc::new(engine),
68-
// #[cfg(feature = "rpc")]
69-
// rpc_handler: Default::default(),
75+
engine,
76+
api, // #[cfg(feature = "rpc")]
77+
// rpc_handler: Default::default(),
7078
}
7179
}
7280

0 commit comments

Comments
 (0)