Tidied up registry log message, line lengths and use of static final int instead of an enum

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5265 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Jamie Doornbos
2008-07-25 22:15:56 +00:00
parent 79d7d60770
commit 4a5f474183
@@ -202,8 +202,7 @@ public class BureauRegistry
public void setLauncher (String bureauType, Launcher launcher) public void setLauncher (String bureauType, Launcher launcher)
{ {
if (_launchers.get(bureauType) != null) { if (_launchers.get(bureauType) != null) {
log.warning("Launcher for type already exists [type=" + log.warning("Launcher for type already exists", "type", bureauType);
bureauType + "]");
return; return;
} }
@@ -220,11 +219,10 @@ public class BureauRegistry
_omgr.registerObject(agent); _omgr.registerObject(agent);
log.info("Bureau ready, sending createAgent " + log.info("Bureau ready, sending createAgent", "agent", agent);
StringUtil.toString(agent));
BureauSender.createAgent(bureau.clientObj, agent.getOid()); BureauSender.createAgent(bureau.clientObj, agent.getOid());
bureau.agentStates.put(agent, Bureau.STARTED); bureau.agentStates.put(agent, AgentState.STARTED);
bureau.summarize(); bureau.summarize();
@@ -235,19 +233,15 @@ public class BureauRegistry
Launcher launcher = _launchers.get(agent.bureauType); Launcher launcher = _launchers.get(agent.bureauType);
if (launcher == null) { if (launcher == null) {
log.warning("Launcher not found for agent's " + log.warning("Launcher not found", "agent", agent);
"bureau type " + StringUtil.toString(agent));
return; return;
} }
log.info("Creating new bureau " + log.info("Creating new bureau", "bureauId", agent.bureauId, "launcher", launcher);
StringUtil.toString(agent.bureauId) + " " +
StringUtil.toString(launcher));
bureau = new Bureau(); bureau = new Bureau();
bureau.bureauId = agent.bureauId; bureau.bureauId = agent.bureauId;
bureau.token = generateToken(bureau.bureauId); bureau.token = generateToken(bureau.bureauId);
bureau.launcher = launcher; bureau.launcher = launcher;
_invoker.postUnit(new LauncherUnit(bureau)); _invoker.postUnit(new LauncherUnit(bureau));
@@ -256,10 +250,9 @@ public class BureauRegistry
} }
_omgr.registerObject(agent); _omgr.registerObject(agent);
bureau.agentStates.put(agent, Bureau.PENDING); bureau.agentStates.put(agent, AgentState.PENDING);
log.info("Bureau not ready, pending agent " + log.info("Bureau not ready, pending agent", "agent", agent);
StringUtil.toString(agent));
bureau.summarize(); bureau.summarize();
} }
@@ -275,31 +268,26 @@ public class BureauRegistry
return; return;
} }
log.info("Destroying agent " + StringUtil.toString(agent)); log.info("Destroying agent", "agent", agent);
// transition the agent to a new state and perform the effect of the transition // transition the agent to a new state and perform the effect of the transition
switch (found.state) { if (found.state == AgentState.PENDING) {
case Bureau.PENDING:
found.bureau.agentStates.remove(found.agent); found.bureau.agentStates.remove(found.agent);
// !TODO: is the the right place to destroy it? // !TODO: is the the right place to destroy it?
_omgr.destroyObject(found.agent.getOid()); _omgr.destroyObject(found.agent.getOid());
break;
case Bureau.STARTED: } else if (found.state == AgentState.STARTED) {
found.bureau.agentStates.put(found.agent, Bureau.STILL_BORN); found.bureau.agentStates.put(found.agent, AgentState.STILL_BORN);
break;
case Bureau.RUNNING: } else if (found.state == AgentState.RUNNING) {
BureauSender.destroyAgent(found.bureau.clientObj, agent.getOid()); BureauSender.destroyAgent(found.bureau.clientObj, agent.getOid());
found.bureau.agentStates.put(found.agent, Bureau.DESTROYED); found.bureau.agentStates.put(found.agent, AgentState.DESTROYED);
break;
case Bureau.DESTROYED: } else if (found.state == AgentState.DESTROYED ||
case Bureau.STILL_BORN: found.state == AgentState.STILL_BORN) {
log.warning("Acknowledging a request to destory an agent, but agent " + log.warning(
"is in state " + found.state + ", ignoring request " + "Ignoring request to destroy agent in unexpected state", "state", found.state,
StringUtil.toString(found.agent)); "agent", found.agent);
break;
} }
found.bureau.summarize(); found.bureau.summarize();
@@ -321,7 +309,7 @@ public class BureauRegistry
{ {
Bureau bureau = _bureaus.get(id); Bureau bureau = _bureaus.get(id);
if (bureau == null) { if (bureau == null) {
log.warning("Starting session for unknoqn bureau", "id", id, "client", client); log.warning("Starting session for unknown bureau", "id", id, "client", client);
return; return;
} }
if (bureau.client != null) { if (bureau.client != null) {
@@ -357,32 +345,30 @@ public class BureauRegistry
{ {
final Bureau bureau = _bureaus.get(bureauId); final Bureau bureau = _bureaus.get(bureauId);
if (bureau == null) { if (bureau == null) {
log.warning("Acknowledging initialization of non-existent bureau " + log.warning("Initialization of non-existent bureau", "bureauId", bureauId);
StringUtil.toString(bureauId));
return; return;
} }
bureau.clientObj = client; bureau.clientObj = client;
log.info("Bureau created " + StringUtil.toString(bureau) + log.info("Bureau created, launching pending agents", "bureau", bureau);
", launching pending agents");
// find all pending agents // find all pending agents
Set<AgentObject> pending = Sets.newHashSet(); Set<AgentObject> pending = Sets.newHashSet();
for (Map.Entry<AgentObject, Integer> entry : for (Map.Entry<AgentObject, AgentState> entry :
bureau.agentStates.entrySet()) { bureau.agentStates.entrySet()) {
if (entry.getValue() == Bureau.PENDING) { if (entry.getValue() == AgentState.PENDING) {
pending.add(entry.getKey()); pending.add(entry.getKey());
} }
} }
// create them // create them
for (AgentObject agent : pending) { for (AgentObject agent : pending) {
log.info("Creating agent " + StringUtil.toString(agent)); log.info("Creating agent", "agent", agent);
BureauSender.createAgent(bureau.clientObj, agent.getOid()); BureauSender.createAgent(bureau.clientObj, agent.getOid());
bureau.agentStates.put(agent, Bureau.STARTED); bureau.agentStates.put(agent, AgentState.STARTED);
} }
bureau.summarize(); bureau.summarize();
@@ -398,26 +384,22 @@ public class BureauRegistry
return; return;
} }
log.info("Agent creation confirmed " + StringUtil.toString(found.agent)); log.info("Agent creation confirmed", "agent", found.agent);
switch (found.state) { if (found.state == AgentState.STARTED) {
case Bureau.STARTED: found.bureau.agentStates.put(found.agent, AgentState.RUNNING);
found.bureau.agentStates.put(found.agent, Bureau.RUNNING);
found.agent.setClientOid(client.getOid()); found.agent.setClientOid(client.getOid());
break;
case Bureau.STILL_BORN: } else if (found.state == AgentState.STILL_BORN) {
BureauSender.destroyAgent(found.bureau.clientObj, agentId); BureauSender.destroyAgent(found.bureau.clientObj, agentId);
found.bureau.agentStates.put(found.agent, Bureau.DESTROYED); found.bureau.agentStates.put(found.agent, AgentState.DESTROYED);
break;
case Bureau.PENDING: } else if (found.state == AgentState.PENDING ||
case Bureau.RUNNING: found.state == AgentState.RUNNING ||
case Bureau.DESTROYED: found.state == AgentState.DESTROYED) {
log.warning("Received acknowledgement of the creation of an " + log.warning(
"agent in state " + found.state + ", ignoring request " + "Ignoring confirmation of creation of an agent in an unexpected state", "state",
StringUtil.toString(found.agent)); found.state, "agent", found.agent);
break;
} }
found.bureau.summarize(); found.bureau.summarize();
@@ -433,21 +415,18 @@ public class BureauRegistry
return; return;
} }
log.info("Agent creation failed " + StringUtil.toString(found.agent)); log.info("Agent creation failed", "agent", found.agent);
switch (found.state) { if (found.state == AgentState.STARTED ||
case Bureau.STARTED: found.state == AgentState.STILL_BORN) {
case Bureau.STILL_BORN:
found.bureau.agentStates.remove(found.agent); found.bureau.agentStates.remove(found.agent);
break;
case Bureau.PENDING: } else if (found.state == AgentState.PENDING ||
case Bureau.RUNNING: found.state == AgentState.RUNNING ||
case Bureau.DESTROYED: found.state == AgentState.DESTROYED) {
log.warning("Received acknowledgement of creation failure for " + log.warning(
"agent in state " + found.state + ", ignoring request " + "Ignoring failure of creation of an agent in an unexpected state", "state",
StringUtil.toString(found.agent)); found.state, "agent", found.agent);
break;
} }
found.bureau.summarize(); found.bureau.summarize();
@@ -463,21 +442,18 @@ public class BureauRegistry
return; return;
} }
log.info("Agent destruction confirmed " + StringUtil.toString(found.agent)); log.info("Agent destruction confirmed", "agent", found.agent);
switch (found.state) { if (found.state == AgentState.DESTROYED) {
case Bureau.DESTROYED:
found.bureau.agentStates.remove(found.agent); found.bureau.agentStates.remove(found.agent);
break;
case Bureau.PENDING: } else if (found.state == AgentState.PENDING ||
case Bureau.STARTED: found.state == AgentState.STARTED ||
case Bureau.RUNNING: found.state == AgentState.RUNNING ||
case Bureau.STILL_BORN: found.state == AgentState.STILL_BORN) {
log.warning("Acknowledging agent destruction, but state is " + log.warning(
found.state + ", ignoring request " + "Ignoring confirmation of destruction of agent in unexpected state", "state",
StringUtil.toString(found.agent)); found.state, "agent", found.agent);
break;
} }
found.bureau.summarize(); found.bureau.summarize();
@@ -488,8 +464,7 @@ public class BureauRegistry
*/ */
protected void clientDestroyed (Bureau bureau) protected void clientDestroyed (Bureau bureau)
{ {
log.info("Client destroyed, destroying all agents " + log.info("Client destroyed, destroying all agents", "bureau", bureau);
StringUtil.toString(bureau));
// clean up any agents attached to this bureau // clean up any agents attached to this bureau
for (AgentObject agent : bureau.agentStates.keySet()) { for (AgentObject agent : bureau.agentStates.keySet()) {
@@ -509,36 +484,31 @@ public class BureauRegistry
{ {
com.threerings.presents.dobj.DObject dobj = _omgr.getObject(agentId); com.threerings.presents.dobj.DObject dobj = _omgr.getObject(agentId);
if (dobj == null) { if (dobj == null) {
log.warning("Non-existent agent in " + resolver + log.warning("Non-existent agent", "function", resolver, "agentId", agentId);
" [agentId=" + agentId + "]");
return null; return null;
} }
if (!(dobj instanceof AgentObject)) { if (!(dobj instanceof AgentObject)) {
log.warning("Object not an agent in " + resolver + log.warning("Object not an agent", "function", resolver, "obj", dobj);
" " + StringUtil.toString(dobj));
return null; return null;
} }
AgentObject agent = (AgentObject)dobj; AgentObject agent = (AgentObject)dobj;
Bureau bureau = _bureaus.get(agent.bureauId); Bureau bureau = _bureaus.get(agent.bureauId);
if (bureau == null) { if (bureau == null) {
log.warning("Bureau not found for agent in " + resolver + log.warning("Bureau not found for agent", "function", resolver, "agent", agent);
" " + StringUtil.toString(agent));
return null; return null;
} }
if (!bureau.agentStates.containsKey(agent)) { if (!bureau.agentStates.containsKey(agent)) {
log.warning("Bureau does not have agent in " + resolver + log.warning("Bureau does not have agent", "function", resolver, "agent", agent);
" " + StringUtil.toString(agent));
return null; return null;
} }
if (client != null && bureau.clientObj != client) { if (client != null && bureau.clientObj != client) {
log.warning("Masquerading request in " + resolver + log.warning(
" " + StringUtil.toString(agent) + "Masquerading request", "function", resolver, "agent", agent, "client",
" " + StringUtil.toString(bureau.clientObj) + bureau.clientObj, "client", client);
" " + StringUtil.toString(client));
return null; return null;
} }
@@ -592,13 +562,28 @@ public class BureauRegistry
protected Process _result; protected Process _result;
} }
protected enum AgentState
{
// Not yet stated, waiting for bureau to ack
PENDING,
// Bureau acked, agent told to start
STARTED,
// Agent ack'ed, now live and hosting, ready to tell other clients
RUNNING,
// Agent destruction requested, waiting for acknowledge (after which the agent is removed
// from the Bureau, so has no state)
DESTROYED,
// Edge case: destroy request prior to RUNNING
STILL_BORN
}
/** Models the results of searching for an agent. */ /** Models the results of searching for an agent. */
protected static class FoundAgent protected static class FoundAgent
{ {
FoundAgent ( FoundAgent (
Bureau bureau, Bureau bureau,
AgentObject agent, AgentObject agent,
int state) AgentState state)
{ {
this.bureau = bureau; this.bureau = bureau;
this.agent = agent; this.agent = agent;
@@ -612,32 +597,12 @@ public class BureauRegistry
AgentObject agent; AgentObject agent;
// The state of the agent // The state of the agent
int state; AgentState state;
} }
/** Models a bureau, including the process handle, all running agents and their states. */ /** Models a bureau, including the process handle, all running agents and their states. */
protected static class Bureau protected static class Bureau
{ {
// Agent states {
// Not yet stated, waiting for bureau to ack
static final int PENDING = 0;
// Bureau acked, agent told to start
static final int STARTED = 1;
// Agent ack'ed, now live and hosting, ready to tell other clients
static final int RUNNING = 2;
// Agent destruction requested, waiting for acknowledge (after which the agent is removed
// from the Bureau, so has no state)
static final int DESTROYED = 3;
// Edge case: destroy request prior to RUNNING
static final int STILL_BORN = 4;
// }
// non-null once the bureau is scheduled but not yet kicked off // non-null once the bureau is scheduled but not yet kicked off
Launcher launcher; Launcher launcher;
@@ -659,7 +624,7 @@ public class BureauRegistry
PresentsClient client; PresentsClient client;
// The states of the various agents allocated to this bureau // The states of the various agents allocated to this bureau
Map<AgentObject, Integer> agentStates = Maps.newHashMap(); Map<AgentObject, AgentState> agentStates = Maps.newHashMap();
@Override @Override
public String toString () public String toString ()
@@ -685,16 +650,16 @@ public class BureauRegistry
StringBuilder agentSummary (StringBuilder str) StringBuilder agentSummary (StringBuilder str)
{ {
int[] counts = {0, 0, 0, 0, 0}; int[] counts = new int[AgentState.values().length];
for (Map.Entry<AgentObject, Integer> me : agentStates.entrySet()) { for (Map.Entry<AgentObject, AgentState> me : agentStates.entrySet()) {
counts[me.getValue()]++; counts[me.getValue().ordinal()]++;
}
for (AgentState state : AgentState.values()) {
if (state.ordinal() > 0) {
str.append(", ");
}
str.append(counts[state.ordinal()]).append(" ").append(state.name());
} }
str.append(counts[PENDING]).append(" pending, ").
append(counts[STARTED]).append(" started, ").
append(counts[RUNNING]).append(" running, ").
append(counts[DESTROYED]).append(" destroyed, ").
append(counts[STILL_BORN]).append(" still born");
return str; return str;
} }