mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 13:28:09 +02:00
MOnday spent on improving the Agent/Clint coordination, but still bugs
This commit is contained in:
parent
1be494a257
commit
1b197987ec
3 changed files with 217 additions and 154 deletions
|
@ -14,8 +14,8 @@ function printDebug(message) {
|
||||||
}
|
}
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
var COMMAND_CHANNEL = "com.highfidelity.PlaybackChannel1";
|
var SERVICE_CHANNEL = "com.highfidelity.playback.service";
|
||||||
var ANNOUNCE_CHANNEL = "com.highfidelity.playbackAgent.announceID";
|
var COMMAND_CHANNEL = "com.highfidelity.playback.command";
|
||||||
|
|
||||||
// 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;
|
||||||
|
@ -23,15 +23,15 @@ function printDebug(message) {
|
||||||
|
|
||||||
// Service Actions
|
// Service Actions
|
||||||
var MASTER_ID = -1;
|
var MASTER_ID = -1;
|
||||||
var AGENT_READY = "ready";
|
var INVALID_AGENT = -2;
|
||||||
var AGENT_LOST = "agentLost";
|
|
||||||
|
|
||||||
var INVALID_ACTOR = -2;
|
var BROADCAST_AGENTS = -3;
|
||||||
|
|
||||||
|
var MASTER_ALIVE = "MASTER_ALIVE";
|
||||||
var AGENTS_BROADCAST = -1;
|
var AGENT_ALIVE = "AGENT_ALIVE";
|
||||||
var MASTER_ALIVE = -1;
|
var AGENT_READY = "READY";
|
||||||
var MASTER_FIRE_AGENT = -2;
|
var MASTER_HIRE_AGENT = "HIRE"
|
||||||
|
var MASTER_FIRE_AGENT = "FIRE";
|
||||||
|
|
||||||
var makeUniqueUUID = function(SEUUID) {
|
var makeUniqueUUID = function(SEUUID) {
|
||||||
//return SEUUID + Math.random();
|
//return SEUUID + Math.random();
|
||||||
|
@ -39,7 +39,7 @@ function printDebug(message) {
|
||||||
return (Math.random() * 10000).toFixed(0);
|
return (Math.random() * 10000).toFixed(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
var packAnnounceMessage = function(dest, command, src) {
|
var packServiceMessage = function(dest, command, src) {
|
||||||
var message = {
|
var message = {
|
||||||
dest: dest,
|
dest: dest,
|
||||||
command: command,
|
command: command,
|
||||||
|
@ -48,7 +48,7 @@ function printDebug(message) {
|
||||||
return JSON.stringify(message);
|
return JSON.stringify(message);
|
||||||
};
|
};
|
||||||
|
|
||||||
var unpackAnnounceMessage = function(message) {
|
var unpackServiceMessage = function(message) {
|
||||||
return JSON.parse(message);
|
return JSON.parse(message);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -68,13 +68,27 @@ function printDebug(message) {
|
||||||
// Actor
|
// Actor
|
||||||
//---------------------------------
|
//---------------------------------
|
||||||
var Actor = function() {
|
var Actor = function() {
|
||||||
this.agentID = INVALID_ACTOR;
|
this.agentID = INVALID_AGENT;
|
||||||
|
this.lastAliveCycle = 0;
|
||||||
this.onHired = function(actor) {};
|
this.onHired = function(actor) {};
|
||||||
this.onLost = function(actor) {};
|
this.onFired = function(actor) {};
|
||||||
};
|
};
|
||||||
|
|
||||||
Actor.prototype.isConnected = function () {
|
Actor.prototype.isConnected = function () {
|
||||||
return (this.agentID != INVALID_ACTOR);
|
return (this.agentID != INVALID_AGENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
Actor.prototype.alive = function () {
|
||||||
|
printDebug("Agent UUID =" + this.agentID + " Alive was " + this.lastAliveCycle);
|
||||||
|
this.lastAliveCycle = 0;
|
||||||
|
}
|
||||||
|
Actor.prototype.incrementAliveCycle = function () {
|
||||||
|
printDebug("Actor.prototype.incrementAliveCycle UUID =" + this.agentID + " Alive pre increment " + this.lastAliveCycle);
|
||||||
|
if (this.isConnected()) {
|
||||||
|
this.lastAliveCycle++;
|
||||||
|
printDebug("Agent UUID =" + this.agentID + " Alive incremented " + this.lastAliveCycle);
|
||||||
|
}
|
||||||
|
return this.lastAliveCycle;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.Actor = Actor;
|
this.Actor = Actor;
|
||||||
|
@ -91,7 +105,7 @@ function printDebug(message) {
|
||||||
|
|
||||||
MasterController.prototype.destroy = function() {
|
MasterController.prototype.destroy = function() {
|
||||||
if (this.subscribed) {
|
if (this.subscribed) {
|
||||||
Messages.unsubscribe(ANNOUNCE_CHANNEL);
|
Messages.unsubscribe(SERVICE_CHANNEL);
|
||||||
Messages.unsubscribe(COMMAND_CHANNEL);
|
Messages.unsubscribe(COMMAND_CHANNEL);
|
||||||
this.subscribed = true;
|
this.subscribed = true;
|
||||||
}
|
}
|
||||||
|
@ -102,11 +116,11 @@ function printDebug(message) {
|
||||||
|
|
||||||
if (!this.subscribed) {
|
if (!this.subscribed) {
|
||||||
Messages.subscribe(COMMAND_CHANNEL);
|
Messages.subscribe(COMMAND_CHANNEL);
|
||||||
Messages.subscribe(ANNOUNCE_CHANNEL);
|
Messages.subscribe(SERVICE_CHANNEL);
|
||||||
var localThis = this;
|
var localThis = this;
|
||||||
Messages.messageReceived.connect(function (channel, message, senderID) {
|
Messages.messageReceived.connect(function (channel, message, senderID) {
|
||||||
if (channel == ANNOUNCE_CHANNEL) {
|
if (channel == SERVICE_CHANNEL) {
|
||||||
localThis._processAnnounceMessage(message, senderID);
|
localThis._processServiceMessage(message, senderID);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -116,34 +130,26 @@ function printDebug(message) {
|
||||||
printDebug("Master Started");
|
printDebug("Master Started");
|
||||||
};
|
};
|
||||||
|
|
||||||
MasterController.prototype._processAnnounceMessage = function(message, senderID) {
|
MasterController.prototype._processServiceMessage = function(message, senderID) {
|
||||||
var message = unpackAnnounceMessage(message);
|
var message = unpackServiceMessage(message);
|
||||||
if (message.dest == MASTER_ID) {
|
if (message.dest == MASTER_ID) {
|
||||||
if (message.command == AGENT_READY) {
|
if (message.command == AGENT_READY) {
|
||||||
// check to see if we know about this agent
|
// check to see if we know about this agent
|
||||||
var agentIndex = this.knownAgents.indexOf(message.src);
|
var agentIndex = this.knownAgents.indexOf(message.src);
|
||||||
if (agentIndex < 0) {
|
if (agentIndex < 0) {
|
||||||
if (this.hiringAgentsQueue.length > 0) {
|
this._onAgentAvailableForHiring(message.src);
|
||||||
var hiringHandler = this.hiringAgentsQueue.pop();
|
|
||||||
hiringHandler(message.src);
|
|
||||||
} else {
|
|
||||||
//No hiring in queue so bail
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Master think the agent is hired but not the other way around, forget about it
|
// 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");
|
printDebug("New agent still sending ready ? " + message.src + " " + agentIndex + " Forgeting about it");
|
||||||
lostActor.agentID = INVALID_ACTOR;
|
this._removeAgent(agentIndex);
|
||||||
lostActor.onLost(lostActor);
|
|
||||||
_clearAgent(agentIndex);
|
|
||||||
}
|
}
|
||||||
} else if (message.command == AGENT_LOST) {
|
} else if (message.command == AGENT_ALIVE) {
|
||||||
// check to see if we know about this agent
|
// check to see if we know about this agent
|
||||||
var agentIndex = this.knownAgents.indexOf(message.src);
|
var agentIndex = this.knownAgents.indexOf(message.src);
|
||||||
if (agentIndex >= 0) {
|
if (agentIndex >= 0) {
|
||||||
lostActor.agentID = INVALID_ACTOR;
|
// yes so reset its alive beat
|
||||||
lostActor.onLost(lostActor);
|
this.hiredActors[agentIndex].alive();
|
||||||
_clearAgent(agentIndex);
|
return;
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -151,10 +157,32 @@ function printDebug(message) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
MasterController.prototype._onAgentAvailableForHiring = function(agentID) {
|
||||||
|
if (this.hiringAgentsQueue.length == 0) {
|
||||||
|
printDebug("No Actor on the hiring queue");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printDebug("MasterController.prototype._onAgentAvailableForHiring " + agentID);
|
||||||
|
var newActor = this.hiringAgentsQueue.pop();
|
||||||
|
|
||||||
|
var indexOfNewAgent = this.knownAgents.push(agentID);
|
||||||
|
newActor.alive();
|
||||||
|
newActor.agentID = agentID;
|
||||||
|
this.hiredActors.push(newActor);
|
||||||
|
|
||||||
|
printDebug("New agent available to be hired " + agentID + " " + indexOfNewAgent);
|
||||||
|
Messages.sendMessage(SERVICE_CHANNEL, packServiceMessage(agentID, MASTER_HIRE_AGENT, MASTER_ID));
|
||||||
|
printDebug("message sent calling the actor" + JSON.stringify(newActor) );
|
||||||
|
|
||||||
|
newActor.onHired(newActor);
|
||||||
|
}
|
||||||
|
|
||||||
MasterController.prototype.sendCommand = function(target, action, argument) {
|
MasterController.prototype.sendCommand = function(target, action, argument) {
|
||||||
if (this.subscribed) {
|
if (this.subscribed) {
|
||||||
var messageJSON = packCommandMessage(target, action, argument);
|
var command = packCommandMessage(target, action, argument);
|
||||||
Messages.sendMessage(COMMAND_CHANNEL, messageJSON);
|
printDebug(command);
|
||||||
|
Messages.sendMessage(COMMAND_CHANNEL, command);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -162,7 +190,31 @@ 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;
|
||||||
this.sendCommand(AGENTS_BROADCAST, MASTER_ALIVE);
|
Messages.sendMessage(SERVICE_CHANNEL, packServiceMessage(BROADCAST_AGENTS, MASTER_ALIVE, MASTER_ID));
|
||||||
|
|
||||||
|
printDebug("checking the agents status");
|
||||||
|
{
|
||||||
|
// Check for alive connected agents
|
||||||
|
var lostAgents = new Array();
|
||||||
|
for (var i = 0; i < this.hiredActors.length; i++) {
|
||||||
|
var actor = this.hiredActors[i];
|
||||||
|
printDebug("checking :" + JSON.stringify(actor));
|
||||||
|
var lastAlive = actor.incrementAliveCycle()
|
||||||
|
if (lastAlive > NUM_CYCLES_BEFORE_RESET) {
|
||||||
|
printDebug("Agent Lost, firing Agent");
|
||||||
|
lostAgents.push(actor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// now fire gathered lost agents
|
||||||
|
if (lostAgents.length > 0) {
|
||||||
|
printDebug("Firing " + lostAgents.length + " agents" + JSON.stringify(lostAgents));
|
||||||
|
|
||||||
|
for (var i = 0; i < lostAgents.length; i++) {
|
||||||
|
this.fireAgent(lostAgents[l]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -172,54 +224,51 @@ function printDebug(message) {
|
||||||
printDebug("trying to hire an agent with a null actor, abort");
|
printDebug("trying to hire an agent with a null actor, abort");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var localThis = this;
|
if (actor.isConnected()) {
|
||||||
this.hiringAgentsQueue.unshift(function(agentID) {
|
printDebug("trying to hire an agent already connected, abort");
|
||||||
printDebug("hiring callback with agent " + agentID+ " " + JSON.stringify(localThis) );
|
return;
|
||||||
|
}
|
||||||
var indexOfNewAgent = localThis.knownAgents.push(agentID)
|
this.hiringAgentsQueue.unshift(actor);
|
||||||
actor.agentID = agentID;
|
|
||||||
localThis.hiredActors.push(actor);
|
|
||||||
|
|
||||||
printDebug("New agent available to be hired " + agentID + " " + indexOfNewAgent);
|
|
||||||
var hireMessage = "HIRE." + indexOfNewAgent;
|
|
||||||
var answerMessage = packAnnounceMessage(agentID, hireMessage, MASTER_ID);
|
|
||||||
Messages.sendMessage(ANNOUNCE_CHANNEL, answerMessage);
|
|
||||||
|
|
||||||
printDebug("message sent calling the actor" + JSON.stringify(actor) );
|
|
||||||
|
|
||||||
actor.onHired(actor);
|
|
||||||
})
|
|
||||||
};
|
};
|
||||||
|
|
||||||
MasterController.prototype.fireAgent = function(actor) {
|
MasterController.prototype.fireAgent = function(actor) {
|
||||||
// check to see if we know about this agent
|
// check to see if we know about this agent
|
||||||
printDebug("MasterController.prototype.fireAgent" + JSON.stringify(actor) + " " + JSON.stringify(this.knownAgents));
|
printDebug("MasterController.prototype.fireAgent" + JSON.stringify(actor) + " " + JSON.stringify(this.knownAgents));
|
||||||
|
|
||||||
|
var waitingIndex = this.hiringAgentsQueue.indexOf(actor);
|
||||||
|
if (waitingIndex >= 0) {
|
||||||
|
printDebug("MasterController.prototype.fireAgent found actor on waiting queue #" + waitingIndex);
|
||||||
|
this.hiringAgentsQueue.splice(waitingIndex, 1);
|
||||||
|
}
|
||||||
|
|
||||||
var actorIndex = this.knownAgents.indexOf(actor.agentID);
|
var actorIndex = this.knownAgents.indexOf(actor.agentID);
|
||||||
if (actorIndex >= 0) {
|
if (actorIndex >= 0) {
|
||||||
printDebug("fired actor found #" + actorIndex);
|
printDebug("fired actor found #" + actorIndex);
|
||||||
|
this._removeAgent(actorIndex);
|
||||||
var currentAgentID = actor.agentID;
|
|
||||||
this._clearAgent(actorIndex);
|
|
||||||
printDebug("fired actor found #" + actorIndex);
|
|
||||||
|
|
||||||
if (currentAgentID != INVALID_ACTOR) {
|
|
||||||
printDebug("fired actor is still connected, send fire command");
|
|
||||||
this.sendCommand(currentAgentID, MASTER_FIRE_AGENT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MasterController.prototype._clearAgent = function(actorIndex) {
|
MasterController.prototype._removeAgent = function(actorIndex) {
|
||||||
// check to see if we know about this agent
|
// check to see if we know about this agent
|
||||||
if (actorIndex >= 0) {
|
if (actorIndex >= 0) {
|
||||||
printDebug("before _clearAgent #" + actorIndex + " " + JSON.stringify(this))
|
printDebug("before _removeAgent #" + actorIndex + " " + JSON.stringify(this))
|
||||||
this.knownAgents.splice(actorIndex, 1);
|
this.knownAgents.splice(actorIndex, 1);
|
||||||
|
|
||||||
var lostActor = this.hiredActors[actorIndex];
|
var lostActor = this.hiredActors[actorIndex];
|
||||||
this.hiredActors.splice(actorIndex, 1);
|
this.hiredActors.splice(actorIndex, 1);
|
||||||
lostActor.agentID = INVALID_ACTOR;
|
|
||||||
printDebug("Clearing agent " + actorIndex + " Forgeting about it");
|
|
||||||
printDebug("after _clearAgent #" + actorIndex + " " + JSON.stringify(this))
|
|
||||||
|
|
||||||
|
var lostAgentID = lostActor.agentID;
|
||||||
|
lostActor.agentID = INVALID_AGENT;
|
||||||
|
|
||||||
|
printDebug("Clearing agent " + actorIndex + " Forgeting about it");
|
||||||
|
printDebug("after _removeAgent #" + actorIndex + " " + JSON.stringify(this))
|
||||||
|
|
||||||
|
if (lostAgentID != INVALID_AGENT) {
|
||||||
|
printDebug("fired actor is still connected, send fire command");
|
||||||
|
Messages.sendMessage(SERVICE_CHANNEL, packServiceMessage(lostAgentID, MASTER_FIRE_AGENT, MASTER_ID));
|
||||||
|
}
|
||||||
|
|
||||||
|
lostActor.onFired(lostActor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,18 +287,17 @@ function printDebug(message) {
|
||||||
};
|
};
|
||||||
|
|
||||||
AgentController.prototype._init = function() {
|
AgentController.prototype._init = function() {
|
||||||
this.actorIndex= INVALID_ACTOR;
|
this.isHired= false;
|
||||||
this.notifyAlive = false;
|
|
||||||
this.timeSinceLastAlive = 0;
|
this.timeSinceLastAlive = 0;
|
||||||
this.numCyclesWithoutAlive = 0;
|
this.numCyclesWithoutAlive = 0;
|
||||||
this.actorUUID = makeUniqueUUID(Agent.sessionUUID);
|
this.agentUUID = makeUniqueUUID(Agent.sessionUUID);
|
||||||
printDebug("this.actorUUID = " + this.actorUUID);
|
printDebug("this.agentUUID = " + this.agentUUID);
|
||||||
}
|
}
|
||||||
|
|
||||||
AgentController.prototype.destroy = function() {
|
AgentController.prototype.destroy = function() {
|
||||||
if (this.subscribed) {
|
if (this.subscribed) {
|
||||||
this.fired();
|
this.fired();
|
||||||
Messages.unsubscribe(ANNOUNCE_CHANNEL);
|
Messages.unsubscribe(SERVICE_CHANNEL);
|
||||||
Messages.unsubscribe(COMMAND_CHANNEL);
|
Messages.unsubscribe(COMMAND_CHANNEL);
|
||||||
this.subscribed = true;
|
this.subscribed = true;
|
||||||
}
|
}
|
||||||
|
@ -261,11 +309,11 @@ function printDebug(message) {
|
||||||
|
|
||||||
if (!this.subscribed) {
|
if (!this.subscribed) {
|
||||||
Messages.subscribe(COMMAND_CHANNEL);
|
Messages.subscribe(COMMAND_CHANNEL);
|
||||||
Messages.subscribe(ANNOUNCE_CHANNEL);
|
Messages.subscribe(SERVICE_CHANNEL);
|
||||||
var localThis = this;
|
var localThis = this;
|
||||||
Messages.messageReceived.connect(function (channel, message, senderID) {
|
Messages.messageReceived.connect(function (channel, message, senderID) {
|
||||||
if (channel == ANNOUNCE_CHANNEL) {
|
if (channel == SERVICE_CHANNEL) {
|
||||||
localThis._processAnnounceMessage(message, senderID);
|
localThis._processServiceMessage(message, senderID);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (channel == COMMAND_CHANNEL) {
|
if (channel == COMMAND_CHANNEL) {
|
||||||
|
@ -278,44 +326,39 @@ function printDebug(message) {
|
||||||
printDebug("Client Started");
|
printDebug("Client Started");
|
||||||
};
|
};
|
||||||
|
|
||||||
AgentController.prototype._processAnnounceMessage = function(message, senderID) {
|
AgentController.prototype._processServiceMessage = function(message, senderID) {
|
||||||
var announce = unpackAnnounceMessage(message);
|
var announce = unpackServiceMessage(message);
|
||||||
//printDebug("Client " + this.actorIndex + " Received Announcement = " + message);
|
//printDebug("Client " + this.agentUUID + " Received Announcement = " + message);
|
||||||
if (announce.dest == this.actorUUID) {
|
if (announce.dest == this.agentUUID) {
|
||||||
if (announce.command != AGENT_READY) {
|
if (announce.command != AGENT_READY) {
|
||||||
// this may be a message to hire me if i m not already
|
|
||||||
if (this.actorIndex == INVALID_ACTOR) {
|
|
||||||
printDebug(announce.command);
|
|
||||||
|
|
||||||
var parts = announce.command.split(".");
|
var parts = announce.command.split(".");
|
||||||
var commandPart0 = parts[0];
|
|
||||||
var commandPart1 = parts[1];
|
// this is potnetially a message to hire me if i m not already
|
||||||
//printDebug("Client " + Agent.sessionUUID + " - " + agentID + " Hired!");
|
if (!this.isHired && (parts[0] == MASTER_HIRE_AGENT)) {
|
||||||
// if (agentID == Agent.sessionUUID) {
|
printDebug(announce.command);
|
||||||
this.actorIndex = commandPart1;
|
this.isHired = true;
|
||||||
printDebug("Client " + this.actorIndex + " Hired!");
|
printDebug("Client Hired by master UUID" + senderID);
|
||||||
this.onHired();
|
this.onHired();
|
||||||
// Messages.unsubscribe(ANNOUNCE_CHANNEL); // id announce channel
|
return;
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
if (this.isHired && (parts[0] == MASTER_FIRE_AGENT)) {
|
||||||
|
printDebug("Client Fired by master UUID" + senderID);
|
||||||
|
this.fired();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (announce.src == MASTER_ID && announce.command == MASTER_ALIVE) {
|
||||||
|
this.numCyclesWithoutAlive = 0;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AgentController.prototype._processCommandMessage = function(message, senderID) {
|
AgentController.prototype._processCommandMessage = function(message, senderID) {
|
||||||
var command = unpackCommandMessage(message);
|
var command = unpackCommandMessage(message);
|
||||||
//printDebug("Received command = " + JSON.stringify(command));
|
if ((command.dest_key == this.agentUUID) || (command.dest_key == BROADCAST_AGENTS)) {
|
||||||
|
printDebug("Command received = " + JSON.stringify(command) + senderID);
|
||||||
if ((command.dest_key == this.actorUUID) || (command.dest_key == AGENTS_BROADCAST)) {
|
|
||||||
if (command.action_key == MASTER_ALIVE) {
|
|
||||||
this.notifyAlive = true;
|
|
||||||
} else if (command.action_key == MASTER_FIRE_AGENT) {
|
|
||||||
printDebug("Master firing Agent");
|
|
||||||
this.fired();
|
|
||||||
} else {
|
|
||||||
printDebug("True action received = " + JSON.stringify(command) + senderID);
|
|
||||||
this.onCommand(command);
|
this.onCommand(command);
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// ignored
|
// ignored
|
||||||
}
|
}
|
||||||
|
@ -326,17 +369,18 @@ function printDebug(message) {
|
||||||
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.isHired) {
|
||||||
Messages.sendMessage(ANNOUNCE_CHANNEL, packAnnounceMessage(MASTER_ID, AGENT_READY, this.actorUUID));
|
Messages.sendMessage(SERVICE_CHANNEL, packServiceMessage(MASTER_ID, AGENT_READY, this.agentUUID));
|
||||||
//printDebug("Client Ready" + ANNOUNCE_CHANNEL + AGENT_READY);
|
//printDebug("Client Ready" + SERVICE_CHANNEL + AGENT_READY);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
// Send alive beat
|
||||||
|
Messages.sendMessage(SERVICE_CHANNEL, packServiceMessage(MASTER_ID, AGENT_ALIVE, this.agentUUID));
|
||||||
|
|
||||||
|
// Listen for master beat
|
||||||
this.numCyclesWithoutAlive++;
|
this.numCyclesWithoutAlive++;
|
||||||
if (this.notifyAlive) {
|
if (this.numCyclesWithoutAlive > NUM_CYCLES_BEFORE_RESET) {
|
||||||
this.notifyAlive = false;
|
printDebug("Master Lost, self firing Agent");
|
||||||
this.numCyclesWithoutAlive = 0;
|
|
||||||
printDebug("Master Alive");
|
|
||||||
} else if (this.numCyclesWithoutAlive > NUM_CYCLES_BEFORE_RESET) {
|
|
||||||
printDebug("Master Lost, firing Agent");
|
|
||||||
this.fired();
|
this.fired();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -348,12 +392,7 @@ 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.isHired;
|
||||||
|
|
||||||
// Post a last message to master in case it still listen to warn that this agent is losing it
|
|
||||||
if (wasHired) {
|
|
||||||
Messages.sendMessage(ANNOUNCE_CHANNEL, packAnnounceMessage(MASTER_ID, AGENT_LOST, this.actorUUID));
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset
|
// reset
|
||||||
this._init();
|
this._init();
|
||||||
|
@ -366,6 +405,10 @@ function printDebug(message) {
|
||||||
|
|
||||||
|
|
||||||
this.AgentController = AgentController;
|
this.AgentController = AgentController;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
this.BROADCAST_AGENTS = BROADCAST_AGENTS;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,6 @@ var totalTime = 0;
|
||||||
var WAIT_FOR_AUDIO_MIXER = 1;
|
var WAIT_FOR_AUDIO_MIXER = 1;
|
||||||
|
|
||||||
// Script. DO NOT MODIFY BEYOND THIS LINE.
|
// Script. DO NOT MODIFY BEYOND THIS LINE.
|
||||||
var ALIVE = -1;
|
|
||||||
var PLAY = 1;
|
var PLAY = 1;
|
||||||
var PLAY_LOOP = 2;
|
var PLAY_LOOP = 2;
|
||||||
var STOP = 3;
|
var STOP = 3;
|
||||||
|
@ -45,11 +44,11 @@ Recording.setPlayerUseAttachments(useAttachments);
|
||||||
Recording.setPlayerUseHeadModel(false);
|
Recording.setPlayerUseHeadModel(false);
|
||||||
Recording.setPlayerUseSkeletonModel(useAvatarModel);
|
Recording.setPlayerUseSkeletonModel(useAvatarModel);
|
||||||
|
|
||||||
function getAction(command) {
|
function agentCommand(command) {
|
||||||
if(true) {
|
if(true) {
|
||||||
|
|
||||||
// var command = JSON.parse(message);
|
// var command = JSON.parse(message);
|
||||||
print("I'm the agent " + this.actorUUID + " and I received this: Dest: " + command.dest_key + " Action: " + command.action_key + " URL: " + command.argument_key);
|
print("I'm the agent " + this.agentUUID + " and I received this: Dest: " + command.dest_key + " Action: " + command.action_key + " URL: " + command.argument_key);
|
||||||
|
|
||||||
switch(command.action_key) {
|
switch(command.action_key) {
|
||||||
case PLAY:
|
case PLAY:
|
||||||
|
@ -82,12 +81,18 @@ function getAction(command) {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LOAD:
|
case LOAD:
|
||||||
print("Load");
|
{
|
||||||
if(command.argument_key !== null) {
|
print("Load" + command.argument_key);
|
||||||
|
print("Agent #" + command.dest_key + " loading clip URL: " + command.argument_key);
|
||||||
|
Recording.loadRecording(command.argument_key);
|
||||||
|
print("After Load" + command.argument_key);
|
||||||
|
|
||||||
|
/*if(command.argument_key == null) {
|
||||||
|
print("Agent #" + id + " loading clip URL is NULL, nothing happened");
|
||||||
|
} else *{
|
||||||
print("Agent #" + id + " loading clip URL: " + command.argument_key);
|
print("Agent #" + id + " loading clip URL: " + command.argument_key);
|
||||||
Recording.loadRecording(command.argument_key);
|
Recording.loadRecording(command.argument_key);
|
||||||
} else {
|
}*/
|
||||||
print("Agent #" + id + " loading clip URL is NULL, nothing happened");
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -119,7 +124,7 @@ function update(deltaTime) {
|
||||||
if (totalTime > WAIT_FOR_AUDIO_MIXER) {
|
if (totalTime > WAIT_FOR_AUDIO_MIXER) {
|
||||||
if (!agentController.subscribed) {
|
if (!agentController.subscribed) {
|
||||||
agentController.reset();
|
agentController.reset();
|
||||||
agentController.onCommand = getAction;
|
agentController.onCommand = agentCommand;
|
||||||
agentController.onHired = agentHired;
|
agentController.onHired = agentHired;
|
||||||
agentController.onFired = agentFired;
|
agentController.onFired = agentFired;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,6 @@ Script.include(HIFI_PUBLIC_BUCKET + "scripts/libraries/toolBars.js");
|
||||||
Tool.IMAGE_HEIGHT /= 2;
|
Tool.IMAGE_HEIGHT /= 2;
|
||||||
Tool.IMAGE_WIDTH /= 2;
|
Tool.IMAGE_WIDTH /= 2;
|
||||||
|
|
||||||
var DO_NOTHING = 0;
|
|
||||||
var PLAY = 1;
|
var PLAY = 1;
|
||||||
var PLAY_LOOP = 2;
|
var PLAY_LOOP = 2;
|
||||||
var STOP = 3;
|
var STOP = 3;
|
||||||
|
@ -149,10 +148,10 @@ Director.prototype.destroy = function () {
|
||||||
|
|
||||||
Director.prototype.clearActors = function () {
|
Director.prototype.clearActors = function () {
|
||||||
print("Director.prototype.clearActors")
|
print("Director.prototype.clearActors")
|
||||||
for (var i = 0; i < this.actors.length; i++) {
|
while (this.actors.length > 0) {
|
||||||
print("Destroy actor #" + i)
|
this.actors.pop().destroy();
|
||||||
this.actors[i].destroy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.actors = new Array();// Brand new actors
|
this.actors = new Array();// Brand new actors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,23 +221,40 @@ Director.prototype._buildUI = function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
Director.prototype.onMousePressEvent = function(clickedOverlay) {
|
Director.prototype.onMousePressEvent = function(clickedOverlay) {
|
||||||
if (this.onOffIcon === this.toolbar.clicked(clickedOverlay, false)) {
|
if (this.playIcon === this.toolbar.clicked(clickedOverlay, false)) {
|
||||||
|
print("master play");
|
||||||
|
masterController.sendCommand(BROADCAST_AGENTS, PLAY);
|
||||||
|
} else if (this.onOffIcon === this.toolbar.clicked(clickedOverlay, false)) {
|
||||||
this.clearActors();
|
this.clearActors();
|
||||||
return true;
|
return true;
|
||||||
} else if (this.playIcon === this.toolbar.clicked(clickedOverlay, false)) {
|
|
||||||
masterController.sendCommand(AGENTS_BROADCAST, PLAY);
|
|
||||||
} else if (this.playLoopIcon === this.toolbar.clicked(clickedOverlay, false)) {
|
} else if (this.playLoopIcon === this.toolbar.clicked(clickedOverlay, false)) {
|
||||||
masterController.sendCommand(AGENTS_BROADCAST, PLAY_LOOP);
|
masterController.sendCommand(BROADCAST_AGENTS, PLAY_LOOP);
|
||||||
} else if (this.stopIcon === this.toolbar.clicked(clickedOverlay, false)) {
|
} else if (this.stopIcon === this.toolbar.clicked(clickedOverlay, false)) {
|
||||||
masterController.sendCommand(AGENTS_BROADCAST, STOP);
|
masterController.sendCommand(BROADCAST_AGENTS, STOP);
|
||||||
} else if (this.loadIcon === this.toolbar.clicked(clickedOverlay, false)) {
|
} else if (this.loadIcon === this.toolbar.clicked(clickedOverlay, false)) {
|
||||||
input_text = Window.prompt("Insert the url of the clip: ","");
|
input_text = Window.prompt("Insert the url of the clip: ","");
|
||||||
if (!(input_text === "" || input_text === null)) {
|
if (!(input_text === "" || input_text === null)) {
|
||||||
print("Performance file ready to be loaded url = " + input_text);
|
print("Performance file ready to be loaded url = " + input_text);
|
||||||
|
var urlpartition = input_text.split(".");
|
||||||
|
print(urlpartition[0]);
|
||||||
|
print(urlpartition[1]);
|
||||||
|
|
||||||
|
if ((urlpartition.length > 1) && (urlpartition[urlpartition.length - 1] === "hfr")) {
|
||||||
|
print("detected a unique clip url");
|
||||||
|
var oneClipPerformance = new Object();
|
||||||
|
oneClipPerformance.avatarClips = new Array();
|
||||||
|
oneClipPerformance.avatarClips[0] = input_text;
|
||||||
|
|
||||||
|
print(JSON.stringify(oneClipPerformance));
|
||||||
|
|
||||||
|
// we make a local simple performance file with a single clip and pipe in directly
|
||||||
|
this.onPerformanceLoaded(oneClipPerformance);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
// FIXME: I cannot pass directly this.onPerformanceLoaded, is that exepected ?
|
// FIXME: I cannot pass directly this.onPerformanceLoaded, is that exepected ?
|
||||||
var localThis = this;
|
var localThis = this;
|
||||||
Assets.downloadData(input_text, function(data) { localThis.onPerformanceLoaded(data); });
|
Assets.downloadData(input_text, function(data) { localThis.onPerformanceLoaded(JSON.parse(data)); });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Check individual controls
|
// Check individual controls
|
||||||
|
@ -270,20 +286,18 @@ Director.prototype.moveUI = function(pos) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Director.prototype.onPerformanceLoaded = function(performanceData) {
|
Director.prototype.onPerformanceLoaded = function(performanceJSON) {
|
||||||
var performanceJSON = JSON.parse(performanceData);
|
// First fire all the current actors
|
||||||
|
this.clearActors();
|
||||||
|
|
||||||
print("Director.prototype.onPerformanceLoaded = " + JSON.stringify(performanceJSON));
|
print("Director.prototype.onPerformanceLoaded = " + JSON.stringify(performanceJSON));
|
||||||
if (performanceJSON.avatarClips != null) {
|
if (performanceJSON.avatarClips != null) {
|
||||||
var numClips = performanceJSON.avatarClips.length;
|
var numClips = performanceJSON.avatarClips.length;
|
||||||
print("Found " + numClips + "in the performance file, and currently using " + this.actors.length + " actor(s)");
|
print("Found " + numClips + "in the performance file, and currently using " + this.actors.length + " actor(s)");
|
||||||
|
|
||||||
for (var i = 0; i < numClips; i++) {
|
for (var i = 0; i < numClips; i++) {
|
||||||
if (i < this.actors.length) {
|
|
||||||
// load correct clip to actor
|
|
||||||
} else {
|
|
||||||
this.hireActor(performanceJSON.avatarClips[i]);
|
this.hireActor(performanceJSON.avatarClips[i]);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -309,13 +323,14 @@ Director.prototype.hireActor = function(clipURL) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
newActor.onLost = function(actor) {
|
newActor.onFired = function(actor) {
|
||||||
print("agent lost from playbackMaster! " + actor.agentID);
|
print("agent fired from playbackMaster! " + actor.agentID);
|
||||||
var index = localThis.actors.indexOf(actor);
|
var index = localThis.actors.indexOf(actor);
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
localThis.actors.splice(index, 1);
|
localThis.actors.splice(index, 1);
|
||||||
}
|
}
|
||||||
actor.destroy();
|
actor.destroy();
|
||||||
|
moveUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
newActor.resetClip(clipURL, function(actor) {
|
newActor.resetClip(clipURL, function(actor) {
|
||||||
|
|
Loading…
Reference in a new issue