cyfs_lib/ndn/
output_request.rs

1use super::def::*;
2use super::input_request::*;
3use crate::base::{NDNDataRequestRange, NDNDataResponseRange};
4use cyfs_base::*;
5
6use async_std::io::Read;
7use std::fmt;
8
9#[derive(Debug, Clone)]
10pub struct NDNOutputRequestCommon {
11    // 请求路径,可为空
12    pub req_path: Option<String>,
13
14    // 来源DEC
15    pub dec_id: Option<ObjectId>,
16
17    // api级别
18    pub level: NDNAPILevel,
19
20    // 用以处理默认行为
21    pub target: Option<ObjectId>,
22
23    // 需要处理数据的关联对象,主要用以chunk/file等
24    pub referer_object: Vec<NDNDataRefererObject>,
25
26    pub flags: u32,
27}
28
29impl NDNOutputRequestCommon {
30    pub fn new(level: NDNAPILevel) -> Self {
31        Self {
32            req_path: None,
33            dec_id: None,
34            level,
35            target: None,
36            referer_object: vec![],
37            flags: 0,
38        }
39    }
40}
41
42impl fmt::Display for NDNOutputRequestCommon {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "req_path: {:?}", self.req_path)?;
45        write!(f, ", level: {:?}", self.level)?;
46
47        if let Some(dec_id) = &self.dec_id {
48            write!(f, ", dec_id: {}", dec_id)?;
49        }
50
51        if let Some(target) = &self.target {
52            write!(f, ", target: {}", target.to_string())?;
53        }
54
55        if self.referer_object.is_empty() {
56            write!(f, ", referer_object: {:?}", self.referer_object)?;
57        }
58
59        write!(f, ", flags: {}", self.flags)?;
60
61        Ok(())
62    }
63}
64
65serde_with_json_codec!(NDNOutputRequestCommon);
66// put requests
67// 目前支持ChunkId
68pub struct NDNPutDataOutputRequest {
69    pub common: NDNOutputRequestCommon,
70
71    pub object_id: ObjectId,
72
73    pub length: u64,
74    pub data: Box<dyn Read + Unpin + Send + Sync + 'static>,
75}
76
77impl fmt::Display for NDNPutDataOutputRequest {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        write!(f, "common: {}", self.common)?;
80        write!(f, ", object_id: {}", self.object_id)?;
81
82        write!(f, ", length: {}", self.length)
83    }
84}
85
86impl NDNPutDataOutputRequest {
87    pub fn new(
88        level: NDNAPILevel,
89        object_id: ObjectId,
90        length: u64,
91        data: Box<dyn Read + Unpin + Send + Sync + 'static>,
92    ) -> Self {
93        Self {
94            common: NDNOutputRequestCommon::new(level),
95            object_id,
96            length,
97            data,
98        }
99    }
100
101    pub fn new_with_buffer(level: NDNAPILevel, object_id: ObjectId, data: Vec<u8>) -> Self {
102        let length = data.len() as u64;
103        let data = async_std::io::Cursor::new(data);
104
105        Self {
106            common: NDNOutputRequestCommon::new(level),
107            object_id,
108            length,
109            data: Box::new(data),
110        }
111    }
112
113    pub fn new_ndc(
114        object_id: ObjectId,
115        length: u64,
116        data: Box<dyn Read + Unpin + Send + Sync + 'static>,
117    ) -> Self {
118        Self::new(NDNAPILevel::NDC, object_id, length, data)
119    }
120
121    pub fn new_ndn(
122        target: Option<DeviceId>,
123        object_id: ObjectId,
124        length: u64,
125        data: Box<dyn Read + Unpin + Send + Sync + 'static>,
126    ) -> Self {
127        let mut ret = Self::new(NDNAPILevel::NDN, object_id, length, data);
128        ret.common.target = target.map(|v| v.into());
129
130        ret
131    }
132
133    pub fn new_router(
134        target: Option<ObjectId>,
135        object_id: ObjectId,
136        length: u64,
137        data: Box<dyn Read + Unpin + Send + Sync + 'static>,
138    ) -> Self {
139        let mut ret = Self::new(NDNAPILevel::Router, object_id, length, data);
140        ret.common.target = target;
141
142        ret
143    }
144
145    pub fn new_router_with_buffer(
146        target: Option<ObjectId>,
147        object_id: ObjectId,
148        data: Vec<u8>,
149    ) -> Self {
150        let mut ret = Self::new_with_buffer(NDNAPILevel::Router, object_id, data);
151        ret.common.target = target;
152
153        ret
154    }
155}
156
157pub struct NDNPutDataOutputRequestWithBuffer {
158    pub common: NDNOutputRequestCommon,
159
160    pub object_id: ObjectId,
161    pub data: Vec<u8>,
162}
163
164impl NDNPutDataOutputRequestWithBuffer {
165    pub fn new(level: NDNAPILevel, object_id: ObjectId, data: Vec<u8>) -> Self {
166        Self {
167            common: NDNOutputRequestCommon::new(level),
168            object_id,
169            data,
170        }
171    }
172
173    pub fn new_ndc(object_id: ObjectId, data: Vec<u8>) -> Self {
174        Self::new(NDNAPILevel::NDC, object_id, data)
175    }
176
177    pub fn new_ndn(target: Option<DeviceId>, object_id: ObjectId, data: Vec<u8>) -> Self {
178        let mut ret = Self::new(NDNAPILevel::NDN, object_id, data);
179        ret.common.target = target.map(|v| v.into());
180
181        ret
182    }
183
184    pub fn new_router(target: Option<ObjectId>, object_id: ObjectId, data: Vec<u8>) -> Self {
185        let mut ret = Self::new(NDNAPILevel::Router, object_id, data);
186        ret.common.target = target;
187
188        ret
189    }
190}
191
192impl fmt::Display for NDNPutDataOutputRequestWithBuffer {
193    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
194        write!(f, "common: {}", self.common)?;
195        write!(f, ", object_id: {}", self.object_id)?;
196
197        write!(f, ", length: {}", self.data.len())
198    }
199}
200
201pub struct NDNPutDataOutputResponse {
202    pub result: NDNPutDataResult,
203}
204
205impl fmt::Display for NDNPutDataOutputResponse {
206    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207        write!(f, "result: {}", self.result.to_string())
208    }
209}
210
211// get requests
212
213/*
214支持三种形式:
215chunk_id
216file_id
217dir_id|object_map + inner_path
218*/
219#[derive(Clone)]
220pub struct NDNGetDataOutputRequest {
221    pub common: NDNOutputRequestCommon,
222
223    // 目前只支持ChunkId/FileId/DirId
224    pub object_id: ObjectId,
225
226    pub range: Option<NDNDataRequestRange>,
227
228    // 对dir_id有效
229    pub inner_path: Option<String>,
230
231    // get data from context instead of common.target
232    pub context: Option<String>,
233
234    // trans data task's group
235    pub group: Option<String>,
236}
237
238impl NDNGetDataOutputRequest {
239    pub fn new(level: NDNAPILevel, object_id: ObjectId, inner_path: Option<String>) -> Self {
240        Self {
241            common: NDNOutputRequestCommon::new(level),
242            object_id,
243            range: None,
244            inner_path,
245            context: None,
246            group: None,
247        }
248    }
249
250    pub fn new_ndc(object_id: ObjectId, inner_path: Option<String>) -> Self {
251        Self::new(NDNAPILevel::NDC, object_id, inner_path)
252    }
253
254    pub fn new_ndn(
255        target: Option<DeviceId>,
256        object_id: ObjectId,
257        inner_path: Option<String>,
258    ) -> Self {
259        let mut ret = Self::new(NDNAPILevel::NDN, object_id, inner_path);
260        ret.common.target = target.map(|v| v.into());
261
262        ret
263    }
264
265    pub fn new_router(
266        target: Option<ObjectId>,
267        object_id: ObjectId,
268        inner_path: Option<String>,
269    ) -> Self {
270        let mut ret = Self::new(NDNAPILevel::Router, object_id, inner_path);
271        ret.common.target = target;
272
273        ret
274    }
275
276    pub fn new_context(
277        context: impl Into<String>,
278        object_id: ObjectId,
279        inner_path: Option<String>,
280    ) -> Self {
281        let mut ret = Self::new(NDNAPILevel::Router, object_id, inner_path);
282        ret.context = Some(context.into());
283
284        ret
285    }
286}
287
288impl fmt::Display for NDNGetDataOutputRequest {
289    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
290        write!(f, "common: {}", self.common)?;
291        write!(f, ", object_id: {}", self.object_id)?;
292
293        if let Some(range) = &self.range {
294            write!(f, ", range: {}", range.to_display_string())?;
295        }
296
297        write!(f, ", inner_path: {:?}", self.inner_path)?;
298
299        if let Some(context) = &self.context {
300            write!(f, ", context: {}", context)?;
301        }
302
303        if let Some(group) = &self.group {
304            write!(f, ", group: {}", group)?;
305        }
306
307        Ok(())
308    }
309}
310
311pub struct NDNGetDataOutputResponse {
312    // chunk_id/file_id
313    pub object_id: ObjectId,
314
315    // file's owner
316    pub owner_id: Option<ObjectId>,
317
318    // 所属file的attr
319    pub attr: Option<Attributes>,
320
321    // resp ranges
322    pub range: Option<NDNDataResponseRange>,
323
324    // task group
325    pub group: Option<String>,
326
327    // content
328    pub length: u64,
329    pub data: Box<dyn Read + Unpin + Send + Sync + 'static>,
330}
331
332impl fmt::Display for NDNGetDataOutputResponse {
333    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
334        write!(f, "object_id: {}", self.object_id)?;
335
336        if let Some(owner) = &self.owner_id {
337            write!(f, ", owner: {}", owner)?;
338        }
339
340        if let Some(attr) = &self.attr {
341            write!(f, ", attr: {:?}", attr)?;
342        }
343
344        if let Some(range) = &self.range {
345            write!(f, ", range: {:?}", range)?;
346        }
347
348        if let Some(group) = &self.group {
349            write!(f, ", group: {:?}", group)?;
350        }
351
352        write!(f, ", length: {}", self.length)
353    }
354}
355
356#[derive(Clone)]
357pub struct NDNDeleteDataOutputRequest {
358    pub common: NDNOutputRequestCommon,
359
360    pub object_id: ObjectId,
361
362    // 对dir_id有效
363    pub inner_path: Option<String>,
364}
365
366impl NDNDeleteDataOutputRequest {
367    pub fn new(level: NDNAPILevel, object_id: ObjectId, inner_path: Option<String>) -> Self {
368        Self {
369            common: NDNOutputRequestCommon::new(level),
370            object_id,
371            inner_path,
372        }
373    }
374
375    pub fn new_ndc(object_id: ObjectId, inner_path: Option<String>) -> Self {
376        Self::new(NDNAPILevel::NDC, object_id, inner_path)
377    }
378
379    pub fn new_ndn(
380        target: Option<DeviceId>,
381        object_id: ObjectId,
382        inner_path: Option<String>,
383    ) -> Self {
384        let mut ret = Self::new(NDNAPILevel::NDN, object_id, inner_path);
385        ret.common.target = target.map(|v| v.into());
386
387        ret
388    }
389
390    pub fn new_router(
391        target: Option<ObjectId>,
392        object_id: ObjectId,
393        inner_path: Option<String>,
394    ) -> Self {
395        let mut ret = Self::new(NDNAPILevel::Router, object_id, inner_path);
396        ret.common.target = target;
397
398        ret
399    }
400}
401
402impl fmt::Display for NDNDeleteDataOutputRequest {
403    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
404        write!(f, "common: {}", self.common)?;
405        write!(f, ", object_id: {}", self.object_id)?;
406
407        write!(f, ", inner_path: {:?}", self.inner_path)
408    }
409}
410
411pub struct NDNDeleteDataOutputResponse {
412    pub object_id: ObjectId,
413}
414
415impl fmt::Display for NDNDeleteDataOutputResponse {
416    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
417        write!(f, "object_id: {}", self.object_id)
418    }
419}
420
421#[derive(Clone)]
422pub struct NDNQueryFileOutputRequest {
423    pub common: NDNOutputRequestCommon,
424
425    pub param: NDNQueryFileParam,
426}
427
428impl NDNQueryFileOutputRequest {
429    pub fn new(level: NDNAPILevel, param: NDNQueryFileParam) -> Self {
430        Self {
431            common: NDNOutputRequestCommon::new(level),
432            param,
433        }
434    }
435
436    pub fn new_ndc(param: NDNQueryFileParam) -> Self {
437        Self::new(NDNAPILevel::NDC, param)
438    }
439
440    pub fn new_ndn(target: Option<DeviceId>, param: NDNQueryFileParam) -> Self {
441        let mut ret = Self::new(NDNAPILevel::NDN, param);
442        ret.common.target = target.map(|v| v.into());
443
444        ret
445    }
446
447    pub fn new_router(target: Option<ObjectId>, param: NDNQueryFileParam) -> Self {
448        let mut ret = Self::new(NDNAPILevel::Router, param);
449        ret.common.target = target;
450
451        ret
452    }
453}
454
455impl fmt::Display for NDNQueryFileOutputRequest {
456    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
457        write!(f, "common: {}", self.common)?;
458        write!(f, ", param: {:?}", self.param)
459    }
460}
461
462pub type NDNQueryFileOutputResponse = NDNQueryFileInputResponse;