OSDN Git Service

worker空のリスナはmanagerで一旦トラップする
authoryukihane <yukihane.feather@gmail.com>
Sat, 27 Aug 2011 05:55:49 +0000 (14:55 +0900)
committeryukihane <yukihane.feather@gmail.com>
Sat, 27 Aug 2011 05:55:49 +0000 (14:55 +0900)
frontend/src/yukihane/inqubus/manager/TaskKind.java [new file with mode: 0644]
frontend/src/yukihane/inqubus/manager/TaskManage.java
frontend/src/yukihane/inqubus/manager/TaskManageListener.java [new file with mode: 0644]
frontend/src/yukihane/inqubus/manager/TaskStatus.java [new file with mode: 0644]

diff --git a/frontend/src/yukihane/inqubus/manager/TaskKind.java b/frontend/src/yukihane/inqubus/manager/TaskKind.java
new file mode 100644 (file)
index 0000000..8cebdbf
--- /dev/null
@@ -0,0 +1,20 @@
+package yukihane.inqubus.manager;
+
+/**
+ *
+ * @author yuki
+ */
+public enum TaskKind {
+
+    DOWNLOAD("ダウンロード"), CONVERT("変換");
+    private final String text;
+
+    private TaskKind(String text) {
+        this.text = text;
+    }
+
+    @Override
+    public String toString() {
+        return text;
+    }
+}
index d38c3ce..70edbab 100644 (file)
@@ -5,15 +5,16 @@ 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.WorkerListener;
 import saccubus.worker.convert.Convert;
+import saccubus.worker.convert.ConvertProgress;
 import saccubus.worker.convert.ConvertResult;
 import saccubus.worker.download.Download;
+import saccubus.worker.download.DownloadProgress;
 import saccubus.worker.download.DownloadResult;
 import saccubus.worker.profile.ConvertProfile;
 import saccubus.worker.profile.DownloadProfile;
 
-
 /**
  *
  * @author yuki
@@ -24,10 +25,20 @@ public class TaskManage {
     private final ExecutorService convertExecutorService;
     private final Map<Integer, ManageTarget<DownloadResult>> downloadTargets = new HashMap<>();
     private final Map<Integer, ManageTarget<ConvertResult>> convertTargets = new HashMap<>();
+    private final TaskManageListener clientListner;
+
+    public TaskManage() {
+        this(1, 1, null);
+    }
 
     public TaskManage(int maxDownload, int maxConvert) {
+        this(maxDownload, maxConvert, null);
+    }
+
+    public TaskManage(int maxDownload, int maxConvert, TaskManageListener listener) {
         downloadExecutorService = Executors.newFixedThreadPool(maxDownload);
         convertExecutorService = Executors.newFixedThreadPool(maxConvert);
+        this.clientListner = listener;
     }
 
     public synchronized boolean add(Request request) {
@@ -35,21 +46,56 @@ public class TaskManage {
         final ConvertProfile cp = request.getConvertProfile();
         if (dp != null && !dp.getVideoProfile().isDownload() && !dp.getCommentProfile().isDownload()) {
             final Download task = new Download(dp, request.getVideoId(),
-                    request.getDownloadProgressListener());
+                    new DownloadListener(request.getRowId()));
             final Future<DownloadResult> future = downloadExecutorService.submit(task);
             downloadTargets.put(request.getRowId(), new ManageTarget<>(request, future));
             return true;
 
         } else if (cp != null && cp.isConvert()) {
             final Convert task = new Convert(cp, dp.getVideoProfile().getLocalFile(), dp.getCommentProfile().
-                    getLocalFile(), request.getConvertProgressListener());
+                    getLocalFile(), new ConvertListener(request.getRowId()));
             final Future<ConvertResult> future = convertExecutorService.submit(task);
             convertTargets.put(request.getRowId(), new ManageTarget<>(request, future));
             return true;
         }
         return false;
     }
+
+    private class DownloadListener implements WorkerListener<DownloadProgress> {
+
+        private final int rowId;
+
+        private DownloadListener(int rowId) {
+            this.rowId = rowId;
+        }
+
+        @Override
+        public void process(DownloadProgress progress) {
+            if (clientListner != null) {
+                // TODO
+                clientListner.process(rowId, TaskKind.DOWNLOAD, TaskStatus.DOING, 0.0, "");
+            }
+        }
+    }
+
+    private class ConvertListener implements WorkerListener<ConvertProgress> {
+
+        private final int id;
+
+        private ConvertListener(int id) {
+            this.id = id;
+        }
+
+        @Override
+        public void process(ConvertProgress progress) {
+            if (clientListner != null) {
+                // TODO
+                clientListner.process(id, TaskKind.CONVERT, TaskStatus.DOING, 0.0, "");
+            }
+        }
+    }
 }
+
 class ManageTarget<T> {
 
     private final Request request;
diff --git a/frontend/src/yukihane/inqubus/manager/TaskManageListener.java b/frontend/src/yukihane/inqubus/manager/TaskManageListener.java
new file mode 100644 (file)
index 0000000..7b58817
--- /dev/null
@@ -0,0 +1,10 @@
+package yukihane.inqubus.manager;
+
+/**
+ *
+ * @author yuki
+ */
+public interface TaskManageListener {
+
+    void process(int id, TaskKind kind, TaskStatus status, double percentage, String message);
+}
diff --git a/frontend/src/yukihane/inqubus/manager/TaskStatus.java b/frontend/src/yukihane/inqubus/manager/TaskStatus.java
new file mode 100644 (file)
index 0000000..7a7735f
--- /dev/null
@@ -0,0 +1,20 @@
+package yukihane.inqubus.manager;
+
+/**
+ *
+ * @author yuki
+ */
+public enum TaskStatus {
+
+    READY("待機"), DOING("実行"), DONE("完了");
+    private final String text;
+
+    private TaskStatus(String text) {
+        this.text = text;
+    }
+
+    @Override
+    public String toString() {
+        return text;
+    }
+}