OSDN Git Service

Manager導入
authoryukihane <yukihane.feather@gmail.com>
Fri, 26 Aug 2011 05:49:10 +0000 (14:49 +0900)
committeryukihane <yukihane.feather@gmail.com>
Fri, 26 Aug 2011 06:18:09 +0000 (15:18 +0900)
frontend/src/saccubus/Saccubus.java
frontend/src/saccubus/prompt/Prompt.java
frontend/src/saccubus/worker/Convert.java
frontend/src/saccubus/worker/Download.java
frontend/src/saccubus/worker/SaccubusCallable.java [new file with mode: 0644]
frontend/src/saccubus/worker/TestFrame.java [deleted file]
frontend/src/yukihane/inqubus/gui/MainFrame.java
frontend/src/yukihane/inqubus/manager/Request.java [new file with mode: 0644]
frontend/src/yukihane/inqubus/manager/TaskManager.java [new file with mode: 0644]

index 526857c..f1d3b47 100644 (file)
@@ -62,7 +62,7 @@ public class Saccubus {
         * @param args
         *            String[]
         */
-       public static void main(String[] args) throws IOException {
+       public static void main(String[] args) throws Exception {
                //引数が有る場合はCUIで起動
                if (args.length > 0) {
             Prompt.main(args);
index 681855b..39ebf6b 100644 (file)
@@ -13,6 +13,7 @@ import org.apache.commons.cli.Option;
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
 import saccubus.worker.Download;
+import saccubus.worker.DownloadResult;
 import saccubus.worker.classic.profile.Profile;
 
 /**
@@ -37,7 +38,7 @@ import saccubus.worker.classic.profile.Profile;
  */
 public class Prompt {
 
-    public static void main(String[] args) throws IOException {
+    public static void main(String[] args) throws Exception {
         // 第1引数がメールアドレスと思しき時は昔の引数であるとみなしてパース、実行する.
         if (args.length > 0 && args[0].contains("@")) {
             doWithOldArguments(args);
@@ -63,9 +64,9 @@ public class Prompt {
 
         Download conv = new Download(profile, "dummy0");
         ExecutorService es = Executors.newSingleThreadExecutor();
-        Future<Boolean> future = es.submit((Callable<Boolean>) conv);
+        final Future<DownloadResult> future = es.submit(conv);
         try {
-            boolean res = future.get().booleanValue();
+            boolean res = future.get().getResultValue();
             if (!res) {
                 throw new IOException("ffmpeg変換処理が正常に終了しませんでした。");
             }
@@ -142,7 +143,7 @@ public class Prompt {
     /**
      * 昔の引数形式でプログラムを実行する.
      */
-    private static void doWithOldArguments(String[] args) throws IOException {
+    private static void doWithOldArguments(String[] args) throws Exception {
         String mail = args[0];
         String pass = args[1];
         String tag = args[2];
@@ -160,7 +161,7 @@ public class Prompt {
         System.out.println("VideoID: " + tag);
         System.out.println("WaybackTime: " + time);
         System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
-        conv.run();
+        conv.call();
         System.out.println("Finished.");
     }
 }
index 0a1c27e..8a5a708 100644 (file)
@@ -31,7 +31,7 @@ import yukihane.swf.Cws2Fws;
  * 動画を(コメント付きに)変換するワーカクラス.
  * @author yuki
  */
-public class Convert extends SwingWorker<ConvertResult, ConvertProgress> {
+public class Convert extends SaccubusCallable<ConvertResult, ConvertProgress> {
 
     private static final Logger logger = Logger.getLogger(Convert.class.getName());
     private final ConvertProfile profile;
@@ -47,13 +47,15 @@ public class Convert extends SwingWorker<ConvertResult, ConvertProgress> {
      * @throws IOException 変換失敗.
      */
     public Convert(ConvertProfile profile, File video, File comment) {
+        // TODO listener登録
+        super(null);
         this.profile = profile;
         this.videoFile = video;
         this.commentFile = comment;
     }
 
     @Override
-    protected ConvertResult doInBackground() throws Exception {
+    public ConvertResult call() throws Exception {
         if (!profile.isConvert()) {
             return new ConvertResult(true, "");
         }
index c10d0a3..c1fa449 100644 (file)
@@ -31,7 +31,7 @@ import saccubus.worker.profile.ProxyProfile;
  * @author 未入力
  * @version 1.0
  */
-public class Download extends SwingWorker<DownloadResult, DownloadProgress> implements Callable<Boolean> {
+public class Download extends SaccubusCallable<DownloadResult, DownloadProgress>  {
 
     private static final Logger logger = Logger.getLogger(Download.class.getName());
     private final DownloadProfile profile;
@@ -46,20 +46,22 @@ public class Download extends SwingWorker<DownloadResult, DownloadProgress> impl
      * @param flag
      */
     public Download(DownloadProfile profile, String videoId) {
+        // TODO listener登録
+        super(null);
         this.videoId = videoId;
         this.profile = profile;
     }
 
-    @Override
-    public Boolean call() throws Exception {
-        try {
-            final DownloadResult result = doInBackground();
-            return Boolean.valueOf(result.getResultValue());
-        } finally {
-            // TODO 何か処理が必要?
-//            getStopFlag().finished();
-        }
-    }
+//    @Override
+//    public Boolean call() throws Exception {
+//        try {
+//            final DownloadResult result = doInBackground();
+//            return Boolean.valueOf(result.getResultValue());
+//        } finally {
+//            // TODO 何か処理が必要?
+////            getStopFlag().finished();
+//        }
+//    }
 
 //    // TODO Runnableを実装しなくなったので削除する
 //    public void run() {
@@ -72,7 +74,7 @@ public class Download extends SwingWorker<DownloadResult, DownloadProgress> impl
 //        }
 //    }
     @Override
-    protected DownloadResult doInBackground() throws Exception {
+    public DownloadResult call() throws Exception {
 
         publish(new DownloadProgress("ログイン中"));
 
diff --git a/frontend/src/saccubus/worker/SaccubusCallable.java b/frontend/src/saccubus/worker/SaccubusCallable.java
new file mode 100644 (file)
index 0000000..d266c93
--- /dev/null
@@ -0,0 +1,35 @@
+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);
+    }
+}
diff --git a/frontend/src/saccubus/worker/TestFrame.java b/frontend/src/saccubus/worker/TestFrame.java
deleted file mode 100644 (file)
index aaf1c1f..0000000
+++ /dev/null
@@ -1,487 +0,0 @@
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-package saccubus.worker;
-
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.io.File;
-import java.util.List;
-import java.util.concurrent.ExecutionException;
-import javax.swing.GroupLayout;
-import javax.swing.JButton;
-import javax.swing.JFrame;
-import javax.swing.JPanel;
-import javax.swing.JTextField;
-import javax.swing.SwingUtilities;
-import saccubus.worker.profile.CommentProfile;
-import saccubus.worker.profile.ConvertProfile;
-import saccubus.worker.profile.FfmpegProfile;
-import saccubus.worker.profile.GeneralProfile;
-import saccubus.worker.profile.LoginProfile;
-import saccubus.worker.profile.OutputProfile;
-import saccubus.worker.profile.DownloadProfile;
-import saccubus.worker.profile.ProxyProfile;
-import saccubus.worker.profile.VideoProfile;
-
-/**
- *
- * @author yuki
- */
-public class TestFrame extends JFrame {
-
-    private static final long serialVersionUID = 1L;
-    private final JTextField fldVideoId = new JTextField();
-    private final JButton btnDownload = new JButton("DOWNLOAD");
-    private final JButton btnCancel = new JButton("Cancel");
-    private final JTextField fldStatus = new JTextField();
-    private final JButton btnConv = new JButton("Conv");
-    private final JButton btnConvCancel = new JButton("ConvCancl");
-    private Download downloader;
-    private Convert converter;
-
-    public TestFrame() {
-        JPanel panel = new JPanel();
-        GroupLayout lo = new GroupLayout(panel);
-        panel.setLayout(lo);
-
-        lo.setHorizontalGroup(lo.createParallelGroup()
-                .addGroup(lo.createSequentialGroup()
-                    .addComponent(fldVideoId).addComponent(btnDownload))
-                .addGroup(lo.createSequentialGroup()
-                    .addComponent(fldStatus).addComponent(btnCancel))
-                .addGroup(lo.createSequentialGroup()
-                    .addComponent(btnConv).addComponent(btnConvCancel)));
-
-        lo.setVerticalGroup(lo.createSequentialGroup()
-                .addGroup(lo.createParallelGroup()
-                    .addComponent(fldVideoId).addComponent(btnDownload))
-                .addGroup(lo.createParallelGroup()
-                    .addComponent(fldStatus).addComponent(btnCancel))
-                .addGroup(lo.createParallelGroup()
-                    .addComponent(btnConv).addComponent(btnConvCancel)));
-
-        setContentPane(panel);
-        pack();
-
-
-        btnDownload.addActionListener(new DownloadListener());
-        btnCancel.addActionListener(new CancelListener());
-        btnConv.addActionListener(new ConvertListener());
-        btnConvCancel.addActionListener(new ConvCnclListener());
-    }
-
-    private class ConvCnclListener implements ActionListener {
-
-        @Override
-        public void actionPerformed(ActionEvent e) {
-            if (converter != null) {
-                converter.cancel(true);
-            }
-        }
-    }
-
-    private class ConvertListener implements ActionListener {
-
-        @Override
-        public void actionPerformed(ActionEvent e) {
-            converter = new Convert(new MyConvProfile(), new File("out/sm8994079.mp4"), new File("out/sm9.xml")) {
-
-                @Override
-                protected void process(List<ConvertProgress> chunks) {
-                    ConvertProgress chunk = chunks.get(chunks.size() - 1);
-                    fldStatus.setText(chunk.getMessage());
-                }
-
-                @Override
-                protected void done() {
-                    btnConv.setEnabled(true);
-                    try {
-                        ConvertResult res = get();
-                    } catch (InterruptedException ex) {
-                        ex.printStackTrace();
-                    } catch (ExecutionException ex) {
-                        ex.printStackTrace();
-                    }
-                }
-            };
-
-            btnConv.setEnabled(false);
-            converter.execute();
-
-        }
-    }
-
-    private class CancelListener implements ActionListener {
-
-        @Override
-        public void actionPerformed(ActionEvent e) {
-            if (downloader != null) {
-                downloader.cancel(true);
-            }
-        }
-    }
-
-    private class DownloadListener implements ActionListener {
-
-        @Override
-        public void actionPerformed(ActionEvent e) {
-            downloader = new Download(new MyProfile(), fldVideoId.getText()) {
-
-                @Override
-                protected void process(List<DownloadProgress> chunks) {
-                    DownloadProgress chunk = chunks.get(chunks.size() - 1);
-                    fldStatus.setText(chunk.getMessage());
-                }
-
-                @Override
-                protected void done() {
-                    btnDownload.setEnabled(true);
-                    try {
-                        DownloadResult res = get();
-                    } catch (InterruptedException ex) {
-                        ex.printStackTrace();
-                    } catch (ExecutionException ex) {
-                        ex.printStackTrace();
-                    }
-                }
-            };
-
-            btnDownload.setEnabled(false);
-            downloader.execute();
-
-        }
-    }
-
-    public static void main(String[] args) {
-        SwingUtilities.invokeLater(new Runnable() {
-
-            @Override
-            public void run() {
-                TestFrame view = new TestFrame();
-                view.setDefaultCloseOperation(EXIT_ON_CLOSE);
-                view.setVisible(true);
-            }
-        });
-    }
-
-    private static class MyProfile implements DownloadProfile {
-
-        @Override
-        public LoginProfile getLoginInfo() {
-            return new LoginProfile() {
-
-                @Override
-                public String getMail() {
-                    return "yamamoto5_5963@hotmail.com";
-                }
-
-                @Override
-                public String getPassword() {
-                    return "piyopiyo";
-                }
-            };
-        }
-
-        @Override
-        public ProxyProfile getProxyProfile() {
-            return new ProxyProfile() {
-
-                @Override
-                public boolean use() {
-                    return false;
-                }
-
-                @Override
-                public String getHost() {
-                    throw new UnsupportedOperationException("Not supported yet.");
-                }
-
-                @Override
-                public int getPort() {
-                    throw new UnsupportedOperationException("Not supported yet.");
-                }
-            };
-        }
-
-        @Override
-        public VideoProfile getVideoProfile() {
-            return new VideoProfile() {
-
-                @Override
-                public boolean isDownload() {
-                    return true;
-                }
-
-                @Override
-                public File getDir() {
-                    return new File("out");
-                }
-
-                @Override
-                public String getFileName() {
-                    return "{id}";
-                }
-
-                @Override
-                public File getLocalFile() {
-                    throw new UnsupportedOperationException("Not supported yet.");
-                }
-            };
-        }
-
-        @Override
-        public CommentProfile getCommentProfile() {
-            return new CommentProfile() {
-
-                @Override
-                public int getLengthRelatedCommentSize() {
-                    return -1;
-                }
-
-                @Override
-                public boolean isDisablePerMinComment() {
-                    return false;
-                }
-
-                @Override
-                public int getPerMinCommentSize() {
-                    return -1;
-                }
-
-                @Override
-                public long getBackLogPoint() {
-                    return -1;
-                }
-
-                @Override
-                public boolean isDownload() {
-                    return true;
-                }
-
-                @Override
-                public File getDir() {
-                    return new File("out");
-                }
-
-                @Override
-                public String getFileName() {
-                    return "{id}_{title}";
-                }
-
-                @Override
-                public File getLocalFile() {
-                    throw new UnsupportedOperationException("Not supported yet.");
-                }
-            };
-        }
-
-//        @Override
-//        public OutputProfile getOutputFileSetting() {
-//            return new OutputProfile() {
-//
-//                @Override
-//                public boolean isConvert() {
-//                    return false;
-//                }
-//
-//                @Override
-//                public boolean isAddComment() {
-//                    throw new UnsupportedOperationException("Not supported yet.");
-//                }
-//
-//                @Override
-//                public File getDir() {
-//                    throw new UnsupportedOperationException("Not supported yet.");
-//                }
-//
-//                @Override
-//                public String getFileName() {
-//                    throw new UnsupportedOperationException("Not supported yet.");
-//                }
-//            };
-//        }
-        @Override
-        public GeneralProfile getGeneralProfile() {
-            return new GeneralProfile() {
-
-                @Override
-                public String getReplaceFrom() {
-                    return "<>\\/#";
-                }
-
-                @Override
-                public String getReplaceTo() {
-                    return "_";
-                }
-            };
-        }
-    }
-
-    private class MyConvProfile implements ConvertProfile {
-
-        @Override
-        public FfmpegProfile getFfmpegOption() {
-            return new FfmpegProfile() {
-
-                @Override
-                public String getExtOption() {
-                    return "";
-                }
-
-                @Override
-                public String getInOption() {
-                    return "";
-                }
-
-                @Override
-                public String getMainOption() {
-                    return "";
-                }
-
-                @Override
-                public String getOutOption() {
-                    return "-f ipod -g 150 -qcomp 0.7 -qmin 20 -qmax 30 -qdiff 4 -subq 6 -me_range 16 -i_qfactor 0.714286";
-                }
-
-                @Override
-                public String getAvfilterOption() {
-                    return "";
-                }
-
-                @Override
-                public boolean isResize() {
-                    return false;
-                }
-
-                @Override
-                public int getResizeWidth() {
-                    throw new UnsupportedOperationException("Not supported yet.");
-                }
-
-                @Override
-                public int getResizeHeight() {
-                    throw new UnsupportedOperationException("Not supported yet.");
-                }
-
-                @Override
-                public boolean isAdjustRatio() {
-                    throw new UnsupportedOperationException("Not supported yet.");
-                }
-            };
-        }
-
-        @Override
-        public File getFfmpeg() {
-           return new File("bin/ffmpeg.exe");
-        }
-
-        @Override
-        public boolean isVhookDisabled() {
-            return true;
-        }
-
-        @Override
-        public File getVhook() {
-            throw new UnsupportedOperationException("Not supported yet.");
-        }
-
-        @Override
-        public File getTempDir() {
-            return new File("out");
-        }
-
-        @Override
-        public File getFont() {
-            throw new UnsupportedOperationException("Not supported yet.");
-        }
-
-        @Override
-        public int getFontIndex() {
-            throw new UnsupportedOperationException("Not supported yet.");
-        }
-
-        @Override
-        public boolean isCommentOpaque() {
-            throw new UnsupportedOperationException("Not supported yet.");
-        }
-
-        @Override
-        public boolean isDisableFontSizeArrange() {
-            throw new UnsupportedOperationException("Not supported yet.");
-        }
-
-        @Override
-        public int getShadowIndex() {
-            throw new UnsupportedOperationException("Not supported yet.");
-        }
-
-        @Override
-        public boolean isShowConverting() {
-            throw new UnsupportedOperationException("Not supported yet.");
-        }
-
-        @Override
-        public int getMaxNumOfComment() {
-            throw new UnsupportedOperationException("Not supported yet.");
-        }
-
-        @Override
-        public HideCondition getNgSetting() {
-            throw new UnsupportedOperationException("Not supported yet.");
-        }
-
-        @Override
-        public OutputProfile getOutputProfile() {
-            return new OutputProfile() {
-
-                @Override
-                public File getDir() {
-                    return new File("out");
-                }
-
-                @Override
-                public String getFileName() {
-                    return "outconv";
-                }
-
-                @Override
-                public String getVideoId() {
-                    return "";
-                }
-
-                @Override
-                public String getTitile() {
-                    return "";
-                }
-            };
-        }
-
-        @Override
-        public GeneralProfile getGeneralProfile() {
-            return new GeneralProfile() {
-
-                @Override
-                public String getReplaceFrom() {
-                    return "";
-                }
-
-                @Override
-                public String getReplaceTo() {
-                    return "";
-                }
-            };
-        }
-
-        @Override
-        public boolean isConvert() {
-            return true;
-        }
-
-        @Override
-        public boolean isCommentOverlay() {
-            return false;
-        }
-    }
-}
index 206f827..f246344 100644 (file)
@@ -346,7 +346,8 @@ public class MainFrame extends JFrame {
             final DownloadProfile prof = new InqubusDownloadProfile();
             final String id = Util.getVideoId(fldId.getText());
             logger.log(Level.INFO, prof.toString());
-            new Download(prof, id).execute();
+            // TODO 処理開始
+//            new Download(prof, id).execute();
         }
     }
     /** This method is called from within the constructor to
diff --git a/frontend/src/yukihane/inqubus/manager/Request.java b/frontend/src/yukihane/inqubus/manager/Request.java
new file mode 100644 (file)
index 0000000..57bddb5
--- /dev/null
@@ -0,0 +1,9 @@
+package yukihane.inqubus.manager;
+
+/**
+ *
+ * @author yuki
+ */
+public interface Request {
+
+}
diff --git a/frontend/src/yukihane/inqubus/manager/TaskManager.java b/frontend/src/yukihane/inqubus/manager/TaskManager.java
new file mode 100644 (file)
index 0000000..6bdeb41
--- /dev/null
@@ -0,0 +1,26 @@
+package yukihane.inqubus.manager;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ *
+ * @author yuki
+ */
+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 TaskManager(int maxDownload, int maxConvert) {
+        downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
+        convertExecutorService = Executors.newFixedThreadPool(maxConvert);
+    }
+
+    public void add(Request task) {
+        // TODO タスク種判断
+        // TODO タスク追加
+    }
+}