cyfs_lib/util/
output_request.rs

1use crate::{prelude::*, GlobalStateAccessMode, TransPublishChunkMethod};
2use crate::zone::ZoneRole;
3use cyfs_base::*;
4use cyfs_core::ZoneId;
5use cyfs_core::*;
6use cyfs_bdt::SnStatus;
7use std::convert::TryFrom;
8
9use serde::{Deserialize, Serialize};
10use std::fmt::{self, Display};
11use std::path::PathBuf;
12use std::str::FromStr;
13
14#[derive(Debug, Clone)]
15pub struct UtilOutputRequestCommon {
16    // 请求路径,可为空
17    pub req_path: Option<String>,
18
19    // 来源DEC
20    pub dec_id: Option<ObjectId>,
21
22    // 用以默认行为
23    pub target: Option<ObjectId>,
24
25    pub flags: u32,
26}
27
28impl Default for UtilOutputRequestCommon {
29    fn default() -> Self {
30        Self {
31            req_path: None,
32            dec_id: None,
33            target: None,
34            flags: 0,
35        }
36    }
37}
38
39impl fmt::Display for UtilOutputRequestCommon {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        write!(f, "req_path: {:?}", self.req_path)?;
42
43        if let Some(dec_id) = &self.dec_id {
44            write!(f, ", dec_id: {}", dec_id)?;
45        }
46
47        if let Some(target) = &self.target {
48            write!(f, ", target: {}", target.to_string())?;
49        }
50
51        write!(f, ", flags: {}", self.flags)?;
52
53        Ok(())
54    }
55}
56
57#[derive(Debug, Clone)]
58pub struct UtilGetDeviceOutputRequest {
59    pub common: UtilOutputRequestCommon,
60}
61
62impl Display for UtilGetDeviceOutputRequest {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        write!(f, "common: {}", self.common)
65    }
66}
67
68impl UtilGetDeviceOutputRequest {
69    pub fn new() -> Self {
70        Self {
71            common: UtilOutputRequestCommon::default(),
72        }
73    }
74}
75
76#[derive(Debug, Clone)]
77pub struct UtilGetDeviceOutputResponse {
78    pub device_id: DeviceId,
79    pub device: Device,
80}
81
82impl Display for UtilGetDeviceOutputResponse {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        write!(f, "device_id: {}", self.device_id)
85    }
86}
87
88#[derive(Debug, Clone)]
89pub struct UtilGetZoneOutputRequest {
90    pub common: UtilOutputRequestCommon,
91
92    pub object_id: Option<ObjectId>,
93    pub object_raw: Option<Vec<u8>>,
94}
95
96impl UtilGetZoneOutputRequest {
97    pub fn new(object_id: Option<ObjectId>, object_raw: Option<Vec<u8>>) -> Self {
98        Self {
99            common: UtilOutputRequestCommon::default(),
100            object_id,
101            object_raw,
102        }
103    }
104}
105
106impl Display for UtilGetZoneOutputRequest {
107    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108        write!(f, "common: {}", self.common)?;
109
110        write!(f, "object_id: {:?}", self.object_id)
111    }
112}
113
114#[derive(Debug, Clone)]
115pub struct UtilGetZoneOutputResponse {
116    pub zone_id: ZoneId,
117    pub zone: Zone,
118    pub device_id: DeviceId,
119}
120
121impl Display for UtilGetZoneOutputResponse {
122    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123        write!(f, "zone_id: {}", self.zone_id)?;
124
125        write!(f, "device_id: {:?}", self.device_id)
126    }
127}
128
129#[derive(Debug, Clone)]
130pub struct UtilResolveOODOutputRequest {
131    pub common: UtilOutputRequestCommon,
132
133    pub object_id: ObjectId,
134    pub owner_id: Option<ObjectId>,
135}
136
137impl UtilResolveOODOutputRequest {
138    pub fn new(object_id: ObjectId, owner_id: Option<ObjectId>) -> Self {
139        Self {
140            common: UtilOutputRequestCommon::default(),
141            object_id,
142            owner_id,
143        }
144    }
145}
146
147impl Display for UtilResolveOODOutputRequest {
148    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149        write!(f, "common: {}", self.common)?;
150
151        write!(f, "object_id: {:?}", self.object_id)?;
152
153        if let Some(owner_id) = &self.owner_id {
154            write!(f, "owner_id: {:?}", owner_id)?;
155        }
156
157        Ok(())
158    }
159}
160
161#[derive(Debug, Clone)]
162pub struct UtilResolveOODOutputResponse {
163    pub device_list: Vec<DeviceId>,
164}
165
166impl Display for UtilResolveOODOutputResponse {
167    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168        write!(f, "device_list: {:?}", self.device_list)
169    }
170}
171
172#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
173pub enum OODNetworkType {
174    Unknown,
175    Intranet,
176    Extranet,
177}
178
179impl Display for OODNetworkType {
180    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
181        let v = match self {
182            Self::Unknown => "unknown",
183            Self::Intranet => "intranet",
184            Self::Extranet => "extranet",
185        };
186
187        write!(f, "{}", v)
188    }
189}
190
191#[derive(Serialize, Deserialize, Debug, Clone)]
192pub struct OODStatus {
193    pub network: OODNetworkType,
194
195    pub first_ping: u64,
196    pub first_success_ping: u64,
197    pub last_success_ping: u64,
198
199    pub last_ping: u64,
200    pub last_ping_result: u16,
201
202    pub ping_count: u32,
203    pub ping_success_count: u64,
204
205    // 当前连续失败的次数,成功后重置
206    pub cont_fail_count: u64,
207
208    pub ping_avg_during: u64,
209    pub ping_max_during: u64,
210    pub ping_min_during: u64,
211
212    // current zone's ood
213    pub ood_device_id: DeviceId,
214
215    // is root-state sync enable on this device
216    pub enable_sync: bool,
217
218    // device local root-state
219    pub device_root_state: ObjectId,
220    pub device_root_state_revision: u64,
221
222    // zone local root-state
223    pub zone_root_state: Option<ObjectId>,
224    pub zone_root_state_revision: u64,
225}
226
227#[derive(Debug, Clone)]
228pub struct UtilGetOODStatusOutputRequest {
229    pub common: UtilOutputRequestCommon,
230}
231
232impl Display for UtilGetOODStatusOutputRequest {
233    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234        write!(f, "common: {}", self.common)
235    }
236}
237
238impl UtilGetOODStatusOutputRequest {
239    pub fn new() -> Self {
240        Self {
241            common: UtilOutputRequestCommon::default(),
242        }
243    }
244}
245
246#[derive(Serialize, Deserialize, Debug, Clone)]
247pub struct UtilGetOODStatusOutputResponse {
248    pub status: OODStatus,
249}
250
251impl Display for UtilGetOODStatusOutputResponse {
252    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
253        write!(f, "status: {:?}", self.status)
254    }
255}
256
257#[derive(Debug, Clone)]
258pub struct UtilGetNOCInfoOutputRequest {
259    pub common: UtilOutputRequestCommon,
260}
261
262impl Display for UtilGetNOCInfoOutputRequest {
263    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
264        write!(f, "common: {}", self.common)
265    }
266}
267
268impl UtilGetNOCInfoOutputRequest {
269    pub fn new() -> Self {
270        Self {
271            common: UtilOutputRequestCommon::default(),
272        }
273    }
274}
275
276#[derive(Serialize, Deserialize, Debug, Clone)]
277pub struct UtilGetNOCInfoOutputResponse {
278    pub stat: NamedObjectCacheStat,
279}
280
281impl Display for UtilGetNOCInfoOutputResponse {
282    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
283        write!(f, "stat: {:?}", self.stat)
284    }
285}
286
287// 设备的一些静态信息
288#[derive(Debug, Clone)]
289pub struct DeviceStaticInfo {
290    // 当前设备id
291    pub device_id: DeviceId,
292    pub device: Device,
293
294    // 当前设备是不是ood
295    pub is_ood_device: bool,
296
297    pub ood_work_mode: OODWorkMode,
298
299    pub zone_role: ZoneRole,
300
301    pub root_state_access_mode: GlobalStateAccessMode,
302    pub local_cache_access_mode: GlobalStateAccessMode,
303    
304    // 当前zone的主ood id
305    pub ood_device_id: DeviceId,
306
307    // 当前所属zone
308    pub zone_id: ZoneId,
309
310    // 当前zone的owner
311    pub owner_id: Option<ObjectId>,
312
313    // 当前协议栈的cyfs根目录
314    pub cyfs_root: String,
315
316    // current sn list config
317    pub sn_list: Vec<DeviceId>,
318    pub known_sn_list: Vec<DeviceId>,
319}
320
321#[derive(Debug, Clone)]
322pub struct UtilGetDeviceStaticInfoOutputRequest {
323    pub common: UtilOutputRequestCommon,
324}
325
326impl Display for UtilGetDeviceStaticInfoOutputRequest {
327    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328        write!(f, "common: {}", self.common)
329    }
330}
331
332impl UtilGetDeviceStaticInfoOutputRequest {
333    pub fn new() -> Self {
334        Self {
335            common: UtilOutputRequestCommon::default(),
336        }
337    }
338}
339
340#[derive(Debug, Clone)]
341pub struct UtilGetDeviceStaticInfoOutputResponse {
342    pub info: DeviceStaticInfo,
343}
344
345impl Display for UtilGetDeviceStaticInfoOutputResponse {
346    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
347        write!(f, "info: {:?}", self.info)
348    }
349}
350
351#[derive(Debug, Eq, PartialEq, Clone)]
352pub enum BdtNetworkAccessType {
353    NAT,
354    WAN,
355}
356
357impl fmt::Display for BdtNetworkAccessType {
358    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
359        let v = match self {
360            Self::NAT => "nat",
361            Self::WAN => "wan",
362        };
363
364        write!(f, "{}", v)
365    }
366}
367
368impl FromStr for BdtNetworkAccessType {
369    type Err = BuckyError;
370
371    fn from_str(s: &str) -> BuckyResult<Self> {
372        match s {
373            "nat" => Ok(Self::NAT),
374            "wan" => Ok(Self::WAN),
375            _ => {
376                let msg = format!("unknown BdtNetworkAccessType value: {}", s);
377                error!("{}", msg);
378
379                Err(BuckyError::new(BuckyErrorCode::InvalidData, msg))
380            }
381        }
382    }
383}
384
385#[derive(Debug, Eq, PartialEq, Clone)]
386pub struct BdtNetworkAccessEndpoint {
387    pub lan_ep: Endpoint,
388    pub wan_ep: Endpoint,
389
390    pub access_type: BdtNetworkAccessType,
391}
392
393#[derive(Debug, Eq, PartialEq, Clone)]
394pub struct BdtNetworkAccessSn {
395    pub sn: DeviceId,
396    pub sn_status: SnStatus,
397}
398
399#[derive(Debug, Clone)]
400pub struct BdtNetworkAccessInfo {
401    pub v4: Vec<BdtNetworkAccessEndpoint>,
402    pub v6: Vec<BdtNetworkAccessEndpoint>,
403
404    pub sn: Vec<BdtNetworkAccessSn>,
405}
406
407impl Default for BdtNetworkAccessInfo {
408    fn default() -> Self {
409        Self {
410            v4: Vec::new(),
411            v6: Vec::new(),
412            sn: Vec::new(),
413        }
414    }
415}
416
417#[derive(Debug, Clone)]
418pub struct UtilGetNetworkAccessInfoOutputRequest {
419    pub common: UtilOutputRequestCommon,
420}
421
422impl Display for UtilGetNetworkAccessInfoOutputRequest {
423    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
424        write!(f, "common: {}", self.common)
425    }
426}
427
428impl UtilGetNetworkAccessInfoOutputRequest {
429    pub fn new() -> Self {
430        Self {
431            common: UtilOutputRequestCommon::default(),
432        }
433    }
434}
435
436#[derive(Debug, Clone)]
437pub struct UtilGetNetworkAccessInfoOutputResponse {
438    pub info: BdtNetworkAccessInfo,
439}
440
441impl Display for UtilGetNetworkAccessInfoOutputResponse {
442    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
443        write!(f, "info: {:?}", self.info)
444    }
445}
446
447#[derive(Debug, Clone)]
448pub struct UtilGetSystemInfoOutputRequest {
449    pub common: UtilOutputRequestCommon,
450}
451
452impl Display for UtilGetSystemInfoOutputRequest {
453    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
454        write!(f, "common: {}", self.common)
455    }
456}
457
458impl UtilGetSystemInfoOutputRequest {
459    pub fn new() -> Self {
460        Self {
461            common: UtilOutputRequestCommon::default(),
462        }
463    }
464}
465
466#[derive(Debug, Clone, Serialize, Deserialize)]
467pub struct UtilGetSystemInfoOutputResponse {
468    pub info: cyfs_util::SystemInfo,
469}
470
471impl Display for UtilGetSystemInfoOutputResponse {
472    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
473        write!(f, "info: {:?}", self.info)
474    }
475}
476
477#[derive(Debug, Clone)]
478pub struct VersionInfo {
479    pub version: String,
480    pub channel: CyfsChannel,
481    pub target: String,
482}
483
484#[derive(Debug, Clone)]
485pub struct UtilGetVersionInfoOutputRequest {
486    pub common: UtilOutputRequestCommon,
487}
488
489impl Display for UtilGetVersionInfoOutputRequest {
490    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
491        write!(f, "common: {}", self.common)
492    }
493}
494
495impl UtilGetVersionInfoOutputRequest {
496    pub fn new() -> Self {
497        Self {
498            common: UtilOutputRequestCommon::default(),
499        }
500    }
501}
502
503#[derive(Debug, Clone)]
504pub struct UtilGetVersionInfoOutputResponse {
505    pub info: VersionInfo,
506}
507
508impl Display for UtilGetVersionInfoOutputResponse {
509    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
510        write!(f, "info: {:?}", self.info)
511    }
512}
513
514#[derive(Debug, Clone)]
515pub struct UtilBuildFileOutputRequest {
516    pub common: UtilOutputRequestCommon,
517    pub local_path: PathBuf,
518    pub owner: ObjectId,
519    pub chunk_size: u32, 
520    pub chunk_method: TransPublishChunkMethod, 
521    pub access: Option<AccessString>,
522}
523
524pub struct UtilBuildFileOutputResponse {
525    pub object_id: ObjectId,
526    pub object_raw: Vec<u8>,
527}
528
529#[derive(Debug, Clone, Copy)]
530#[repr(u16)]
531pub enum BuildDirType {
532    Zip,
533}
534
535impl TryFrom<u16> for BuildDirType {
536    type Error = BuckyError;
537
538    fn try_from(value: u16) -> Result<Self, Self::Error> {
539        match value {
540            0 => Ok(BuildDirType::Zip),
541            v @ _ => {
542                let msg = format!("unknown build dir type value {}", v);
543                log::error!("{}", msg.as_str());
544                Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg))
545            }
546        }
547    }
548}
549
550#[derive(Debug, Clone)]
551pub struct UtilBuildDirFromObjectMapOutputRequest {
552    pub common: UtilOutputRequestCommon,
553    pub object_map_id: ObjectId,
554    pub dir_type: BuildDirType,
555}
556
557pub struct UtilBuildDirFromObjectMapOutputResponse {
558    pub object_id: ObjectId,
559}