Skip to content

Commit

Permalink
Combine the GroupInputRequestCommon with the request parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
streetycat committed May 18, 2023
1 parent 533e8e7 commit c2bfcaf
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 123 deletions.
7 changes: 7 additions & 0 deletions src/component/cyfs-group-lib/src/input_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;

use cyfs_base::ObjectId;
use cyfs_core::GroupProposal;
use cyfs_lib::{NONObjectInfo, RequestSourceInfo};

#[derive(Clone, Debug)]
Expand All @@ -18,12 +19,18 @@ impl fmt::Display for GroupInputRequestCommon {
}

pub struct GroupStartServiceInputRequest {
pub common: GroupInputRequestCommon,
pub group_id: ObjectId,
pub rpath: String,
}

pub struct GroupStartServiceInputResponse {}

pub struct GroupPushProposalInputRequest {
pub common: GroupInputRequestCommon,
pub proposal: GroupProposal,
}

pub struct GroupPushProposalInputResponse {
pub object: Option<NONObjectInfo>,
}
20 changes: 18 additions & 2 deletions src/component/cyfs-group-lib/src/output_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;
use std::fmt::{self, Debug};

use cyfs_base::{DeviceId, ObjectId};
use cyfs_base::{NamedObject, ObjectDesc, ObjectId};
use cyfs_core::GroupProposal;
use cyfs_lib::NONObjectInfo;

#[derive(Clone, Debug)]
Expand All @@ -27,12 +28,27 @@ impl fmt::Display for GroupOutputRequestCommon {

#[derive(Debug)]
pub struct GroupStartServiceOutputRequest {
pub common: GroupOutputRequestCommon,
pub group_id: ObjectId,
pub rpath: String,
}

pub struct GroupStartServiceOutputResponse {}

pub struct GroupPushProposalOutputRequest {
pub common: GroupOutputRequestCommon,
pub proposal: GroupProposal,
}

impl Debug for GroupPushProposalOutputRequest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GroupPushProposalOutputRequest")
.field("common", &self.common)
.field("proposal", &self.proposal.desc().object_id())
.finish()
}
}

pub struct GroupPushProposalOutputResponse {
pub object: Option<NONObjectInfo>,
}
8 changes: 3 additions & 5 deletions src/component/cyfs-group-lib/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ use cyfs_base::BuckyResult;
use cyfs_core::GroupProposal;

use crate::{
GroupOutputRequestCommon, GroupPushProposalOutputResponse, GroupStartServiceOutputRequest,
GroupStartServiceOutputResponse,
GroupOutputRequestCommon, GroupPushProposalOutputRequest, GroupPushProposalOutputResponse,
GroupStartServiceOutputRequest, GroupStartServiceOutputResponse,
};

#[async_trait::async_trait]
pub trait GroupOutputProcessor: Send + Sync {
async fn start_service(
&self,
req_common: GroupOutputRequestCommon,
req: GroupStartServiceOutputRequest,
) -> BuckyResult<GroupStartServiceOutputResponse>;
async fn push_proposal(
&self,
req_common: GroupOutputRequestCommon,
req: GroupProposal,
req: GroupPushProposalOutputRequest,
) -> BuckyResult<GroupPushProposalOutputResponse>;
}

Expand Down
39 changes: 38 additions & 1 deletion src/component/cyfs-group-lib/src/request_codec.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
use cyfs_base::{BuckyResult, JsonCodec, JsonCodecHelper};
use serde_json::{Map, Value};

use crate::{output_request::GroupStartServiceOutputRequest, GroupStartServiceInputRequest};
use crate::{
output_request::GroupStartServiceOutputRequest, GroupInputRequestCommon,
GroupOutputRequestCommon, GroupStartServiceInputRequest,
};

impl JsonCodec<GroupOutputRequestCommon> for GroupOutputRequestCommon {
fn encode_json(&self) -> Map<String, Value> {
let mut obj = Map::new();
JsonCodecHelper::encode_option_string_field(&mut obj, "dec-id", self.dec_id.as_ref());

obj
}

fn decode_json(obj: &Map<String, Value>) -> BuckyResult<Self> {
Ok(Self {
dec_id: JsonCodecHelper::decode_option_string_field(obj, "dec-id")?,
})
}
}

