Improving the Master/Agent api

This commit is contained in:
Sam Cake 2015-11-25 02:50:12 -08:00
parent bad01d02f8
commit 574eb3dc99
3 changed files with 147 additions and 213 deletions

View file

@ -9,12 +9,16 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
function printDebug(message) {
print(message);
}
(function() {
var COMMAND_CHANNEL = "com.highfidelity.PlaybackChannel1"; var COMMAND_CHANNEL = "com.highfidelity.PlaybackChannel1";
var ANNOUNCE_CHANNEL = "com.highfidelity.playbackAgent.announceID"; var ANNOUNCE_CHANNEL = "com.highfidelity.playbackAgent.announceID";
// The time between alive messages on the command channel // The time between alive messages on the command channel
var ALIVE_PERIOD = 1; var ALIVE_PERIOD = 3;
var NUM_CYCLES_BEFORE_RESET = 5; var NUM_CYCLES_BEFORE_RESET = 5;
// Service Actions // Service Actions
@ -24,12 +28,7 @@ var INVALID_ACTOR = -2;
var MASTER_INDEX = -1; var MASTER_INDEX = -1;
var MASTER_ALIVE = -1; var MASTER_ALIVE = -1;
var MASTER_FIRE_AGENT = -2;
function printDebug(message) {
print(message);
}
(function() {
var makeMessage = function(id, action, argument) { var makeMessage = function(id, action, argument) {
var message = { var message = {
@ -45,38 +44,41 @@ function printDebug(message) {
}; };
// master side // master side
//---------------------------------
var MasterController = function() { var MasterController = function() {
this.timeSinceLastAlive = 0; this.timeSinceLastAlive = 0;
this.knownAgents = new Array; this.knownAgents = new Array;
this.subscribed = false; this.subscribed = false;
}; };
MasterController.prototype.reset = function() { MasterController.prototype.destroy = function() {
if (this.subscribed) { if (this.subscribed) {
this.timeSinceLastAlive = 0; Messages.unsubscribe(ANNOUNCE_CHANNEL);
Messages.unsubscribe(COMMAND_CHANNEL);
}
Messages.subscribe(COMMAND_CHANNEL);
Messages.subscribe(ANNOUNCE_CHANNEL);
this.timeSinceLastAlive = 0;
var localThis = this;
Messages.messageReceived.connect(function (channel, message, senderID) {
printDebug("Agent received");
if (channel == ANNOUNCE_CHANNEL) {
printDebug("Agent received");
// localThis._processAgentMessage(message, senderID);
}
});
// ready to roll, enable
this.subscribed = true; this.subscribed = true;
}
}; };
MasterController.prototype._processAgentMessage = function(message, senderID) { MasterController.prototype.reset = function() {
this.timeSinceLastAlive = 0;
if (!this.subscribed) {
Messages.subscribe(COMMAND_CHANNEL);
Messages.subscribe(ANNOUNCE_CHANNEL);
var localThis = this;
Messages.messageReceived.connect(function (channel, message, senderID) {
if (channel == ANNOUNCE_CHANNEL) {
localThis._processAnnounceMessage(message, senderID);
return;
}
});
}
// ready to roll, enable
this.subscribed = true;
printDebug("Master Started");
};
MasterController.prototype._processAnnounceMessage = function(message, senderID) {
if (message == AGENT_READY) { if (message == AGENT_READY) {
// check to see if we know about this agent // check to see if we know about this agent
@ -89,21 +91,11 @@ function printDebug(message) {
var acknowledgeMessage = senderID + "." + indexOfNewAgent; var acknowledgeMessage = senderID + "." + indexOfNewAgent;
Messages.sendMessage(ANNOUNCE_CHANNEL, acknowledgeMessage); Messages.sendMessage(ANNOUNCE_CHANNEL, acknowledgeMessage);
} else { } else {
printDebug("New agent still sending ready ? " + senderID); printDebug("New agent still sending ready ? " + senderID);
} }
} }
}; };
MasterController.prototype.destroy = function() {
if (this.subscribed) {
Messages.unsubscribe(ANNOUNCE_CHANNEL);
Messages.unsubscribe(COMMAND_CHANNEL);
this.timeSinceLastAlive = 0;
this.subscribed = true;
}
};
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 = JSON.stringify(makeMessage(target, action, argument));
@ -121,83 +113,109 @@ function printDebug(message) {
} }
}; };
this.MasterController = MasterController; this.MasterController = MasterController;
// agent side // agent side
//---------------------------------
var AgentController = function() { var AgentController = function() {
this.timeSinceLastAlive = 0;
this.subscribed = false; this.subscribed = false;
this.timeSinceLastAlive = 0;
this.numCyclesWithoutAlive = 0;
this.actorIndex = INVALID_ACTOR; this.actorIndex = INVALID_ACTOR;
this.notifyAlive = false; this.notifyAlive = false;
this.onHired = function() {};
this.onCommand = function(command) {};
this.onFired = function() {};
};
AgentController.prototype.destroy = function() {
if (this.subscribed) {
this.fire();
Messages.unsubscribe(ANNOUNCE_CHANNEL);
Messages.unsubscribe(COMMAND_CHANNEL);
this.subscribed = true;
}
}; };
AgentController.prototype.reset = function() { AgentController.prototype.reset = function() {
// If already hired, fire
this.fired();
if (!this.subscribed) { if (!this.subscribed) {
Messages.subscribe(COMMAND_CHANNEL); Messages.subscribe(COMMAND_CHANNEL);
Messages.subscribe(ANNOUNCE_CHANNEL); Messages.subscribe(ANNOUNCE_CHANNEL);
}
this.timeSinceLastAlive = 0;
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 == ANNOUNCE_CHANNEL) {
if (message != AGENT_READY) { localThis._processAnnounceMessage(message, senderID);
// this may be a message to hire me if i m not already
if (id == INVALID_ACTOR) {
var parts = message.split(".");
var agentID = parts[0];
var actorIndex = parts[1];
if (agentID == Agent.sessionUUID) {
// localThis.actorIndex = agentIndex;
Messages.unsubscribe(ANNOUNCE_CHANNEL); // id announce channel
}
}
}
return; return;
} }
if (channel == COMMAND_CHANNEL) { if (channel == COMMAND_CHANNEL) {
localThis._processCommandMessage(message, senderID);
return;
}
});
}
this.subscribed = true;
printDebug("Client Started");
};
AgentController.prototype._processAnnounceMessage = function(message, senderID) {
//printDebug("Client " + this.actorIndex + " Received Announcement = " + message);
if (message != AGENT_READY) {
// this may be a message to hire me if i m not already
if (this.actorIndex == INVALID_ACTOR) {
var parts = message.split(".");
var agentID = parts[0];
var actorIndex = parts[1];
//printDebug("Client " + Agent.sessionUUID + " - " + agentID + " Hired!");
if (agentID == Agent.sessionUUID) {
this.actorIndex = actorIndex;
printDebug("Client " + this.actorIndex + " Hired!");
this.onHired();
// Messages.unsubscribe(ANNOUNCE_CHANNEL); // id announce channel
}
}
}
}
AgentController.prototype._processCommandMessage = function(message, senderID) {
var command = unpackMessage(message); var command = unpackMessage(message);
printDebug("Received command = " == command); //printDebug("Received command = " + JSON.stringify(command));
if (command.id_key == localThis.actorIndex || command.id_key == MASTER_INDEX) { if (command.id_key == localThis.actorIndex || command.id_key == MASTER_INDEX) {
if (command.action_key == MASTER_ALIVE) { if (command.action_key == MASTER_ALIVE) {
// localThis.notifyAlive = true; this.notifyAlive = true;
} else if (command.action_key == MASTER_FIRE_AGENT) {
printDebug("Master firing Agent");
this.fire();
} else { } else {
// localThis._processMasterMessage(command, senderID); printDebug("True action received = " + JSON.stringify(command) + senderID);
this.onCommand(command);
} }
} else { } else {
// ignored // ignored
} }
return;
}
});
// ready to roll, enable
this.subscribed = true;
printDebug("In Reset");
};
AgentController.prototype._processMasterMessage = function(command, senderID) {
printDebug("True action received = " + JSON.stringify(command) + senderID);
}; };
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) {
printDebug("In update of client");
if (this.actorIndex == INVALID_ACTOR) { if (this.actorIndex == INVALID_ACTOR) {
Messages.sendMessage(ANNOUNCE_CHANNEL, AGENT_READY); Messages.sendMessage(ANNOUNCE_CHANNEL, AGENT_READY);
printDebug("Client Ready"); //printDebug("Client Ready" + ANNOUNCE_CHANNEL + AGENT_READY);
} else { } else {
this.numCyclesWithoutAlive++;
if (this.notifyAlive) { if (this.notifyAlive) {
this.notifyAlive = false; this.notifyAlive = false;
this.numCyclesWithoutAlive = 0;
printDebug("Master Alive"); printDebug("Master Alive");
} else if (this.timeSinceLastAlive > NUM_CYCLES_BEFORE_RESET * ALIVE_PERIOD) { } else if (this.numCyclesWithoutAlive > NUM_CYCLES_BEFORE_RESET) {
printDebug("Master Lost, reseting Agent"); printDebug("Master Lost, firing Agent");
this.actorIndex = UNKNOWN_AGENT_ID; this.fired();
} }
} }
} }
@ -206,6 +224,19 @@ function printDebug(message) {
} }
}; };
AgentController.prototype.fired = function() {
// clear the state first
var wasHired = (this.actorIndex != INVALID_ACTOR);
this.actorIndex= INVALID_ACTOR;
this.notifyAlive = false;
this.timeSinceLastAlive = 0;
this.numCyclesWithoutAlive = 0;
// then custom fire if was hired
if (wasHired) {
this.onFired();
}
}
this.AgentController = AgentController; this.AgentController = AgentController;
})(); })();

