Skip to content

Commit

Permalink
Fixed file sending bug
Browse files Browse the repository at this point in the history
Large files were not saved due to the chunk index always being 0.
  • Loading branch information
MathJud committed Sep 30, 2022
1 parent e2ee87e commit f30f9ac
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion rust/clients/cli/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl Chat {
proto::MessageStatus::Sent => print!("✓. | "),
proto::MessageStatus::Confirmed => print!("✓✓ | "),
proto::MessageStatus::ConfirmedByAll => print!("✓✓✓| "),
proto::MessageStatus::Receiving => print!("📨 | "),
proto::MessageStatus::Receiving => print!("🚚 | "),
proto::MessageStatus::Received => print!("📨 | "),
}

Expand Down
38 changes: 25 additions & 13 deletions rust/libqaul/src/services/chat/file.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright (c) 2022 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! # Qaul File Sharing Service
//! # Chat File Transfer
//!
//! The File sharing service sends and receives file messages into the network.
//! The File messages carry on the Messaging service
//! Messaging(FileMessage(ChatFileContainer(FileInfo, FileData, Confirmation)))
//! Sending files via the chat messenger to other users and groups.
//! The chat file messages use the messaging service.

use libp2p::PeerId;
use prost::Message;
Expand Down Expand Up @@ -133,7 +132,7 @@ impl UserFiles {
/// (first_key, last_key)
fn get_chunk_key_range(file_id: &Vec<u8>) -> (Vec<u8>, Vec<u8>) {
let first_key = Self::get_chunk_key(file_id, 0);
let last_key = Self::get_chunk_key(file_id, 0xFFFFFFFF);
let last_key = Self::get_chunk_key(file_id, u32::MAX);
(first_key, last_key)
}

Expand All @@ -142,6 +141,8 @@ impl UserFiles {
// get chunk key
let key = Self::get_chunk_key(&file_id.to_be_bytes().to_vec(), index);

log::trace!("save file chunk {} with key: {:?}", index, key);

// save file chunk into data base
if let Err(e) = self.file_chunks.insert(key, data) {
log::error!("Error saving file chunk to data base: {}", e);
Expand All @@ -155,6 +156,8 @@ impl UserFiles {
}

/// count file chunks
///
/// Count how many chunks of a file we already have in the data base
pub fn count_file_chunks(&self, file_id: &Vec<u8>) -> usize {
// get key range
let (first_key, last_key) = Self::get_chunk_key_range(file_id);
Expand All @@ -180,16 +183,17 @@ impl UserFiles {
/// File State
#[derive(Serialize, Deserialize, Clone)]
pub enum FileState {
/// We are in the process of sending this file
Sending,
/// File has been sent to another
/// File has been sent to another user
Sent,
/// File reception has been confirmed
Confirmed,
/// Confirmed by all recipients
ConfirmedByAll,
/// Receiving
Receiving,
/// File successfully
/// File successfully received
Received,
}

Expand Down Expand Up @@ -241,11 +245,10 @@ impl FileHistory {
/// the function returns a boolean that indicates, whether the user finished receiving
/// {user completed}
pub fn reception_confirmed(&mut self, receiver_id: PeerId) -> bool {
// set received state
let key = receiver_id.to_bytes();
if let Some(tracking) = self.reception_tracking.get_mut(&key) {
tracking.package_count = tracking.package_count + 1;
log::debug!("package_count {}", tracking.package_count);
log::trace!("package_count {}", tracking.package_count);

// check if user has received all messages
if tracking.package_count >= self.message_count {
Expand Down Expand Up @@ -606,6 +609,7 @@ impl ChatFile {
// read file contents and create and send FileData messages
let mut buffer: [u8; DEF_PACKAGE_SIZE as usize] = [0; DEF_PACKAGE_SIZE as usize];
let mut left_size = size;
let mut chunk_index: u32 = 0;

while left_size > 0 {
let mut read_size = left_size;
Expand All @@ -623,7 +627,7 @@ impl ChatFile {
message: Some(proto_net::chat_file_container::Message::FileData(
proto_net::ChatFileData {
file_id,
start_index: 0,
start_index: chunk_index,
message_count: mesage_count,
data: buffer[0..(read_size as usize)].iter().cloned().collect(),
},
Expand All @@ -638,6 +642,8 @@ impl ChatFile {
timestamp,
data.encode_to_vec(),
);

chunk_index = chunk_index + 1;
}

// set file status to sent
Expand Down Expand Up @@ -759,13 +765,18 @@ impl ChatFile {
// check how many chunks have been downloaded
let count = user_files.count_file_chunks(&file_history.file_id.to_be_bytes().to_vec());

log::trace!(
"received {} chunks of {}",
count + 1,
file_history.message_count
);

// if we downloaded all chunks, save it to file if we received the file info
if (count + 1) as u32 == file_history.message_count {
log::trace!("store_file");

Self::store_file(user_account, user_files, file_history);
}

// TODO
// reference it in chat
}

/// Store a completely downloaded file
Expand Down Expand Up @@ -831,6 +842,7 @@ impl ChatFile {
Self::try_store_file(user_account, user_files, file_history);
}
None => {
log::warn!("haven't received file info message yet");
// TODO: create stub file history file
/*
// create a file history stub if nothing was found
Expand Down
21 changes: 9 additions & 12 deletions rust/libqaul/src/services/messaging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Messaging {
user_account: UserAccount,
confirmation: proto::Confirmation,
) {
log::trace!("messgae confirmed");
log::trace!("message confirmed");

let unconfirmed = UNCONFIRMED.get().write().unwrap();

Expand All @@ -199,25 +199,25 @@ impl Messaging {
// check message and decide what to do
match unconfirmed.message_type {
MessagingServiceType::Unconfirmed => {
log::debug!("Confirmation: Unconfirmed");
log::trace!("Confirmation: Unconfirmed");
}
MessagingServiceType::DtnOrigin => {
log::debug!("Confirmation: DtnOrigin");
log::trace!("Confirmation: DtnOrigin");
// what kind of message do we have here?
// TODO: check chat storage as sent ...
}
MessagingServiceType::DtnStored => {
log::debug!("Confirmation: DtnStored");
log::trace!("Confirmation: DtnStored");
}
MessagingServiceType::Crypto => {
log::debug!("Confirmation: Crypto");
log::trace!("Confirmation: Crypto");
}
MessagingServiceType::Group => {
log::debug!("Confirmation: Group");
log::trace!("Confirmation: Group");
// don't do anything for group messages
}
MessagingServiceType::Chat => {
log::debug!("Confirmation: Chat");
log::trace!("Confirmation: Chat");
// set received info in chat data base
ChatStorage::update_confirmation(
user_account.id,
Expand Down Expand Up @@ -246,7 +246,7 @@ impl Messaging {
}
}
MessagingServiceType::Rtc => {
log::debug!("Confirmation: Rtc");
log::trace!("Confirmation: Rtc");
// TODO CONFIRM RTC MESSAGE
}
}
Expand Down Expand Up @@ -317,7 +317,7 @@ impl Messaging {
message_id: &Vec<u8>,
is_common_message: bool,
) -> Result<Vec<u8>, String> {
log::debug!("pack_and_send_message to {}", receiver.to_base58());
log::trace!("pack_and_send_message to {}", receiver.to_base58());

// encrypt data
let encrypted_message: proto::Encrypted;
Expand Down Expand Up @@ -349,9 +349,6 @@ impl Messaging {
payload: envelop_payload.encode_to_vec(),
};

// debug
log::trace!("envelope len: {}", envelope.encoded_len());

// encode envelope
let mut envelope_buf = Vec::with_capacity(envelope.encoded_len());
envelope
Expand Down
2 changes: 1 addition & 1 deletion rust/libqaul/src/services/messaging/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl MessagingProcess {
data: &Vec<u8>,
signature: &Vec<u8>,
) {
log::debug!("on_decrypted_message arrived");
log::trace!("on_decrypted_message arrived");

// decode messaging
let messaging;
Expand Down

0 comments on commit f30f9ac

Please sign in to comment.