Bureaucracy

* Fixed signature of Receiver methods
* Implemented create/destroy mechanism and bootstrap request in BureauDirector
* Added BureauContext class to expose bureau information on top of usual 
  presents stuff
* Added BureauContext implementation in BureauClient
* Added code to kick off the director when the client logs on
* Added credentials for a bureau logon, which is just a token passed to 
  command line launcher
* Added concept of bureau types to distinguish between executable and command 
  line arguments; includes interface BureauRegistry.Launcher
* Implemented backend for the registry


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5027 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Jamie Doornbos
2008-04-28 18:34:57 +00:00
parent b10cc3643d
commit 74dac11f97
11 changed files with 724 additions and 73 deletions
@@ -21,9 +21,15 @@
package com.threerings.bureau.client;
import com.threerings.bureau.data.AgentObject;
/**
* Represents an agent running within a bureau client.
*/
public class Agent
{
/**
* The shared agent object.
*/
public AgentObject agentObject;
}
@@ -1,8 +1,33 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.bureau.client;
import com.threerings.presents.client.Client;
import com.threerings.presents.net.Credentials;
import com.samskivert.util.RunQueue;
import com.threerings.bureau.data.BureauCredentials;
import com.threerings.bureau.util.BureauContext;
import com.threerings.presents.dobj.DObjectManager;
import com.samskivert.util.Config;
/**
* Represents a client embedded in a bureau.
@@ -14,8 +39,43 @@ public class BureauClient extends Client
* @param creds the credentials supplied during connection
* @param runQueue the place to post tasks required by clients
*/
public BureauClient (Credentials creds, RunQueue runQueue)
public BureauClient (String token, String bureauId, RunQueue runQueue)
{
super(creds, runQueue);
super(null, runQueue);
_bureauId = bureauId;
BureauCredentials creds = new BureauCredentials();
creds.sessionToken = token;
_creds = creds;
_ctx = createContext();
_director = new BureauDirector(_ctx);
}
protected BureauContext createContext ()
{
return new BureauContext () {
public BureauDirector getBureauDirector () {
return _director;
}
public DObjectManager getDObjectManager () {
return _omgr;
}
public Client getClient () {
return BureauClient.this;
}
public Config getConfig () {
return _config;
}
public String getBureauId () {
return _bureauId;
}
};
}
protected BureauContext _ctx;
protected String _bureauId;
protected BureauDirector _director;
protected Config _config = new Config("bureau");
}
@@ -23,7 +23,6 @@ package com.threerings.bureau.client;
import com.threerings.bureau.client.BureauReceiver;
import com.threerings.presents.client.InvocationDecoder;
import com.threerings.presents.data.ClientObject;
/**
* Dispatches calls to a {@link BureauReceiver} instance.
@@ -62,13 +61,13 @@ public class BureauDecoder extends InvocationDecoder
switch (methodId) {
case CREATE_AGENT:
((BureauReceiver)receiver).createAgent(
(ClientObject)args[0], ((Integer)args[1]).intValue()
((Integer)args[0]).intValue()
);
return;
case DESTROY_AGENT:
((BureauReceiver)receiver).destroyAgent(
(ClientObject)args[0], ((Integer)args[1]).intValue()
((Integer)args[0]).intValue()
);
return;
@@ -2,32 +2,127 @@ package com.threerings.bureau.client;
import com.threerings.presents.client.BasicDirector;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.util.PresentsContext;
import com.threerings.bureau.data.BureauCodes;
import com.samskivert.util.HashIntMap;
import com.threerings.bureau.data.AgentObject;
import com.threerings.bureau.Log;
import com.threerings.bureau.util.BureauContext;
import com.threerings.presents.client.Client;
import com.threerings.presents.dobj.Subscriber;
import com.threerings.presents.dobj.ObjectAccessException;
/**
* Allows the server to create and destroy agents on a client.
* @see BureauRegistry
* @see BureauRegistry
*/
public class BureauDirector extends BasicDirector
implements BureauReceiver
implements BureauReceiver, Subscriber<AgentObject>
{
protected BureauDirector (PresentsContext ctx)
public BureauDirector (BureauContext ctx)
{
super(ctx);
_ctx = ctx;
}
// from BureauReceiver
public synchronized void createAgent (int agentId)
{
_ctx.getDObjectManager().subscribeToObject(agentId, this);
}
// from BureauReceiver
public synchronized void destroyAgent (int agentId)
{
Agent agent = null;
try {
agent = _agents.remove(agentId);
// TODO: stop the agent somehow
}
catch (Throwable t) {
Log.warning("Could not create agent [id=" + agentId + "]");
Log.logStackTrace(t);
// TODO: failure notification?
return;
}
_ctx.getDObjectManager().unsubscribeFromObject(agentId, this);
doStopAgent(agent);
_bureauService.agentDestroyed(_ctx.getClient(), agentId);
}
// from Subscriber
public synchronized void objectAvailable (AgentObject agentObject)
{
int oid = agentObject.getOid();
try {
Agent agent = new Agent();
agent.agentObject = agentObject;
doStartAgent(agent);
_agents.put(oid, agent);
}
catch (Exception e) {
Log.warning("Could not create agent [obj=" + agentObject + "]");
Log.logStackTrace(e);
// TODO: failure notification?
return;
}
// TODO: post to runqueue?
_bureauService.agentCreated(_ctx.getClient(), oid);
}
// from Subscriber
public synchronized void requestFailed (int oid, ObjectAccessException cause)
{
Log.warning("Could not subscribe to agent [oid=" + oid + "]");
Log.logStackTrace(cause);
}
@Override // from BasicDirector
public void clientDidLogon (Client client)
{
super.clientDidLogon(client);
_bureauService.bureauInitialized(_ctx.getClient(), _ctx.getBureauId());
}
@Override // from BasicDirector
protected void registerServices (Client client)
{
super.registerServices(client);
// Require the bureau services
client.addServiceGroup(BureauCodes.BUREAU_GROUP);
// Set up our decoder so we can receive method calls
// from the server
_ctx.getClient().getInvocationDirector().
client.getInvocationDirector().
registerReceiver(new BureauDecoder(this));
}
// from BureauReceiver
public void createAgent (ClientObject client, int agentId)
@Override // from BasicDirector
protected void fetchServices (Client client)
{
super.fetchServices(client);
_bureauService = client.getService(BureauService.class);
}
/**
* Called when the agent object is ready and it is time to run his code.
*/
protected void doStartAgent (Agent agent)
{
}
// from BureauReceiver
public void destroyAgent (ClientObject client, int agentId)
/**
* Called when the agent object is being destroyed and the client code should stop
* processing.
*/
protected void doStopAgent (Agent agent)
{
}
protected BureauContext _ctx;
protected BureauService _bureauService;
protected HashIntMap<Agent> _agents = new HashIntMap<Agent>();
}
@@ -36,7 +36,7 @@ public interface BureauReceiver extends InvocationReceiver
* @param client the client receiving the request
* @param agentId the id of the <code>AgentObject</code> that needs an <code>Agent</code>
*/
void createAgent (ClientObject client, int agentId);
void createAgent (int agentId);
/**
* Destroys a previously created agent. Implementors should destroy the agent that was created
@@ -46,5 +46,5 @@ public interface BureauReceiver extends InvocationReceiver
* @param agentId the id of the <code>AgentObject</code> whose <code>Agent</code>
* should be destroyed
*/
void destroyAgent (ClientObject client, int agentId);
void destroyAgent (int agentId);
}