1 package yukihane.inqubus.manager;
3 import java.util.HashMap;
5 import java.util.concurrent.ExecutorService;
6 import java.util.concurrent.Executors;
7 import java.util.concurrent.Future;
8 import java.util.logging.Level;
9 import java.util.logging.Logger;
10 import saccubus.worker.WorkerListener;
11 import saccubus.worker.impl.convert.Convert;
12 import saccubus.worker.impl.convert.ConvertProgress;
13 import saccubus.worker.impl.convert.ConvertResult;
14 import saccubus.worker.impl.download.Download;
15 import saccubus.worker.impl.download.DownloadProgress;
16 import saccubus.worker.impl.download.DownloadResult;
17 import saccubus.worker.profile.ConvertProfile;
18 import saccubus.worker.profile.DownloadProfile;
24 public class TaskManage {
25 private static final Logger logger = Logger.getLogger(TaskManage.class.getName());
26 private final ExecutorService downloadExecutorService;
27 private final ExecutorService convertExecutorService;
28 private final Map<Integer, ManageTarget<DownloadResult>> downloadTargets = new HashMap<>();
29 private final Map<Integer, ManageTarget<ConvertResult>> convertTargets = new HashMap<>();
30 private final TaskManageListener clientListener;
36 public TaskManage(int maxDownload, int maxConvert) {
37 this(maxDownload, maxConvert, null);
40 public TaskManage(int maxDownload, int maxConvert, TaskManageListener listener) {
41 downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
42 convertExecutorService = Executors.newFixedThreadPool(maxConvert);
43 this.clientListener = listener;
46 public synchronized boolean add(RequestProcess request) {
47 final DownloadProfile dp = request.getDownloadProfile();
48 final ConvertProfile cp = request.getConvertProfile();
49 if (dp != null && (dp.getVideoProfile().isDownload() || dp.getCommentProfile().isDownload())) {
50 // ダウンロードするものがあればまずダウンロード処理
51 final Download task = new Download(dp, request.getVideoId(),
52 new DownloadListener(request.getRowId()));
53 final Future<DownloadResult> future = downloadExecutorService.submit(task);
54 downloadTargets.put(request.getRowId(), new ManageTarget<>(request, future));
57 } else if (cp != null && cp.isConvert()) {
58 final Convert task = new Convert(cp, dp.getVideoProfile().getLocalFile(), dp.getCommentProfile().
59 getLocalFile(), new ConvertListener(request.getRowId()));
60 final Future<ConvertResult> future = convertExecutorService.submit(task);
61 convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
67 private class DownloadListener extends TaskManageInnerListener<DownloadResult, DownloadProgress> {
69 private DownloadListener(int rowId) {
74 public void done(DownloadResult result) {
76 // TODO 変換が必要なら変換キューに入れる
80 protected TaskKind getKind() {
81 return TaskKind.DOWNLOAD;
85 private class ConvertListener extends TaskManageInnerListener<ConvertResult, ConvertProgress> {
87 private ConvertListener(int rowId) {
92 protected TaskKind getKind() {
93 return TaskKind.CONVERT;
97 abstract class TaskManageInnerListener<T, V> implements WorkerListener<T, V> {
99 private final int rowId;
101 protected TaskManageInnerListener(int rowId) {
105 private void notify(TaskStatus status) {
106 notify(status, 0.0, "");
109 private void notify(TaskStatus status, double percentage, String message) {
110 if (getListener() == null) {
113 getListener().process(rowId, getKind(), status, percentage, message);
116 private TaskManageListener getListener() {
117 return clientListener;
120 protected abstract TaskKind getKind();
123 public void process(V progress) {
124 logger.log(Level.FINEST, "process: {0}", progress);
126 notify(TaskStatus.DOING, 0.0, "");
130 public void cancelled() {
131 logger.log(Level.FINE, "cancelled: {0}", toString());
132 notify(TaskStatus.CANCELLED);
136 public void done(T result) {
137 logger.log(Level.FINE, "done: {0}", result);
138 notify(TaskStatus.DONE);
142 public void error(Throwable th) {
143 logger.log(Level.SEVERE, "error", th);
144 notify(TaskStatus.ERROR, 0.0, th.getMessage());
148 class ManageTarget<T> {
150 private final RequestProcess request;
151 private final Future<T> future;
153 ManageTarget(RequestProcess request, Future<T> future) {
154 this.request = request;
155 this.future = future;
158 Future<T> getFuture() {
162 RequestProcess getRequest() {