From c125645823f1fdf7bbb45432fb8b7f859706bc11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Dahlgren?= Date: Mon, 7 Apr 2014 00:58:17 +0200 Subject: [PATCH] Handle start and stopping servers --- main.js | 1 + manager.js | 51 ++++++++++++++++++++++++++--- public/js/app/views/servers/info.js | 20 +++++++++-- public/js/tpl/servers/info.html | 8 ++++- routes/servers.js | 26 +++++++++++++-- 5 files changed, 96 insertions(+), 10 deletions(-) diff --git a/main.js b/main.js index 9e6f8a7..c332110 100644 --- a/main.js +++ b/main.js @@ -19,6 +19,7 @@ app.resource('api/servers', servers); app.resource('api/settings', require('./routes/settings')); app.get('/api/servers/:server/start', servers.start); +app.get('/api/servers/:server/stop', servers.stop); app.get('/', function (req, res){ res.sendfile(__dirname + '/public/index.html'); diff --git a/manager.js b/manager.js index 0352bf0..4a1641f 100644 --- a/manager.js +++ b/manager.js @@ -27,19 +27,49 @@ Server.prototype.start = function() { var mods = this.makeModsParameter(); var port = this.makePortParameter(); - var server = spawn(this.armaServerPath(), [mods, port, '-config=server.cfg', '-noSound', '-world=empty']); + var process = spawn(this.armaServerPath(), [mods, port, '-config=server.cfg', '-noSound', '-world=empty']); + var self = this; - server.stdout.on('data', function (data) { + process.stdout.on('data', function (data) { console.log('stdout: ' + data); }); - server.stderr.on('data', function (data) { + process.stderr.on('data', function (data) { console.log('stderr: ' + data); }); - server.on('close', function (code) { + process.on('close', function (code) { console.log('child process exited with code ' + code); + self.pid = null; + self.process = null; }); + + this.pid = process.pid; + this.process = process; + + return this; +} + +Server.prototype.stop = function(cb) { + var handled = false; + + this.process.on('close', function (code) { + if (!handled) { + handled = true; + cb(); + } + }); + + this.process.kill(); + + setTimeout(function() { + if (!handled) { + handled = true; + cb(); + } + }, 5000); + + return this; } function Manager() { @@ -87,7 +117,18 @@ Manager.prototype.load = (function () { }); Manager.prototype.save = (function () { - fs.writeFile(filePath, JSON.stringify(this.serversArr), function(err) { + var data = []; + + this.serversArr.forEach(function (server) { + data.push({ + id: server.id, + title: server.title, + port: server.port, + mods: server.mods, + }) + }); + + fs.writeFile(filePath, JSON.stringify(data), function(err) { if(err) throw err; }); }); diff --git a/public/js/app/views/servers/info.js b/public/js/app/views/servers/info.js index 205189e..0410649 100644 --- a/public/js/app/views/servers/info.js +++ b/public/js/app/views/servers/info.js @@ -12,7 +12,8 @@ define(function (require) { template: _.template(tpl), events: { - "click #start": "start" + "click #start": "start", + "click #stop": "stop", }, start: function (event) { @@ -22,7 +23,22 @@ define(function (require) { url: "/api/servers/" + this.model.get('id') + "/start", type: 'GET', success: function (resp) { - console.log(resp); + self.model.set("pid", resp.pid); + self.render(); + }, + error: $.noop + }); + }, + + stop: function (event) { + var self = this; + event.preventDefault(); + $.ajax({ + url: "/api/servers/" + this.model.get('id') + "/stop", + type: 'GET', + success: function (resp) { + self.model.set("pid", resp.pid); + self.render(); }, error: $.noop }); diff --git a/public/js/tpl/servers/info.html b/public/js/tpl/servers/info.html index 741e8e3..bb187b8 100644 --- a/public/js/tpl/servers/info.html +++ b/public/js/tpl/servers/info.html @@ -1 +1,7 @@ -Start +<% if (typeof(pid) != "undefined" && pid) { %> + Server is running as <%= pid %> + + Stop +<% } else { %> + Start +<% } %> diff --git a/routes/servers.js b/routes/servers.js index dec379b..2507f1c 100644 --- a/routes/servers.js +++ b/routes/servers.js @@ -5,7 +5,18 @@ var Manager = require('./../manager'); var manager = new Manager(); exports.index = function (req, res){ - res.send(manager.getServers()); + var servers = []; + + manager.getServers().forEach(function (server) { + servers.push({ + id: server.id, + title: server.title, + port: server.port, + mods: server.mods, + }) + }); + + res.send(servers); }; exports.create = function (req, res){ @@ -29,5 +40,16 @@ exports.destroy = function(req, res){ exports.start = function (req, res){ var server = manager.getServer(req.params.server); server.start(); - res.send({status:"ok"}); + res.send({status:"ok", pid: server.pid}); +}; + +exports.stop = function (req, res){ + var server = manager.getServer(req.params.server); + server.stop(function () { + if (!server.pid) { + res.send({status: true, pid: server.pid}); + } else { + res.send({status: false, pid: server.pid}); + } + }); };