Skip to content

Commit

Permalink
add data str
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jun 8, 2024
1 parent d1ad2c0 commit dd4a622
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl Channel {

let mut room = room.lock().await;
room.remove_client(&self.connection.addr);
room.remove_peer(&self.connection.addr);
}

/// Return connection
Expand Down
5 changes: 5 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ impl File {
&self.path
}

/// Return chat object.
pub fn get_chat(&mut self) -> &mut Chat {
&mut self.chat
}

/// Write the content to file.
pub async fn save(&self) {
// TODO: ..
Expand Down
45 changes: 4 additions & 41 deletions src/handler/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,6 @@ pub fn to_room_path(addr: &SocketAddr, room: &mut Room, path: &String) -> String
path.replace(project_path, &server_path)
}

/// Open file
pub mod open {
use crate::channel::*;
use crate::handler::file::*;
use crate::handler::room::*;
use crate::room::*;
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::Mutex;

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

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;

// XXX: Get this early to avoid borrow errors.
let file_path = json["file"].as_str().unwrap().to_string();
let path = to_room_path(addr, &mut room, &file_path);

let client = room.get_client_mut(addr).unwrap();
let username = client.user().unwrap().username.clone();

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

// Get the registered file.
let file = room.get_file(&path);

// If not registered?
if file.is_none() {
// TODO: Create new file!
}

let file = file.unwrap();
}
}

