OSDN Git Service

worker空のリスナはmanagerで一旦トラップする
[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<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
81     private class ConvertListener implements WorkerListener<ConvertProgress> {
82
83         private final int id;
84
85         private ConvertListener(int id) {
86             this.id = id;
87         }
88
89         @Override
90         public void process(ConvertProgress progress) {
91             if (clientListner != null) {
92                 // TODO
93                 clientListner.process(id, TaskKind.CONVERT, TaskStatus.DOING, 0.0, "");
94             }
95         }
96     }
97 }
98
99 class ManageTarget<T> {
100
101     private final Request request;
102     private final Future<T> future;
103
104     ManageTarget(Request request, Future<T> future) {
105         this.request = request;
106         this.future = future;
107     }
108
109     Future<T> getFuture() {
110         return future;
111     }
112
113     Request getRequest() {
114         return request;
115     }
116 }