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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
use std::{
    sync::RwLock, 
    collections::LinkedList,
    vec::Vec,
};
use async_std::{
    sync::Arc, 
    task
};
use cyfs_base::*;
use crate::{
    types::*, 
    stack::{Stack, WeakStack}
};
use super::super::{
    chunk::*, 
    scheduler::*, 
};
use super::{
    chunk::ChunkTask, 
    chunk_list::ChunkListTask, 
    file::FileTask,
    statistic::{SummaryStatisticTaskPtr, SummaryStatisticTaskImpl}

};

const TASK_COUNT_MAX_DEFAULT: usize = 5;

#[derive(Clone)]
pub struct Config { 
    pub task_count_max: usize,
}

impl std::default::Default for Config {
    fn default() -> Self {
        Self {
            task_count_max: TASK_COUNT_MAX_DEFAULT,
        }
    }
   
}

// 因为按路径加入可能出现同一个object被多次下载的情况;
// 所以需要一个单独的id 而不是 object id标识 sub task
#[derive(Clone)]
enum SubTask {
    ChunkList(IncreaseId, ChunkListTask),
    Chunk(IncreaseId, ChunkTask), 
    File(IncreaseId, FileTask), 
    Dir(IncreaseId, DirTask), 
    End
}

impl std::fmt::Display for SubTask {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ChunkList(id, task) => write!(f, "DirSubTask:{{id:{}, task:{}}}", id, task), 
            Self::Chunk(id, task) => write!(f, "DirSubTask:{{id:{}, task:{}}}", id, task), 
            Self::File(id, task) => write!(f, "DirSubTask:{{id:{}, task:{}}}", id, task), 
            Self::Dir(id, task) => write!(f, "DirSubTask:{{id:{}, task:{}}}", id, task),
            Self::End => write!(f, "DirEndSubTask") 
        }
    }
}


impl SubTask {
    fn id(&self) -> IncreaseId {
        match self {
            Self::ChunkList(id, _) => *id,
            Self::Chunk(id, _) => *id, 
            Self::File(id, _) => *id, 
            Self::Dir(id, _) => *id,
            Self::End => IncreaseId::invalid() 
        }
    }

    fn is_end(&self) -> bool {
        if let Self::End = self {
            true
        } else {
            false
        }
    }

    fn as_task(&self) -> Option<&dyn DownloadTask> {
        match self {
            Self::ChunkList(_, task) => Some(task),
            Self::Chunk(_, task) => Some(task),
            Self::File(_, task) => Some(task), 
            Self::Dir(_, task) => Some(task), 
            _ => None
        }
    }

    fn total_len(&self) -> Option<u64> {
        match self {
            Self::ChunkList(_, task) => Some(task.chunk_list().total_len()),
            Self::Chunk(_, task) => Some(task.chunk().len() as u64),
            Self::File(_, task) => Some(task.file().desc().content().len()),
            Self::Dir(_, _) | _ => None,
        }
    }
}

enum TaskStateImpl {
    Downloading(LinkedList<SubTask>),
    Finished, 
    Canceled(BuckyErrorCode)
}

struct StateImpl {
    schedule_state: TaskStateImpl, 
    downloading_state: Vec<SubTask>,
    downloaded_state: Vec<SubTask>,
    control_state: TaskControlState
}

struct TaskImpl {
    stack: WeakStack, 
    dir: DirId, 
    config: Arc<ChunkDownloadConfig>, 
    resource: ResourceManager, 
    sub_id: IncreaseIdGenerator, 
    state: RwLock<StateImpl>,  
    writers: Vec<Box<dyn ChunkWriter>>,
    statistic_task: SummaryStatisticTaskPtr,
}

pub trait DirTaskControl: Send + Sync {
    // 如果dir meta比较大,包含很多chunk,在dir body中的meta是 chunk id list;首先需要下载meta chunks;
    // 完整加入dir meta的chunk list;dir task进入DownloadingMeta状态;标准的chunk writer的一般实现是写入到内存中展开meta数据;
    fn add_meta(&self, chunk_list: ChunkListDesc, writers: Vec<Box<dyn ChunkWriter>>) -> BuckyResult<()>;
    
