pub mod jsonrpc;
pub mod framing;
pub mod client;
pub mod server;
pub mod credentials;
pub mod error;
pub mod mock;
pub use jsonrpc::{
JsonRpcRequest, JsonRpcResponse, JsonRpcError, JsonRpcErrorResponse,
JsonRpcMessage, JsonRpcNotification, JsonRpcBatch, RequestId, error_codes,
};
pub use framing::{
encode_message, decode_message, send_message, recv_message, write_frame,
FramedMessage, MessageFraming,
};
pub use client::{UnixClient, ClientConfig};
pub use server::{UnixServer, ServerConfig, ConnectionHandler};
pub use credentials::{PeerCredentials, verify_peer_credentials};
pub use error::{ProtocolError, ProtocolResult};
pub use mock::MockDaemonClient;
use serde_json::Value;
pub enum DaemonClient {
Real(UnixClient),
Mock(MockDaemonClient),
}
impl DaemonClient {
pub fn real(socket_path: impl Into<String>) -> Self {
Self::Real(UnixClient::with_socket_path(socket_path))
}
pub fn real_with_config(config: ClientConfig) -> Self {
Self::Real(UnixClient::new(config))
}
pub fn mock() -> Self {
Self::Mock(MockDaemonClient::new())
}
pub fn from_env(socket_path: impl Into<String>) -> Self {
if std::env::var("MOCK_DAEMON")
.unwrap_or_else(|_| "false".to_string())
.parse::<bool>()
.unwrap_or(false)
{
Self::mock()
} else {
Self::real(socket_path)
}
}
pub async fn connect(&self) -> ProtocolResult<()> {
match self {
Self::Real(client) => client.connect().await,
Self::Mock(_) => Ok(()), }
}
pub async fn call(
&self,
method: impl Into<String>,
params: Option<Value>,
) -> ProtocolResult<Value> {
match self {
Self::Real(client) => client.call(method, params).await,
Self::Mock(client) => client.call(method, params).await,
}
}
pub async fn notify(
&self,
method: impl Into<String>,
params: Option<Value>,
) -> ProtocolResult<()> {
match self {
Self::Real(client) => client.notify(method, params).await,
Self::Mock(_) => Ok(()), }
}
pub async fn call_with_retry(
&self,
method: impl Into<String> + Clone,
params: Option<Value>,
) -> ProtocolResult<Value> {
match self {
Self::Real(client) => client.call_with_retry(method, params).await,
Self::Mock(client) => client.call(method, params).await,
}
}
pub fn is_mock(&self) -> bool {
matches!(self, Self::Mock(_))
}
pub async fn disconnect(&self) -> ProtocolResult<()> {
match self {
Self::Real(client) => client.disconnect().await,
Self::Mock(_) => Ok(()),
}
}
}
pub const PROTOCOL_VERSION: &str = "2.0";
pub const MAX_MESSAGE_SIZE: usize = 16 * 1024 * 1024;
pub const DEFAULT_SOCKET_PATH: &str = "/var/run/crrouterd.sock";