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 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;
22 public class TaskManage {
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 clientListner;
34 public TaskManage(int maxDownload, int maxConvert) {
35 this(maxDownload, maxConvert, null);
38 public TaskManage(int maxDownload, int maxConvert, TaskManageListener listener) {
39 downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
40 convertExecutorService = Executors.newFixedThreadPool(maxConvert);
41 this.clientListner = listener;
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 final Download task = new Download(dp, request.getVideoId(),
49 new DownloadListener(request.getRowId()));
50 final Future<DownloadResult> future = downloadExecutorService.submit(task);
51 downloadTargets.put(request.getRowId(), new ManageTarget<>(request, future));
54 } else if (cp != null && cp.isConvert()) {
55 final Convert task = new Convert(cp, dp.getVideoProfile().getLocalFile(), dp.getCommentProfile().
56 getLocalFile(), new ConvertListener(request.getRowId()));
57 final Future<ConvertResult> future = convertExecutorService.submit(task);
58 convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
64 private class DownloadListener implements WorkerListener<DownloadResult, DownloadProgress> {
66 private final int rowId;
68 private DownloadListener(int rowId) {
73 public void process(DownloadProgress progress) {
74 if (clientListner != null) {
76 clientListner.process(rowId, TaskKind.DOWNLOAD, TaskStatus.DOING, 0.0, "");
81 public void cancelled() {
82 throw new UnsupportedOperationException("Not supported yet.");
86 public void done(DownloadResult result) {
87 throw new UnsupportedOperationException("Not supported yet.");
91 public void error(Throwable th) {
92 throw new UnsupportedOperationException("Not supported yet.");
96 private class ConvertListener implements WorkerListener<ConvertResult, ConvertProgress> {
100 private ConvertListener(int id) {
105 public void process(ConvertProgress progress) {
106 if (clientListner != null) {
108 clientListner.process(id, TaskKind.CONVERT, TaskStatus.DOING, 0.0, "");
113 public void cancelled() {
114 throw new UnsupportedOperationException("Not supported yet.");
118 public void done(ConvertResult result) {
119 throw new UnsupportedOperationException("Not supported yet.");
123 public void error(Throwable th) {
124 throw new UnsupportedOperationException("Not supported yet.");
129 class ManageTarget<T> {
131 private final RequestProcess request;
132 private final Future<T> future;
134 ManageTarget(RequestProcess request, Future<T> future) {
135 this.request = request;
136 this.future = future;
139 Future<T> getFuture() {
143 RequestProcess getRequest() {