    // 当本地已有meta 数据后,上层按照dir 的组织形式,在合适的时机组合以下调用,按需添加dir task的sub task;
    // 压缩多个path 到 一个chunk,产生chunk sub task
    fn add_chunk(&self, chunk: ChunkId, writers: Vec<Box<dyn ChunkWriter>>) -> BuckyResult<()>;
    // 产生file sub task
    fn add_file(&self, file: File, writers: Vec<Box<dyn ChunkWriter>>) -> BuckyResult<()>;
    // 嵌套子目录,产生 dir sub task;返回的dir sub task实例;
    fn add_dir(&self, dir: DirId, writers: Vec<Box<dyn ChunkWriter>>) -> BuckyResult<Box<dyn DirTaskControl>>;
    // 所有路径的sub task已经加入到dir task后,调用finish;当dir task的sub task全部完成后,dir task进入finish 状态
    fn finish(&self) -> BuckyResult<()>;
}

#[derive(Clone)]
pub struct DirTask(Arc<TaskImpl>);

impl std::fmt::Display for DirTask {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "DirTask::{{dir:{}}}",  self.dir_id())
    }
}

impl DirTask {
    pub fn new(stack: WeakStack, 
        dir: DirId, 
        config: Arc<ChunkDownloadConfig>, 
        writers: Vec<Box<dyn ChunkWriter>>,
        owner: ResourceManager,
        task_cb: Option<StatisticTaskPtr>) -> Self {
        Self (Arc::new(TaskImpl {
            stack: stack,
            dir: dir,
            config: config,
            resource: owner,
            sub_id: IncreaseIdGenerator::new(),
            state: RwLock::new(StateImpl{
                schedule_state: TaskStateImpl::Downloading(LinkedList::new()),
                downloading_state: Vec::new(),
                downloaded_state: Vec::new(),
                control_state: TaskControlState::Downloading(0, 0),
            }),
            writers: writers,
            statistic_task: 
                {
                    if let Some(cb) = task_cb {
                        SummaryStatisticTaskImpl::new(Some(cb)).ptr()
                    } else {
                        SummaryStatisticTaskImpl::new(Some(DynamicStatisticTask::default().ptr())).ptr()
                    }
                }
        }))
    }
    
    pub fn dir_id(&self) -> &DirId {
        &self.0.dir
    }

    pub fn config(&self) -> &Arc<ChunkDownloadConfig> {
        &self.0.config
    }

    pub fn as_statistic(&self) -> StatisticTaskPtr {
        Arc::from(self.clone())
    }

    fn cancel_by_error(&self, _id: IncreaseId, _err: BuckyErrorCode) -> BuckyResult<()> {
        Ok(())
    }

    fn on_sub_task_error(&self, _id: IncreaseId, _err: BuckyErrorCode) {
    }

    fn on_sub_task_finish(&self, id: IncreaseId) {
        {
            let mut state = self.0.state.write().unwrap();

            for i in 0..state.downloading_state.len() {
                let downloading_task = &state.downloading_state[i];

                if downloading_task.id() == id {
                    let downloading_task = state.downloading_state.remove(i);
                    state.downloaded_state.push(downloading_task.clone());
                    break;
                }
            }
        }

        match self.start() {
            TaskState::Finished => {
                let task = self.clone();
                task::spawn(async move {
                    for w in &task.0.writers {
                        let _ = w.finish().await;
                    }
                });
            }
            _ => {}
        }

    }

    fn on_sub_task_post_finish(&self, _id: IncreaseId) {
    }

    fn add_sub_task(&self, task: SubTask) -> BuckyResult<()> {
        self.add_sub_task_inner(task.clone())
            .map(|start| {
                if start {
                    if let Some(size) = task.total_len() {
                        self.0.statistic_task.add_total_size(size);
                    }
                    let _ = self.start();
                }
            })
            .map_err(|err| {
                if let Some(task) = task.as_task() {
                    let _ = self.resource().remove_child(task.resource());
                }
                err
            })
    }

    fn add_sub_task_inner(&self, task: SubTask) -> BuckyResult<bool> {
        let mut state = self.0.state.write().unwrap();

        match &mut state.schedule_state {
            TaskStateImpl::Downloading(tasks) => {
                if let Some(back) = tasks.back() {
                    if back.is_end() {
                        return Err(BuckyError::new(BuckyErrorCode::ErrorState, "task is waiting finish."));
                    }
                }

                tasks.push_back(task.clone());

                return Ok(true);
            }
            _ => {
                error!("{} ignore sub {} for not pending/downloading", self, task);
                Err(BuckyError::new(BuckyErrorCode::ErrorState, "not pending/downloading"))
            }
        }
    }

