Skip to content

Commit

Permalink
Issue #262: Merge remote-tracking branch 'origin/262-customizing-syst…
Browse files Browse the repository at this point in the history
…em-info' into main
  • Loading branch information
lurenpluto committed May 26, 2023
2 parents 2bb15e1 + 4812323 commit 9eaf33f
Show file tree
Hide file tree
Showing 20 changed files with 582 additions and 174 deletions.
1 change: 1 addition & 0 deletions src/Cargo.lock

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

2 changes: 1 addition & 1 deletion src/component/cyfs-lib/src/ndn/output_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct NDNOutputRequestCommon {
// The request path of method, always used for ACL
pub req_path: Option<String>,

// The caller's source id
// The caller's source dec id
pub dec_id: Option<ObjectId>,

// The API level of NDN apis
Expand Down
2 changes: 1 addition & 1 deletion src/component/cyfs-lib/src/stack/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl SharedCyfsStack {
}

pub async fn stop(&self) {
let requestor_holder = self.requestor_holder.read().unwrap();
let requestor_holder = self.requestor_holder.read().unwrap().clone();
requestor_holder.stop().await;

self.router_handlers.stop().await;
Expand Down
15 changes: 13 additions & 2 deletions src/component/cyfs-lib/src/util/input_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::output_request::*;
use crate::{base::*, TransPublishChunkMethod};
use cyfs_base::*;
use cyfs_util::SystemInfoUpdater;

use std::path::PathBuf;

pub struct UtilInputRequestCommon {
Expand Down Expand Up @@ -77,6 +79,15 @@ pub struct UtilGetSystemInfoInputRequest {

pub type UtilGetSystemInfoInputResponse = UtilGetSystemInfoOutputResponse;

// update_system_info
pub struct UtilUpdateSystemInfoInputRequest {
pub common: UtilInputRequestCommon,

pub info: SystemInfoUpdater,
}

pub type UtilUpdateSystemInfoInputResponse = UtilUpdateSystemInfoOutputResponse;

// get_version_info
pub struct UtilGetVersionInfoInputRequest {
pub common: UtilInputRequestCommon,
Expand All @@ -88,8 +99,8 @@ pub struct UtilBuildFileInputRequest {
pub common: UtilInputRequestCommon,
pub local_path: PathBuf,
pub owner: ObjectId,
pub chunk_size: u32,
pub chunk_method: TransPublishChunkMethod,
pub chunk_size: u32,
pub chunk_method: TransPublishChunkMethod,
pub access: Option<AccessString>,
}

Expand Down
47 changes: 37 additions & 10 deletions src/component/cyfs-lib/src/util/output_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cyfs_base::*;
use cyfs_core::ZoneId;
use cyfs_core::*;
use cyfs_bdt::SnStatus;
use cyfs_util::SystemInfoUpdater;
use std::convert::TryFrom;

use serde::{Deserialize, Serialize};
Expand All @@ -13,13 +14,13 @@ use std::str::FromStr;

#[derive(Debug, Clone)]
pub struct UtilOutputRequestCommon {
// 请求路径,可为空
// The request path of method, always used for ACL
pub req_path: Option<String>,

// 来源DEC
// The caller's source dec id
pub dec_id: Option<ObjectId>,

// 用以默认行为
// The request's target device/zone
pub target: Option<ObjectId>,

pub flags: u32,
Expand Down Expand Up @@ -202,7 +203,7 @@ pub struct OODStatus {
pub ping_count: u32,
pub ping_success_count: u64,

// 当前连续失败的次数,成功后重置
// Current number of consecutive failures, reset after success
pub cont_fail_count: u64,

pub ping_avg_during: u64,
Expand Down Expand Up @@ -287,11 +288,11 @@ impl Display for UtilGetNOCInfoOutputResponse {
// 设备的一些静态信息
#[derive(Debug, Clone)]
pub struct DeviceStaticInfo {
// 当前设备id
// device_id of current cyfs-stack process(device)
pub device_id: DeviceId,
pub device: Device,

// 当前设备是不是ood
// Current cyfs-stack device is the OOD of current zone or not
pub is_ood_device: bool,

pub ood_work_mode: OODWorkMode,
Expand All @@ -301,16 +302,16 @@ pub struct DeviceStaticInfo {
pub root_state_access_mode: GlobalStateAccessMode,
pub local_cache_access_mode: GlobalStateAccessMode,

// 当前zone的主ood id
// Main ood's device_id of current zone
pub ood_device_id: DeviceId,

// 当前所属zone
// The zone of current cyfs-stack device
pub zone_id: ZoneId,

// 当前zone的owner
// The owner of current zone
pub owner_id: Option<ObjectId>,

// 当前协议栈的cyfs根目录
// The {cyfs} root dir of current cyfs-stack process
pub cyfs_root: String,

// current sn list config
Expand Down Expand Up @@ -474,6 +475,32 @@ impl Display for UtilGetSystemInfoOutputResponse {
}
}

#[derive(Debug, Clone, Default)]
pub struct UtilUpdateSystemInfoOutputRequest {
pub common: UtilOutputRequestCommon,

pub info: SystemInfoUpdater,
}

impl Display for UtilUpdateSystemInfoOutputRequest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "common: {}, info: {:?}", self.common, self.info)
}
}

impl UtilUpdateSystemInfoOutputRequest {
pub fn new(info: SystemInfoUpdater) -> Self {
Self {
common: UtilOutputRequestCommon::default(),
info,
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UtilUpdateSystemInfoOutputResponse {
}

#[derive(Debug, Clone)]
pub struct VersionInfo {
pub version: String,
Expand Down
2 changes: 2 additions & 0 deletions src/component/cyfs-lib/src/util/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub trait UtilOutputProcessor: Sync + Send + 'static {

async fn get_system_info(&self, req: UtilGetSystemInfoOutputRequest)
-> BuckyResult<UtilGetSystemInfoOutputResponse>;
async fn update_system_info(&self, req: UtilUpdateSystemInfoOutputRequest)
-> BuckyResult<UtilUpdateSystemInfoOutputResponse>;

async fn get_version_info(&self, req: UtilGetVersionInfoOutputRequest)
-> BuckyResult<UtilGetVersionInfoOutputResponse>;
Expand Down
3 changes: 3 additions & 0 deletions src/component/cyfs-lib/src/util/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub type UtilGetNetworkAccessInfoResponse = UtilGetNetworkAccessInfoOutputRespon
pub type UtilGetSystemInfoRequest = UtilGetSystemInfoOutputRequest;
pub type UtilGetSystemInfoResponse = UtilGetSystemInfoOutputResponse;

pub type UtilUpdateSystemInfoRequest = UtilUpdateSystemInfoOutputRequest;
pub type UtilUpdateSystemInfoResponse = UtilUpdateSystemInfoOutputResponse;

pub type UtilGetVersionInfoRequest = UtilGetVersionInfoOutputRequest;
pub type UtilGetVersionInfoResponse = UtilGetVersionInfoOutputResponse;

Expand Down
106 changes: 75 additions & 31 deletions src/component/cyfs-lib/src/util/requestor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use super::request::*;
use super::processor::*;
use crate::{base::*, requestor::*, SharedObjectStackDecID, UtilBuildDirFromObjectMapOutputRequest, UtilBuildDirFromObjectMapOutputResponse, UtilBuildFileOutputRequest, UtilBuildFileOutputResponse};
use super::request::*;
use crate::{
base::*, requestor::*, SharedObjectStackDecID, UtilBuildDirFromObjectMapOutputRequest,
UtilBuildDirFromObjectMapOutputResponse, UtilBuildFileOutputRequest,
UtilBuildFileOutputResponse,
};
use cyfs_base::*;

use cyfs_core::{Zone, ZoneId};
Expand Down Expand Up @@ -45,7 +49,11 @@ impl UtilRequestor {
}
}

RequestorHelper::encode_opt_header_with_encoding(http_req, cyfs_base::CYFS_REQ_PATH, com_req.req_path.as_deref());
RequestorHelper::encode_opt_header_with_encoding(
http_req,
cyfs_base::CYFS_REQ_PATH,
com_req.req_path.as_deref(),
);

if let Some(target) = &com_req.target {
http_req.insert_header(cyfs_base::CYFS_TARGET, target.to_string());
Expand All @@ -62,9 +70,7 @@ impl UtilRequestor {
http_req
}

async fn decode_get_device_response(
mut resp: Response,
) -> BuckyResult<UtilGetDeviceResponse> {
async fn decode_get_device_response(mut resp: Response) -> BuckyResult<UtilGetDeviceResponse> {
let buf = resp.body_bytes().await.map_err(|e| {
let msg = format!("get_current_device failed, read body bytes error! {}", e);
error!("{}", msg);
Expand Down Expand Up @@ -107,16 +113,18 @@ impl UtilRequestor {
let mut http_req = Request::new(Method::Post, url);
self.encode_common_headers(&req.common, &mut http_req);

RequestorHelper::encode_opt_header(&mut http_req, cyfs_base::CYFS_OBJECT_ID, &req.object_id);
RequestorHelper::encode_opt_header(
&mut http_req,
cyfs_base::CYFS_OBJECT_ID,
&req.object_id,
);
if let Some(object_raw) = req.object_raw {
http_req.set_body(object_raw);
}

http_req
}
async fn decode_get_zone_response(
mut resp: Response,
) -> BuckyResult<UtilGetZoneResponse> {
async fn decode_get_zone_response(mut resp: Response) -> BuckyResult<UtilGetZoneResponse> {
let zone: Zone = RequestorHelper::decode_raw_object_body(&mut resp).await?;
let zone_id: ZoneId = RequestorHelper::decode_header(&resp, cyfs_base::CYFS_ZONE_ID)?;

Expand All @@ -136,10 +144,7 @@ impl UtilRequestor {
// 根据device/people/simplegroup查询所在的zone
// 如果已知object的内容,那么可以附带,加速non-stack的查询
// xxx/util/zone[/object_id]
pub async fn get_zone(
&self,
req: UtilGetZoneRequest,
) -> BuckyResult<UtilGetZoneResponse> {
pub async fn get_zone(&self, req: UtilGetZoneRequest) -> BuckyResult<UtilGetZoneResponse> {
let http_req = self.encode_get_zone_request(req);

let mut resp = self.requestor.request(http_req).await?;
Expand Down Expand Up @@ -333,6 +338,7 @@ impl UtilRequestor {
}
}

// get_system_info
fn encode_get_system_info_request(&self, req: UtilGetSystemInfoRequest) -> Request {
let url = self.service_url.join("system_info").unwrap();
let mut http_req = Request::new(Method::Get, url);
Expand Down Expand Up @@ -369,6 +375,41 @@ impl UtilRequestor {
}
}

// update_system_info
fn encode_update_system_info_request(&self, req: UtilUpdateSystemInfoRequest) -> Request {
let url = self.service_url.join("system_info").unwrap();
let mut http_req = Request::new(Method::Post, url);
self.encode_common_headers(&req.common, &mut http_req);

let body: String = serde_json::to_string(&req.info).unwrap();
http_req.set_body(body);
http_req.set_content_type(::tide::http::mime::JSON);

http_req
}

pub async fn update_system_info(
&self,
req: UtilUpdateSystemInfoRequest,
) -> BuckyResult<UtilUpdateSystemInfoResponse> {
let http_req = self.encode_update_system_info_request(req);

let mut resp = self.requestor.request(http_req).await?;

if resp.status().is_success() {
Ok(UtilUpdateSystemInfoResponse {})
} else {
let e = RequestorHelper::error_from_resp(&mut resp).await;
error!(
"util update_system_info failed: status={}, {}",
resp.status(),
e
);

Err(e)
}
}

fn encode_get_version_info_request(&self, req: UtilGetVersionInfoRequest) -> Request {
let url = self.service_url.join("version_info").unwrap();
let mut http_req = Request::new(Method::Get, url);
Expand Down Expand Up @@ -409,7 +450,7 @@ impl UtilRequestor {

pub async fn build_file_object(
&self,
req: UtilBuildFileOutputRequest
req: UtilBuildFileOutputRequest,
) -> BuckyResult<UtilBuildFileOutputResponse> {
let url = self.service_url.join("build_file").unwrap();
let mut http_req = Request::new(Method::Post, url);
Expand Down Expand Up @@ -442,7 +483,7 @@ impl UtilRequestor {

pub async fn build_dir_from_object_map(
&self,
req: UtilBuildDirFromObjectMapOutputRequest
req: UtilBuildDirFromObjectMapOutputRequest,
) -> BuckyResult<UtilBuildDirFromObjectMapOutputResponse> {
let url = self.service_url.join("build_dir_from_object_map").unwrap();
let mut http_req = Request::new(Method::Post, url);
Expand Down Expand Up @@ -476,24 +517,15 @@ impl UtilRequestor {

#[async_trait::async_trait]
impl UtilOutputProcessor for UtilRequestor {
async fn get_device(
&self,
req: UtilGetDeviceRequest,
) -> BuckyResult<UtilGetDeviceResponse> {
async fn get_device(&self, req: UtilGetDeviceRequest) -> BuckyResult<UtilGetDeviceResponse> {
Self::get_device(&self, req).await
}

async fn get_zone(
&self,
req: UtilGetZoneRequest,
) -> BuckyResult<UtilGetZoneResponse> {
async fn get_zone(&self, req: UtilGetZoneRequest) -> BuckyResult<UtilGetZoneResponse> {
Self::get_zone(&self, req).await
}

async fn resolve_ood(
&self,
req: UtilResolveOODRequest,
) -> BuckyResult<UtilResolveOODResponse> {
async fn resolve_ood(&self, req: UtilResolveOODRequest) -> BuckyResult<UtilResolveOODResponse> {
Self::resolve_ood(&self, req).await
}

Expand Down Expand Up @@ -532,19 +564,31 @@ impl UtilOutputProcessor for UtilRequestor {
Self::get_system_info(&self, req).await
}

async fn update_system_info(
&self,
req: UtilUpdateSystemInfoRequest,
) -> BuckyResult<UtilUpdateSystemInfoResponse> {
Self::update_system_info(&self, req).await
}

async fn get_version_info(
&self,
req: UtilGetVersionInfoRequest,
) -> BuckyResult<UtilGetVersionInfoResponse> {
Self::get_version_info(&self, req).await
}

async fn build_file_object(&self, req: UtilBuildFileOutputRequest) -> BuckyResult<UtilBuildFileOutputResponse> {
async fn build_file_object(
&self,
req: UtilBuildFileOutputRequest,
) -> BuckyResult<UtilBuildFileOutputResponse> {
Self::build_file_object(self, req).await
}

async fn build_dir_from_object_map(&self, req: UtilBuildDirFromObjectMapOutputRequest)
-> BuckyResult<UtilBuildDirFromObjectMapOutputResponse> {
async fn build_dir_from_object_map(
&self,
req: UtilBuildDirFromObjectMapOutputRequest,
) -> BuckyResult<UtilBuildDirFromObjectMapOutputResponse> {
Self::build_dir_from_object_map(self, req).await
}
}
2 changes: 2 additions & 0 deletions src/component/cyfs-stack/src/util/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub trait UtilInputProcessor: Sync + Send + 'static {

async fn get_system_info(&self, req: UtilGetSystemInfoInputRequest)
-> BuckyResult<UtilGetSystemInfoInputResponse>;
async fn update_system_info(&self, req: UtilUpdateSystemInfoInputRequest)
-> BuckyResult<UtilUpdateSystemInfoInputResponse>;

async fn get_version_info(&self, req: UtilGetVersionInfoInputRequest)
-> BuckyResult<UtilGetVersionInfoInputResponse>;
Expand Down
Loading

0 comments on commit 9eaf33f

Please sign in to comment.