OSDN Git Service

ダウンロード処理が必要な条件判定誤り修正
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / manager / TaskManage.java
1 package yukihane.inqubus.manager;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.concurrent.ExecutorService;
6 import java.util.concurrent.Executors;
7 import java.util.concurrent.Future;
8 import saccubus.worker.WorkerListener;
9 import saccubus.worker.convert.Convert;
10 import saccubus.worker.convert.ConvertProgress;
11 import saccubus.worker.convert.ConvertResult;
12 import saccubus.worker.download.Download;
13 import saccubus.worker.download.DownloadProgress;
14 import saccubus.worker.download.DownloadResult;
15 import saccubus.worker.profile.ConvertProfile;
16 import saccubus.worker.profile.DownloadProfile;
17
18 /**
19  *
20  * @author yuki
21  */
22 public class TaskManage {
23
24     private final ExecutorService downloadExecutorService;
25     private final ExecutorService convertExecutorService;
26     private final Map<Integer, ManageTarget<DownloadResult>> downloadTargets = new HashMap<>();
27     private final Map<Integer, ManageTarget<ConvertResult>> convertTargets = new HashMap<>();
28     private final TaskManageListener clientListener;
29
30     public TaskManage() {
31         this(1, 1, null);
32     }
33
34     public TaskManage(int maxDownload, int maxConvert) {
35         this(maxDownload, maxConvert, null);
36     }
37
38     public TaskManage(int maxDownload, int maxConvert, TaskManageListener listener) {
39         downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
40         convertExecutorService = Executors.newFixedThreadPool(maxConvert);
41         this.clientListener = listener;
42     }
43
44     public synchronized boolean add(RequestProcess request) {
45         final DownloadProfile dp = request.getDownloadProfile();
46         final ConvertProfile cp = request.getConvertProfile();
47         if (dp != null && (dp.getVideoProfile().isDownload() || dp.getCommentProfile().isDownload())) {
48             // ダウンロードするものがあればまずダウンロード処理
49             final Download task = new Download(dp, request.getVideoId(),
50                     new DownloadListener(request.getRowId()));
51             final Future<DownloadResult> future = downloadExecutorService.submit(task);
52             downloadTargets.put(request.getRowId(), new ManageTarget<>(request, future));
53             return true;
54
55         } else if (cp != null && cp.isConvert()) {
56             final Convert task = new Convert(cp, dp.getVideoProfile().getLocalFile(), dp.getCommentProfile().
57                     getLocalFile(), new ConvertListener(request.getRowId()));
58             final Future<ConvertResult> future = convertExecutorService.submit(task);
59             convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
60             return true;
61         }
62         return false;
63     }
64
65     private class DownloadListener extends TaskManageInnerListener<DownloadResult, DownloadProgress> {
66
67         private DownloadListener(int rowId) {
68             super(rowId);
69         }
70
71         @Override
72         public void done(DownloadResult result) {
73             super.done(result);
74             // TODO 変換が必要なら変換キューに入れる
75         }
76
77         @Override
78         protected TaskKind getKind() {
79             return TaskKind.DOWNLOAD;
80         }
81     }
82
83     private class ConvertListener extends TaskManageInnerListener<ConvertResult, ConvertProgress> {
84
85         private ConvertListener(int rowId) {
86             super(rowId);
87         }
88
89         @Override
90         protected TaskKind getKind() {
91             return TaskKind.CONVERT;
92         }
93     }
94
95     abstract class TaskManageInnerListener<T, V> implements WorkerListener<T, V> {
96
97         private final int rowId;
98
99         protected TaskManageInnerListener(int rowId) {
100             this.rowId = rowId;
101         }
102
103         private void notify(TaskStatus status) {
104             notify(status, 0.0, "");
105         }
106
107         private void notify(TaskStatus status, double percentage, String message) {
108             if (getListener() == null) {
109                 return;
110             }
111             getListener().process(rowId, getKind(), status, percentage, message);
112         }
113
114         private TaskManageListener getListener() {
115             return clientListener;
116         }
117
118         protected abstract TaskKind getKind();
119
120         @Override
121         public void process(V progress) {
122             // TOOD
123             notify(TaskStatus.DOING, 0.0, "");
124         }
125
126         @Override
127         public void cancelled() {
128             notify(TaskStatus.CANCELLED);
129         }
130
131         @Override
132         public void done(T result) {
133             notify(TaskStatus.DONE);
134         }
135
136         @Override
137         public void error(Throwable th) {
138             notify(TaskStatus.ERROR, 0.0, th.getMessage());
139         }
140     }
141
142     class ManageTarget<T> {
143
144         private final RequestProcess request;
145         private final Future<T> future;
146
147         ManageTarget(RequestProcess request, Future<T> future) {
148             this.request = request;
149             this.future = future;
150         }
151
152         Future<T> getFuture() {
153             return future;
154         }
155
156         RequestProcess getRequest() {
157             return request;
158         }
159     }
160 }