1 package yukihane.inqubus.manager;
4 import java.util.HashMap;
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;
27 public class TaskManage {
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;
40 public TaskManage(int maxDownload, int maxConvert) {
41 this(maxDownload, maxConvert, null);
44 public TaskManage(int maxDownload, int maxConvert, TaskManageListener listener) {
45 downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
46 convertExecutorService = Executors.newFixedThreadPool(maxConvert);
47 this.clientListener = listener;
50 public synchronized boolean add(RequestProcess request) {
51 final DownloadProfile dp = request.getDownloadProfile();
52 final ConvertProfile cp = request.getConvertProfile();
53 if (dp != null && (dp.getVideoProfile().isDownload() || dp.getCommentProfile().isDownload())) {
54 // ダウンロードするものがあればまずダウンロード処理
55 final Download task = new Download(dp, request.getVideoId(),
56 new DownloadListener(request.getRowId()));
57 final Future<DownloadResult> future = downloadExecutorService.submit(task);
58 downloadTargets.put(request.getRowId(), new ManageTarget<>(request, future));
61 } else if (cp != null && cp.isConvert()) {
62 final Convert task = new Convert(cp, dp.getVideoProfile().getLocalFile(), dp.getCommentProfile().
63 getLocalFile(), new ConvertListener(request.getRowId()));
64 final Future<ConvertResult> future = convertExecutorService.submit(task);
65 convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
71 private class DownloadListener extends TaskManageInnerListener<DownloadResult, DownloadProgress> {
73 private DownloadListener(int rowId) {
78 public void done(DownloadResult result) {
80 synchronized (TaskManage.this) {
81 final ManageTarget<DownloadResult> mt = downloadTargets.remove(Integer.valueOf(getRowId()));
82 final RequestProcess request = mt.getRequest();
83 if (request.getConvertProfile().isConvert()) {
84 final DownloadProfile dp = request.getDownloadProfile();
85 final File video = (dp.getVideoProfile().isDownload()) ? result.getDownloadVideo() : dp.
86 getVideoProfile().getLocalFile();
87 final File comment = (dp.getCommentProfile().isDownload()) ? result.getDownloadComment() : dp.
88 getCommentProfile().getLocalFile();
89 final ConvertProfile cp = request.getConvertProfile();
90 final Convert task = new Convert(cp, video, comment, new ConvertListener(getRowId()));
91 final Future<ConvertResult> future = convertExecutorService.submit(task);
92 convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
94 // TODO 変換が必要なら変換キューに入れる
100 protected TaskKind getKind() {
101 return TaskKind.DOWNLOAD;
105 private class ConvertListener extends TaskManageInnerListener<ConvertResult, ConvertProgress> {
107 private ConvertListener(int rowId) {
112 protected TaskKind getKind() {
113 return TaskKind.CONVERT;
117 public void done(ConvertResult result) {
119 synchronized (TaskManage.this) {
120 convertTargets.remove(getRowId());
125 abstract class TaskManageInnerListener<T, V extends PercentageReportable & MessageReportable> implements WorkerListener<T, V> {
127 private final int rowId;
129 protected TaskManageInnerListener(int rowId) {
133 protected int getRowId() {
137 private void notify(TaskStatus status) {
138 notify(status, 0.0, "");
141 private void notify(TaskStatus status, double percentage, String message) {
142 if (getListener() == null) {
145 getListener().process(rowId, getKind(), status, percentage, message);
148 private TaskManageListener getListener() {
149 return clientListener;
152 protected abstract TaskKind getKind();
155 public void process(V progress) {
156 logger.log(Level.FINEST, "process: {0}", progress);
157 notify(TaskStatus.DOING, progress.getPercentage(), progress.getMessage());
161 public void cancelled() {
162 logger.log(Level.FINE, "cancelled: {0}", toString());
163 notify(TaskStatus.CANCELLED);
167 public void done(T result) {
168 logger.log(Level.FINE, "done: {0}", result);
169 notify(TaskStatus.DONE);
173 public void error(Throwable th) {
174 logger.log(Level.SEVERE, "error", th);
175 notify(TaskStatus.ERROR, 0.0, th.getMessage());
179 class ManageTarget<T> {
181 private final RequestProcess request;
182 private final Future<T> future;
184 ManageTarget(RequestProcess request, Future<T> future) {
185 this.request = request;
186 this.future = future;
189 Future<T> getFuture() {
193 RequestProcess getRequest() {