diff --git a/Cargo.lock b/Cargo.lock index 96b0f5f..b205de7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -211,6 +211,7 @@ dependencies = [ "tokio", "tokio-tungstenite", "tracing", + "tracing-subscriber", ] [[package]] @@ -253,6 +254,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] name = "libc" version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -301,6 +308,16 @@ dependencies = [ ] [[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + +[[package]] name = "num_cpus" version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -326,6 +343,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + +[[package]] name = "parking_lot" version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -490,6 +513,15 @@ dependencies = [ ] [[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + +[[package]] name = "signal-hook-registry" version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -555,6 +587,16 @@ dependencies = [ ] [[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] name = "tinyvec" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -641,6 +683,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -707,6 +775,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" [[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -719,6 +793,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] name = "windows-sys" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index f5cf2ce..9438579 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ tokio = { version = "1.32.0", features = ["full"] } tracing = "*" futures-util = "*" serde = { version = "1", features = ["derive"]} -serde_json = "1.0" \ No newline at end of file +serde_json = "1.0" +tracing-subscriber = "0.3" \ No newline at end of file diff --git a/src/connection.rs b/src/connection.rs index 068e57f..63808bb 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -26,7 +26,8 @@ use crate::{ listener::Listeners, model::{ instance::{Instance, InstanceMeta}, - repository::{CommitMetadata, RepositoryView}, + repository::{CommitMetadata, Repository, RepositoryView}, + user::User, }, }; @@ -59,7 +60,7 @@ pub async fn connection_worker( ) { let mut handshaked = false; let this_instance = Instance { - url: String::from("FOO"), + url: String::from("127.0.0.1:8080"), }; while let Some(message) = socket.next().await { @@ -92,6 +93,8 @@ pub async fn connection_worker( } }; + info!("Read payload: {}", std::str::from_utf8(&payload).unwrap()); + if let MessageKind::Handshake(handshake) = message { match handshake { HandshakeMessage::Initiate(_) => { @@ -105,7 +108,10 @@ pub async fn connection_worker( socket .send(Message::Binary( - serde_json::to_vec(&HandshakeMessage::Response(message)).unwrap(), + serde_json::to_vec(&MessageKind::Handshake( + HandshakeMessage::Response(message), + )) + .unwrap(), )) .await .unwrap(); @@ -118,7 +124,10 @@ pub async fn connection_worker( socket .send(Message::Binary( - serde_json::to_vec(&HandshakeMessage::Finalize(message)).unwrap(), + serde_json::to_vec(&MessageKind::Handshake( + HandshakeMessage::Finalize(message), + )) + .unwrap(), )) .await .unwrap(); @@ -128,6 +137,19 @@ pub async fn connection_worker( HandshakeMessage::Finalize(_) => { handshaked = true; + // Send HandshakeMessage::Finalize + let message = HandshakeFinalize { success: true }; + + socket + .send(Message::Binary( + serde_json::to_vec(&MessageKind::Handshake( + HandshakeMessage::Finalize(message), + )) + .unwrap(), + )) + .await + .unwrap(); + continue; } } @@ -139,6 +161,7 @@ pub async fn connection_worker( if let MessageKind::Repository(repository) = &message { if repository.target.instance != this_instance { + info!("Forwarding command to {}", repository.target.instance.url); // We need to send this command to a different instance let mut listener = send_and_get_listener(message, &listeners, &connections).await; @@ -253,6 +276,8 @@ pub async fn connection_worker( } } } + + info!("Connection closed"); } async fn send_and_get_listener( @@ -260,14 +285,20 @@ async fn send_and_get_listener( listeners: &Arc>, mut connections: &Arc>, ) -> Receiver { - let (instance, user, repository) = match &message { - MessageKind::Handshake(_) => { - todo!() - } - MessageKind::Repository(repository) => (None, None, Some(repository.target.clone())), - }; + let (instance, user, repository): (Option, Option, Option) = + match &message { + MessageKind::Handshake(_) => { + todo!() + } + MessageKind::Repository(repository) => (None, None, Some(repository.target.clone())), + }; - let target = todo!(); + let target = match (&instance, &user, &repository) { + (Some(instance), _, _) => instance.clone(), + (_, Some(user), _) => user.instance.clone(), + (_, _, Some(repository)) => repository.instance.clone(), + _ => unreachable!(), + }; let mut listeners = listeners.lock().await; let mut listener = listeners.add(instance, user, repository); diff --git a/src/main.rs b/src/main.rs index e1d9a01..68e9be0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ extern crate tracing; #[tokio::main] async fn main() -> Result<(), Box> { + tracing_subscriber::fmt::init(); let mut listener = TcpListener::bind("127.0.0.1:8080").await?; let mut connections: Arc> = Arc::default(); let mut listeners: Arc> = Arc::default(); @@ -30,6 +31,8 @@ async fn main() -> Result<(), Box> { } }; + info!("Accepted connection from {}", address); + let connection = accept_websocket_connection(stream).await; let connection = match connection { @@ -43,6 +46,8 @@ async fn main() -> Result<(), Box> { } }; + info!("Websocket connection established with {}", address); + let connection = RawConnection { task: tokio::spawn(connection_worker( connection, diff --git a/src/model/repository.rs b/src/model/repository.rs index 9ca976e..fc6a3b4 100644 --- a/src/model/repository.rs +++ b/src/model/repository.rs @@ -10,7 +10,7 @@ pub struct Repository { pub instance: Instance, } -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryView { pub name: String, pub description: String, @@ -19,19 +19,19 @@ pub struct RepositoryView { pub files: Vec, } -#[derive(Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub enum RepositoryFile { Directory(String), File(String), } -#[derive(Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct RepositoryFileWithCommitMetadata { pub file: RepositoryFile, pub commit_metadata: CommitMetadata, } -#[derive(Clone, Serialize, Deserialize, Default)] +#[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct CommitMetadata { pub author: String, pub message: String, diff --git a/src/model/user.rs b/src/model/user.rs index 69d55bd..299f55e 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -1,7 +1,9 @@ use serde::{Deserialize, Serialize}; +use super::instance::Instance; + #[derive(Clone, Hash, Serialize, Deserialize)] pub struct User { pub username: String, - pub instance: String, + pub instance: Instance, }