Skip to content

Commit

Permalink
use constant
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jun 10, 2024
1 parent 726d121 commit 44a90f1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
10 changes: 4 additions & 6 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
use crate::client::*;
use crate::connection::*;
use crate::constant::*;
use crate::handler;
use crate::room::*;
use async_recursion::async_recursion;
Expand Down Expand Up @@ -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(),
Expand All @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions src/constant.rs
Original file line number Diff line number Diff line change
@@ -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";
8 changes: 4 additions & 4 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ mod init {
const METHOD: &str = "init";

pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, 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
Expand Down
22 changes: 9 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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
Expand All @@ -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<String>) {
async fn start_server(prop: Properties, port: u16, working_dir: &str, password: Option<String>) {
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;
}
Expand Down Expand Up @@ -126,7 +122,7 @@ fn setup_cli() -> ArgMatches {
arg!(--port <VALUE>)
.required(false)
.help("Port number")
.default_value(DEFAULT_PORT),
.default_value(PORT),
)
.arg(
Arg::new("no_password")
Expand All @@ -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();

Expand All @@ -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;
}

Expand All @@ -179,5 +175,5 @@ async fn main() {
let _guard = setup_logger(&prop);

// Start the server
start_server(&prop, port, &current_dir, password).await;
start_server(prop, port, &current_dir, password).await;
}
12 changes: 10 additions & 2 deletions src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,7 @@ use tokio::sync::mpsc::UnboundedSender;
const COGUREIGNORE: &str = ".cogruignore";

pub struct Room {
prop: Properties, // server properties
password: Option<String>, // room password
path: String, // workspace path
pub peers: HashMap<SocketAddr, UnboundedSender<String>>, // broadcasting
Expand All @@ -39,8 +41,9 @@ pub struct Room {
}

impl Room {
pub fn new(_path: &str, _password: Option<String>) -> Self {
pub fn new(_prop: Properties, _path: &str, _password: Option<String>) -> Self {
let mut room = Self {
prop: _prop,
path: _path.to_string(),
peers: HashMap::new(),
password: _password,
Expand All @@ -52,6 +55,11 @@ impl Room {
room
}

/// Return properties object.
pub fn get_prop(&self) -> &Properties {
&self.prop
}

/// Return the sender.
///
/// # Arguments
Expand All @@ -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());
}
}
Expand Down

0 comments on commit 44a90f1

Please sign in to comment.