Skip to content

Commit

Permalink
fix: Preload file contents?
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jun 15, 2024
1 parent d67bb0a commit 969312f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
16 changes: 8 additions & 8 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ pub struct File {

impl File {
pub fn new(_path: String) -> Self {
Self {
let mut new_file = Self {
path: _path,
chat: Chat::new(),
view: None,
}
};
new_file.load_file(); // ensure read
new_file
}

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

/// Return file path as relative path.
pub fn get_relative_path(&self, room: &Room) -> String {
pub fn relative_path(&self, room: &Room) -> String {
no_room_path(room, &self.path)
}

Expand All @@ -61,7 +63,6 @@ impl File {
}

pub fn update(&mut self, add_or_delete: &String, beg: usize, end: usize, contents: &String) {
self.load_file(); // ensure read
let view = self.view.as_mut().unwrap();

match add_or_delete.clone().as_str() {
Expand All @@ -78,14 +79,13 @@ impl File {
}

/// Return the file contents.
pub fn contents(&mut self) -> String {
self.load_file(); // ensure read
pub fn contents(&self) -> String {
let view = self.view.clone().unwrap();
view.to_string()
}

/// Write the content to file.
pub fn save(&mut self) {
pub fn save(&self) {
let contents = self.contents();
let _ = std::fs::write(&self.path, contents);
}
Expand Down
76 changes: 40 additions & 36 deletions src/handler/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,10 @@ pub mod update {
return;
}

// First get relative path.
let relative_file = file.unwrap().get_relative_path(&room);

let file = room.get_file_mut(&addr, &path);
let file = file.unwrap();

file.update(&add_or_delete, beg, end, &contents);

// Get the peers that are in the file.
let peers = room.peers_by_file(&room, &relative_file);

let params = &serde_json::json!({
"method": METHOD,
"file": relative_file,
"add_or_delete": add_or_delete,
"beg": beg,
"end": end,
"contens": contents,
"status": "success",
});

for (_addr, _sender) in peers.iter() {
if *_addr == addr {
continue;
}
let _ = _sender.send(params.to_string());
}
}
}

Expand Down Expand Up @@ -161,36 +138,36 @@ pub mod sync {
}

/// Return a list of users in the file.
pub mod users {
pub mod info {
use crate::channel::*;
use crate::client::*;
use crate::handler::room::*;
use crate::room::*;
use crate::user::*;
use crate::util::*;
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::Mutex;

const METHOD: &str = "file::users";

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

if !check_entered(channel, &client, METHOD).await {
return;
}
/// Return a list of user in file.
///
/// # Arguments
///
/// * `room` - It's used to get all users in room.
/// * `client` - Need the target client's file path.
fn get_users(room: &Room, client: &Client) -> Vec<User> {
// Prepare data to send.
let mut users = Vec::new();

let this_user = client.user().unwrap();

// If user is not in the file, ignore it.
if this_user.path.is_none() {
return;
return users;
}

// Prepare data to send.
let mut users = Vec::new();

for _client in room.get_clients().iter() {
let user = _client.user();

Expand Down Expand Up @@ -218,11 +195,38 @@ pub mod users {
users.push(user.clone());
}

users
}

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

let file = data_str(json, "file").unwrap();
let file = room.get_file(&addr, &file);

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

if file.is_none() {
return;
}

let file = file.unwrap();

let file_path = file.relative_path(&room);
let contents = file.contents();

let users = get_users(&room, &client);
let users = serde_json::to_string(&users).unwrap();

channel
.send_json(&serde_json::json!({
"method": METHOD,
"file": file_path,
"contents": contents,
"clients": users,
"status": "success",
}))
Expand Down
2 changes: 1 addition & 1 deletion src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &str)
"file::update" => file::update::handle(channel, room, &val).await,
"file::save" => file::save::handle(channel, room, &val).await,
"file::sync" => file::sync::handle(channel, room, &val).await,
"file::users" => file::users::handle(channel, room, &val).await,
"file::info" => file::info::handle(channel, room, &val).await,
"file::say" => file::say::handle(channel, room, &val).await,
_ => {
tracing::error!("Unkown method request: {:?}", method);
Expand Down

0 comments on commit 969312f

Please sign in to comment.