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);
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 = {};
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 () {
</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 -->
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) {
*
* 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();
// when packages are checked nothing
if (_.isEmpty(checkedPackagesList)) {
- $('#tic-image-create').prop('disabled', true);
return;
}
// 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);
});
}
*/
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() {
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;
--- /dev/null
+'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;