diff --git a/src/channel.rs b/src/channel.rs index 8865f98..075f904 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -15,6 +15,7 @@ */ use crate::client::*; use crate::connection::*; +use crate::constant::*; use crate::handler; use crate::room::*; use async_recursion::async_recursion; @@ -45,12 +46,9 @@ impl Channel { // Create a channel for this peer let (_tx, _rx) = mpsc::unbounded_channel(); - let addr = _connection.addr; - //let addr = _connection.stream.peer_addr().unwrap(); + let addr = _connection.stream.peer_addr().unwrap(); room.peers.insert(addr, _tx); - //room.peers.insert(_connection.addr, _tx); - Self { read_buf: [0; BUF_SIZE], data: Vec::new(), @@ -60,9 +58,9 @@ impl Channel { } /// Return true when channel is local. - pub fn is_local(&self) -> bool { + pub fn is_local(&self, room: &Room) -> bool { let ip = self.connection.addr.ip(); - ip.to_string() == "127.0.0.1" + ip.to_string() == room.get_prop().get_or_default("cogru.Host", HOST) } /// Logic loop. diff --git a/src/constant.rs b/src/constant.rs new file mode 100644 index 0000000..c5cbc34 --- /dev/null +++ b/src/constant.rs @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2024 Cogru Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +pub const DOT_COGRU: &str = "./.cogru"; +pub const PROP_FILE: &str = "./Cogru.properties"; + +pub const HOST: &str = "127.0.0.1"; +pub const PORT: &str = "8786"; diff --git a/src/handler/mod.rs b/src/handler/mod.rs index 5a0efb2..977c5a0 100644 --- a/src/handler/mod.rs +++ b/src/handler/mod.rs @@ -113,15 +113,15 @@ mod init { const METHOD: &str = "init"; pub async fn handle(channel: &mut Channel, room: &Arc>, json: &Value) { + let addr = &channel.get_connection().addr; + let mut room = room.lock().await; + let path = data_str(json, "path").unwrap(); // XXX: Every local client is the admin. - let is_admin = channel.is_local(); - + let is_admin = channel.is_local(&room); let client = Client::new(path.clone(), is_admin); - let addr = &channel.get_connection().addr; - let mut room = room.lock().await; room.add_client(channel.get_connection().addr, client); channel diff --git a/src/main.rs b/src/main.rs index 892b0a6..bb9aacb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,12 +21,14 @@ mod channel; mod chat; mod client; mod connection; +mod constant; mod file; mod handler; mod room; mod server; mod user; mod util; +use crate::constant::*; use crate::room::*; use crate::util::*; use clap::{arg, Arg, ArgMatches, Command}; @@ -42,12 +44,6 @@ use tracing::Level; use tracing_appender::non_blocking::WorkerGuard; use tracing_subscriber::{fmt, layer::SubscriberExt}; -const DOT_COGRU: &str = "./.cogru"; -const PROP_FILE: &str = "./Cogru.properties"; - -const DEFAULT_HOST: &str = "127.0.0.1"; -const DEFAULT_PORT: &str = "8786"; - /// Setup logger rotator. /// /// https://docs.rs/tracing-appender/0.2.3/tracing_appender/non_blocking/struct.WorkerGuard.html @@ -74,11 +70,11 @@ pub fn setup_logger(prop: &Properties) -> WorkerGuard { /// /// * `port` - port to start. /// * `password` - password to enter the session. -async fn start_server(prop: &Properties, port: u16, working_dir: &str, password: Option) { +async fn start_server(prop: Properties, port: u16, working_dir: &str, password: Option) { println!("Start room server :::"); - let host = prop.get_or_default("cogru.Host", DEFAULT_HOST); + let host = prop.get_or_default("cogru.Host", HOST); - let room = Room::new(working_dir, password); + let room = Room::new(prop, working_dir, password); let mut server = Server::new(&host, port, room); let _ = server.start().await; } @@ -126,7 +122,7 @@ fn setup_cli() -> ArgMatches { arg!(--port ) .required(false) .help("Port number") - .default_value(DEFAULT_PORT), + .default_value(PORT), ) .arg( Arg::new("no_password") @@ -144,7 +140,7 @@ fn setup_cli() -> ArgMatches { #[tokio::main] async fn main() { let prop = Properties::new(&PROP_FILE); - let prop_port = prop.get_or_default("cogru.Port", DEFAULT_PORT); + let prop_port = prop.get_or_default("cogru.Port", PORT); let matches = setup_cli(); @@ -160,7 +156,7 @@ async fn main() { // XXX: If the port is the same as default port, we // assumed the user did not input the port number. // Let's respect the properties' port instead. - if port == DEFAULT_PORT { + if port == PORT { port = &prop_port; } @@ -179,5 +175,5 @@ async fn main() { let _guard = setup_logger(&prop); // Start the server - start_server(&prop, port, ¤t_dir, password).await; + start_server(prop, port, ¤t_dir, password).await; } diff --git a/src/room.rs b/src/room.rs index 6ba75c2..58f844b 100644 --- a/src/room.rs +++ b/src/room.rs @@ -17,6 +17,7 @@ use crate::chat::*; use crate::client::*; use crate::connection::*; use crate::file::*; +use crate::server::properties::*; use crate::util::*; use ignore::WalkBuilder; use serde_json::Value; @@ -30,6 +31,7 @@ use tokio::sync::mpsc::UnboundedSender; const COGUREIGNORE: &str = ".cogruignore"; pub struct Room { + prop: Properties, // server properties password: Option, // room password path: String, // workspace path pub peers: HashMap>, // broadcasting @@ -39,8 +41,9 @@ pub struct Room { } impl Room { - pub fn new(_path: &str, _password: Option) -> Self { + pub fn new(_prop: Properties, _path: &str, _password: Option) -> Self { let mut room = Self { + prop: _prop, path: _path.to_string(), peers: HashMap::new(), password: _password, @@ -52,6 +55,11 @@ impl Room { room } + /// Return properties object. + pub fn get_prop(&self) -> &Properties { + &self.prop + } + /// Return the sender. /// /// # Arguments @@ -78,7 +86,7 @@ impl Room { /// /// * `params` - [description] pub fn broadcast_json(&self, params: &Value) { - for (addr, sender) in self.peers.iter() { + for (_addr, sender) in self.peers.iter() { let _ = sender.send(params.to_string()); } }