2023-09-21 21:32:44 +02:00
|
|
|
//! A crate containing constructors to provide instances of a BlobService and
|
2023-11-15 22:30:42 +01:00
|
|
|
//! DirectoryService. Only used for testing purposes, but across crates.
|
2023-09-21 21:32:44 +02:00
|
|
|
//! Should be removed once we have a better concept of a "Service registry".
|
2023-10-07 08:35:31 +02:00
|
|
|
use tonic::transport::{Channel, Endpoint, Server, Uri};
|
2023-09-21 21:32:44 +02:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
blobservice::{BlobService, MemoryBlobService},
|
|
|
|
directoryservice::{DirectoryService, MemoryDirectoryService},
|
2023-10-07 08:35:31 +02:00
|
|
|
proto::{
|
2023-10-08 11:31:45 +02:00
|
|
|
blob_service_client::BlobServiceClient, blob_service_server::BlobServiceServer,
|
2023-10-07 08:35:31 +02:00
|
|
|
directory_service_client::DirectoryServiceClient,
|
2023-10-08 11:31:45 +02:00
|
|
|
directory_service_server::DirectoryServiceServer, GRPCBlobServiceWrapper,
|
|
|
|
GRPCDirectoryServiceWrapper,
|
2023-10-07 08:35:31 +02:00
|
|
|
},
|
2023-09-21 21:32:44 +02:00
|
|
|
};
|
|
|
|
|
2023-12-31 22:33:41 +01:00
|
|
|
pub fn gen_blob_service() -> Box<dyn BlobService> {
|
|
|
|
Box::<MemoryBlobService>::default()
|
2023-09-21 21:32:44 +02:00
|
|
|
}
|
|
|
|
|
2023-12-31 22:33:41 +01:00
|
|
|
pub fn gen_directory_service() -> Box<dyn DirectoryService> {
|
|
|
|
Box::<MemoryDirectoryService>::default()
|
2023-09-21 21:32:44 +02:00
|
|
|
}
|
2023-10-07 08:35:31 +02:00
|
|
|
|
2023-10-08 11:31:45 +02:00
|
|
|
/// This will spawn the a gRPC server with a DirectoryService client, connect a
|
|
|
|
/// gRPC DirectoryService client and return it.
|
2023-10-07 23:01:18 +02:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub(crate) async fn gen_directorysvc_grpc_client() -> DirectoryServiceClient<Channel> {
|
|
|
|
let (left, right) = tokio::io::duplex(64);
|
2023-10-07 08:35:31 +02:00
|
|
|
|
2023-10-07 23:01:18 +02:00
|
|
|
// spin up a server, which will only connect once, to the left side.
|
|
|
|
tokio::spawn(async {
|
|
|
|
// spin up a new DirectoryService
|
|
|
|
let mut server = Server::builder();
|
|
|
|
let router = server.add_service(DirectoryServiceServer::new(
|
2024-01-01 02:10:24 +01:00
|
|
|
GRPCDirectoryServiceWrapper::new(gen_directory_service()),
|
2023-10-07 23:01:18 +02:00
|
|
|
));
|
2023-10-07 08:35:31 +02:00
|
|
|
|
2023-10-07 23:01:18 +02:00
|
|
|
router
|
|
|
|
.serve_with_incoming(tokio_stream::once(Ok::<_, std::io::Error>(left)))
|
|
|
|
.await
|
2023-10-07 08:35:31 +02:00
|
|
|
});
|
|
|
|
|
2023-10-07 23:01:18 +02:00
|
|
|
// Create a client, connecting to the right side. The URI is unused.
|
|
|
|
let mut maybe_right = Some(right);
|
2023-10-12 19:32:32 +02:00
|
|
|
DirectoryServiceClient::new(
|
2023-10-07 23:01:18 +02:00
|
|
|
Endpoint::try_from("http://[::]:50051")
|
|
|
|
.unwrap()
|
|
|
|
.connect_with_connector(tower::service_fn(move |_: Uri| {
|
|
|
|
let right = maybe_right.take().unwrap();
|
|
|
|
async move { Ok::<_, std::io::Error>(right) }
|
|
|
|
}))
|
|
|
|
.await
|
|
|
|
.unwrap(),
|
2023-10-12 19:32:32 +02:00
|
|
|
)
|
2023-10-07 08:35:31 +02:00
|
|
|
}
|
2023-10-08 11:31:45 +02:00
|
|
|
|
|
|
|
/// This will spawn the a gRPC server with a BlobService client, connect a
|
|
|
|
/// gRPC BlobService client and return it.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub(crate) async fn gen_blobsvc_grpc_client() -> BlobServiceClient<Channel> {
|
|
|
|
let (left, right) = tokio::io::duplex(64);
|
|
|
|
|
|
|
|
// spin up a server, which will only connect once, to the left side.
|
|
|
|
tokio::spawn(async {
|
|
|
|
// spin up a new DirectoryService
|
|
|
|
let mut server = Server::builder();
|
2024-01-01 02:23:23 +01:00
|
|
|
let router = server.add_service(BlobServiceServer::new(GRPCBlobServiceWrapper::new(
|
2023-10-08 11:31:45 +02:00
|
|
|
gen_blob_service(),
|
|
|
|
)));
|
|
|
|
|
|
|
|
router
|
|
|
|
.serve_with_incoming(tokio_stream::once(Ok::<_, std::io::Error>(left)))
|
|
|
|
.await
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create a client, connecting to the right side. The URI is unused.
|
|
|
|
let mut maybe_right = Some(right);
|
2023-10-12 19:32:32 +02:00
|
|
|
BlobServiceClient::new(
|
2023-10-08 11:31:45 +02:00
|
|
|
Endpoint::try_from("http://[::]:50051")
|
|
|
|
.unwrap()
|
|
|
|
.connect_with_connector(tower::service_fn(move |_: Uri| {
|
|
|
|
let right = maybe_right.take().unwrap();
|
|
|
|
async move { Ok::<_, std::io::Error>(right) }
|
|
|
|
}))
|
|
|
|
.await
|
|
|
|
.unwrap(),
|
2023-10-12 19:32:32 +02:00
|
|
|
)
|
2023-10-08 11:31:45 +02:00
|
|
|
}
|