From 18e6d9239b755b18471397e098ec9328257e0192 Mon Sep 17 00:00:00 2001 From: fluo10 Date: Fri, 15 Aug 2025 06:50:14 +0900 Subject: [PATCH] Add carretta project --- Cargo.toml | 25 ++++++++- core/src/config/mod.rs | 2 +- core/src/config/rpc.rs | 52 ++++++++++--------- core/src/rpc/mod.rs | 2 + core/src/rpc/server.rs | 16 ++++++ .../rpc/{server => service}/cached_peer.rs | 0 core/src/rpc/{server => service}/mod.rs | 0 desktop/src/cli/args/config.rs | 14 ++--- desktop/src/config/mod.rs | 48 ----------------- desktop/src/lib.rs | 6 --- src/lib.rs | 5 ++ 11 files changed, 81 insertions(+), 89 deletions(-) create mode 100644 core/src/rpc/server.rs rename core/src/rpc/{server => service}/cached_peer.rs (100%) rename core/src/rpc/{server => service}/mod.rs (100%) delete mode 100644 desktop/src/config/mod.rs create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 8e28cf8..3fbb9b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,27 @@ +[package] +name = "caretta" +edition.workspace = true +version.workspace = true +description.workspace = true +license.workspace = true +repository.workspace = true + +[features] +default = [] +mobile = ["dep:caretta-mobile"] +desktop = ["dep:caretta-desktop"] +test = ["caretta-core/test"] + +[dependencies] +caretta-core.workspace = true +caretta-desktop = { path="desktop", optional = true } +caretta-mobile = { path = "mobile", optional = true } + +[dev-dependencies] +caretta-core = {workspace = true, features = ["test"]} + [workspace] -members = [ "core", "core/macros", "desktop", "mobile", "examples/*" ] +members = [ ".", "core", "core/macros", "desktop", "mobile", "examples/*" ] resolver = "3" [workspace.package] @@ -23,3 +45,4 @@ thiserror = "2.0.12" tokio = { version = "1.45.0", features = ["macros", "rt", "rt-multi-thread"] } tonic = "0.14.0" uuid = { version = "1.17.0", features = ["v7"] } + diff --git a/core/src/config/mod.rs b/core/src/config/mod.rs index b3cb8ea..a059b16 100644 --- a/core/src/config/mod.rs +++ b/core/src/config/mod.rs @@ -40,7 +40,7 @@ impl BaseConfig { Self { p2p : PartialP2pConfig::empty().with_new_secret(), storage: PartialStorageConfig::empty(), - rpc: PartialRpcConfig::empty().with_unused_port(), + rpc: PartialRpcConfig::empty(), } } fn from_toml(s: &str) -> Result { diff --git a/core/src/config/rpc.rs b/core/src/config/rpc.rs index a290060..e84c33f 100644 --- a/core/src/config/rpc.rs +++ b/core/src/config/rpc.rs @@ -1,4 +1,4 @@ -use std::{net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener}, path::PathBuf}; +use std::{net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener}, path::PathBuf, str::FromStr}; #[cfg(feature="desktop")] use clap::Args; use crate::{config::PartialConfig, utils::{emptiable::Emptiable, mergeable::Mergeable}}; @@ -7,55 +7,59 @@ use serde::{Deserialize, Serialize}; use crate::config::error::ConfigError; +#[cfg(unix)] +static DEFAULT_SOCKET_PATH: &str = "caretta.sock"; pub struct RpcConfig { - pub listen_address: IpAddr, - pub port: u16, + pub socket_path: PathBuf, } impl TryFrom for RpcConfig { type Error = ConfigError; fn try_from(config: PartialRpcConfig) -> Result { Ok(Self{ - listen_address: config.listen_address.ok_or(ConfigError::MissingConfig("listen_address".to_string()))?, - port: config.port.ok_or(ConfigError::MissingConfig("port".to_string()))?, + socket_path: config.socket_path.ok_or(ConfigError::MissingConfig("port".to_string()))?, }) } } -#[derive(Args, Clone, Debug, Deserialize, Emptiable, Mergeable, Serialize)] +#[cfg_attr(feature="desktop", derive(Args))] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct PartialRpcConfig { - pub listen_address: Option, - pub port: Option, -} -impl PartialRpcConfig { - pub fn with_unused_port(mut self) -> Self { - let listneer = if let Some(x) = self.listen_address { - TcpListener::bind(SocketAddr::new(x,0)).unwrap() - } else { - TcpListener::bind("127.0.0.1:0").unwrap() - }; - self.port = Some(listneer.local_addr().unwrap().port()); - self - } + pub socket_path: Option, } impl Default for PartialRpcConfig { fn default() -> Self { Self{ - listen_address: Some(IpAddr::V4(Ipv4Addr::LOCALHOST)), - port: None, + socket_path: Some(PathBuf::from_str(DEFAULT_SOCKET_PATH).unwrap()), } } } +impl Emptiable for PartialRpcConfig { + fn empty() -> Self { + Self { + socket_path: None, + } + } + fn is_empty(&self) -> bool { + self.socket_path.is_none() + } +} + impl From for PartialRpcConfig { fn from(source: RpcConfig) -> Self { Self { - listen_address: Some(source.listen_address), - port: Some(source.port), + socket_path: Some(source.socket_path), } } } - +impl Mergeable for PartialRpcConfig { + fn merge(&mut self, other: Self) { + if let Some(x) = other.socket_path { + self.socket_path = Some(x); + } + } +} \ No newline at end of file diff --git a/core/src/rpc/mod.rs b/core/src/rpc/mod.rs index 74f47ad..cbd5b6f 100644 --- a/core/src/rpc/mod.rs +++ b/core/src/rpc/mod.rs @@ -1 +1,3 @@ pub mod server; +pub mod service; + diff --git a/core/src/rpc/server.rs b/core/src/rpc/server.rs new file mode 100644 index 0000000..7214e47 --- /dev/null +++ b/core/src/rpc/server.rs @@ -0,0 +1,16 @@ +use tonic::transport::Server; + +use crate::{proto::cached_peer_service_server::CachedPeerServiceServer, rpc::service::cached_peer::CachedPeerService}; + + +pub async fn start_server() ->Result<(), Error> { + let addr = "[::1]:50051".parse()?; + let cached_peer_server = CachedPeerService::default(); + Server::builder() + .add_service(CachedPeerServiceServer::new(cached_peer_server)) + .serve(addr) + .await?; + + Ok(()) + +} \ No newline at end of file diff --git a/core/src/rpc/server/cached_peer.rs b/core/src/rpc/service/cached_peer.rs similarity index 100% rename from core/src/rpc/server/cached_peer.rs rename to core/src/rpc/service/cached_peer.rs diff --git a/core/src/rpc/server/mod.rs b/core/src/rpc/service/mod.rs similarity index 100% rename from core/src/rpc/server/mod.rs rename to core/src/rpc/service/mod.rs diff --git a/desktop/src/cli/args/config.rs b/desktop/src/cli/args/config.rs index df5a7b8..f446d69 100644 --- a/desktop/src/cli/args/config.rs +++ b/desktop/src/cli/args/config.rs @@ -6,20 +6,16 @@ use crate::config::{PartialP2pConfig, PartialStorageConfig}; use serde::{Deserialize, Serialize}; -use crate::{ - config::DesktopBaseConfig, - error::Error, - global::DEFAULT_CONFIG_FILE_PATH -}; +use crate::global::DEFAULT_CONFIG_FILE_PATH; #[derive(Args, Clone, Debug)] pub struct ConfigArgs { #[arg(short = 'c', long = "config")] pub file_path: Option, #[arg(skip)] - pub file_content: Option, + pub file_content: Option, #[command(flatten)] - pub args: DesktopBaseConfig, + pub args: BaseConfig, } @@ -27,9 +23,9 @@ impl ConfigArgs { pub fn get_file_path_or_default(&self) -> PathBuf { self.file_path.clone().unwrap_or((*DEFAULT_CONFIG_FILE_PATH).clone()) } - pub async fn get_or_read_file_content(&mut self) -> &mut DesktopBaseConfig { + pub async fn get_or_read_file_content(&mut self) -> &mut BaseConfig { self.file_content.get_or_insert( - DesktopBaseConfig::read_from(self.get_file_path_or_default()).await.unwrap() + BaseConfig::read_from(self.get_file_path_or_default()).await.unwrap() ) } } diff --git a/desktop/src/config/mod.rs b/desktop/src/config/mod.rs deleted file mode 100644 index 1df308b..0000000 --- a/desktop/src/config/mod.rs +++ /dev/null @@ -1,48 +0,0 @@ -pub mod rpc; - -use clap::Args; -pub use caretta_core::config::*; - -use caretta_core::utils::{emptiable::Emptiable, mergeable::Mergeable}; -use serde::{Deserialize, Serialize}; -#[cfg(unix)] -pub use rpc::*; - -#[cfg(windows)] -pub use windows::*; - -#[derive(Args, Clone, Debug, Deserialize, Emptiable, Mergeable, Serialize)] -pub struct DesktopBaseConfig { - #[command(flatten)] - p2p: PartialP2pConfig, - #[command(flatten)] - storage: PartialStorageConfig, - #[command(flatten)] - rpc: PartialRpcConfig, -} - -impl BaseConfig for DesktopBaseConfig { - fn new() -> Self { - Self { - p2p : PartialP2pConfig::empty().with_new_secret(), - storage: PartialStorageConfig::empty(), - rpc: PartialRpcConfig::empty().with_unused_port(), - } - } -} - -impl Into for &DesktopBaseConfig { - fn into(self) -> PartialP2pConfig { - self.p2p.clone() - } -} -impl Into for &DesktopBaseConfig { - fn into(self) -> PartialStorageConfig { - self.storage.clone() - } -} -impl Into for &DesktopBaseConfig { - fn into(self) -> PartialRpcConfig { - self.rpc.clone() - } -} \ No newline at end of file diff --git a/desktop/src/lib.rs b/desktop/src/lib.rs index 371fa2c..e5a89f0 100644 --- a/desktop/src/lib.rs +++ b/desktop/src/lib.rs @@ -1,9 +1,3 @@ pub mod cli; -pub mod config; pub mod global; pub mod utils; -pub use caretta_core::{ - cache, - data, - error, -}; diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..508000e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +pub use caretta_core::*; +#[cfg(desktop)] +pub use caretta_desktop::*; +#[cfg(mobile)] +pub use caretta_mobile::*; \ No newline at end of file