mirror of
https://github.com/overte-org/overte.git
synced 2025-04-19 15:03:53 +02:00
Improving the Master/Agent api
This commit is contained in:
parent
bad01d02f8
commit
574eb3dc99
3 changed files with 147 additions and 213 deletions
|
@ -9,27 +9,26 @@
|
|||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
var COMMAND_CHANNEL = "com.highfidelity.PlaybackChannel1";
|
||||
var ANNOUNCE_CHANNEL = "com.highfidelity.playbackAgent.announceID";
|
||||
|
||||
// The time between alive messages on the command channel
|
||||
var ALIVE_PERIOD = 1;
|
||||
|
||||
var NUM_CYCLES_BEFORE_RESET = 5;
|
||||
|
||||
// Service Actions
|
||||
var AGENT_READY = "ready";
|
||||
var INVALID_ACTOR = -2;
|
||||
|
||||
var MASTER_INDEX = -1;
|
||||
|
||||
var MASTER_ALIVE = -1;
|
||||
|
||||
function printDebug(message) {
|
||||
print(message);
|
||||
}
|
||||
|
||||
(function() {
|
||||
var COMMAND_CHANNEL = "com.highfidelity.PlaybackChannel1";
|
||||
var ANNOUNCE_CHANNEL = "com.highfidelity.playbackAgent.announceID";
|
||||
|
||||
// The time between alive messages on the command channel
|
||||
var ALIVE_PERIOD = 3;
|
||||
var NUM_CYCLES_BEFORE_RESET = 5;
|
||||
|
||||
// Service Actions
|
||||
var AGENT_READY = "ready";
|
||||
var INVALID_ACTOR = -2;
|
||||
|
||||
var MASTER_INDEX = -1;
|
||||
|
||||
var MASTER_ALIVE = -1;
|
||||
var MASTER_FIRE_AGENT = -2;
|
||||
|
||||
var makeMessage = function(id, action, argument) {
|
||||
var message = {
|
||||
|
@ -45,38 +44,41 @@ function printDebug(message) {
|
|||
};
|
||||
|
||||
// master side
|
||||
//---------------------------------
|
||||
var MasterController = function() {
|
||||
this.timeSinceLastAlive = 0;
|
||||
this.knownAgents = new Array;
|
||||
this.subscribed = false;
|
||||
};
|
||||
|
||||
MasterController.prototype.reset = function() {
|
||||
if (this.subscribed) {
|
||||
this.timeSinceLastAlive = 0;
|
||||
|
||||
MasterController.prototype.destroy = function() {
|
||||
if (this.subscribed) {
|
||||
Messages.unsubscribe(ANNOUNCE_CHANNEL);
|
||||
Messages.unsubscribe(COMMAND_CHANNEL);
|
||||
this.subscribed = true;
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
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._processAgentMessage = function(message, senderID) {
|
||||
MasterController.prototype._processAnnounceMessage = function(message, senderID) {
|
||||
if (message == AGENT_READY) {
|
||||
|
||||
// check to see if we know about this agent
|
||||
|
@ -89,20 +91,10 @@ function printDebug(message) {
|
|||
var acknowledgeMessage = senderID + "." + indexOfNewAgent;
|
||||
Messages.sendMessage(ANNOUNCE_CHANNEL, acknowledgeMessage);
|
||||
} else {
|
||||
|
||||
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) {
|
||||
if (this.subscribed) {
|
||||
|
@ -121,83 +113,109 @@ function printDebug(message) {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
this.MasterController = MasterController;
|
||||
|
||||
// agent side
|
||||
//---------------------------------
|
||||
var AgentController = function() {
|
||||
this.subscribed = false;
|
||||
|
||||
this.timeSinceLastAlive = 0;
|
||||
this.subscribed = false;
|
||||
this.numCyclesWithoutAlive = 0;
|
||||
this.actorIndex = INVALID_ACTOR;
|
||||
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() {
|
||||
// If already hired, fire
|
||||
this.fired();
|
||||
|
||||
if (!this.subscribed) {
|
||||
Messages.subscribe(COMMAND_CHANNEL);
|
||||
Messages.subscribe(ANNOUNCE_CHANNEL);
|
||||
}
|
||||
|
||||
this.timeSinceLastAlive = 0;
|
||||
|
||||
var localThis = this;
|
||||
Messages.messageReceived.connect(function (channel, message, senderID) {
|
||||
if (channel == ANNOUNCE_CHANNEL) {
|
||||
if (message != AGENT_READY) {
|
||||
// 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;
|
||||
}
|
||||
if (channel == COMMAND_CHANNEL) {
|
||||
var command = unpackMessage(message);
|
||||
printDebug("Received command = " == command);
|
||||
|
||||
if (command.id_key == localThis.actorIndex || command.id_key == MASTER_INDEX) {
|
||||
if (command.action_key == MASTER_ALIVE) {
|
||||
// localThis.notifyAlive = true;
|
||||
} else {
|
||||
// localThis._processMasterMessage(command, senderID);
|
||||
}
|
||||
} else {
|
||||
// ignored
|
||||
var localThis = this;
|
||||
Messages.messageReceived.connect(function (channel, message, senderID) {
|
||||
if (channel == ANNOUNCE_CHANNEL) {
|
||||
localThis._processAnnounceMessage(message, senderID);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
// ready to roll, enable
|
||||
if (channel == COMMAND_CHANNEL) {
|
||||
localThis._processCommandMessage(message, senderID);
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
this.subscribed = true;
|
||||
printDebug("In Reset");
|
||||
printDebug("Client Started");
|
||||
};
|
||||
|
||||
AgentController.prototype._processMasterMessage = function(command, senderID) {
|
||||
printDebug("True action received = " + JSON.stringify(command) + senderID);
|
||||
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);
|
||||
//printDebug("Received command = " + JSON.stringify(command));
|
||||
|
||||
if (command.id_key == localThis.actorIndex || command.id_key == MASTER_INDEX) {
|
||||
if (command.action_key == MASTER_ALIVE) {
|
||||
this.notifyAlive = true;
|
||||
} else if (command.action_key == MASTER_FIRE_AGENT) {
|
||||
printDebug("Master firing Agent");
|
||||
this.fire();
|
||||
} else {
|
||||
printDebug("True action received = " + JSON.stringify(command) + senderID);
|
||||
this.onCommand(command);
|
||||
}
|
||||
} else {
|
||||
// ignored
|
||||
}
|
||||
};
|
||||
|
||||
AgentController.prototype.update = function(deltaTime) {
|
||||
this.timeSinceLastAlive += deltaTime;
|
||||
if (this.timeSinceLastAlive > ALIVE_PERIOD) {
|
||||
if (this.timeSinceLastAlive > ALIVE_PERIOD) {
|
||||
if (this.subscribed) {
|
||||
printDebug("In update of client");
|
||||
if (this.actorIndex == INVALID_ACTOR) {
|
||||
Messages.sendMessage(ANNOUNCE_CHANNEL, AGENT_READY);
|
||||
printDebug("Client Ready");
|
||||
//printDebug("Client Ready" + ANNOUNCE_CHANNEL + AGENT_READY);
|
||||
} else {
|
||||
if (this.notifyAlive) {
|
||||
this.numCyclesWithoutAlive++;
|
||||
if (this.notifyAlive) {
|
||||
this.notifyAlive = false;
|
||||
this.numCyclesWithoutAlive = 0;
|
||||
printDebug("Master Alive");
|
||||
} else if (this.timeSinceLastAlive > NUM_CYCLES_BEFORE_RESET * ALIVE_PERIOD) {
|
||||
printDebug("Master Lost, reseting Agent");
|
||||
this.actorIndex = UNKNOWN_AGENT_ID;
|
||||
} else if (this.numCyclesWithoutAlive > NUM_CYCLES_BEFORE_RESET) {
|
||||
printDebug("Master Lost, firing Agent");
|
||||
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;
|
||||
})();
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
//
|
||||
Agent.isAvatar = true;
|
||||
Script.include("./AgentPoolControler.js");
|
||||
var agentController = new AgentController();
|
||||
|
||||
// Set the following variables to the values needed
|
||||
var playFromCurrentLocation = true;
|
||||
|
@ -17,18 +18,6 @@ var useDisplayName = true;
|
|||
var useAttachments = 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
|
||||
Avatar.position = { x:0, y: 0, z: 0 };
|
||||
|
@ -36,7 +25,6 @@ Avatar.orientation = Quat.fromPitchYawRollDegrees(0, 0, 0);
|
|||
Avatar.scale = 1.0;
|
||||
|
||||
var totalTime = 0;
|
||||
var subscribed = false;
|
||||
var WAIT_FOR_AUDIO_MIXER = 1;
|
||||
|
||||
// Script. DO NOT MODIFY BEYOND THIS LINE.
|
||||
|
@ -55,10 +43,10 @@ Recording.setPlayerUseAttachments(useAttachments);
|
|||
Recording.setPlayerUseHeadModel(false);
|
||||
Recording.setPlayerUseSkeletonModel(useAvatarModel);
|
||||
|
||||
function getAction(channel, message, senderID) {
|
||||
if(subscribed) {
|
||||
function getAction(command) {
|
||||
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);
|
||||
|
||||
if (command.id_key == id || command.id_key == -1) {
|
||||
|
@ -114,17 +102,13 @@ function getAction(channel, message, senderID) {
|
|||
if (!Agent.isAvatar) {
|
||||
Agent.isAvatar = true;
|
||||
}
|
||||
if(command.clip_url_key !== null) {
|
||||
print("Agent #" + id + " loading clip URL: " + command.clip_url_key);
|
||||
Recording.loadRecording(command.clip_url_key);
|
||||
if(command.argument_key !== null) {
|
||||
print("Agent #" + id + " loading clip URL: " + command.argument_key);
|
||||
Recording.loadRecording(command.argument_key);
|
||||
} else {
|
||||
print("Agent #" + id + " loading clip URL is NULL, nothing happened");
|
||||
}
|
||||
break;
|
||||
case MASTER_ALIVE:
|
||||
print("Alive");
|
||||
notifyAlive = true;
|
||||
break;
|
||||
case DO_NOTHING:
|
||||
break;
|
||||
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) {
|
||||
totalTime += deltaTime;
|
||||
if (totalTime > WAIT_FOR_AUDIO_MIXER) {
|
||||
totalTime += deltaTime;
|
||||
if (totalTime > WAIT_FOR_AUDIO_MIXER) {
|
||||
if (!agentController.subscribed) {
|
||||
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);
|
||||
}
|
||||
/*
|
||||
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() {
|
||||
|
||||
|
|
|
@ -190,31 +190,9 @@ function sendCommand(id, action, argument) {
|
|||
id = -1;
|
||||
|
||||
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 {
|
||||
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.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();
|
||||
|
|
Loading…
Reference in a new issue