OSDN Git Service

共通化
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / manager / TaskManage.java
1 package yukihane.inqubus.manager;
2
3 import java.util.HashMap;
4 import java.util.Map;
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;
17
18 /**
19  *
20  * @author yuki
21  */
22 public class TaskManage {
23
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;
29
30     public TaskManage() {
31         this(1, 1, null);
32     }
33
34     public TaskManage(int maxDownload, int maxConvert) {
35         this(maxDownload, maxConvert, null);
36     }
37
38     public TaskManage(int maxDownload, int maxConvert, TaskManageListener listener) {
39         downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
40         convertExecutorService = Executors.newFixedThreadPool(maxConvert);
41         this.clientListener = listener;
42     }
43
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));
52             return true;
53
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));
59             return true;
60         }
61         return false;
62     }
63
64     private class DownloadListener extends TaskManageInnerListener<DownloadResult, DownloadProgress> {
65
66         private DownloadListener(int rowId) {
67             super(rowId);
68         }
69
70         @Override
71         public void done(DownloadResult result) {
72             super.done(result);
73             // TODO 変換が必要なら変換キューに入れる
74         }
75
76         @Override
77         protected TaskKind getKind() {
78             return TaskKind.DOWNLOAD;
79         }
80     }
81
82     private class ConvertListener extends TaskManageInnerListener<ConvertResult, ConvertProgress> {
83
84         private ConvertListener(int rowId) {
85             super(rowId);
86         }
87
88         @Override
89         protected TaskKind getKind() {
90             return TaskKind.CONVERT;
91         }
92     }
93
94     abstract class TaskManageInnerListener<T, V> implements WorkerListener<T, V> {
95
96         private final int rowId;
97
98         protected TaskManageInnerListener(int rowId) {
99             this.rowId = rowId;
100         }
101
102         private void notify(TaskStatus status) {
103             notify(status, 0.0, "");
104         }
105
106         private void notify(TaskStatus status, double percentage, String message) {
107             if (getListener() == null) {
108                 return;
109             }
110             getListener().process(rowId, getKind(), status, percentage, message);
111         }
112
113         private TaskManageListener getListener() {
114             return clientListener;
115         }
116
117         protected abstract TaskKind getKind();
118
119         @Override
120         public void process(V progress) {
121             // TOOD
122             notify(TaskStatus.DOING, 0.0, "");
123         }
124
125         @Override
126         public void cancelled() {
127             notify(TaskStatus.CANCELLED);
128         }
129
130         @Override
131         public void done(T result) {
132             notify(TaskStatus.DONE);
133         }
134
135         @Override
136         public void error(Throwable th) {
137             notify(TaskStatus.ERROR, 0.0, th.getMessage());
138         }
139     }
140
141     class ManageTarget<T> {
142
143         private final RequestProcess request;
144         private final Future<T> future;
145
146         ManageTarget(RequestProcess request, Future<T> future) {
147             this.request = request;
148             this.future = future;
149         }
150
151         Future<T> getFuture() {
152             return future;
153         }
154
155         RequestProcess getRequest() {
156             return request;
157         }
158     }
159 }