Cleanup and add syncing of resources

This commit is contained in:
onyinyang 2023-09-13 12:08:48 -04:00
parent d4c54e969c
commit be0d026fe8
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
5 changed files with 89 additions and 74 deletions

View File

@ -25,6 +25,19 @@ pub struct LoxServerContext {
} }
impl LoxServerContext { impl LoxServerContext {
pub fn bridgetable_is_empty(&self) -> bool {
let mut ba_obj = self.ba.lock().unwrap();
ba_obj.is_empty()
}
// Populate an empty bridgetable for the first time
/* pub fn populate_bridgetable(&self, bridgelines: Vec<BridgeLine>, percent_spares: Option<usize>) {
if Some(percent_spares) {
let partition: usize = bridgelines.len()*percent_spares/100;
}
} */
pub fn append_extra_bridges(&self, bridge: BridgeLine) { pub fn append_extra_bridges(&self, bridge: BridgeLine) {
let mut extra_bridges = self.extra_bridges.lock().unwrap(); let mut extra_bridges = self.extra_bridges.lock().unwrap();
extra_bridges.push(bridge); extra_bridges.push(bridge);

View File

@ -1,7 +1,5 @@
use chrono::Utc;
use clap::Parser; use clap::Parser;
use futures::future; use futures::future;
use futures::StreamExt;
use hyper::{ use hyper::{
server::conn::AddrStream, server::conn::AddrStream,
service::{make_service_fn, service_fn}, service::{make_service_fn, service_fn},
@ -9,7 +7,7 @@ use hyper::{
}; };
use lox_library::bridge_table::{BridgeLine, MAX_BRIDGES_PER_BUCKET}; use lox_library::bridge_table::{BridgeLine, MAX_BRIDGES_PER_BUCKET};
use rdsys_backend::{proto::Resource, proto::ResourceDiff, request_resources, start_stream}; use rdsys_backend::{proto::Resource, request_resources};
use serde::Deserialize; use serde::Deserialize;
use std::{ use std::{
@ -22,7 +20,7 @@ mod lox_context;
mod request_handler; mod request_handler;
use request_handler::handle; use request_handler::handle;
mod resource_parser; mod resource_parser;
use resource_parser::parse_resource; use resource_parser::parse_resources;
use tokio::{ use tokio::{
signal, spawn, signal, spawn,
@ -171,8 +169,15 @@ async fn context_manager(
Rdsys { resources } => { Rdsys { resources } => {
let mut count = 0; let mut count = 0;
let mut bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET]; let mut bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET];
for resource in resources { if context.bridgetable_is_empty() {
let bridgeline = parse_resource(resource); // otherwise, for each resource, check if the resource fingerprint is failing tests, if it is check for how long
// check if the resource is already in the Lox bridgetable
// if it is, it's probably fine to remove or replace the existing resource with the incoming one
// to account for changes unless we want to track the number of changes on the lox side?
// that should be sufficient to keep it in sync
let bridgelines = parse_resources(resources);
for bridgeline in bridgelines {
//context.populate_bridgetable(bridgelines. None);
println!("What is the bridgeline: {:?}", bridgeline); println!("What is the bridgeline: {:?}", bridgeline);
if context.to_be_replaced_bridges.lock().unwrap().len() > 0 { if context.to_be_replaced_bridges.lock().unwrap().len() > 0 {
println!("BridgeLine to be replaced: {:?}", bridgeline); println!("BridgeLine to be replaced: {:?}", bridgeline);
@ -211,7 +216,9 @@ async fn context_manager(
// Handle the extra buckets that were not allocated already // Handle the extra buckets that were not allocated already
if count != 0 { if count != 0 {
for val in 0..count { for val in 0..count {
if context.extra_bridges.lock().unwrap().len() < (MAX_BRIDGES_PER_BUCKET) { if context.extra_bridges.lock().unwrap().len()
< (MAX_BRIDGES_PER_BUCKET)
{
context.append_extra_bridges(bucket[val]); context.append_extra_bridges(bucket[val]);
} else { } else {
bucket = context.remove_extra_bridges(); bucket = context.remove_extra_bridges();
@ -219,6 +226,7 @@ async fn context_manager(
} }
} }
} }
}
/* /*
let bridgeline = parse_resource(resource); let bridgeline = parse_resource(resource);
println!("BridgeLine to be changed: {:?}", bridgeline); println!("BridgeLine to be changed: {:?}", bridgeline);

View File

@ -1,7 +1,9 @@
use lox_library::bridge_table::{BridgeLine, BRIDGE_BYTES}; use lox_library::bridge_table::{BridgeLine, BRIDGE_BYTES};
use rdsys_backend::proto::Resource; use rdsys_backend::proto::Resource;
pub fn parse_resource(resource: Resource) -> BridgeLine { pub fn parse_resources(resources: Vec<Resource>) -> Vec<BridgeLine> {
let mut bridgelines: Vec<BridgeLine> = Vec::new();
for resource in resources {
let mut ip_bytes: [u8; 16] = [0; 16]; let mut ip_bytes: [u8; 16] = [0; 16];
ip_bytes[..resource.address.len()].copy_from_slice(resource.address.as_bytes()); ip_bytes[..resource.address.len()].copy_from_slice(resource.address.as_bytes());
let resource_uid = resource let resource_uid = resource
@ -21,10 +23,12 @@ pub fn parse_resource(resource: Resource) -> BridgeLine {
let mut info_bytes: [u8; BRIDGE_BYTES - 26] = [0; BRIDGE_BYTES - 26]; let mut info_bytes: [u8; BRIDGE_BYTES - 26] = [0; BRIDGE_BYTES - 26];
info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes()); info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes());
BridgeLine { bridgelines.push(BridgeLine {
addr: ip_bytes, addr: ip_bytes,
port: resource.port, port: resource.port,
uid_fingerprint: resource_uid, uid_fingerprint: resource_uid,
info: info_bytes, info: info_bytes,
})
} }
bridgelines
} }

View File

@ -330,6 +330,10 @@ impl BridgeAuth {
} }
} }
pub fn is_empty(&mut self) -> bool {
self.bridge_table.buckets.is_empty()
}
/// Insert a set of open invitation bridges. /// Insert a set of open invitation bridges.
/// ///
/// Each of the bridges will be given its own open invitation /// Each of the bridges will be given its own open invitation
@ -378,7 +382,6 @@ impl BridgeAuth {
// TODO Ensure synchronization of Lox bridge_table with rdsys // TODO Ensure synchronization of Lox bridge_table with rdsys
pub fn sync_table(&mut self) { pub fn sync_table(&mut self) {
// Create a hashtable (?) of bridges in the lox distributor from new resources // Create a hashtable (?) of bridges in the lox distributor from new resources
// accept the hashtable and recreate the bridge table from the hash table here // accept the hashtable and recreate the bridge table from the hash table here
// using existing reachable bridges, other table checks and placements from existing bridge table // using existing reachable bridges, other table checks and placements from existing bridge table

View File

@ -40,17 +40,6 @@ impl From<io::Error> for Error {
} }
} }
pub struct StaticResourceRequest {}
impl StaticResourceRequest {
pub fn new(rx: mpsc::Receiver<Bytes>) -> StaticResourceRequest {
StaticResourceRequest {
}
}
}
/// An iterable wrapper of ResourceDiff items for the streamed chunks of Bytes /// An iterable wrapper of ResourceDiff items for the streamed chunks of Bytes
/// received from the connection to the rdsys backend /// received from the connection to the rdsys backend
pub struct ResourceStream { pub struct ResourceStream {
@ -295,7 +284,6 @@ pub async fn request_resources( api_endpoint: String,
.body(json) .body(json)
.send() .send()
.await.unwrap(); .await.unwrap();
println!("Success? {:?}", response);
match response.status() { match response.status() {
reqwest::StatusCode::OK => { reqwest::StatusCode::OK => {
fetched_resources = match dbg!(response.json::<Vec<proto::Resource>>().await) { fetched_resources = match dbg!(response.json::<Vec<proto::Resource>>().await) {
@ -307,6 +295,5 @@ pub async fn request_resources( api_endpoint: String,
fetched_resources = Err(Error::String(other)) fetched_resources = Err(Error::String(other))
} }
}; };
println!("Resources: {:?}", fetched_resources);
fetched_resources fetched_resources
} }