Skip to content

Commit

Permalink
mark
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jun 13, 2024
1 parent c752818 commit 071f376
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 23 deletions.
19 changes: 11 additions & 8 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ use tokio::sync::mpsc;
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::sync::Mutex;

const SEPARATOR_LEN: usize = "\r\n".len();
const BUF_SIZE: usize = 1024 * 1;

pub struct Channel {
read_buf: [u8; BUF_SIZE], // read buffer
read_buf: Vec<u8>, // read buffer
data: Vec<u8>, // hold json data
connection: Connection,
rx: UnboundedReceiver<String>,
Expand All @@ -47,14 +44,20 @@ impl Channel {
let (_tx, _rx) = mpsc::unbounded_channel();

let addr = _connection.stream.peer_addr().unwrap();
room.peers.insert(addr, _tx);
room.peers().insert(addr, _tx);

let buf_size = room.get_prop().buf_size();

Self {
read_buf: [0; BUF_SIZE],
let mut new_channel = Self {
read_buf: Vec::new(),
data: Vec::new(),
connection: _connection,
rx: _rx,
}
};

new_channel.read_buf.resize(buf_size, 0);

new_channel
}

/// Return true when channel is local.
Expand Down
3 changes: 3 additions & 0 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ pub const PROP_FILE: &str = "./Cogru.properties";

pub const HOST: &str = "127.0.0.1";
pub const PORT: &str = "8786";

pub const SEPARATOR_LEN: usize = "\r\n".len();
pub const BUF_SIZE: usize = 1024 * 8; // Default is 8192
12 changes: 9 additions & 3 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,16 @@ impl File {
}
}

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

/// Write the content to file.
pub fn save(&mut self) {
let contents = self.contents();
let _ = std::fs::write(&self.path, contents);
}
}
15 changes: 10 additions & 5 deletions src/handler/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ pub mod save {
let file = file.unwrap();
file.save();

let contents = file.contents();
let relative_path = no_room_path(&room, &path);

room.broadcast_json(&serde_json::json!({
"method": METHOD,
"file": relative_path,
"status": "success",
}));
room.broadcast_json_except(
&serde_json::json!({
"method": METHOD,
"file": relative_path,
"contents": contents,
"status": "success",
}),
addr,
);
}
}

Expand Down
33 changes: 26 additions & 7 deletions src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ 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
clients: HashMap<SocketAddr, Client>, // Connections in this room
files: HashMap<String, File>, // files are being visited
chat: Chat, // messages in this file
prop: Properties, // server properties
password: Option<String>, // room password
path: String, // workspace path
peers: HashMap<SocketAddr, UnboundedSender<String>>, // broadcasting
clients: HashMap<SocketAddr, Client>, // Connections in this room
files: HashMap<String, File>, // files are being visited
chat: Chat, // messages in this file
}

impl Room {
Expand All @@ -60,6 +60,11 @@ impl Room {
&self.prop
}

/// Get peers
pub fn peers(&mut self) -> &mut HashMap<SocketAddr, UnboundedSender<String>> {
&mut self.peers
}

/// Return the sender.
///
/// # Arguments
Expand Down Expand Up @@ -91,6 +96,20 @@ impl Room {
}
}

/// Send JSON data to all clients.
///
/// # Arguments
///
/// * `params` - [description]
pub fn broadcast_json_except(&self, params: &Value, addr: &SocketAddr) {
for (_addr, sender) in self.peers.iter() {
if _addr == addr {
continue;
}
let _ = sender.send(params.to_string());
}
}

/// Sync files in the room
pub fn sync_files(&mut self) {
let mut builder = WalkBuilder::new(&self.path);
Expand Down
10 changes: 10 additions & 0 deletions src/server/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::constant::*;
use crate::room::*;
use java_properties::read;
use java_properties::write;
use java_properties::PropertiesIter;
Expand Down Expand Up @@ -84,4 +86,12 @@ impl Properties {
}
data.unwrap()
}

/* Customization */

/// Return property value `cogru.BufferSize`.
pub fn buf_size(&self) -> usize {
let buf_size = self.get_or_default("cogru.BufferSize", &BUF_SIZE.to_string());
buf_size.parse::<usize>().unwrap()
}
}

0 comments on commit 071f376

Please sign in to comment.