package yukihane.inqubus.manager;
+import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import saccubus.worker.WorkerListener;
-import saccubus.worker.convert.Convert;
-import saccubus.worker.convert.ConvertProgress;
-import saccubus.worker.convert.ConvertResult;
-import saccubus.worker.download.Download;
-import saccubus.worker.download.DownloadProgress;
-import saccubus.worker.download.DownloadResult;
+import saccubus.worker.impl.MessageReportable;
+import saccubus.worker.impl.PercentageReportable;
+import saccubus.worker.impl.convert.Convert;
+import saccubus.worker.impl.convert.ConvertProgress;
+import saccubus.worker.impl.convert.ConvertResult;
+import saccubus.worker.impl.download.Download;
+import saccubus.worker.impl.download.DownloadProgress;
+import saccubus.worker.impl.download.DownloadResult;
import saccubus.worker.profile.ConvertProfile;
import saccubus.worker.profile.DownloadProfile;
*/
public class TaskManage {
+ private static final Logger logger = Logger.getLogger(TaskManage.class.getName());
private final ExecutorService downloadExecutorService;
private final ExecutorService convertExecutorService;
private final Map<Integer, ManageTarget<DownloadResult>> downloadTargets = new HashMap<>();
private final Map<Integer, ManageTarget<ConvertResult>> convertTargets = new HashMap<>();
- private final TaskManageListener clientListner;
+ private final TaskManageListener clientListener;
public TaskManage() {
this(1, 1, null);
public TaskManage(int maxDownload, int maxConvert, TaskManageListener listener) {
downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
convertExecutorService = Executors.newFixedThreadPool(maxConvert);
- this.clientListner = listener;
+ this.clientListener = listener;
}
- public synchronized boolean add(Request request) {
+ public synchronized boolean add(RequestProcess request) {
final DownloadProfile dp = request.getDownloadProfile();
final ConvertProfile cp = request.getConvertProfile();
- if (dp != null && !dp.getVideoProfile().isDownload() && !dp.getCommentProfile().isDownload()) {
+ if (dp != null && (dp.getVideoProfile().isDownload() || dp.getCommentProfile().isDownload())) {
+ // ダウンロードするものがあればまずダウンロード処理
final Download task = new Download(dp, request.getVideoId(),
new DownloadListener(request.getRowId()));
final Future<DownloadResult> future = downloadExecutorService.submit(task);
return false;
}
- private class DownloadListener implements WorkerListener<DownloadResult, DownloadProgress> {
-
- private final int rowId;
+ private class DownloadListener extends TaskManageInnerListener<DownloadResult, DownloadProgress> {
private DownloadListener(int rowId) {
- this.rowId = rowId;
+ super(rowId);
}
@Override
- public void process(DownloadProgress progress) {
- if (clientListner != null) {
- // TODO
- clientListner.process(rowId, TaskKind.DOWNLOAD, TaskStatus.DOING, 0.0, "");
+ public void done(DownloadResult result) {
+ super.done(result);
+ synchronized (TaskManage.this) {
+ final ManageTarget<DownloadResult> mt = downloadTargets.remove(Integer.valueOf(getRowId()));
+ final RequestProcess request = mt.getRequest();
+ if (request.getConvertProfile().isConvert()) {
+ final DownloadProfile dp = request.getDownloadProfile();
+ final File video = (dp.getVideoProfile().isDownload()) ? result.getDownloadVideo() : dp.
+ getVideoProfile().getLocalFile();
+ final File comment = (dp.getCommentProfile().isDownload()) ? result.getDownloadComment() : dp.
+ getCommentProfile().getLocalFile();
+ final ConvertProfile cp = request.getConvertProfile();
+ final Convert task = new Convert(cp, video, comment, new ConvertListener(getRowId()));
+ final Future<ConvertResult> future = convertExecutorService.submit(task);
+ convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
+ }
+ // TODO 変換が必要なら変換キューに入れる
}
+
}
@Override
- public void cancelled() {
- throw new UnsupportedOperationException("Not supported yet.");
+ protected TaskKind getKind() {
+ return TaskKind.DOWNLOAD;
+ }
+ }
+
+ private class ConvertListener extends TaskManageInnerListener<ConvertResult, ConvertProgress> {
+
+ private ConvertListener(int rowId) {
+ super(rowId);
}
@Override
- public void done(DownloadResult result) {
- throw new UnsupportedOperationException("Not supported yet.");
+ protected TaskKind getKind() {
+ return TaskKind.CONVERT;
}
@Override
- public void error(Throwable th) {
- throw new UnsupportedOperationException("Not supported yet.");
+ public void done(ConvertResult result) {
+ super.done(result);
+ synchronized (TaskManage.this) {
+ convertTargets.remove(getRowId());
+ }
}
}
- private class ConvertListener implements WorkerListener<ConvertResult, ConvertProgress> {
+ abstract class TaskManageInnerListener<T, V extends PercentageReportable & MessageReportable> implements WorkerListener<T, V> {
- private final int id;
+ private final int rowId;
- private ConvertListener(int id) {
- this.id = id;
+ protected TaskManageInnerListener(int rowId) {
+ this.rowId = rowId;
}
- @Override
- public void process(ConvertProgress progress) {
- if (clientListner != null) {
- // TODO
- clientListner.process(id, TaskKind.CONVERT, TaskStatus.DOING, 0.0, "");
+ protected int getRowId() {
+ return rowId;
+ }
+
+ private void notify(TaskStatus status) {
+ notify(status, -1.0, "");
+ }
+
+ private void notify(TaskStatus status, double percentage, String message) {
+ if (getListener() == null) {
+ return;
}
+ getListener().process(rowId, getKind(), status, percentage, message);
+ }
+
+ private TaskManageListener getListener() {
+ return clientListener;
+ }
+
+ protected abstract TaskKind getKind();
+
+ @Override
+ public void process(V progress) {
+ logger.log(Level.FINEST, "process: {0}", progress);
+ notify(TaskStatus.DOING, progress.getPercentage(), progress.getMessage());
}
@Override
public void cancelled() {
- throw new UnsupportedOperationException("Not supported yet.");
+ logger.log(Level.FINE, "cancelled: {0}", toString());
+ notify(TaskStatus.CANCELLED);
}
@Override
- public void done(ConvertResult result) {
- throw new UnsupportedOperationException("Not supported yet.");
+ public void done(T result) {
+ logger.log(Level.FINE, "done: {0}", result);
+ notify(TaskStatus.DONE);
}
@Override
public void error(Throwable th) {
- throw new UnsupportedOperationException("Not supported yet.");
+ logger.log(Level.SEVERE, "error", th);
+ notify(TaskStatus.ERROR, 0.0, th.getMessage());
}
}
-}
-class ManageTarget<T> {
+ class ManageTarget<T> {
- private final Request request;
- private final Future<T> future;
+ private final RequestProcess request;
+ private final Future<T> future;
- ManageTarget(Request request, Future<T> future) {
- this.request = request;
- this.future = future;
- }
+ ManageTarget(RequestProcess request, Future<T> future) {
+ this.request = request;
+ this.future = future;
+ }
- Future<T> getFuture() {
- return future;
- }
+ Future<T> getFuture() {
+ return future;
+ }
- Request getRequest() {
- return request;
+ RequestProcess getRequest() {
+ return request;
+ }
}
-}
+}
\ No newline at end of file