use lox_library::bridge_table::{BridgeLine, BRIDGE_BYTES, MAX_BRIDGES_PER_BUCKET}; use rdsys_backend::proto::Resource; // Parse each resource from rdsys into a Bridgeline as expected by the Lox Bridgetable pub fn parse_into_bridgelines(resources: Vec) -> Vec { let mut bridgelines: Vec = Vec::new(); for resource in resources { let mut ip_bytes: [u8; 16] = [0; 16]; ip_bytes[..resource.address.len()].copy_from_slice(resource.address.as_bytes()); let resource_uid = resource .get_uid() .expect("Unable to get Fingerprint UID of resource"); let infostr: String = format!( "type={} blocked_in={:?} protocol={} fingerprint={:?} or_addresses={:?} distribution={} flags={:?} params={:?}", resource.r#type, resource.blocked_in, resource.protocol, resource.fingerprint, resource.or_addresses, resource.distribution, resource.flags, resource.params, ); let mut info_bytes: [u8; BRIDGE_BYTES - 26] = [0; BRIDGE_BYTES - 26]; info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes()); bridgelines.push(BridgeLine { addr: ip_bytes, port: resource.port, uid_fingerprint: resource_uid, info: info_bytes, }) } bridgelines } // Allocate each Bridgeline into a bucket that will later be allocated into spare buckets or open invitation buckets // Any leftover buckets from total_bridgelines % MAX_BRIDGES_PER_BUCKET are returned in a separate Vec // TODO: Improve this function to sort bridgelines into buckets in a more intentional manner. This could include // sorting bridgelines with high bandwidth into buckets that are only distributed to more trusted users or sorting // bridgelines by location pub fn parse_into_buckets( mut bridgelines: Vec, ) -> (Vec<[BridgeLine; MAX_BRIDGES_PER_BUCKET]>, Vec) { let mut buckets: Vec<[BridgeLine; MAX_BRIDGES_PER_BUCKET]> = Vec::new(); let mut count = 0; let mut bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET]; let mut leftovers: Vec = Vec::new(); for bridgeline in bridgelines.clone() { println!("What is the bridgeline: {:?}", bridgeline); if count < MAX_BRIDGES_PER_BUCKET { bucket[count] = bridgeline; count += 1; } else { buckets.push(bucket); count = 0; bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET]; } } // Handle the extra buckets that were not allocated already if count != 0 { for _ in 0..count { // Assumes that the unallocated bridgelines will be the last x of the passed bridgelines leftovers.push(bridgelines.pop().unwrap()); } } (buckets, leftovers) }