1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use super::output_request::*;
use crate::{NDNInputRequestCommon, TransTaskControlAction, TransTaskInfo, TransTaskStatus};
use cyfs_base::{*};
use cyfs_core::TransContext;
use cyfs_util::cache::FileDirRef;

use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::str::FromStr;

pub struct TransGetContextInputRequest {
    pub common: NDNInputRequestCommon,
    pub context_id: Option<ObjectId>,
    pub context_path: Option<String>,
}

pub type TransGetContextInputResponse = TransGetContextOutputResponse;

pub struct TransUpdateContextInputRequest {
    pub common: NDNInputRequestCommon,

    pub context: TransContext,
    pub access: Option<AccessString>,
}

#[derive(Debug)]
pub struct TransCreateTaskInputRequest {
    pub common: NDNInputRequestCommon,
    pub object_id: ObjectId,
    // 保存到的本地目录or文件
    pub local_path: PathBuf,
    pub device_list: Vec<DeviceId>,

    pub group: Option<String>,
    pub context: Option<String>,

    pub auto_start: bool,
}

impl TransCreateTaskInputRequest {
    pub fn check_valid(&self) -> BuckyResult<()> {
        self.common.check_param_with_referer(&self.object_id)
    }
}

// 控制传输一个任务的状态
#[derive(Debug)]
pub struct TransControlTaskInputRequest {
    // 用以处理acl
    pub common: NDNInputRequestCommon,
    pub task_id: String,
    pub action: TransTaskControlAction,
}

#[derive(Debug)]
pub struct TransGetTaskStateInputRequest {
    // 用以处理acl
    pub common: NDNInputRequestCommon,
    pub task_id: String,
}

pub type TransGetTaskStateInputResponse = TransGetTaskStateOutputResponse;

// method how to deal with chunk position tracker record 
#[derive(Debug, Clone, Copy)]
pub enum TransPublishChunkMethod {
    // track local postion 
    Track, 
    // copy to chunk cache path
    Copy, 
    // do nothing
    None
}

impl Default for TransPublishChunkMethod {
    fn default() -> Self {
        Self::Track
    }
}

impl TryFrom<u8> for TransPublishChunkMethod {
    type Error = BuckyError;

    fn try_from(v: u8) -> BuckyResult<Self> {
        match v {
            0 => Ok(Self::Track), 
            1 => Ok(Self::Copy), 
            2 => Ok(Self::None), 
            _ => Err(BuckyError::new(BuckyErrorCode::InvalidInput, format!("invalid track chunk method {}", v)))
        }
    }
}

impl Into<u8> for TransPublishChunkMethod {
    fn into(self) -> u8 {
        match self {
            Self::Track => 0, 
            Self::Copy => 1, 
            Self::None => 2
        }
    }
}

impl FromStr for TransPublishChunkMethod {
    type Err = BuckyError;

    fn from_str(str: &str) -> BuckyResult<Self> {
        match str {
            "Track" => Ok(Self::Track), 
            "Copy" => Ok(Self::Copy), 
            "None" => Ok(Self::None),
            _ => Err(BuckyError::new(BuckyErrorCode::InvalidInput, format!("invalid chunk method {}", str)))
        }
    }
}

impl ProtobufTransform<TransPublishChunkMethod> for i32 {
    fn transform(value: TransPublishChunkMethod) -> BuckyResult<Self> {
        Ok(Into::<u8>::into(value) as i32)
    }
}

impl ProtobufTransform<Option<i32>> for TransPublishChunkMethod {
    fn transform(value: Option<i32>) -> BuckyResult<Self> {
        value.map(|v|  Self::try_from(v as u8)).unwrap_or(Ok(Self::Track))
    }
}

#[derive(Debug)]
pub struct TransPublishFileInputRequest {
    // 用以处理acl
    pub common: NDNInputRequestCommon,
    // 文件所属者
    pub owner: ObjectId,

    // 文件的本地路径
    pub local_path: PathBuf,
    // chunk大小
    pub chunk_size: u32,
    // how to deal with chunk position tracker record 
    pub chunk_method: TransPublishChunkMethod, 

    pub access: Option<AccessString>,
    
    pub file_id: Option<ObjectId>,
    // 关联的dirs
    pub dirs: Option<Vec<FileDirRef>>,
}

#[derive(Debug)]
pub struct TransQueryTasksInputRequest {
    pub common: NDNInputRequestCommon,
    pub task_status: Option<TransTaskStatus>,
    pub range: Option<(u64, u32)>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TransPublishFileInputResponse {
    pub file_id: ObjectId,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TransCreateTaskInputResponse {
    pub task_id: String,
}

pub struct TransQueryTasksInputResponse {
    pub task_list: Vec<TransTaskInfo>,
}

// get task group state
#[derive(Debug)]
pub struct TransGetTaskGroupStateInputRequest {
    pub common: NDNInputRequestCommon,
    pub group_type: TransTaskGroupType, 
    pub group: String,
    pub speed_when: Option<u64>,
}

pub type TransGetTaskGroupStateInputResponse = TransGetTaskGroupStateOutputResponse;

// control task group
#[derive(Debug)]
pub struct TransControlTaskGroupInputRequest {
    pub common: NDNInputRequestCommon,
    pub group_type: TransTaskGroupType, 
    pub group: String,
    pub action: TransTaskGroupControlAction,
}

pub type TransControlTaskGroupInputResponse = TransControlTaskGroupOutputResponse;