use std::hash::{DefaultHasher, Hash, Hasher}; use bincode; use riblt; // Example implementation of a struct that implements the Symbol trait #[derive(Clone, Debug, PartialEq, Eq, Hash)] struct SimpleSymbol { unique_id: u64, timestamp: u64, } impl riblt::Symbol for SimpleSymbol { // const BYTE_ARRAY_LENGTH: usize = 10; const BYTE_ARRAY_LENGTH: usize = 16; fn encode_to_bytes(&self) -> Vec { let mut buffer = vec![0u8; 16]; buffer[0..8].copy_from_slice(&self.unique_id.to_le_bytes()); buffer[8..16].copy_from_slice(&self.timestamp.to_le_bytes()); buffer } fn decode_from_bytes(bytes: &Vec) -> Self { let unique_id = u64::from_le_bytes(bytes[0..8].try_into().unwrap()); let timestamp = u64::from_le_bytes(bytes[8..16].try_into().unwrap()); SimpleSymbol { unique_id, timestamp, } } fn hash_(&self) -> u64 { let mut hasher = DefaultHasher::new(); self.unique_id.hash(&mut hasher); hasher.finish() } // fn xor(&mut self, other: &Self) -> Self { // Self { // unique_id: self.unique_id ^ other.unique_id, // timestamp: self.timestamp ^ other.timestamp, // } // } // fn empty() -> Self { // SimpleSymbol { // unique_id: 0, // timestamp: 0, // } // } } fn main() { use std::collections::HashSet; // A set of symbols we have on our local server let local_items: HashSet = HashSet::from([ SimpleSymbol { unique_id: 7, timestamp: 0}, SimpleSymbol { unique_id: 15, timestamp: 0}, SimpleSymbol { unique_id: 16, timestamp: 0}, SimpleSymbol { unique_id: 17, timestamp: 0}, //local only ]); let mut managed_local_iblt = riblt::RatelessIBLT::new(local_items); // A set of symbols on a remote server let remote_items: HashSet = HashSet::from([ SimpleSymbol { unique_id: 7, timestamp: 0}, SimpleSymbol { unique_id: 15, timestamp: 0}, SimpleSymbol { unique_id: 16, timestamp: 0}, SimpleSymbol { unique_id: 18, timestamp: 0}, //remote only ]); let mut managed_remote_iblt = riblt::RatelessIBLT::new(remote_items); let mut local_copy_of_remote : riblt::UnmanagedRatelessIBLT = riblt::UnmanagedRatelessIBLT::new(); for i in 0..20 { println!("Getting coded symbol {}", i); let one_coded_symbol = managed_remote_iblt.get_coded_symbol(i); let encoded_coded_symbol = bincode::serialize(&one_coded_symbol).unwrap(); let decoded_coded_symbol : riblt::CodedSymbol = bincode::deserialize(&encoded_coded_symbol).unwrap(); local_copy_of_remote.add_coded_symbol(&decoded_coded_symbol); let mut collapsed = managed_local_iblt.collapse(&local_copy_of_remote); let peeled_symbols = collapsed.peel_all_symbols(); if collapsed.is_empty() { println!("Peeled all symbols"); println!("{:?}", peeled_symbols); break; } } // for i in 0..10_000 { // local_items.insert(SimpleSymbol { // unique_id: i, // timestamp: 0, // }); // } // let local_coded_symbol_block = riblt::produce_block(local_items, 0); // let remote_coded_symbol_block = riblt::produce_block(remote_items, 0); // let collapsed_coded_symbol_block = // riblt::collapse(&local_coded_symbol_block, &remote_coded_symbol_block); // println!("{:?}", collapsed_coded_symbol_block); // let test_data = SimpleSymbol { unique_id: 17 }; // let test_data_encoded = test_data.encode_to_bytes(); // let test_data_decoded = SimpleSymbol::decode_from_bytes(&test_data_encoded); // println!("{:?}", test_data_decoded); // println!("{:?}", managed_local_iblt.coded_symbols); // managed_local_iblt.get_coded_symbol(1); // managed_local_iblt.get_coded_symbol(30); // managed_local_iblt.get_coded_symbol(13_000); // for cs in managed_local_iblt.coded_symbols.iter() { // println!("{:?}", cs); // } // let get_coded_symbol = managed_local_iblt.get_coded_symbol(0); // let encoded_coded_symbol = bincode::serialize(&get_coded_symbol).unwrap(); // println!("{:?}", encoded_coded_symbol); // println!("{:?}", managed_local_iblt.coded_symbols.len()); // println!("{:?}", managed_local_iblt.coded_symbols); // Expected usage, // Local server asks remote server to begin sending a stream of coded symbols // Local server collapses against its own coded symbols to see if it can be peeled to zero }