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 clientListner;
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.clientListner = listener;
42     }
43
44     public synchronized boolean add(Request 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 implements WorkerListener<DownloadResult, DownloadProgress> {
65
66         private final int rowId;
67
68         private DownloadListener(int rowId) {
69             this.rowId = rowId;
70         }
71
72         @Override
73         public void process(DownloadProgress progress) {
74             if (clientListner != null) {
75                 // TODO
76                 clientListner.process(rowId, TaskKind.DOWNLOAD, TaskStatus.DOING, 0.0, "");
77             }
78         }
79
80         @Override
81         public void cancelled() {
82             throw new UnsupportedOperationException("Not supported yet.");
83         }
84
85         @Override
86         public void done(DownloadResult result) {
87             throw new UnsupportedOperationException("Not supported yet.");
88         }
89
90         @Override
91         public void error(Throwable th) {
92             throw new UnsupportedOperationException("Not supported yet.");
93         }
94     }
95
96     private class ConvertListener implements WorkerListener<ConvertResult, ConvertProgress> {
97
98         private final int id;
99
100         private ConvertListener(int id) {
101             this.id = id;
102         }
103
104         @Override
105         public void process(ConvertProgress progress) {
106             if (clientListner != null) {
107                 // TODO
108                 clientListner.process(id, TaskKind.CONVERT, TaskStatus.DOING, 0.0, "");
109             }
110         }
111
112         @Override
113         public void cancelled() {
114             throw new UnsupportedOperationException("Not supported yet.");
115         }
116
117         @Override
118         public void done(ConvertResult result) {
119             throw new UnsupportedOperationException("Not supported yet.");
120         }
121
122         @Override
123         public void error(Throwable th) {
124             throw new UnsupportedOperationException("Not supported yet.");
125         }
126     }
127 }
128
129 class ManageTarget<T> {
130
131     private final Request request;
132     private final Future<T> future;
133
134     ManageTarget(Request request, Future<T> future) {
135         this.request = request;
136         this.future = future;
137     }
138
139     Future<T> getFuture() {
140         return future;
141     }
142
143     Request getRequest() {
144         return request;
145     }
146 }