    fn meta_writer(&self, 
                   id: IncreaseId, 
                   writers: Vec<Box<dyn ChunkWriter>>) -> Box<dyn ChunkWriter> {
        struct MetaWriterImpl {
            dir: DirTask, 
            id: IncreaseId, 
            writers: Vec<Box<dyn ChunkWriter>>
        }

        #[derive(Clone)]
        struct MetaWriter(Arc<MetaWriterImpl>);

        impl std::fmt::Display for MetaWriter {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "MetaWriter::{{dir:{}}}",  self.0.dir.dir_id())
            }
        }

        #[async_trait::async_trait]
        impl ChunkWriter for MetaWriter {
            fn clone_as_writer(&self) -> Box<dyn ChunkWriter> {
                Box::new(self.clone())
            }

            async fn write(&self, chunk: &ChunkId, content: Arc<Vec<u8>>) -> BuckyResult<()> {
                for w in &self.0.writers {
                    let _ = w.write(chunk, content.clone()).await;
                }
                Ok(())
            }

            async fn finish(&self) -> BuckyResult<()> {
                self.0.dir.on_sub_task_finish(self.0.id);

                for w in &self.0.writers {
                    let _ = w.finish().await;
                }

                Ok(())
            }

            async fn err(&self, e: BuckyErrorCode) -> BuckyResult<()> {
                for w in &self.0.writers {
                    let _ = w.err(e).await;
                }

                // self.0.dir.on_sub_task_error(self.0.id, e);
                // 完成此次任务,不结束整体任务 
                self.0.dir.on_sub_task_finish(self.0.id);

                Ok(())
            }
        }

        Box::new(MetaWriter(Arc::new(MetaWriterImpl { dir: self.clone(), id, writers })))
    }

    fn sub_writer(
        &self, 
        sub_id: IncreaseId, 
        writers: Vec<Box<dyn ChunkWriter>>) -> Box<dyn ChunkWriter> {
        struct WriterImpl {
            dir: DirTask, 
            id: IncreaseId, 
            writers: Vec<Box<dyn ChunkWriter>>
        }

        #[derive(Clone)]
        struct Writer(Arc<WriterImpl>);

        impl std::fmt::Display for Writer {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "FileWriter::{{dir:{}, id:{}}}",  self.0.dir.dir_id(), self.0.id)
            }
        } 

        #[async_trait::async_trait]
        impl ChunkWriter for Writer {
            fn clone_as_writer(&self) -> Box<dyn ChunkWriter> {
                Box::new(self.clone())
            }

            async fn write(&self, chunk: &ChunkId, content: Arc<Vec<u8>>) -> BuckyResult<()> {
                for w in &self.0.writers {
                    let _ = w.write(chunk, content.clone()).await;
                }
                Ok(())
            }

            async fn finish(&self) -> BuckyResult<()> {
                self.0.dir.on_sub_task_finish(self.0.id);

                for w in &self.0.writers {
                    let _ = w.finish().await;
                }

                Ok(())
            }

            async fn err(&self, e: BuckyErrorCode) -> BuckyResult<()> {
                for w in &self.0.writers {
                    let _ = w.err(e).await;
                }
                self.0.dir.on_sub_task_finish(self.0.id);
                Ok(())
            }
        }

        Box::new(Writer(Arc::new(WriterImpl {
            dir: self.clone(), 
            id: sub_id, 
            writers
        })))
    }

}

impl DirTaskControl for DirTask {
    fn add_meta(&self, chunk_list: ChunkListDesc, writers: Vec<Box<dyn ChunkWriter>>) -> BuckyResult<()> {
        let sub_id = self.0.sub_id.generate();
        let task = ChunkListTask::new(self.0.stack.clone(),
                                                    format!("DirMeta:{}", self.dir_id()).to_owned(),
                                                    chunk_list,
                                                    self.0.config.clone(),
                                                    vec![self.meta_writer(sub_id.clone(), writers)],
                                                    self.resource().clone(),
                                                    Some(self.as_statistic()));

        self.add_sub_task(SubTask::ChunkList(sub_id.clone(), task))
    }

    fn add_chunk(&self, chunk: ChunkId, writers: Vec<Box<dyn ChunkWriter>>) -> BuckyResult<()> {
        let sub_id = self.0.sub_id.generate();
        let chunk_task = ChunkTask::new(self.0.stack.clone(), 
                                                   chunk, 
                                                   self.config().clone(), 
                                                   vec![self.sub_writer(sub_id.clone(), writers)], 
                                                   self.resource().clone(),
                                                   Some(self.as_statistic()));

        self.add_sub_task(SubTask::Chunk(sub_id.clone(), chunk_task))
    }