impl JsonCodec<GroupInputRequestCommon> for GroupInputRequestCommon {
fn encode_json(&self) -> Map<String, Value> {
let mut obj = Map::new();
JsonCodecHelper::encode_field(&mut obj, "source", &self.source);

obj
}

fn decode_json(obj: &Map<String, Value>) -> BuckyResult<Self> {
Ok(Self {
source: JsonCodecHelper::decode_field(obj, "source")?,
})
}
}

impl JsonCodec<GroupStartServiceOutputRequest> for GroupStartServiceOutputRequest {
fn encode_json(&self) -> Map<String, Value> {
let mut obj = Map::new();
JsonCodecHelper::encode_string_field(&mut obj, "group-id", &self.group_id);
JsonCodecHelper::encode_string_field(&mut obj, "rpath", self.rpath.as_str());
JsonCodecHelper::encode_field(&mut obj, "common", &self.common);

obj
}
Expand All @@ -16,6 +50,7 @@ impl JsonCodec<GroupStartServiceOutputRequest> for GroupStartServiceOutputReques
Ok(Self {
group_id: JsonCodecHelper::decode_string_field(obj, "group-id")?,
rpath: JsonCodecHelper::decode_string_field(obj, "rpath")?,
common: JsonCodecHelper::decode_field(obj, "common")?,
})
}
}
Expand All @@ -25,6 +60,7 @@ impl JsonCodec<GroupStartServiceInputRequest> for GroupStartServiceInputRequest
let mut obj = Map::new();
JsonCodecHelper::encode_string_field(&mut obj, "group-id", &self.group_id);
JsonCodecHelper::encode_string_field(&mut obj, "rpath", self.rpath.as_str());
JsonCodecHelper::encode_field(&mut obj, "common", &self.common);

obj
}
Expand All @@ -33,6 +69,7 @@ impl JsonCodec<GroupStartServiceInputRequest> for GroupStartServiceInputRequest
Ok(Self {
group_id: JsonCodecHelper::decode_string_field(obj, "group-id")?,
rpath: JsonCodecHelper::decode_string_field(obj, "rpath")?,
common: JsonCodecHelper::decode_field(obj, "common")?,
})
}
}
37 changes: 17 additions & 20 deletions src/component/cyfs-group-lib/src/requestor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use http_types::{Method, Request, Url};
use crate::{
output_request::GroupStartServiceOutputRequest,
processor::{GroupOutputProcessor, GroupOutputProcessorRef},
GroupOutputRequestCommon, GroupPushProposalOutputResponse, GroupStartServiceOutputResponse,
GroupOutputRequestCommon, GroupPushProposalOutputRequest, GroupPushProposalOutputResponse,
GroupStartServiceOutputResponse,
};