View file

@ -10,6 +10,7 @@
// //
Agent.isAvatar = true; Agent.isAvatar = true;
Script.include("./AgentPoolControler.js"); Script.include("./AgentPoolControler.js");
var agentController = new AgentController();
// Set the following variables to the values needed // Set the following variables to the values needed
var playFromCurrentLocation = true; var playFromCurrentLocation = true;
@ -17,18 +18,6 @@ var useDisplayName = true;
var useAttachments = true; var useAttachments = true;
var useAvatarModel = true; var useAvatarModel = true;
var agentController = new AgentController();
// ID of the agent. Two agents can't have the same ID.
//var UNKNOWN_AGENT_ID = -2;
//var id = UNKNOWN_AGENT_ID; // unknown until aknowledged
// The time between alive messages on the command channel
/*var timeSinceLastAlive = 0;
var ALIVE_PERIOD = 5;
var NUM_CYCLES_BEFORE_RESET = 5;
var notifyAlive = false;
*/
// Set position/orientation/scale here if playFromCurrentLocation is true // Set position/orientation/scale here if playFromCurrentLocation is true
Avatar.position = { x:0, y: 0, z: 0 }; Avatar.position = { x:0, y: 0, z: 0 };
@ -36,7 +25,6 @@ Avatar.orientation = Quat.fromPitchYawRollDegrees(0, 0, 0);
Avatar.scale = 1.0; Avatar.scale = 1.0;
var totalTime = 0; var totalTime = 0;
var subscribed = false;
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.
@ -55,10 +43,10 @@ Recording.setPlayerUseAttachments(useAttachments);
Recording.setPlayerUseHeadModel(false); Recording.setPlayerUseHeadModel(false);
Recording.setPlayerUseSkeletonModel(useAvatarModel); Recording.setPlayerUseSkeletonModel(useAvatarModel);
function getAction(channel, message, senderID) { function getAction(command) {
if(subscribed) { if(true) {
var command = JSON.parse(message); // var command = JSON.parse(message);
print("I'm the agent " + id + " and I received this: ID: " + command.id_key + " Action: " + command.action_key + " URL: " + command.clip_url_key); print("I'm the agent " + id + " and I received this: ID: " + command.id_key + " Action: " + command.action_key + " URL: " + command.clip_url_key);
if (command.id_key == id || command.id_key == -1) { if (command.id_key == id || command.id_key == -1) {
@ -114,17 +102,13 @@ function getAction(channel, message, senderID) {
if (!Agent.isAvatar) { if (!Agent.isAvatar) {
Agent.isAvatar = true; Agent.isAvatar = true;
} }
if(command.clip_url_key !== null) { if(command.argument_key !== null) {
print("Agent #" + id + " loading clip URL: " + command.clip_url_key); print("Agent #" + id + " loading clip URL: " + command.argument_key);
Recording.loadRecording(command.clip_url_key); Recording.loadRecording(command.argument_key);
} else { } else {
print("Agent #" + id + " loading clip URL is NULL, nothing happened"); print("Agent #" + id + " loading clip URL is NULL, nothing happened");
} }
break; break;
case MASTER_ALIVE:
print("Alive");
notifyAlive = true;
break;
case DO_NOTHING: case DO_NOTHING:
break; break;
default: default:
@ -139,66 +123,29 @@ function getAction(channel, message, senderID) {
} }
} }
function agentHired() {
print("Agent Hired from playbackAgents.js");
}
function agentFired() {
print("Agent Fired from playbackAgents.js");
}
function update(deltaTime) { function update(deltaTime) {
totalTime += deltaTime; totalTime += 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.onHired = agentHired;
agentController.onFired = agentFired;
} }
} }
/*
totalTime += deltaTime;
if (totalTime > WAIT_FOR_AUDIO_MIXER) {
if (!subscribed) {
Messages.subscribe(commandChannel); // command channel
Messages.subscribe(announceIDChannel); // id announce channel
subscribed = true;
print("I'm the agent and I am ready to receive!");
}
if (subscribed && id == UNKNOWN_AGENT_ID) {
Messages.sendMessage(announceIDChannel, "ready");
}
}
if (subscribed && id != UNKNOWN_AGENT_ID) {
timeSinceLastAlive += deltaTime;
if (notifyAlive) {
timeSinceLastAlive = 0;
notifyAlive = false;
print("Master Alive");
} else if (timeSinceLastAlive > NUM_CYCLES_BEFORE_RESET * ALIVE_PERIOD) {
print("Master Lost, reseting Agent");
if (Recording.isPlaying()) {
Recording.stopPlaying();
}
Agent.isAvatar = false;
id = UNKNOWN_AGENT_ID;
}
}*/
agentController.update(deltaTime); agentController.update(deltaTime);
} }
/*
Messages.messageReceived.connect(function (channel, message, senderID) {
if (channel == announceIDChannel && message != "ready") {
// If I don't yet know if my ID has been recieved, then check to see if the master has acknowledged me
if (id == UNKNOWN_AGENT_ID) {
var parts = message.split(".");
var agentID = parts[0];
var agentIndex = parts[1];
if (agentID == Agent.sessionUUID) {
id = agentIndex;
Messages.unsubscribe(announceIDChannel); // id announce channel
}
}
}
if (channel == commandChannel) {
getAction(channel, message, senderID);
}
});*/
function scriptEnding() { function scriptEnding() {

View file

@ -190,31 +190,9 @@ function sendCommand(id, action, argument) {
id = -1; id = -1;
masterController.sendMessage(id, action, argument); masterController.sendMessage(id, action, argument);
/*
var message = {
id_key: id,
action_key: action,
clip_url_key: argument
};
if(subscribed){
Messages.sendMessage(commandChannel, JSON.stringify(message));
print("Message sent!");
}*/
} }
} else { } else {
masterController.sendMessage(id, action, argument); masterController.sendMessage(id, action, argument);
/*
var message = {
id_key: id,
action_key: action,
clip_url_key: argument
};
if(subscribed){
Messages.sendMessage(commandChannel, JSON.stringify(message));
print("Message sent!");
}*/
} }
} }
@ -312,26 +290,4 @@ Controller.mousePressEvent.connect(mousePressEvent);
Script.update.connect(update); Script.update.connect(update);
Script.scriptEnding.connect(scriptEnding); Script.scriptEnding.connect(scriptEnding);
/*
Messages.subscribe(announceIDChannel);
Messages.messageReceived.connect(function (channel, message, senderID) {
if (channel == announceIDChannel && message == "ready") {
// check to see if we know about this agent
if (knownAgents.indexOf(senderID) < 0) {
print("New agent to be hired " + senderID);
var indexOfNewAgent = knownAgents.length;
knownAgents[indexOfNewAgent] = senderID;
var acknowledgeMessage = senderID + "." + indexOfNewAgent;
Overlays.editOverlay(nameOverlays[indexOfNewAgent], { backgroundColor: { red: 0, green: 255, blue: 0 }, text: "Agent-Hired" } );
print("Hired new Agent " + senderID + " #" + indexOfNewAgent);
Messages.sendMessage(announceIDChannel, acknowledgeMessage);
} else {
print("New agent still sending ready ? " + senderID);
}
}
});
*/
moveUI(); moveUI();