OSDN Git Service

workerのコンストラクタでリスナを受け取る
authoryukihane <yukihane.feather@gmail.com>
Sat, 27 Aug 2011 04:53:02 +0000 (13:53 +0900)
committeryukihane <yukihane.feather@gmail.com>
Sat, 27 Aug 2011 04:53:02 +0000 (13:53 +0900)
frontend/src/saccubus/worker/Convert.java
frontend/src/saccubus/worker/Download.java
frontend/src/saccubus/worker/SaccubusCallable.java [deleted file]
frontend/src/yukihane/inqubus/gui/MainFrame.java
frontend/src/yukihane/inqubus/manager/Request.java
frontend/src/yukihane/inqubus/manager/TaskManager.java

index 8a5a708..f87102e 100644 (file)
@@ -31,13 +31,17 @@ import yukihane.swf.Cws2Fws;
  * 動画を(コメント付きに)変換するワーカクラス.
  * @author yuki
  */
-public class Convert extends SaccubusCallable<ConvertResult, ConvertProgress> {
+public class Convert extends Worker<ConvertResult, ConvertProgress> {
 
     private static final Logger logger = Logger.getLogger(Convert.class.getName());
     private final ConvertProfile profile;
     private final File videoFile;
     private final File commentFile;
 
+    public Convert(ConvertProfile profile, File video, File comment) {
+        this(profile, video, comment, null);
+    }
+
     /**
      * 変換ワーカコンストラクタ.
      * @param profile 変換用プロファイル.
@@ -46,9 +50,8 @@ public class Convert extends SaccubusCallable<ConvertResult, ConvertProgress> {
      * @param output 変換後出力動画.
      * @throws IOException 変換失敗.
      */
-    public Convert(ConvertProfile profile, File video, File comment) {
-        // TODO listener登録
-        super(null);
+    public Convert(ConvertProfile profile, File video, File comment, SaccubusListener<ConvertProgress> listener) {
+        super(listener);
         this.profile = profile;
         this.videoFile = video;
         this.commentFile = comment;
index c1fa449..c2044fe 100644 (file)
@@ -31,12 +31,16 @@ import saccubus.worker.profile.ProxyProfile;
  * @author 未入力
  * @version 1.0
  */
-public class Download extends SaccubusCallable<DownloadResult, DownloadProgress>  {
+public class Download extends Worker<DownloadResult, DownloadProgress> {
 
     private static final Logger logger = Logger.getLogger(Download.class.getName());
     private final DownloadProfile profile;
     private final String videoId;
 
+    public Download(DownloadProfile profile, String videoId) {
+        this(profile, videoId, null);
+    }
+
     /**
      * コンバータを構築します.
      * @param videoId 対象となる動画のID.
@@ -45,9 +49,9 @@ public class Download extends SaccubusCallable<DownloadResult, DownloadProgress>
      * @param listener
      * @param flag
      */
-    public Download(DownloadProfile profile, String videoId) {
+    public Download(DownloadProfile profile, String videoId, SaccubusListener<DownloadProgress> listener) {
         // TODO listener登録
-        super(null);
+        super(listener);
         this.videoId = videoId;
         this.profile = profile;
     }
@@ -62,7 +66,6 @@ public class Download extends SaccubusCallable<DownloadResult, DownloadProgress>
 ////            getStopFlag().finished();
 //        }
 //    }
-
 //    // TODO Runnableを実装しなくなったので削除する
 //    public void run() {
 //        try {
@@ -196,7 +199,6 @@ public class Download extends SaccubusCallable<DownloadResult, DownloadProgress>
 //            }
 //        }
 //    }
-
     /**
      * HttpClientを生成し, ニコニコ動画サーバへログインします.
      * @return 生成したHttpClientインスタンス.
diff --git a/frontend/src/saccubus/worker/SaccubusCallable.java b/frontend/src/saccubus/worker/SaccubusCallable.java
deleted file mode 100644 (file)
index d266c93..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-package saccubus.worker;
-
-import java.util.concurrent.Callable;
-
-/**
- * 途中経過を報告できるCallableです.
- *
- * @author yuki
- */
-public abstract class SaccubusCallable<T, V> implements Callable<T> {
-    private static int serialNumber = 0;
-
-    private final int id;
-    private final SaccubusListener<V> listener;
-
-    public SaccubusCallable(SaccubusListener<V> listener) {
-        this.id = ++serialNumber;
-        this.listener = listener;
-    }
-
-    public final int getId() {
-        return id;
-    }
-
-    protected final void publish(V value) {
-        if (listener != null) {
-            listener.process(value);
-        }
-    }
-
-    public interface SaccubusListener<V> {
-
-        void process(V progress);
-    }
-}
index 3b05f30..e3a2dd1 100644 (file)
@@ -51,7 +51,7 @@ import saccubus.MainFrame_AboutBox;
 import saccubus.util.WayBackTimeParser;
 import saccubus.worker.ConvertProgress;
 import saccubus.worker.DownloadProgress;
-import saccubus.worker.SaccubusCallable.SaccubusListener;
+import saccubus.worker.Worker.SaccubusListener;
 import saccubus.worker.profile.CommentProfile;
 import saccubus.worker.profile.DownloadProfile;
 import saccubus.worker.profile.GeneralProfile;
@@ -60,6 +60,7 @@ import saccubus.worker.profile.ProxyProfile;
 import saccubus.worker.profile.VideoProfile;
 import yukihane.Util;
 import yukihane.inqubus.Config;
+import yukihane.inqubus.manager.TaskManager;
 import yukihane.inqubus.model.Target;
 import yukihane.inqubus.model.TargetsTableModel;
 
@@ -80,6 +81,9 @@ public class MainFrame extends JFrame {
     private static final String FILE_OUTPUTFIELD_TOOLTIP
             = "ファイル命名規則入力します。";
     private final TargetsTableModel targetModel = new TargetsTableModel();
+    private final TaskManager taskManager;
+    private final DownloadProgressListener downloadProgressListener = new DownloadProgressListener();
+    private final ConvertProgressListener convertProgressListener = new ConvertProgressListener();
 
     /** Creates new form MainFrame */
     public MainFrame() {
@@ -339,6 +343,11 @@ public class MainFrame extends JFrame {
         initInputPanel();
         pnlMain.setTransferHandler(new DownloadListTransferHandler());
         tblDisplay.setTransferHandler(new TableTransferHandler());
+
+        final Config p = Config.INSTANCE;
+        // TODO コンフィグからスレッド数
+        taskManager = new TaskManager(1, 1);
+
     }
 
     private class ApplyActionListener implements ActionListener {
index 9833a16..fd26eba 100644 (file)
@@ -1,6 +1,9 @@
 package yukihane.inqubus.manager;
 
 import java.io.File;
+import saccubus.worker.ConvertProgress;
+import saccubus.worker.DownloadProgress;
+import saccubus.worker.Worker.SaccubusListener;
 import saccubus.worker.profile.ConvertProfile;
 import saccubus.worker.profile.DownloadProfile;
 
@@ -9,18 +12,23 @@ import saccubus.worker.profile.DownloadProfile;
  * @author yuki
  */
 public class Request {
-    private static int serialId;
 
+    private static int serialId;
     private final int rowId;
     private final DownloadProfile downloadProfile;
     private final String videoId;
+    private final SaccubusListener<DownloadProgress> downloadProgressListener;
     private final ConvertProfile convertProfile;
+    private final SaccubusListener<ConvertProgress> convertProgressListener;
 
-    public Request(DownloadProfile download, String videoId, ConvertProfile convert, File video, File comment) {
+    public Request(DownloadProfile download, String videoId, SaccubusListener<DownloadProgress> downListener,
+            ConvertProfile convert, SaccubusListener<ConvertProgress> convListener) {
         this.rowId = ++serialId;
         this.downloadProfile = download;
         this.videoId = videoId;
+        this.downloadProgressListener = downListener;
         this.convertProfile = convert;
+        this.convertProgressListener = convListener;
     }
 
     public ConvertProfile getConvertProfile() {
@@ -38,4 +46,12 @@ public class Request {
     Integer getRowId() {
         return rowId;
     }
+
+    SaccubusListener<DownloadProgress> getDownloadProgressListener() {
+        return downloadProgressListener;
+    }
+
+    SaccubusListener<ConvertProgress> getConvertProgressListener() {
+        return convertProgressListener;
+    }
 }
index ea5d39f..563e176 100644 (file)
@@ -5,6 +5,7 @@ import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.concurrent.FutureTask;
 import saccubus.worker.Convert;
 import saccubus.worker.ConvertResult;
 import saccubus.worker.Download;
@@ -18,14 +19,12 @@ import saccubus.worker.profile.DownloadProfile;
  */
 public class TaskManager {
 
-//    private final LinkedBlockingQueue<DownloadTask> downloadTaskQueue = new LinkedBlockingQueue<>();
-//    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) {
+    public TaskManager(int maxDownload, int maxConvert) {
         downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
         convertExecutorService = Executors.newFixedThreadPool(maxConvert);
     }
@@ -34,13 +33,16 @@ public class TaskManager {
         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()));
+            final Download task = new Download(dp, request.getVideoId(),
+                    request.getDownloadProgressListener());
+            final Future<DownloadResult> future = downloadExecutorService.submit(task);
             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()));
+            final Convert task = new Convert(cp, dp.getVideoProfile().getLocalFile(), dp.getCommentProfile().
+                    getLocalFile(), request.getConvertProgressListener());
+            final Future<ConvertResult> future = convertExecutorService.submit(task);
             convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
             return true;
         }