From 12dd6d4a5d04526e6a2fb7559c7d0becd93226ba Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 1 Sep 2022 16:11:50 +0200 Subject: [PATCH] node: Start control socket server Signed-off-by: Alexis Sellier --- node/src/client.rs | 1 - node/src/{client/socket.rs => control.rs} | 3 ++- node/src/crypto.rs | 16 +--------------- node/src/lib.rs | 1 + node/src/main.rs | 14 +++++++++++--- node/src/storage/git.rs | 8 ++++---- 6 files changed, 19 insertions(+), 24 deletions(-) rename node/src/{client/socket.rs => control.rs} (96%) diff --git a/node/src/client.rs b/node/src/client.rs index 83f33942..0b9ad2e2 100644 --- a/node/src/client.rs +++ b/node/src/client.rs @@ -11,7 +11,6 @@ use crate::protocol; use crate::storage::git::Storage; pub mod handle; -pub mod socket; /// Client configuration. #[derive(Debug, Clone)] diff --git a/node/src/client/socket.rs b/node/src/control.rs similarity index 96% rename from node/src/client/socket.rs rename to node/src/control.rs index a8bd9406..1e11d09f 100644 --- a/node/src/client/socket.rs +++ b/node/src/control.rs @@ -1,3 +1,4 @@ +//! Client control socket implementation. use std::io::prelude::*; use std::io::BufReader; use std::os::unix::net::UnixListener; @@ -13,7 +14,7 @@ use crate::client::handle::Handle; use crate::identity::ProjId; /// Default name for control socket file. -pub const DEFAULT_NAME: &str = "radicle.sock"; +pub const DEFAULT_SOCKET_NAME: &str = "radicle.sock"; #[derive(thiserror::Error, Debug)] pub enum Error { diff --git a/node/src/crypto.rs b/node/src/crypto.rs index dcd76166..7066e521 100644 --- a/node/src/crypto.rs +++ b/node/src/crypto.rs @@ -1,4 +1,3 @@ -use std::rc::Rc; use std::sync::Arc; use std::{fmt, ops::Deref, str::FromStr}; @@ -8,26 +7,13 @@ use thiserror::Error; pub use ed25519::Signature; -pub trait Signer: 'static { +pub trait Signer: Send + Sync + 'static { /// Return this signer's public/verification key. fn public_key(&self) -> &PublicKey; /// Sign a message and return the signature. fn sign(&self, msg: &[u8]) -> Signature; } -impl Signer for Rc -where - T: Signer + ?Sized, -{ - fn sign(&self, msg: &[u8]) -> Signature { - self.deref().sign(msg) - } - - fn public_key(&self) -> &PublicKey { - self.deref().public_key() - } -} - impl Signer for Arc where T: Signer + ?Sized, diff --git a/node/src/lib.rs b/node/src/lib.rs index 7e7523b5..a02e24a9 100644 --- a/node/src/lib.rs +++ b/node/src/lib.rs @@ -2,6 +2,7 @@ pub use nakamoto_net::{Io, Link, LocalDuration, LocalTime}; pub mod client; +pub mod control; pub mod crypto; mod address_book; diff --git a/node/src/main.rs b/node/src/main.rs index 0a968edf..c1e4c492 100644 --- a/node/src/main.rs +++ b/node/src/main.rs @@ -1,8 +1,9 @@ -use std::net; use std::path::Path; +use std::thread; +use std::{env, net}; -use radicle_node::client; use radicle_node::crypto::{PublicKey, Signature, Signer}; +use radicle_node::{client, control}; type Reactor = nakamoto_net_poll::Reactor; @@ -21,8 +22,15 @@ impl Signer for FailingSigner { fn main() -> anyhow::Result<()> { let signer = FailingSigner {}; let client = client::Client::::new(Path::new("."), signer)?; + let handle = client.handle(); + let config = client::Config::default(); + let socket = env::var("RAD_SOCKET").unwrap_or_else(|_| control::DEFAULT_SOCKET_NAME.to_owned()); - client.run(client::Config::default())?; + let t1 = thread::spawn(move || control::listen(socket, handle)); + let t2 = thread::spawn(move || client.run(config)); + + t1.join().unwrap()?; + t2.join().unwrap()?; Ok(()) } diff --git a/node/src/storage/git.rs b/node/src/storage/git.rs index e0e4c114..047bdb8a 100644 --- a/node/src/storage/git.rs +++ b/node/src/storage/git.rs @@ -1,5 +1,5 @@ use std::path::{Path, PathBuf}; -use std::rc::Rc; +use std::sync::Arc; use std::{fmt, fs, io}; use git_ref_format::refspec; @@ -26,7 +26,7 @@ pub static NAMESPACES_GLOB: Lazy = pub struct Storage { path: PathBuf, - signer: Rc, + signer: Arc, } impl fmt::Debug for Storage { @@ -77,7 +77,7 @@ impl Storage { Ok(Self { path, - signer: Rc::new(signer), + signer: Arc::new(signer), }) } @@ -85,7 +85,7 @@ impl Storage { self.path.as_path() } - pub fn signer(&self) -> Rc { + pub fn signer(&self) -> Arc { self.signer.clone() }