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 implements WorkerListener<DownloadResult, DownloadProgress> {
65
66         private final int rowId;
67
68         private DownloadListener(int rowId) {
69             this.rowId = rowId;
70         }
71
72         private void notify(TaskStatus status) {
73             notify(status, 0.0, "");
74         }
75
76         private void notify(TaskStatus status, double percentage, String message) {
77             if (clientListener == null) {
78                 return;
79             }
80             clientListener.process(rowId, TaskKind.DOWNLOAD, status, percentage, message);
81         }
82
83         @Override
84         public void process(DownloadProgress progress) {
85             // TOOD
86             notify(TaskStatus.DOING, 0.0, "");
87         }
88
89         @Override
90         public void cancelled() {
91             notify(TaskStatus.CANCELLED);
92         }
93
94         @Override
95         public void done(DownloadResult result) {
96             notify(TaskStatus.DONE);
97         }
98
99         @Override
100         public void error(Throwable th) {
101             notify(TaskStatus.ERROR, 0.0, th.getMessage());
102         }
103     }
104
105     private class ConvertListener implements WorkerListener<ConvertResult, ConvertProgress> {
106
107         private final int rowId;
108
109         private ConvertListener(int rowId) {
110             this.rowId = rowId;
111         }
112
113         private void notify(TaskStatus status) {
114             notify(status, 0.0, "");
115         }
116
117         private void notify(TaskStatus status, double percentage, String message) {
118             if (clientListener == null) {
119                 return;
120             }
121             clientListener.process(rowId, TaskKind.DOWNLOAD, status, percentage, message);
122         }
123
124         @Override
125         public void process(ConvertProgress progress) {
126             // TOOD
127             notify(TaskStatus.DOING, 0.0, "");
128         }
129
130         @Override
131         public void cancelled() {
132             notify(TaskStatus.CANCELLED);
133         }
134
135         @Override
136         public void done(ConvertResult result) {
137             notify(TaskStatus.DONE);
138         }
139
140         @Override
141         public void error(Throwable th) {
142             notify(TaskStatus.ERROR, 0.0, th.getMessage());
143         }
144     }
145 }
146
147 class ManageTarget<T> {
148
149     private final RequestProcess request;
150     private final Future<T> future;
151
152     ManageTarget(RequestProcess request, Future<T> future) {
153         this.request = request;
154         this.future = future;
155     }
156
157     Future<T> getFuture() {
158         return future;
159     }
160
161     RequestProcess getRequest() {
162         return request;
163     }
164 }