OSDN Git Service

コンフィグからスレッド数と待機時間を取得して設定
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / manager / TaskManage.java
1 package yukihane.inqubus.manager;
2
3 import java.io.File;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.Executors;
8 import java.util.concurrent.Future;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11 import saccubus.worker.WorkerListener;
12 import saccubus.worker.impl.MessageReportable;
13 import saccubus.worker.impl.PercentageReportable;
14 import saccubus.worker.impl.convert.Convert;
15 import saccubus.worker.impl.convert.ConvertProgress;
16 import saccubus.worker.impl.convert.ConvertResult;
17 import saccubus.worker.impl.download.Download;
18 import saccubus.worker.impl.download.DownloadProgress;
19 import saccubus.worker.impl.download.DownloadResult;
20 import saccubus.worker.profile.ConvertProfile;
21 import saccubus.worker.profile.DownloadProfile;
22
23 /**
24  *
25  * @author yuki
26  */
27 public class TaskManage {
28
29     private static final Logger logger = Logger.getLogger(TaskManage.class.getName());
30     private final ExecutorService downloadExecutorService;
31     private final ExecutorService convertExecutorService;
32     private final Map<Integer, ManageTarget<DownloadResult>> downloadTargets = new HashMap<>();
33     private final Map<Integer, ManageTarget<ConvertResult>> convertTargets = new HashMap<>();
34     private final TaskManageListener clientListener;
35     private final int waitDownload;
36
37     public TaskManage() {
38         this(1, 30, 1, null);
39     }
40
41     public TaskManage(int maxDownload, int maxConvert) {
42         this(maxDownload, 30, maxConvert, null);
43     }
44
45     public TaskManage(int maxDownload, int waitDownload, int maxConvert, TaskManageListener listener) {
46         downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
47         convertExecutorService = Executors.newFixedThreadPool(maxConvert);
48         this.waitDownload = waitDownload;
49         this.clientListener = listener;
50     }
51
52     public synchronized boolean add(RequestProcess request) {
53         final DownloadProfile dp = request.getDownloadProfile();
54         final ConvertProfile cp = request.getConvertProfile();
55         if (dp != null && (dp.getVideoProfile().isDownload() || dp.getCommentProfile().isDownload())) {
56             // ダウンロードするものがあればまずダウンロード処理
57             final Download task = new Download(dp, request.getVideoId(),
58                     new DownloadListener(request.getRowId()), waitDownload);
59             final Future<DownloadResult> future = downloadExecutorService.submit(task);
60             downloadTargets.put(request.getRowId(), new ManageTarget<>(request, future));
61             return true;
62
63         } else if (cp != null && cp.isConvert()) {
64             final Convert task = new Convert(cp, dp.getVideoProfile().getLocalFile(), dp.getCommentProfile().
65                     getLocalFile(), new ConvertListener(request.getRowId()));
66             final Future<ConvertResult> future = convertExecutorService.submit(task);
67             convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
68             return true;
69         }
70         return false;
71     }
72
73     private class DownloadListener extends TaskManageInnerListener<DownloadResult, DownloadProgress> {
74
75         private DownloadListener(int rowId) {
76             super(rowId);
77         }
78
79         @Override
80         public void done(DownloadResult result) {
81             super.done(result);
82             synchronized (TaskManage.this) {
83                 final ManageTarget<DownloadResult> mt = downloadTargets.remove(Integer.valueOf(getRowId()));
84                 final RequestProcess request = mt.getRequest();
85                 if (request.getConvertProfile().isConvert()) {
86                     final DownloadProfile dp = request.getDownloadProfile();
87                     final File video = (dp.getVideoProfile().isDownload()) ? result.getDownloadVideo() : dp.
88                             getVideoProfile().getLocalFile();
89                     final File comment = (dp.getCommentProfile().isDownload()) ? result.getDownloadComment() : dp.
90                             getCommentProfile().getLocalFile();
91                     final ConvertProfile cp = request.getConvertProfile();
92                     final Convert task = new Convert(cp, video, comment, new ConvertListener(getRowId()));
93                     final Future<ConvertResult> future = convertExecutorService.submit(task);
94                     convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
95                 }
96                 // TODO 変換が必要なら変換キューに入れる
97             }
98
99         }
100
101         @Override
102         protected TaskKind getKind() {
103             return TaskKind.DOWNLOAD;
104         }
105     }
106
107     private class ConvertListener extends TaskManageInnerListener<ConvertResult, ConvertProgress> {
108
109         private ConvertListener(int rowId) {
110             super(rowId);
111         }
112
113         @Override
114         protected TaskKind getKind() {
115             return TaskKind.CONVERT;
116         }
117
118         @Override
119         public void done(ConvertResult result) {
120             super.done(result);
121             synchronized (TaskManage.this) {
122                 convertTargets.remove(getRowId());
123             }
124         }
125     }
126
127     abstract class TaskManageInnerListener<T, V extends PercentageReportable & MessageReportable> implements WorkerListener<T, V> {
128
129         private final int rowId;
130
131         protected TaskManageInnerListener(int rowId) {
132             this.rowId = rowId;
133         }
134
135         protected int getRowId() {
136             return rowId;
137         }
138
139         private void notify(TaskStatus status) {
140             notify(status, -1.0, "");
141         }
142
143         private void notify(TaskStatus status, double percentage, String message) {
144             if (getListener() == null) {
145                 return;
146             }
147             getListener().process(rowId, getKind(), status, percentage, message);
148         }
149
150         private TaskManageListener getListener() {
151             return clientListener;
152         }
153
154         protected abstract TaskKind getKind();
155
156         @Override
157         public void process(V progress) {
158             logger.log(Level.FINEST, "process: {0}", progress);
159             notify(TaskStatus.DOING, progress.getPercentage(), progress.getMessage());
160         }
161
162         @Override
163         public void cancelled() {
164             logger.log(Level.FINE, "cancelled: {0}", toString());
165             notify(TaskStatus.CANCELLED);
166         }
167
168         @Override
169         public void done(T result) {
170             logger.log(Level.FINE, "done: {0}", result);
171             notify(TaskStatus.DONE);
172         }
173
174         @Override
175         public void error(Throwable th) {
176             logger.log(Level.SEVERE, "error", th);
177             notify(TaskStatus.ERROR, 0.0, th.getMessage());
178         }
179     }
180
181     class ManageTarget<T> {
182
183         private final RequestProcess request;
184         private final Future<T> future;
185
186         ManageTarget(RequestProcess request, Future<T> future) {
187             this.request = request;
188             this.future = future;
189         }
190
191         Future<T> getFuture() {
192             return future;
193         }
194
195         RequestProcess getRequest() {
196             return request;
197         }
198     }
199 }