Skip to content

Commit

Permalink
feat: Broadcast update
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jun 15, 2024
1 parent 09bf5c6 commit d67bb0a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
use crate::chat::*;
use crate::room::*;
use crate::user::*;
use crate::util::*;
use crop::Rope;
Expand Down Expand Up @@ -41,6 +42,11 @@ impl File {
&self.path
}

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

/// Return chat object.
pub fn get_chat(&mut self) -> &mut Chat {
&mut self.chat
Expand Down
32 changes: 27 additions & 5 deletions src/handler/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

// Addition and Deletion to the file.
/// Addition and Deletion to the file.
pub mod update {
use crate::channel::*;
use crate::handler::file::*;
Expand All @@ -37,18 +37,41 @@ pub mod update {
let end = data_usize(json, "end").unwrap();
let contents = data_str(json, "contents").unwrap();

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

if file.is_none() {
tracing::debug!("Updating an non-existence file: {}", path);
// TODO: Create one?
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 All @@ -70,8 +93,7 @@ pub mod save {
let mut room = room.lock().await;

let path = data_str(json, "path").unwrap();
let path = to_room_path(&addr, &room, &path);
let file = room.get_file_mut(&path);
let file = room.get_file_mut(&addr, &path);

if file.is_none() {
tracing::debug!("Updating an non-existence file: {}", path);
Expand Down
23 changes: 20 additions & 3 deletions src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ impl Room {
/// # Arguments
///
/// * `file` - The file path.
pub fn peers_by_file(&self, room: &Room, file: &String) -> HashMap<&SocketAddr, &UnboundedSender<String>> {
pub fn peers_by_file(
&self,
room: &Room,
file: &String,
) -> HashMap<&SocketAddr, &UnboundedSender<String>> {
let file = no_room_path(&room, &file);

let mut data = HashMap::new();
Expand Down Expand Up @@ -167,9 +171,22 @@ impl Room {
///
/// # Arguments
///
/// * `SocketAddr` - Socket address to convert to full path.
/// * `path` - The file path.
pub fn get_file(&self, addr: &SocketAddr, path: &String) -> Option<&File> {
let path = to_room_path(addr, self, path);
self.files.get(&path)
}

/// Return the file object by file path.
///
/// # Arguments
///
/// * `SocketAddr` - Socket address to convert to full path.
/// * `path` - The file path.
pub fn get_file_mut(&mut self, path: &String) -> Option<&mut File> {
self.files.get_mut(path)
pub fn get_file_mut(&mut self, addr: &SocketAddr, path: &String) -> Option<&mut File> {
let path = to_room_path(addr, self, path);
self.files.get_mut(&path)
}

/// Return a list of files need to be sync.
Expand Down

0 comments on commit d67bb0a

Please sign in to comment.