    fn add_file(&self, file: File, writers: Vec<Box<dyn ChunkWriter>>) -> BuckyResult<()> {
        let sub_id = self.0.sub_id.generate();
        let file_task = FileTask::new(self.0.stack.clone(), 
                                                file, 
                                                None, 
                                                self.config().clone(), 
                                                vec![self.sub_writer(sub_id.clone(), writers)],
                                                self.resource().clone(),
                                                Some(self.as_statistic()));
        self.add_sub_task(SubTask::File(sub_id.clone(), file_task))
    }

    fn add_dir(&self, dir: DirId, writers: Vec<Box<dyn ChunkWriter>>) -> BuckyResult<Box<dyn DirTaskControl>> {
        let sub_id = self.0.sub_id.generate();
        let dir_task = Self::new(self.0.stack.clone(), 
                                          dir, 
                                          self.config().clone(), 
                                          vec![self.sub_writer(sub_id, writers)],
                                          self.resource().clone(),
                                          Some(self.as_statistic()));
    
        self.add_sub_task(SubTask::Dir(sub_id.clone(), dir_task.clone()))
            .map(|_| Box::new(dir_task) as Box<dyn DirTaskControl>)
    }

    fn finish(&self) -> BuckyResult<()> {
        self.add_sub_task(SubTask::End)
    }
}


impl TaskSchedule for DirTask {
    fn schedule_state(&self) -> TaskState {
        match &self.0.state.read().unwrap().schedule_state {
            TaskStateImpl::Downloading(_) => TaskState::Running(0), 
            TaskStateImpl::Finished => TaskState::Finished, 
            TaskStateImpl::Canceled(err) => TaskState::Canceled(*err)
        }
    }

    fn resource(&self) -> &ResourceManager {
        &self.0.resource
    }

    //保证不会重复调用
    fn start(&self) -> TaskState {
        enum NextStep {
            Content(Box<dyn DownloadTask>),
            Pending,
            Finish,
        }

        let step = {
            let dir_config = Stack::from(&self.0.stack).config().ndn.dir.clone();

            let mut state = self.0.state.write().unwrap();

            if state.downloading_state.len() >= dir_config.task_count_max {
                NextStep::Pending
            } else {
                match &mut state.schedule_state {
                    TaskStateImpl::Downloading(content) => {
                        if let Some(task) = content.front() {
                            if task.is_end() {
                                if state.downloading_state.len() > 0 {
                                    NextStep::Pending
                                } else {
                                    NextStep::Finish
                                }
                            } else {
                                if let Some(task) = content.pop_front() {
                                    state.downloading_state.push(task.clone());
                                    if let Some(task_impl) = task.as_task() {
                                        NextStep::Content(task_impl.clone_as_download_task())
                                    } else {
                                        NextStep::Pending
                                    }
                                } else {
                                    unreachable!();
                                }
                            }
                        } else {
                            NextStep::Pending
                        }
                    },
                    _ => {
                        NextStep::Finish
                    }
                }
            }
        };

        match step {
            NextStep::Content(task) => {
                task.start()
            },
            NextStep::Pending => TaskState::Pending,
            _ =>{
                self.0.state.write().unwrap().control_state = TaskControlState::Finished;
                TaskState::Finished
            }
        }
    }
}

impl DownloadTaskControl for DirTask {
    fn control_state(&self) -> TaskControlState {
        self.0.state.read().unwrap().control_state.clone()
    }

    fn pause(&self) -> BuckyResult<TaskControlState> {
        Ok(self.control_state())
        //unimplemented!()
    }

    fn resume(&self) -> BuckyResult<TaskControlState> {
        Ok(self.control_state())
        //unimplemented!()
    }

    fn cancel(&self) -> BuckyResult<TaskControlState> {
        Ok(self.control_state())
        //unimplemented!()
    }
}

impl DownloadTask for DirTask {
    fn clone_as_download_task(&self) -> Box<dyn DownloadTask> {
        Box::new(self.clone())
    }
}

impl StatisticTask for DirTask {
    fn reset(&self) {
        self.0.statistic_task.reset()
    }

    fn on_stat(&self, size: u64) -> BuckyResult<Box<dyn PerfDataAbstract>> {
        let r = self.0.statistic_task.on_stat(size).unwrap();

        {
            let mut state = self.0.state.write().unwrap();
            match state.control_state {
                TaskControlState::Downloading(_, _) => {
                    state.control_state = TaskControlState::Downloading(r.bandwidth() as usize, self.0.statistic_task.progress())
                }
                _ => {}
            }
        }

        Ok(r)
    }

}