#[derive(Clone)]
Expand Down Expand Up @@ -51,21 +52,20 @@ impl GroupRequestor {

pub async fn start_service(
&self,
req_common: GroupOutputRequestCommon,
group_id: &ObjectId,
rpath: &str,
req: GroupStartServiceOutputRequest,
) -> BuckyResult<GroupStartServiceOutputResponse> {
log::info!("will start group service: {:?}", rpath);
log::info!("will start group service: {:?}", req.rpath);

let url = self.service_url.join("service").unwrap();
let mut http_req = Request::new(Method::Put, url);
self.encode_common_headers(&req.common, &mut http_req);

let req = GroupStartServiceOutputRequest {
group_id: group_id.clone(),
rpath: rpath.to_string(),
group_id: req.group_id.clone(),
rpath: req.rpath.to_string(),
common: req.common,
};

self.encode_common_headers(&req_common, &mut http_req);
let body = req.encode_string();
http_req.set_body(body);

Expand Down Expand Up @@ -99,7 +99,7 @@ impl GroupRequestor {
let e = RequestorHelper::error_from_resp(&mut resp).await;
log::error!(
"group start service failed: rpath={:?}, status={}, {}",
rpath,
req.rpath,
code,
e
);
Expand All @@ -110,24 +110,23 @@ impl GroupRequestor {

pub async fn push_proposal(
&self,
req_common: GroupOutputRequestCommon,
proposal: &GroupProposal,
req: GroupPushProposalOutputRequest,
) -> BuckyResult<GroupPushProposalOutputResponse> {
let proposal_id = proposal.desc().object_id();
let proposal_id = req.proposal.desc().object_id();
log::info!(
"will push proposal: {:?}, {}",
proposal.rpath(),
req.proposal.rpath(),
proposal_id
);

let url = self.service_url.join("proposal").unwrap();
let mut http_req = Request::new(Method::Put, url);

self.encode_common_headers(&req_common, &mut http_req);
self.encode_common_headers(&req.common, &mut http_req);

NONRequestorHelper::encode_object_info(
&mut http_req,
NONObjectInfo::new(proposal_id, proposal.to_vec()?, None),
NONObjectInfo::new(proposal_id, req.proposal.to_vec()?, None),
);

let mut resp = self.requestor.request(http_req).await?;
Expand Down Expand Up @@ -163,17 +162,15 @@ impl GroupRequestor {
impl GroupOutputProcessor for GroupRequestor {
async fn start_service(
&self,
req_common: GroupOutputRequestCommon,
req: GroupStartServiceOutputRequest,
) -> BuckyResult<GroupStartServiceOutputResponse> {
GroupRequestor::start_service(self, req_common, &req.group_id, req.rpath.as_str()).await
GroupRequestor::start_service(self, req).await
}

async fn push_proposal(
&self,
req_common: GroupOutputRequestCommon,
req: GroupProposal,
req: GroupPushProposalOutputRequest,
) -> BuckyResult<GroupPushProposalOutputResponse> {
GroupRequestor::push_proposal(self, req_common, &req).await
GroupRequestor::push_proposal(self, req).await
}
}
23 changes: 13 additions & 10 deletions src/component/cyfs-group-lib/src/rpath_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use cyfs_lib::{
SingleOpEnvStub,
};

use crate::{ExecuteResult, GroupObjectMapProcessor, GroupRequestor, RPathDelegate};
use crate::{
ExecuteResult, GroupObjectMapProcessor, GroupPushProposalOutputRequest, GroupRequestor,
GroupStartServiceOutputRequest, RPathDelegate,
};

struct RPathServiceRaw {
rpath: GroupRPath,
Expand All @@ -31,10 +34,10 @@ impl RPathService {
// post http
self.0
.requestor
.push_proposal(
GroupRequestor::make_default_common(proposal.rpath().dec_id().clone()),
proposal,
)
.push_proposal(GroupPushProposalOutputRequest {
common: GroupRequestor::make_default_common(proposal.rpath().dec_id().clone()),
proposal: proposal.clone(),
})
.await
.map(|resp| resp.object)
}
Expand All @@ -57,11 +60,11 @@ impl RPathService {
// post create command
self.0
.requestor
.start_service(
GroupRequestor::make_default_common(self.0.rpath.dec_id().clone()),
self.rpath().group_id(),
self.rpath().rpath(),
)
.start_service(GroupStartServiceOutputRequest {
common: GroupRequestor::make_default_common(self.0.rpath.dec_id().clone()),
group_id: self.rpath().group_id().clone(),
rpath: self.rpath().rpath().to_string(),
})
.await
.map(|_| {})
}
Expand Down
7 changes: 2 additions & 5 deletions src/component/cyfs-stack/src/group/processor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use cyfs_base::*;
use cyfs_core::GroupProposal;
use cyfs_group_lib::{
GroupInputRequestCommon, GroupPushProposalInputResponse, GroupStartServiceInputRequest,
GroupPushProposalInputRequest, GroupPushProposalInputResponse, GroupStartServiceInputRequest,
GroupStartServiceInputResponse,
};

Expand All @@ -11,14 +10,12 @@ use std::sync::Arc;
pub(crate) trait GroupInputProcessor: Sync + Send {
async fn start_service(
&self,
req_common: GroupInputRequestCommon,
req: GroupStartServiceInputRequest,
) -> BuckyResult<GroupStartServiceInputResponse>;

async fn push_proposal(
&self,
req_common: GroupInputRequestCommon,
req: GroupProposal,
req: GroupPushProposalInputRequest,
) -> BuckyResult<GroupPushProposalInputResponse>;
}

Expand Down
Loading

0 comments on commit c2bfcaf

Please sign in to comment.