mirror of
https://github.com/overte-org/overte.git
synced 2025-08-04 20:57:09 +02:00
Creating the master / agent hiring system
This commit is contained in:
parent
184669762a
commit
3273fbcad4
2 changed files with 292 additions and 212 deletions
|
@ -19,27 +19,49 @@ function printDebug(message) {
|
||||||
|
|
||||||
// The time between alive messages on the command channel
|
// The time between alive messages on the command channel
|
||||||
var ALIVE_PERIOD = 3;
|
var ALIVE_PERIOD = 3;
|
||||||
var NUM_CYCLES_BEFORE_RESET = 5;
|
var NUM_CYCLES_BEFORE_RESET = 8;
|
||||||
|
|
||||||
// Service Actions
|
// Service Actions
|
||||||
|
var MASTER_ID = -1;
|
||||||
var AGENT_READY = "ready";
|
var AGENT_READY = "ready";
|
||||||
|
var AGENT_LOST = "agentLost";
|
||||||
|
|
||||||
var INVALID_ACTOR = -2;
|
var INVALID_ACTOR = -2;
|
||||||
|
|
||||||
var MASTER_INDEX = -1;
|
|
||||||
|
|
||||||
|
var AGENTS_BROADCAST = -1;
|
||||||
var MASTER_ALIVE = -1;
|
var MASTER_ALIVE = -1;
|
||||||
var MASTER_FIRE_AGENT = -2;
|
var MASTER_FIRE_AGENT = -2;
|
||||||
|
|
||||||
var makeMessage = function(id, action, argument) {
|
var makeUniqueUUID = function(SEUUID) {
|
||||||
|
//return SEUUID + Math.random();
|
||||||
|
// forget complexity, just give me a four digit pin
|
||||||
|
return (Math.random() * 10000).toFixed(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
var packAnnounceMessage = function(dest, command, src) {
|
||||||
|
var message = {
|
||||||
|
dest: dest,
|
||||||
|
command: command,
|
||||||
|
src: src
|
||||||
|
};
|
||||||
|
return JSON.stringify(message);
|
||||||
|
};
|
||||||
|
|
||||||
|
var unpackAnnounceMessage = function(message) {
|
||||||
|
return JSON.parse(message);
|
||||||
|
};
|
||||||
|
|
||||||
|
var packCommandMessage = function(id, action, argument) {
|
||||||
var message = {
|
var message = {
|
||||||
id_key: id,
|
id_key: id,
|
||||||
action_key: action,
|
action_key: action,
|
||||||
argument_key: argument
|
argument_key: argument
|
||||||
};
|
};
|
||||||
return message;
|
return JSON.stringify(message);
|
||||||
};
|
};
|
||||||
|
|
||||||
var unpackMessage = function(message) {
|
var unpackCommandMessage = function(message) {
|
||||||
return JSON.parse(message);
|
return JSON.parse(message);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -48,6 +70,7 @@ function printDebug(message) {
|
||||||
var MasterController = function() {
|
var MasterController = function() {
|
||||||
this.timeSinceLastAlive = 0;
|
this.timeSinceLastAlive = 0;
|
||||||
this.knownAgents = new Array;
|
this.knownAgents = new Array;
|
||||||
|
this.hiringAgentsQueue = new Array;
|
||||||
this.subscribed = false;
|
this.subscribed = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -79,28 +102,39 @@ function printDebug(message) {
|
||||||
};
|
};
|
||||||
|
|
||||||
MasterController.prototype._processAnnounceMessage = function(message, senderID) {
|
MasterController.prototype._processAnnounceMessage = function(message, senderID) {
|
||||||
if (message == AGENT_READY) {
|
var message = unpackAnnounceMessage(message);
|
||||||
|
if (message.dest == MASTER_ID) {
|
||||||
|
if (message.command == AGENT_READY) {
|
||||||
// check to see if we know about this agent
|
// check to see if we know about this agent
|
||||||
if (this.knownAgents.indexOf(senderID) < 0) {
|
var agentIndex = this.knownAgents.indexOf(message.src);
|
||||||
|
if (agentIndex < 0) {
|
||||||
var indexOfNewAgent = this.knownAgents.length;
|
if (this.hiringAgentsQueue.length > 0) {
|
||||||
this.knownAgents[indexOfNewAgent] = senderID;
|
var hiringHandler = this.hiringAgentsQueue.pop();
|
||||||
printDebug("New agent available to be hired " + senderID);
|
hiringHandler(message.src);
|
||||||
|
|
||||||
var acknowledgeMessage = senderID + "." + indexOfNewAgent;
|
|
||||||
Messages.sendMessage(ANNOUNCE_CHANNEL, acknowledgeMessage);
|
|
||||||
} else {
|
} else {
|
||||||
printDebug("New agent still sending ready ? " + senderID);
|
//No hiring in queue so bail
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Master think the agent is hired but not the other way around, forget about it
|
||||||
|
printDebug("New agent still sending ready ? " + message.src + " " + agentIndex + " Forgeting about it");
|
||||||
|
this.knownAgents[agentIndex] = INVALID_ACTOR;
|
||||||
|
}
|
||||||
|
} else if (message.command == AGENT_LOST) {
|
||||||
|
// check to see if we know about this agent
|
||||||
|
var agentIndex = this.knownAgents.indexOf(message.src);
|
||||||
|
if (agentIndex < 0) {
|
||||||
|
printDebug("Lost agent " + message.src + " " + agentIndex + " Forgeting about it");
|
||||||
|
this.knownAgents[agentIndex] = INVALID_ACTOR;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
MasterController.prototype.sendCommand = function(target, action, argument) {
|
MasterController.prototype.sendCommand = function(target, action, argument) {
|
||||||
if (this.subscribed) {
|
if (this.subscribed) {
|
||||||
var messageJSON = JSON.stringify(makeMessage(target, action, argument));
|
var messageJSON = packCommandMessage(target, action, argument);
|
||||||
Messages.sendMessage(COMMAND_CHANNEL, messageJSON);
|
Messages.sendMessage(COMMAND_CHANNEL, messageJSON);
|
||||||
printDebug("Master sent message: " + messageJSON);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -108,11 +142,30 @@ function printDebug(message) {
|
||||||
this.timeSinceLastAlive += deltaTime;
|
this.timeSinceLastAlive += deltaTime;
|
||||||
if (this.timeSinceLastAlive > ALIVE_PERIOD) {
|
if (this.timeSinceLastAlive > ALIVE_PERIOD) {
|
||||||
this.timeSinceLastAlive = 0;
|
this.timeSinceLastAlive = 0;
|
||||||
printDebug("Master ping alive");
|
this.sendCommand(AGENTS_BROADCAST, MASTER_ALIVE);
|
||||||
this.sendCommand(MASTER_INDEX, MASTER_ALIVE);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
MasterController.prototype.hireAgent = function(onHired) {
|
||||||
|
var localThis = this;
|
||||||
|
this.hiringAgentsQueue.unshift(function(agentID) {
|
||||||
|
printDebug("hiring callback with agent " + agentID);
|
||||||
|
|
||||||
|
var indexOfNewAgent = localThis.knownAgents.length;
|
||||||
|
localThis.knownAgents[indexOfNewAgent] = agentID;
|
||||||
|
|
||||||
|
printDebug("New agent available to be hired " + agentID + " " + index);
|
||||||
|
var hireMessage = "HIRE." + index;
|
||||||
|
var answerMessage = packAnnounceMessage(agentID, hireMessage, MASTER_ID);
|
||||||
|
Messages.sendMessage(ANNOUNCE_CHANNEL, answerMessage);
|
||||||
|
|
||||||
|
if (onHired != null) {
|
||||||
|
onHired(index);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
this.MasterController = MasterController;
|
this.MasterController = MasterController;
|
||||||
|
|
||||||
// agent side
|
// agent side
|
||||||
|
@ -120,16 +173,22 @@ function printDebug(message) {
|
||||||
var AgentController = function() {
|
var AgentController = function() {
|
||||||
this.subscribed = false;
|
this.subscribed = false;
|
||||||
|
|
||||||
this.timeSinceLastAlive = 0;
|
this._init();
|
||||||
this.numCyclesWithoutAlive = 0;
|
|
||||||
this.actorIndex = INVALID_ACTOR;
|
|
||||||
this.notifyAlive = false;
|
|
||||||
|
|
||||||
this.onHired = function() {};
|
this.onHired = function() {};
|
||||||
this.onCommand = function(command) {};
|
this.onCommand = function(command) {};
|
||||||
this.onFired = function() {};
|
this.onFired = function() {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
AgentController.prototype._init = function() {
|
||||||
|
this.actorIndex= INVALID_ACTOR;
|
||||||
|
this.notifyAlive = false;
|
||||||
|
this.timeSinceLastAlive = 0;
|
||||||
|
this.numCyclesWithoutAlive = 0;
|
||||||
|
this.actorUUID = makeUniqueUUID(Agent.sessionUUID);
|
||||||
|
printDebug("this.actorUUID = " + this.actorUUID);
|
||||||
|
}
|
||||||
|
|
||||||
AgentController.prototype.destroy = function() {
|
AgentController.prototype.destroy = function() {
|
||||||
if (this.subscribed) {
|
if (this.subscribed) {
|
||||||
this.fire();
|
this.fire();
|
||||||
|
@ -163,29 +222,34 @@ function printDebug(message) {
|
||||||
};
|
};
|
||||||
|
|
||||||
AgentController.prototype._processAnnounceMessage = function(message, senderID) {
|
AgentController.prototype._processAnnounceMessage = function(message, senderID) {
|
||||||
|
var announce = unpackCommandMessage(message);
|
||||||
//printDebug("Client " + this.actorIndex + " Received Announcement = " + message);
|
//printDebug("Client " + this.actorIndex + " Received Announcement = " + message);
|
||||||
if (message != AGENT_READY) {
|
if (announce.dest == this.actorUUID) {
|
||||||
|
if (announce.command != AGENT_READY) {
|
||||||
// this may be a message to hire me if i m not already
|
// this may be a message to hire me if i m not already
|
||||||
if (this.actorIndex == INVALID_ACTOR) {
|
if (this.actorIndex == INVALID_ACTOR) {
|
||||||
var parts = message.split(".");
|
printDebug(announce.command);
|
||||||
|
|
||||||
|
var parts = announce.command.split(".");
|
||||||
var agentID = parts[0];
|
var agentID = parts[0];
|
||||||
var actorIndex = parts[1];
|
var actorIndex = parts[1];
|
||||||
//printDebug("Client " + Agent.sessionUUID + " - " + agentID + " Hired!");
|
//printDebug("Client " + Agent.sessionUUID + " - " + agentID + " Hired!");
|
||||||
if (agentID == Agent.sessionUUID) {
|
// if (agentID == Agent.sessionUUID) {
|
||||||
this.actorIndex = actorIndex;
|
this.actorIndex = actorIndex;
|
||||||
printDebug("Client " + this.actorIndex + " Hired!");
|
printDebug("Client " + this.actorIndex + " Hired!");
|
||||||
this.onHired();
|
this.onHired();
|
||||||
// Messages.unsubscribe(ANNOUNCE_CHANNEL); // id announce channel
|
// Messages.unsubscribe(ANNOUNCE_CHANNEL); // id announce channel
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AgentController.prototype._processCommandMessage = function(message, senderID) {
|
AgentController.prototype._processCommandMessage = function(message, senderID) {
|
||||||
var command = unpackMessage(message);
|
var command = unpackCommandMessage(message);
|
||||||
//printDebug("Received command = " + JSON.stringify(command));
|
//printDebug("Received command = " + JSON.stringify(command));
|
||||||
|
|
||||||
if (command.id_key == localThis.actorIndex || command.id_key == MASTER_INDEX) {
|
if ((command.id_key == this.actorUUID) || (command.id_key == AGENTS_BROADCAST)) {
|
||||||
if (command.action_key == MASTER_ALIVE) {
|
if (command.action_key == MASTER_ALIVE) {
|
||||||
this.notifyAlive = true;
|
this.notifyAlive = true;
|
||||||
} else if (command.action_key == MASTER_FIRE_AGENT) {
|
} else if (command.action_key == MASTER_FIRE_AGENT) {
|
||||||
|
@ -200,12 +264,13 @@ function printDebug(message) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
AgentController.prototype.update = function(deltaTime) {
|
AgentController.prototype.update = function(deltaTime) {
|
||||||
this.timeSinceLastAlive += deltaTime;
|
this.timeSinceLastAlive += deltaTime;
|
||||||
if (this.timeSinceLastAlive > ALIVE_PERIOD) {
|
if (this.timeSinceLastAlive > ALIVE_PERIOD) {
|
||||||
if (this.subscribed) {
|
if (this.subscribed) {
|
||||||
if (this.actorIndex == INVALID_ACTOR) {
|
if (this.actorIndex == INVALID_ACTOR) {
|
||||||
Messages.sendMessage(ANNOUNCE_CHANNEL, AGENT_READY);
|
Messages.sendMessage(ANNOUNCE_CHANNEL, packAnnounceMessage(MASTER_ID, AGENT_READY, this.actorUUID));
|
||||||
//printDebug("Client Ready" + ANNOUNCE_CHANNEL + AGENT_READY);
|
//printDebug("Client Ready" + ANNOUNCE_CHANNEL + AGENT_READY);
|
||||||
} else {
|
} else {
|
||||||
this.numCyclesWithoutAlive++;
|
this.numCyclesWithoutAlive++;
|
||||||
|
@ -227,10 +292,15 @@ function printDebug(message) {
|
||||||
AgentController.prototype.fired = function() {
|
AgentController.prototype.fired = function() {
|
||||||
// clear the state first
|
// clear the state first
|
||||||
var wasHired = (this.actorIndex != INVALID_ACTOR);
|
var wasHired = (this.actorIndex != INVALID_ACTOR);
|
||||||
this.actorIndex= INVALID_ACTOR;
|
|
||||||
this.notifyAlive = false;
|
// Post a last message to master in case it still listen to warn that this agent is losing it
|
||||||
this.timeSinceLastAlive = 0;
|
if (wasHired) {
|
||||||
this.numCyclesWithoutAlive = 0;
|
Messages.sendMessage(ANNOUNCE_CHANNEL, packAnnounceMessage(MASTER_ID, AGENT_LOST, this.actorUUID));
|
||||||
|
}
|
||||||
|
|
||||||
|
// reset
|
||||||
|
this._init();
|
||||||
|
|
||||||
// then custom fire if was hired
|
// then custom fire if was hired
|
||||||
if (wasHired) {
|
if (wasHired) {
|
||||||
this.onFired();
|
this.onFired();
|
||||||
|
|
|
@ -54,13 +54,23 @@ var performanceLoadedNeedUpdate = false;
|
||||||
|
|
||||||
setupPlayback();
|
setupPlayback();
|
||||||
|
|
||||||
|
function onHiring(agentID, index) {
|
||||||
|
print("agent hired from playbackMaster! " + agentID + " " + index)
|
||||||
|
}
|
||||||
|
|
||||||
function setupPlayback() {
|
function setupPlayback() {
|
||||||
ac_number = Window.prompt("Insert number of agents: ","1");
|
ac_number = Window.prompt("Insert number of agents: ","1");
|
||||||
if (ac_number === "" || ac_number === null) {
|
if (ac_number === "" || ac_number === null) {
|
||||||
ac_number = 1;
|
ac_number = 1;
|
||||||
}
|
}
|
||||||
setupToolBars();
|
|
||||||
masterController.reset();
|
masterController.reset();
|
||||||
|
|
||||||
|
for (var i = 0; i < ac_number; i++) {
|
||||||
|
masterController.hireAgent(onHiring);
|
||||||
|
}
|
||||||
|
|
||||||
|
setupToolBars();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupToolBars() {
|
function setupToolBars() {
|
||||||
|
|
Loading…
Reference in a new issue