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 clientListener;
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.clientListener = 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 extends TaskManageInnerListener<DownloadResult, DownloadProgress> {
66 private DownloadListener(int rowId) {
71 public void done(DownloadResult result) {
73 // TODO 変換が必要なら変換キューに入れる
77 protected TaskKind getKind() {
78 return TaskKind.DOWNLOAD;
82 private class ConvertListener extends TaskManageInnerListener<ConvertResult, ConvertProgress> {
84 private ConvertListener(int rowId) {
89 protected TaskKind getKind() {
90 return TaskKind.CONVERT;
94 abstract class TaskManageInnerListener<T, V> implements WorkerListener<T, V> {
96 private final int rowId;
98 protected TaskManageInnerListener(int rowId) {
102 private void notify(TaskStatus status) {
103 notify(status, 0.0, "");
106 private void notify(TaskStatus status, double percentage, String message) {
107 if (getListener() == null) {
110 getListener().process(rowId, getKind(), status, percentage, message);
113 private TaskManageListener getListener() {
114 return clientListener;
117 protected abstract TaskKind getKind();
120 public void process(V progress) {
122 notify(TaskStatus.DOING, 0.0, "");
126 public void cancelled() {
127 notify(TaskStatus.CANCELLED);
131 public void done(T result) {
132 notify(TaskStatus.DONE);
136 public void error(Throwable th) {
137 notify(TaskStatus.ERROR, 0.0, th.getMessage());
141 class ManageTarget<T> {
143 private final RequestProcess request;
144 private final Future<T> future;
146 ManageTarget(RequestProcess request, Future<T> future) {
147 this.request = request;
148 this.future = future;
151 Future<T> getFuture() {
155 RequestProcess getRequest() {