Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/69-stack-supports-multiple-and-d…
Browse files Browse the repository at this point in the history
…ynamic-snpn-configuration' into main
  • Loading branch information
lurenpluto committed Nov 25, 2022
2 parents ca9f118 + fac8819 commit 0d671e5
Show file tree
Hide file tree
Showing 31 changed files with 1,255 additions and 199 deletions.
27 changes: 27 additions & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ members = [
"./tools/bdt-debuger",
"./tools/bdt-debuger-deamon",
"./tools/cyfs-check",
"./tools/sn-updater",

"./meta/browser-meta-spv",
"./meta/cyfs-meta",
Expand Down
4 changes: 4 additions & 0 deletions src/component/cyfs-base/src/base/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ pub const PROXY_MINER_NAME: &str = "proxy-miner";
pub const CACHE_MINER_NAME: &str = "cache-miner";
pub const SN_MINER_NAME: &str = "sn-miner";
pub const IP_RELAY_MINER_NAME: &str = "ip-relay-miner";


//// meta chain names
pub const CYFS_SN_NAME: &str = "cyfs-sn";
3 changes: 3 additions & 0 deletions src/component/cyfs-base/src/base/paths.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Every app has one
pub const CYFS_GLOBAL_STATE_META_PATH: &str = "/.cyfs/meta";

// Config path, the system-dec must has one
pub const CYFS_GLOBAL_STATE_CONFIG_PATH: &str = "/.cyfs/config";

// Friends, in system dec's global state
pub const CYFS_FRIENDS_PATH: &str = "/user/friends";
pub const CYFS_FRIENDS_LIST_PATH: &str = "/user/friends/list";
Expand Down
2 changes: 1 addition & 1 deletion src/component/cyfs-lib/src/base/base_requestor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl HttpRequestor for BdtHttpRequestor {
// 如果device对象里面没有指定sn_list,那么使用默认值
let mut sn_list = self.device.connect_info().sn_list().clone();
if sn_list.is_empty() {
sn_list = vec![cyfs_util::get_default_sn_desc().desc().device_id()];
sn_list = cyfs_util::get_default_sn_desc_id_list();
}

let begin = std::time::Instant::now();
Expand Down
95 changes: 13 additions & 82 deletions src/component/cyfs-stack-loader/src/known_objects.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use cyfs_base::*;
use cyfs_debug::Mutex;
use cyfs_stack::KnownObject;
use cyfs_util::DirObjectsSyncLoader;

use async_std::fs;
use async_std::prelude::*;
use lazy_static::lazy_static;
use std::path::{Path, PathBuf};
use std::sync::Arc;

struct KnownObjectsLoader {
roots: Vec<PathBuf>,
desc_folder: PathBuf,
objects: Vec<KnownObject>,
}

Expand All @@ -18,67 +17,25 @@ impl KnownObjectsLoader {
let desc_folder = cyfs_util::get_cyfs_root_path().join("etc").join("desc");

Self {
roots: vec![desc_folder],
desc_folder,
objects: Vec::new(),
}
}

pub async fn load(&mut self) {
self.scan().await;
}

async fn scan(&mut self) {
let mut i = 0;
loop {
if i >= self.roots.len() {
break;
}

let root = self.roots[i].clone();
let _ = self.scan_root(&root).await;

i += 1;
}
}

async fn scan_root(&mut self, root: &Path) -> BuckyResult<()> {
if !root.is_dir() {
return Ok(());
}
let mut loader = DirObjectsSyncLoader::new(self.desc_folder.clone());
loader.load();

let mut entries = fs::read_dir(root).await.map_err(|e| {
error!(
"read known object dir failed! dir={}, {}",
root.display(),
e
);
e
})?;

while let Some(res) = entries.next().await {
let entry = res.map_err(|e| {
error!("read entry error: {}", e);
e
})?;

let file_path = root.join(entry.file_name());
if file_path.is_dir() {
self.roots.push(file_path);
let objects = loader.into_objects();
for (file_path, data) in objects {
let ret = self.load_obj(&file_path, data).await;
if ret.is_err() {
continue;
}

if !file_path.is_file() {
warn!("path is not file: {}", file_path.display());
continue;
}
let ret = ret.unwrap();

if !Self::is_desc_file(&file_path) {
debug!("not desc file: {}", file_path.display());
continue;
}

if let Ok(ret) = self.load_obj(&file_path).await {
if !self
if !self
.objects
.iter()
.any(|item| item.object_id == ret.object_id)
Expand All @@ -91,36 +48,10 @@ impl KnownObjectsLoader {
file_path.display()
);
}
}
}

Ok(())
}

fn is_desc_file(file_path: &Path) -> bool {
match file_path.extension() {
Some(ext) => {
let ext = ext.to_string_lossy();

#[cfg(windows)]
let ext = ext.to_lowercase();

if ext == "desc" {
true
} else {
false
}
}
None => false,
}
}

async fn load_obj(&self, file: &Path) -> BuckyResult<KnownObject> {
let buf = fs::read(file).await.map_err(|e| {
error!("load known object failed! file={}, {}", file.display(), e);
e
})?;


async fn load_obj(&self, file: &Path, buf: Vec<u8>) -> BuckyResult<KnownObject> {
let (object, _) = AnyNamedObject::raw_decode(&buf).map_err(|e| {
let msg = format!(
"invalid known object body buffer: file={}, {}",
Expand Down
18 changes: 8 additions & 10 deletions src/component/cyfs-stack-loader/src/stack_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,15 @@ impl StackInfo {
// should not change the device's inner sn_list and pn_list
info!("current device: {}", device_info.device.format_json());

let sn = cyfs_util::get_default_sn_desc();
let sn_id = sn.desc().device_id();
info!("default sn: {}", sn_id);

init_sn_peers.push(sn);

if let Some(pn) = cyfs_util::get_pn_desc() {
let pn_id = pn.desc().device_id();
info!("default pn: {}", pn_id);
// only use the sn in local config dir
for (id, sn) in cyfs_util::get_local_sn_desc() {
info!("will use sn: {}", id);
init_sn_peers.push(sn.to_owned());
}

init_pn_peers.push(pn);
for (id, pn) in cyfs_util::get_local_pn_desc() {
info!("will use pn: {}", id);
init_pn_peers.push(pn.to_owned());
}

let init_known_peers = cyfs_util::get_default_known_peers();
Expand Down
4 changes: 3 additions & 1 deletion src/component/cyfs-stack/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod global_config;
mod sn_config;

pub use global_config::*;
pub use global_config::*;
pub use sn_config::*;
Loading

0 comments on commit 0d671e5

Please sign in to comment.