Skip to content

Commit

Permalink
Merge pull request #52 from albertinad/albertinad/stop-and-simulate
Browse files Browse the repository at this point in the history
[review] Stop simulation programmatically API
  • Loading branch information
albertinad committed Jun 8, 2016
2 parents 09c9998 + c0d118f commit 16ca630
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var fs = require('fs'),
path = require('path');

var config = {};
var simulationFilePath;

// module properties
[
Expand Down Expand Up @@ -44,3 +43,9 @@ function getValue(prop, optional) {
}
return config[prop];
}

function newInstance() {
config = {};
}

module.exports.newInstance = newInstance;
9 changes: 6 additions & 3 deletions src/server/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ var plugins;
var pluginsTelemetry;
var _router;

resetPluginsData();

function resetPluginsData() {
plugins = {};
pluginsTelemetry = {
Expand Down Expand Up @@ -149,7 +147,6 @@ function findPluginSourceFilePath(projectRoot, pluginId, file) {
var pluginPath = path.join(projectRoot, 'plugins', pluginId, 'src/simulation');
var pluginFilePath = path.resolve(pluginPath, file);
return fs.existsSync(pluginFilePath) ? pluginPath : findBuiltInPluginSourceFilePath(pluginId, file);

}

function findBuiltInPluginSourceFilePath(pluginId, file) {
Expand Down Expand Up @@ -224,7 +221,13 @@ function getRouter() {
return _router;
}

function reset() {
resetPluginsData();
_router = null;
}

module.exports.initPlugins = initPlugins;
module.exports.reset = reset;
module.exports.getRouter = getRouter;
module.exports.getPlugins = function () {
return plugins;
Expand Down
7 changes: 6 additions & 1 deletion src/server/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var lastPlatform;

/**
* Prepares the project and initializes the simulation plugin list.
*
*
* @param {Object=} currentState (Optional) The current state of the project for caching purposes.
*/
function prepare() {
Expand Down Expand Up @@ -199,3 +199,8 @@ function getMtimeForFiles(dir) {
module.exports.prepare = prepare;
module.exports.execCordovaPrepare = execCordovaPrepare;
module.exports.updateTimeStampForFile = updateTimeStampForFile;
module.exports.reset = function () {
previousPrepareStates = {};
preparePromise = null;
lastPlatform = null;
};
13 changes: 12 additions & 1 deletion src/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var fs = require('fs'),
plugins = require('./plugins'),
prepare = require('./prepare'),
simFiles = require('./sim-files'),
simSocket = require('./socket'),
log = require('./log');

var pluginSimulationFiles = require('./plugin-files');
Expand Down Expand Up @@ -130,6 +131,16 @@ function processPluginHtml(html, pluginId) {
});
}

function stop() {
simSocket.closeConnections();

config.newInstance();
plugins.reset();
simFiles.reset();
prepare.reset();
}

module.exports = {
attach: attach
attach: attach,
stop: stop
};
6 changes: 6 additions & 0 deletions src/server/sim-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,13 @@ function getHostJsFile(hostType) {
return hostJsFiles[hostType];
}

function reset() {
hostJsFiles = {};
builtOnce = {};
}

module.exports.createSimHostJsFile = createSimHostJsFile;
module.exports.createAppHostJsFile = createAppHostJsFile;
module.exports.validateSimHostPlugins = validateSimHostPlugins;
module.exports.getHostJsFile = getHostJsFile;
module.exports.reset = reset;
34 changes: 33 additions & 1 deletion src/server/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@ var APP_HOST = 'app-host';
var SIM_HOST = 'sim-host';
var DEBUG_HOST = 'debug-host';

var io;
var hostSockets = {};
var pendingEmits = {};
pendingEmits[APP_HOST] = [];
pendingEmits[SIM_HOST] = [];
pendingEmits[DEBUG_HOST] = [];

function reset() {
hostSockets = {};
pendingEmits = {};
pendingEmits[APP_HOST] = [];
pendingEmits[SIM_HOST] = [];
pendingEmits[DEBUG_HOST] = [];
}

function init(server) {
var io = require('socket.io')(server);
reset();

io = require('socket.io')(server);

io.on('connection', function (socket) {
socket.on('register-app-host', function () {
Expand Down Expand Up @@ -152,6 +163,27 @@ function invalidateSimHost() {
hostSockets[SIM_HOST] = null;
}

function closeConnections() {
var hostType,
socket;
for (hostType in hostSockets) {
if (hostSockets.hasOwnProperty(hostType)) {
socket = hostSockets[hostType];
if (socket) {
socket.disconnect(true);
}
}
}

if (io) {
io.close();
io = null;
}

reset();
}

module.exports.init = init;
module.exports.emitToHost = emitToHost;
module.exports.invalidateSimHost = invalidateSimHost;
module.exports.closeConnections = closeConnections;
43 changes: 39 additions & 4 deletions src/simulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ var fs = require('fs'),
simSocket = require('./server/socket'),
dirs = require('./server/dirs');

var server = cordovaServe();
var server,
connections;

var launchServer = function (opts) {
opts = opts || {};

server = cordovaServe();

var platform = opts.platform || 'browser';
var appUrl, simHostUrl, simHostOpts;

Expand Down Expand Up @@ -50,6 +53,8 @@ var launchServer = function (opts) {
root: opts.dir,
noServerInfo: true
}).then(function () {
trackServerConnections();

simSocket.init(server.server);
config.server = server.server;
var projectRoot = server.projectRoot;
Expand All @@ -65,9 +70,33 @@ var launchServer = function (opts) {
};

var closeServer = function () {
return server.server && server.server.close();
server.server && server.server.close();

for (var id in connections) {
var socket = connections[id];
socket && socket.destroy();
}
};

var stopSimulate = function () {
closeServer();
simServer.stop();
server = null;
};

function trackServerConnections() {
var nextId = 0;
connections = {};
server.server.on('connection', function (socket) {
var id = nextId++;
connections[id] = socket;

socket.on('close', function () {
delete connections[id];
});
});
}

var launchBrowser = function (target, url) {
return cordovaServe.launchBrowser({ target: target, url: url });
};
Expand Down Expand Up @@ -137,7 +166,7 @@ var makeDirectoryRecursiveSync = function (dirPath) {
fs.mkdirSync(dirPath);
};

var configureSimulationDirectory = function (projectRoot, opts) {
var configureSimulationDirectory = function (projectRoot, opts) {
var simPath = opts.simulationpath || path.join(config.projectRoot, 'simulation');
config.simulationFilePath = path.resolve(simPath);

Expand All @@ -150,6 +179,12 @@ module.exports = simulate;
module.exports.launchBrowser = launchBrowser;
module.exports.launchServer = launchServer;
module.exports.closeServer = closeServer;
module.exports.stopSimulate = stopSimulate;
module.exports.dirs = dirs;
module.exports.app = server.app;
module.exports.log = log;

Object.defineProperty(module.exports, 'app', {
get: function () {
return (server) ? server.app : null;
}
});

0 comments on commit 16ca630

Please sign in to comment.