1
1
//! Quic RPC implementation for docs.
2
2
3
- use proto:: RpcService ;
4
- use quic_rpc:: server:: { ChannelTypes , RpcChannel } ;
3
+ use proto:: { Request , RpcService } ;
4
+ use quic_rpc:: {
5
+ server:: { ChannelTypes , RpcChannel } ,
6
+ RpcClient , RpcServer ,
7
+ } ;
8
+ use tokio_util:: task:: AbortOnDropHandle ;
5
9
6
10
use crate :: engine:: Engine ;
7
11
@@ -14,15 +18,22 @@ type RpcError = serde_error::Error;
14
18
type RpcResult < T > = std:: result:: Result < T , RpcError > ;
15
19
16
20
impl < D : iroh_blobs:: store:: Store > Engine < D > {
21
+ /// Get an in memory client to interact with the docs engine.
22
+ pub fn client ( & self ) -> & client:: docs:: MemClient {
23
+ & self
24
+ . rpc_handler
25
+ . get_or_init ( || RpcHandler :: new ( self ) )
26
+ . client
27
+ }
28
+
17
29
/// Handle a docs request from the RPC server.
18
30
pub async fn handle_rpc_request < C : ChannelTypes < RpcService > > (
19
- & self ,
20
- msg : crate :: rpc :: proto :: Request ,
31
+ self ,
32
+ msg : Request ,
21
33
chan : RpcChannel < RpcService , C > ,
22
34
) -> Result < ( ) , quic_rpc:: server:: RpcServerError < C > > {
23
- use crate :: rpc:: proto:: Request :: * ;
24
-
25
- let this = self . clone ( ) ;
35
+ use Request :: * ;
36
+ let this = self ;
26
37
match msg {
27
38
Open ( msg) => chan. rpc ( msg, this, Self :: doc_open) . await ,
28
39
Close ( msg) => chan. rpc ( msg, this, Self :: doc_close) . await ,
@@ -65,3 +76,23 @@ impl<D: iroh_blobs::store::Store> Engine<D> {
65
76
}
66
77
}
67
78
}
79
+
80
+ #[ derive( Debug ) ]
81
+ pub ( crate ) struct RpcHandler {
82
+ /// Client to hand out
83
+ client : client:: docs:: MemClient ,
84
+ /// Handler task
85
+ _handler : AbortOnDropHandle < ( ) > ,
86
+ }
87
+
88
+ impl RpcHandler {
89
+ fn new < D : iroh_blobs:: store:: Store > ( engine : & Engine < D > ) -> Self {
90
+ let engine = engine. clone ( ) ;
91
+ let ( listener, connector) = quic_rpc:: transport:: flume:: channel ( 1 ) ;
92
+ let listener = RpcServer :: new ( listener) ;
93
+ let client = client:: docs:: MemClient :: new ( RpcClient :: new ( connector) ) ;
94
+ let _handler = listener
95
+ . spawn_accept_loop ( move |req, chan| engine. clone ( ) . handle_rpc_request ( req, chan) ) ;
96
+ Self { client, _handler }
97
+ }
98
+ }
0 commit comments