Skip to content

Commit

Permalink
handle sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jun 6, 2024
1 parent 8e79d50 commit 3360eee
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ chrono = "0.4.38"
clap = "4.5.4"
dunce = "1.0.4"
ignore = "0.4.22"
path-slash = "0.2.1"
rpassword = "7.3.1"
serde_json = "1.0.117"
tokio = { version = "1.37.0", features = ["full"] }
Expand Down
21 changes: 6 additions & 15 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,7 @@
* limitations under the License.
*/
use crate::chat::*;

struct Region {
start: u64,
end: u64,
}

struct Mouse {
point: u64,
}

struct User {
username: String,
mouse: Mouse,
region: Region,
}
use crate::user::*;

pub struct File {
path: String, // absolute path
Expand All @@ -47,6 +33,11 @@ impl File {
}
}

/// Return the file path.
pub fn get_path(&self) -> &String {
&self.path
}

/// Write the content to file.
pub async fn save(&self) {
// TODO: ..
Expand Down
1 change: 1 addition & 0 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &str)
"room::kick" => room::kick::handle(channel, room, &val).await,
"room::broadcast" => room::broadcast::handle(channel, room, &val).await,
"room::list_users" => room::list_users::handle(channel, room, &val).await,
"room::sync" => room::sync::handle(channel, room, &val).await,
"file::open" => file::open::handle(channel, room, &val).await,
"file::close" => file::close::handle(channel, room, &val).await,
"file::say" => file::say::handle(channel, room, &val).await,
Expand Down
33 changes: 31 additions & 2 deletions src/handler/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::channel::*;
use crate::client::*;
use crate::room::*;
use serde_json::Value;
use std::fs;
use std::sync::Arc;
use tokio::sync::Mutex;

Expand Down Expand Up @@ -298,17 +299,45 @@ pub mod sync {
use crate::channel::*;
use crate::handler::room::*;
use crate::room::*;
use path_slash::PathBufExt as _;
use serde_json::Value;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::sync::Mutex;

const METHOD: &str = "room::sync";

pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &Value) {
if !ensure_entered(channel, room, METHOD).await {
let addr = &channel.get_connection().addr;
let mut room = room.lock().await;
let client = room.get_client_mut(addr).unwrap();

if !check_entered(channel, client, METHOD).await {
return;
}

// TODO: ..
let project_path = json["path"].as_str().unwrap().to_string();

let room_path = room.get_path().clone();
let files = room.get_files();

for file in files.iter() {
let abs_path = file.get_path();
let content = fs::read_to_string(abs_path).expect("Unable to read file");

// Replace the room path to client's project path, so the client
// can use the path directly.
let path = abs_path.replace(&room_path, &project_path);
let path = PathBuf::from_slash(&path).to_slash().unwrap().to_string();

channel
.send_json(&serde_json::json!({
"method": METHOD,
"path": path,
"content": content,
"status": "success",
}))
.await;
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod file;
mod handler;
mod room;
mod server;
mod user;
use crate::room::*;
use clap::{arg, Arg, ArgMatches, Command};
use dunce;
Expand Down
10 changes: 10 additions & 0 deletions src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ impl Room {
}
}

/// Return the project path.
pub fn get_path(&self) -> &String {
&self.path
}

/// Return a list of files need to be sync.
pub fn get_files(&mut self) -> &mut Vec<File> {
&mut self.files
}

/// Return the custom ignore file path.
fn ignore_file(&self) -> String {
let ignore = Path::new(&self.path).join(COGUREIGNORE);
Expand Down
33 changes: 33 additions & 0 deletions src/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.
*/

#[derive(Default)]
struct Region {
start: u64,
end: u64,
}

#[derive(Default)]
struct Mouse {
point: u64,
}

#[derive(Default)]
pub struct User {
username: String,
mouse: Mouse,
region: Region,
}

0 comments on commit 3360eee

Please sign in to comment.