/// Return a list of users in the file.
pub mod users {
use crate::channel::*;
Expand Down Expand Up @@ -109,11 +70,12 @@ pub mod sync {
let mut room = room.lock().await;

// XXX: Get this early to avoid borrow errors.
let file_path = json["file"].as_str().unwrap().to_string();
let file_path = data_str(json, "file");
let local_path = to_room_path(addr, &mut room, &file_path);

let client = room.get_client_mut(addr).unwrap();

// Check entered the room.
if !check_entered(channel, client, METHOD).await {
return;
}
Expand All @@ -136,6 +98,7 @@ pub mod say {
use crate::channel::*;
use crate::handler::room::*;
use crate::room::*;
use crate::util::*;
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::Mutex;
Expand All @@ -151,7 +114,7 @@ pub mod say {
return;
}

let message = json["message"].as_str().unwrap().to_string();
let message = data_str(json, "message");

room.broadcast_json(&serde_json::json!({
"method": METHOD,
Expand Down
4 changes: 3 additions & 1 deletion src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &str)
"room::broadcast" => room::broadcast::handle(channel, room, &val).await,
"room::users" => room::users::handle(channel, room, &val).await,
"room::sync" => room::sync::handle(channel, room, &val).await,
"room::update" => room::update::handle(channel, room, &val).await,
"file::say" => file::say::handle(channel, room, &val).await,
"file::sync" => file::sync::handle(channel, room, &val).await,
_ => {
Expand Down Expand Up @@ -102,6 +103,7 @@ mod init {
use crate::channel::*;
use crate::client::*;
use crate::room::*;
use crate::util::*;
use chrono;
use serde_json::Value;
use std::sync::Arc;
Expand All @@ -110,7 +112,7 @@ mod init {
const METHOD: &str = "init";

pub async fn handle(channel: &mut Channel, room: &Arc<Mutex<Room>>, json: &Value) {
let path = json["path"].as_str().unwrap().to_string();
let path = data_str(json, "path");

// XXX: Every local client is the admin.
let is_admin = channel.is_local();
Expand Down
54 changes: 47 additions & 7 deletions src/handler/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub async fn check_admin(channel: &mut Channel, client: &mut Client, method: &st
pub mod enter {
use crate::channel::*;
use crate::room::*;
use crate::util::*;
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::Mutex;
Expand All @@ -112,11 +113,11 @@ pub mod enter {
return;
}

let username = json["username"].as_str().unwrap().to_string();
let username = data_str(json, "username");
let password = if json["password"].is_null() {
None
} else {
Some(json["password"].as_str().unwrap().to_string())
Some(data_str(json, "password"))
};

let (entered, message) = room.enter(addr, &username, &password);
Expand Down Expand Up @@ -191,6 +192,7 @@ pub mod kick {
use crate::channel::*;
use crate::handler::room::*;
use crate::room::*;
use crate::util::*;
use serde_json::Value;
use std::sync::{Arc, MutexGuard};
use tokio::sync::Mutex;
Expand All @@ -213,7 +215,7 @@ pub mod kick {

let admin_name = client.user().unwrap().username.clone();
// target user to kick out
let target_name = json["username"].as_str().unwrap().to_string();
let target_name = data_str(json, "username");

// kick
let (kicked, message) = room.kick(&target_name);
Expand Down Expand Up @@ -247,6 +249,7 @@ pub mod broadcast {
use crate::channel::*;
use crate::handler::room::*;
use crate::room::*;
use crate::util::*;
use serde_json::Value;
use std::sync::{Arc, MutexGuard};
use tokio::sync::Mutex;
Expand All @@ -264,7 +267,7 @@ pub mod broadcast {
return;
}

let message = json["message"].as_str().unwrap().to_string();
let message = data_str(json, "message");

room.broadcast_json(&serde_json::json!({
"method": METHOD,
Expand All @@ -275,6 +278,37 @@ pub mod broadcast {
}
}

/// Update a single client's information.
pub mod update {
use crate::channel::*;
use crate::handler::room::*;
use crate::room::*;
use crate::util::*;
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::Mutex;

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

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 client = room.get_client_mut(addr).unwrap();

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

let username = data_str(json, "username");
let path = data_str(json, "path");
let point = data_str(json, "point");
let region_start = data_str(json, "region_start");
let region_end = data_str(json, "region_end");

// TODO: ..
}
}

/// Room Users
///
/// Return a list of users in room.
Expand All @@ -289,11 +323,17 @@ pub mod users {
const METHOD: &str = "room::users";

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: ..
for client in room.get_clients().iter() {
let user = client.user();
}
}
}

Expand All @@ -320,7 +360,7 @@ pub mod sync {
return;
}

let project_path = json["path"].as_str().unwrap().to_string();
let project_path = data_str(json, "path");

let room_path = room.get_path().clone();
let files = room.get_files();
Expand Down
14 changes: 14 additions & 0 deletions src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ impl Room {
return (true, "");
}

/// Return a list of client.
pub fn get_clients(&mut self) -> Vec<&Client> {
self.clients.values().clone().collect::<Vec<&Client>>()
}

/// Add a client to room.
///
/// # Arguments
Expand All @@ -214,6 +219,15 @@ impl Room {
self.clients.remove(addr);
}

/// Remove a client by address.
///
/// # Arguments
///
/// * `addr` - Key socket address.
pub fn remove_peer(&mut self, addr: &SocketAddr) {
self.peers.remove(addr);
}

/// Return the socket address by username.
///
/// # Arguments
Expand Down
4 changes: 2 additions & 2 deletions src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Mouse {

pub struct User {
pub username: String,
pub path: String, // the user's location
pub path: Option<String>, // the user's location
pub mouse: Mouse,
pub region: Region,
}
Expand All @@ -36,7 +36,7 @@ impl User {
pub fn new(_username: String) -> Self {
Self {
username: _username,
path: "".to_string(),
path: None,
mouse: Mouse::default(),
region: Region::default(),
}
Expand Down
11 changes: 11 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use serde_json::Value;

/// Get data as string.
///
/// # Arguments
///
/// * `json` - JSON object.
/// * `key` - Key to the data.
pub fn data_str(json: &Value, key: &str) -> String {
json[key].as_str().unwrap().to_string()
}

/// Wrapper to fs::read_to_string
///
Expand Down

0 comments on commit dd4a622

Please sign in to comment.