Support variable number of headless clients

This commit is contained in:
Björn Dahlgren 2017-09-11 11:28:47 +02:00 committed by Björn Dahlgren
parent d5ea4e3f1c
commit a241242cfc
5 changed files with 77 additions and 53 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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(),

View File

@ -35,6 +35,14 @@
</div>
</div>
<div class="form-group">
<label for="headless-clients" class="col-sm-2 control-label">Headless Clients</label>
<div class="col-sm-10">
<input type="number" class="form-control headless-clients" data-field="headless-clients" placeholder="Headless Clients" value="<%- number_of_headless_clients %>">
<p class="help-block">Number of headless clients that will be launched</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
@ -55,16 +63,6 @@
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox" class="headless" <% if (headless) { %>checked="checked"<% } %>> Headless Client
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">