OSDN Git Service

マネージャ実装
authoryukihane <yukihane.feather@gmail.com>
Fri, 26 Aug 2011 08:00:12 +0000 (17:00 +0900)
committeryukihane <yukihane.feather@gmail.com>
Fri, 26 Aug 2011 08:00:12 +0000 (17:00 +0900)
frontend/src/yukihane/inqubus/gui/MainFrame.java
frontend/src/yukihane/inqubus/manager/Request.java
frontend/src/yukihane/inqubus/manager/TaskManager.java

index f246344..3b05f30 100644 (file)
@@ -49,7 +49,9 @@ import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import saccubus.MainFrame_AboutBox;
 import saccubus.util.WayBackTimeParser;
-import saccubus.worker.Download;
+import saccubus.worker.ConvertProgress;
+import saccubus.worker.DownloadProgress;
+import saccubus.worker.SaccubusCallable.SaccubusListener;
 import saccubus.worker.profile.CommentProfile;
 import saccubus.worker.profile.DownloadProfile;
 import saccubus.worker.profile.GeneralProfile;
@@ -567,6 +569,24 @@ public class MainFrame extends JFrame {
         return menuBar;
     }
 
+    private class DownloadProgressListener implements SaccubusListener<DownloadProgress> {
+
+        @Override
+        public void process(DownloadProgress progress) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+    }
+
+    private class ConvertProgressListener implements SaccubusListener<ConvertProgress> {
+
+        @Override
+        public void process(ConvertProgress progress) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+    }
+
+
+
     private class DownloadListTransferHandler extends TransferHandler {
 
         private static final long serialVersionUID = 1L;
index 57bddb5..9833a16 100644 (file)
@@ -1,9 +1,41 @@
 package yukihane.inqubus.manager;
 
+import java.io.File;
+import saccubus.worker.profile.ConvertProfile;
+import saccubus.worker.profile.DownloadProfile;
+
 /**
  *
  * @author yuki
  */
-public interface Request {
+public class Request {
+    private static int serialId;
+
+    private final int rowId;
+    private final DownloadProfile downloadProfile;
+    private final String videoId;
+    private final ConvertProfile convertProfile;
+
+    public Request(DownloadProfile download, String videoId, ConvertProfile convert, File video, File comment) {
+        this.rowId = ++serialId;
+        this.downloadProfile = download;
+        this.videoId = videoId;
+        this.convertProfile = convert;
+    }
+
+    public ConvertProfile getConvertProfile() {
+        return convertProfile;
+    }
+
+    public DownloadProfile getDownloadProfile() {
+        return downloadProfile;
+    }
+
+    public String getVideoId() {
+        return videoId;
+    }
 
+    Integer getRowId() {
+        return rowId;
+    }
 }
index 6bdeb41..ea5d39f 100644 (file)
@@ -1,7 +1,16 @@
 package yukihane.inqubus.manager;
 
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import saccubus.worker.Convert;
+import saccubus.worker.ConvertResult;
+import saccubus.worker.Download;
+import saccubus.worker.DownloadResult;
+import saccubus.worker.profile.ConvertProfile;
+import saccubus.worker.profile.DownloadProfile;
 
 /**
  *
@@ -13,14 +22,47 @@ public class TaskManager {
 //    private final LinkedBlockingQueue<ConvertTask> convertTaskQueue = new LinkedBlockingQueue<>();
     private final ExecutorService downloadExecutorService;
     private final ExecutorService convertExecutorService;
+    private final Map<Integer, ManageTarget<DownloadResult>> downloadTargets = new HashMap<>();
+    private final Map<Integer, ManageTarget<ConvertResult>> convertTargets = new HashMap<>();
 
     private TaskManager(int maxDownload, int maxConvert) {
         downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
         convertExecutorService = Executors.newFixedThreadPool(maxConvert);
     }
 
-    public void add(Request task) {
-        // TODO タスク種判断
-        // TODO タスク追加
+    public synchronized boolean add(Request request) {
+        final DownloadProfile dp = request.getDownloadProfile();
+        final ConvertProfile cp = request.getConvertProfile();
+        if (dp != null && !dp.getVideoProfile().isDownload() && !dp.getCommentProfile().isDownload()) {
+            final Future<DownloadResult> future = downloadExecutorService.submit(new Download(dp, request.getVideoId()));
+            downloadTargets.put(request.getRowId(), new ManageTarget<>(request, future));
+            return true;
+
+        } else if (cp != null && cp.isConvert()) {
+            final Future<ConvertResult> future = convertExecutorService.submit(new Convert(cp, dp.getVideoProfile().
+                    getLocalFile(), dp.getCommentProfile().getLocalFile()));
+            convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
+            return true;
+        }
+        return false;
+    }
+}
+
+class ManageTarget<T> {
+
+    private final Request request;
+    private final Future<T> future;
+
+    ManageTarget(Request request, Future<T> future) {
+        this.request = request;
+        this.future = future;
+    }
+
+    Future<T> getFuture() {
+        return future;
+    }
+
+    Request getRequest() {
+        return request;
     }
 }