From a241242cfcb080831ec97f861875a5d49d94c2be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Dahlgren?= Date: Mon, 11 Sep 2017 11:28:47 +0200 Subject: [PATCH] Support variable number of headless clients --- lib/manager.js | 2 +- lib/server.js | 106 +++++++++++++++++----------- public/js/app/models/server.js | 2 +- public/js/app/views/servers/form.js | 2 +- public/js/tpl/servers/form.html | 18 +++-- 5 files changed, 77 insertions(+), 53 deletions(-) diff --git a/lib/manager.js b/lib/manager.js index 5760c75..441bb13 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -104,10 +104,10 @@ Manager.prototype.save = (function () { admin_password: server.admin_password, auto_start: server.auto_start, battle_eye: server.battle_eye, - headless: server.headless, max_players: server.max_players, missions: server.missions, mods: server.mods, + number_of_headless_clients: server.number_of_headless_clients, parameters: server.parameters, password: server.password, persistent: server.persistent, diff --git a/lib/server.js b/lib/server.js index 3115d60..0e049c7 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,3 +1,4 @@ +var _ = require('lodash'); var events = require('events'); var fs = require('fs'); var gamedig = require('gamedig'); @@ -48,10 +49,10 @@ Server.prototype.update = function (options) { this.admin_password = options.admin_password; this.auto_start = options.auto_start; this.battle_eye = options.battle_eye; - this.headless = options.headless; this.max_players = options.max_players; this.missions = options.missions; this.mods = options.mods || []; + this.number_of_headless_clients = options.number_of_headless_clients || 0; this.password = options.password; this.parameters = options.parameters; this.persistent = options.persistent; @@ -92,7 +93,7 @@ Server.prototype.queryStatus = function() { ); }; -Server.prototype.start = function() { +Server.prototype.getParameters = function() { var parameters = []; if (config.parameters && Array.isArray(config.parameters)) { @@ -103,14 +104,19 @@ Server.prototype.start = function() { parameters = parameters.concat(this.parameters); } + return parameters; +} + +Server.prototype.start = function() { + var parameters = this.getParameters(); var server = new ArmaServer.Server({ battleEye: this.battle_eye ? 1 : 0, config: this.id, disableVoN: this.von ? 0 : 1, game: config.game, - headlessClients: this.headless ? ["127.0.0.1"] : null, + headlessClients: this.number_of_headless_clients > 0 ? ["127.0.0.1"] : null, hostname: createServerTitle(this.title), - localClient: this.headless ? ["127.0.0.1"] : null, + localClient: this.number_of_headless_clients > 0 ? ["127.0.0.1"] : null, missions: this.missions, mods: this.mods, parameters: parameters, @@ -157,6 +163,8 @@ Server.prototype.start = function() { self.pid = null; self.instance = null; + self.stopHeadlessClients(); + self.emit('state'); }); @@ -170,44 +178,59 @@ Server.prototype.start = function() { self.queryStatus(); }, queryInterval); - if (this.headless) { - var headless = new ArmaServer.Headless({ - game: config.game, - host: "127.0.0.1", - mods: this.mods, - parameters: parameters, - password: this.password, - path: this.config.path, - platform: this.config.type, - port: this.port, - }); - var headlessInstance = headless.start(); - - headlessInstance.stdout.on('data', function (data) { - console.log(self.id + ' HC: ' + data); - }); - - headlessInstance.stderr.on('data', function (data) { - console.log(self.id + ' HC err: ' + data); - }); - - headlessInstance.on('close', function (code) { - console.log(self.id + ' HC exited with code ' + code); - self.headlessInstance = null; - }); - - headlessInstance.on('error', function (err) { - console.log(err); - }); - - self.headlessInstance = headlessInstance; - } + this.startHeadlessClients() this.emit('state'); return this; }; +Server.prototype.startHeadlessClients = function() { + var parameters = this.getParameters(); + var self = this; + var headlessClientInstances = _.times(this.number_of_headless_clients, function (i) { + var headless = new ArmaServer.Headless({ + game: config.game, + host: "127.0.0.1", + mods: self.mods, + parameters: parameters, + password: self.password, + path: self.config.path, + platform: self.config.type, + port: self.port, + }); + var headlessInstance = headless.start(); + var name = 'HC_' + i; + var logPrefix = self.id + ' ' + name; + console.log(logPrefix + ' starting'); + + headlessInstance.stdout.on('data', function (data) { + console.log(logPrefix + ' stdout: ' + data); + }); + + headlessInstance.stderr.on('data', function (data) { + console.log(logPrefix + ' stderr: ' + data); + }); + + headlessInstance.on('close', function (code) { + console.log(logPrefix + ' exited: ' + code); + + var elementIndex = headlessClientInstances.indexOf(headlessInstance); + if (elementIndex != -1) { + headlessClientInstances.splice(elementIndex, 1); + } + }); + + headlessInstance.on('error', function (err) { + console.log(logPrefix + ' error: ' + err); + }); + + return headlessInstance; + }); + + this.headlessClientInstances = headlessClientInstances; +} + Server.prototype.stop = function(cb) { var handled = false; var self = this; @@ -223,9 +246,6 @@ Server.prototype.stop = function(cb) { }); this.instance.kill(); - if (this.headlessInstance) { - this.headlessInstance.kill(); - } setTimeout(function() { if (!handled) { @@ -240,16 +260,22 @@ Server.prototype.stop = function(cb) { return this; }; +Server.prototype.stopHeadlessClients = function() { + this.headlessClientInstances.map(function (headlessClientInstance) { + headlessClientInstance.kill(); + }); +} + Server.prototype.toJSON = function () { return { admin_password: this.admin_password, auto_start: this.auto_start, battle_eye: this.battle_eye, - headless: this.headless, id: this.id, max_players: this.max_players, missions: this.missions, mods: this.mods, + number_of_headless_clients: this.number_of_headless_clients, parameters: this.parameters, password: this.password, persistent: this.persistent, diff --git a/public/js/app/models/server.js b/public/js/app/models/server.js index 4a0d377..8fe9fbb 100644 --- a/public/js/app/models/server.js +++ b/public/js/app/models/server.js @@ -11,9 +11,9 @@ define(function (require) { admin_password: '', auto_start: false, battle_eye: false, - headless: false, max_players: null, mods: [], + number_of_headless_clients: 0, parameters: [], password: '', persistent: false, diff --git a/public/js/app/views/servers/form.js b/public/js/app/views/servers/form.js index 2e8b441..f012e2e 100644 --- a/public/js/app/views/servers/form.js +++ b/public/js/app/views/servers/form.js @@ -22,8 +22,8 @@ define(function (require) { admin_password: this.$("form .admin-password").val(), auto_start: this.$("form .auto-start").prop("checked"), battle_eye: this.$("form .battle-eye").prop("checked"), - headless: this.$("form .headless").prop("checked"), max_players: this.$("form .max-players").val(), + number_of_headless_clients: this.$("form .headless-clients").val(), password: this.$("form .password").val(), persistent: this.$("form .persistent").prop("checked"), port: this.$("form .port").val(), diff --git a/public/js/tpl/servers/form.html b/public/js/tpl/servers/form.html index 28c71d4..034d195 100644 --- a/public/js/tpl/servers/form.html +++ b/public/js/tpl/servers/form.html @@ -35,6 +35,14 @@ +
+ +
+ +

Number of headless clients that will be launched

+
+
+
@@ -55,16 +63,6 @@
-
-
-
- -
-
-
-