[TIC-Web] add image create cancellation 92/107692/3
authorChangHyun Lee <leechwin.lee@samsung.com>
Thu, 29 Dec 2016 08:38:39 +0000 (17:38 +0900)
committerChangHyun Lee <leechwin.lee@samsung.com>
Fri, 30 Dec 2016 01:24:20 +0000 (10:24 +0900)
- add image create cancellation
- add broadcast log

Change-Id: Ia9db9ad016b6ed080a9b268c1c4fa9c4a00cba66
Signed-off-by: ChangHyun Lee <leechwin.lee@samsung.com>
app.js
public/src/index.html
public/src/js/page/image.js
server/fs/mic.js
server/fs/socketEvent.js [new file with mode: 0644]

diff --git a/app.js b/app.js
index 085469b..2afb24d 100644 (file)
--- a/app.js
+++ b/app.js
@@ -15,6 +15,7 @@ var Config = require('./config.json');
 var FileSystem = require('./server/fs/filesystem');
 var Mic = require('./server/fs/mic');
 var Router = require('./server/routes/router');
+var SocketEvent = require('./server/fs/socketEvent');
 
 /* app config */
 app.set('port', process.env.PORT || Config.TIC_WEB.PORT);
@@ -110,7 +111,7 @@ app.post('/exports', function(req, res) {
 io.on('connection', function (socket) {
     console.log('socket connection');
 
-    socket.on('ws/fs/image/list/from', function (data) {
+    socket.on(SocketEvent.FS_IMAGE_LIST_FROM, function (data) {
         var msgData, fileList, targetDirectory;
 
         msgData = {};
@@ -124,27 +125,29 @@ io.on('connection', function (socket) {
         msgData.list = fileList;
 
         // send
-        socket.emit('ws/fs/image/list/to', msgData);
+        socket.emit(SocketEvent.FS_IMAGE_LIST_TO, msgData);
     });
 
-    socket.on('ws/fs/image/add/from', function (data) {
-        console.log(data);
-        var msgData, ans;
+    socket.on(SocketEvent.FS_IMAGE_ADD_FROM, function (data) {
+        Mic.create(data, io);
 
-        msgData = {};
+        io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
+    });
 
-        function sendMsg(msg) {
-            // send
-            socket.emit('ws/fs/image/add/to', msg);
-        }
+    socket.on(SocketEvent.FS_IMAGE_ADD_KILL, function () {
+        Mic.kill();
 
-        // get the list of file
-        Mic.create(data, socket);
+        io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
     });
 
-    socket.on('ws/fs/image/download/from', function (data) {
+    socket.on(SocketEvent.FS_IMAGE_DOWNLOAD_FROM, function (data) {
         console.log(data);
+        // TODO: Implement or Remove
+    });
 
+    socket.on(SocketEvent.MIC_AVAILABLE_FROM, function () {
+        console.log('mic available: ' + Mic.isAvailable());
+        io.sockets.emit(SocketEvent.MIC_AVAILABLE_TO, Mic.isAvailable());
     });
 
     socket.on('disconnect', function () {
index da62b0c..5185dfe 100644 (file)
                             </div>
                             <div class="panel-footer">
                                 <button type="button" id="tic-image-create" class="btn btn-primary">Create Image</button>
+                                <button type="button" id="tic-image-cancel" class="btn btn-danger hidden">Cancel</button>
                             </div>
                         </div>
                     </div><!-- /End Image Column -->
index 033a5cb..1630866 100644 (file)
@@ -36,6 +36,12 @@ define([
     function initSocket(socket) {
         client = socket;
 
+        client.emit('ws/mic/available/from');
+
+        client.on('ws/mic/available/to', function (data) {
+            $('#tic-image-create').prop('disabled', !data);
+        });
+
         client.on('ws/fs/image/list/to', function (data) {
             var $imageList = $('#tic-image-list').empty();
             _.forEach(data.list, function (file) {
@@ -62,10 +68,17 @@ define([
          *
          * manage the logs for file.
          */
-        // when finished
-        client.on('ws/fs/image/add/finished', function (data) {
+        // when finish
+        client.on('ws/fs/image/add/finish', function (data) {
+            var logItem = _.template(IMAGE_LOG)({
+                log: data
+            });
+            var $imageNewLog = $('#tic-image-new-log').append(logItem);
+            $imageNewLog.animate({ scrollTop : $imageNewLog.height() }, 'slow');
+
             // button enabled
             $('#tic-image-create').prop('disabled', false);
+            $('#tic-image-cancel').toggleClass('hidden', true);
 
             // upate the list of images
             updateList();
@@ -115,7 +128,6 @@ define([
 
         // when packages are checked nothing
         if (_.isEmpty(checkedPackagesList)) {
-            $('#tic-image-create').prop('disabled', true);
             return;
         }
 
@@ -155,16 +167,19 @@ define([
         // confirm
         Util.showConfirmDialog('Are you sure want to create the image?')
         .then(function () {
+            $('#tic-image-cancel').toggleClass('hidden', false);
             getKickstartRecipeFile()
             .then(createImage)
             .catch(function (err) {
                 console.error(err);
                 Util.showAlertDialog('Failed to create a image.<br>Please check the ks file.');
                 $('#tic-image-create').prop('disabled', false);
+                $('#tic-image-cancel').toggleClass('hidden', true);
             });
         })
         .catch(function () {
             $('#tic-image-create').prop('disabled', false);
+            $('#tic-image-cancel').toggleClass('hidden', true);
         });
     }
 
@@ -173,8 +188,13 @@ define([
      */
     function _initWidgets() {
         // button
-        $('#tic-image-create').prop('disabled', true);
-        $('#tic-image-create').click(confirmCreateImage);
+        $('#tic-image-create').prop('disabled', true).on('click', confirmCreateImage);
+
+        function _imageCreateCancel() {
+            client.emit('ws/fs/image/add/kill');
+            $(this).toggleClass('hidden', true);
+        }
+        $('#tic-image-cancel').on('click', _imageCreateCancel);
     }
 
     function init() {
index d24684a..44a78d2 100644 (file)
@@ -7,45 +7,63 @@ var fs = require('fs');
 var path = require('path');
 var exec = require('child_process').exec;
 var util = require('util');
+var SocketEvent = require('./socketEvent');
 
 var Mic = {};
+var ps = null;
 
-Mic.subprocess = function (processname, cb) {
-    var p = exec(processname);
-    p.on('exit', cb.exit);
-    p.stdout.on('data', cb.stdout || function (out) {
+Mic.isAvailable = function () {
+    return (ps === null);
+}
+
+Mic.kill = function () {
+    if (ps === null) {
+        return;
+    }
+    ps.kill();
+    ps = null;
+}
+
+Mic.process = function (command, callback) {
+    ps = exec(command);
+    ps.stdout.on('data', callback.stdout || function (out) {
         process.stdout.write(out);
     });
-    p.stderr.on('data', cb.stderr || function (err) {
+    ps.stderr.on('data', callback.stderr || function (err) {
         process.stdout.write(err);
     });
+    ps.on('error', callback.error);
+    ps.on('exit', callback.exit);
 };
 
-Mic.create = function (paramObj, client) {
-    console.log('Mic.create called');
+Mic.create = function (paramObj, io) {
+    console.log('MIC Create');
 
-    var stdoutPath = 'ws/fs/image/add/to';
-    var exitPath = 'ws/fs/image/add/finished';
-    var micProcess = 'sudo mic cr loop ' + paramObj.pathKsFile + util.format(' -A %s', 'x86_64') + util.format(' -o %s', paramObj.pathOutput);
+    var micCommand = 'sudo mic cr loop ' + paramObj.pathKsFile + util.format(' -A %s', 'x86_64') + util.format(' -o %s', paramObj.pathOutput);
 
     function sendMsg(path, msg) {
-        client.emit(path, msg);
+        io.sockets.emit(path, msg);
     }
 
-    Mic.subprocess(micProcess, {
+    Mic.process(micCommand, {
         stdout: function (out) {
             console.log(out);
-            sendMsg(stdoutPath, out);
+            sendMsg(SocketEvent.FS_IMAGE_ADD_TO, out);
         },
         stderr: function (out) {
-            console.error(out);
-            sendMsg(stdoutPath, out);
+            console.log(out);
+            sendMsg(SocketEvent.FS_IMAGE_ADD_TO, out);
         },
-        exit: function (out) {
+        error: function (out) {
             console.log(out);
-            sendMsg(exitPath, out);
+            sendMsg(SocketEvent.FS_IMAGE_ADD_TO, out);
+        },
+        exit: function (code) {
+            ps = null;
+            console.log('Terminated (' + code + ')');
+            sendMsg(SocketEvent.FS_IMAGE_ADD_FINISH, 'Terminated (' + code + ')');
         }
     });
 };
 
-module.exports = Mic;
\ No newline at end of file
+module.exports = Mic;
diff --git a/server/fs/socketEvent.js b/server/fs/socketEvent.js
new file mode 100644 (file)
index 0000000..c84ca66
--- /dev/null
@@ -0,0 +1,17 @@
+'use strict';
+
+var SocketEvent = {};
+
+SocketEvent.MIC_AVAILABLE_TO = 'ws/mic/available/to';
+SocketEvent.MIC_AVAILABLE_FROM = 'ws/mic/available/from';
+
+SocketEvent.FS_IMAGE_LIST_TO = 'ws/fs/image/list/to';
+SocketEvent.FS_IMAGE_LIST_FROM = 'ws/fs/image/list/from';
+SocketEvent.FS_IMAGE_DOWNLOAD_FROM = 'ws/fs/image/download/from';
+
+SocketEvent.FS_IMAGE_ADD_TO = 'ws/fs/image/add/to';
+SocketEvent.FS_IMAGE_ADD_FROM = 'ws/fs/image/add/from';
+SocketEvent.FS_IMAGE_ADD_KILL = 'ws/fs/image/add/kill';
+SocketEvent.FS_IMAGE_ADD_FINISH = 'ws/fs/image/add/finish';
+
+module.exports = SocketEvent;