OSDN Git Service

ea5d39fbc69f0cc8f821e01219f7671cca24f301
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / manager / TaskManager.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.Convert;
9 import saccubus.worker.ConvertResult;
10 import saccubus.worker.Download;
11 import saccubus.worker.DownloadResult;
12 import saccubus.worker.profile.ConvertProfile;
13 import saccubus.worker.profile.DownloadProfile;
14
15 /**
16  *
17  * @author yuki
18  */
19 public class TaskManager {
20
21 //    private final LinkedBlockingQueue<DownloadTask> downloadTaskQueue = new LinkedBlockingQueue<>();
22 //    private final LinkedBlockingQueue<ConvertTask> convertTaskQueue = new LinkedBlockingQueue<>();
23     private final ExecutorService downloadExecutorService;
24     private final ExecutorService convertExecutorService;
25     private final Map<Integer, ManageTarget<DownloadResult>> downloadTargets = new HashMap<>();
26     private final Map<Integer, ManageTarget<ConvertResult>> convertTargets = new HashMap<>();
27
28     private TaskManager(int maxDownload, int maxConvert) {
29         downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
30         convertExecutorService = Executors.newFixedThreadPool(maxConvert);
31     }
32
33     public synchronized boolean add(Request request) {
34         final DownloadProfile dp = request.getDownloadProfile();
35         final ConvertProfile cp = request.getConvertProfile();
36         if (dp != null && !dp.getVideoProfile().isDownload() && !dp.getCommentProfile().isDownload()) {
37             final Future<DownloadResult> future = downloadExecutorService.submit(new Download(dp, request.getVideoId()));
38             downloadTargets.put(request.getRowId(), new ManageTarget<>(request, future));
39             return true;
40
41         } else if (cp != null && cp.isConvert()) {
42             final Future<ConvertResult> future = convertExecutorService.submit(new Convert(cp, dp.getVideoProfile().
43                     getLocalFile(), dp.getCommentProfile().getLocalFile()));
44             convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
45             return true;
46         }
47         return false;
48     }
49 }
50
51 class ManageTarget<T> {
52
53     private final Request request;
54     private final Future<T> future;
55
56     ManageTarget(Request request, Future<T> future) {
57         this.request = request;
58         this.future = future;
59     }
60
61     Future<T> getFuture() {
62         return future;
63     }
64
65     Request getRequest() {
66         return request;
67     }
68 }