RunQueue related updatery.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5523 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2008-11-08 00:07:35 +00:00
parent a5a8272900
commit 4b9477ca91
4 changed files with 22 additions and 128 deletions
@@ -21,9 +21,8 @@
package com.threerings.bureau.client; package com.threerings.bureau.client;
import com.samskivert.util.BasicRunQueue;
import com.samskivert.util.OneLineLogFormatter; import com.samskivert.util.OneLineLogFormatter;
import com.samskivert.util.Queue;
import com.samskivert.util.RunQueue;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
import com.threerings.bureau.data.AgentObject; import com.threerings.bureau.data.AgentObject;
@@ -36,7 +35,7 @@ import static com.threerings.bureau.Log.log;
*/ */
public class TestClient extends BureauClient public class TestClient extends BureauClient
{ {
public static void main (String args[]) public static void main (String[] args)
throws java.net.MalformedURLException throws java.net.MalformedURLException
{ {
// make log pretty // make log pretty
@@ -52,53 +51,20 @@ public class TestClient extends BureauClient
client.logon(); client.logon();
// run it // run it
client.run(); ((BasicRunQueue)client.getRunQueue()).run();
}
/**
* Implements most basic run queue. Required to instantate a client.
*/
static protected class SimpleRunQueue implements RunQueue
{
public void postRunnable (Runnable r)
{
_queue.append(r);
}
public boolean isDispatchThread ()
{
return _main == Thread.currentThread();
}
public void run ()
{
_main = Thread.currentThread();
while (true) {
Runnable r = _queue.get();
r.run();
}
}
protected Thread _main;
protected Queue<Runnable> _queue = new Queue<Runnable>();
} }
/** /**
* The agent class used by our director. Does not actually load any code, just logs the * The agent class used by our director. Does not actually load any code, just logs the
* start/stop requests. * start/stop requests.
*/ */
static protected class TestAgent extends Agent protected static class TestAgent extends Agent
{ {
@Override @Override public void start () {
public void start ()
{
log.info("Starting agent " + StringUtil.toString(_agentObj)); log.info("Starting agent " + StringUtil.toString(_agentObj));
} }
@Override @Override public void stop () {
public void stop ()
{
log.info("Stopping agent " + StringUtil.toString(_agentObj)); log.info("Stopping agent " + StringUtil.toString(_agentObj));
} }
} }
@@ -108,15 +74,7 @@ public class TestClient extends BureauClient
*/ */
protected TestClient (String token, String bureauId) protected TestClient (String token, String bureauId)
{ {
super(token, bureauId, new SimpleRunQueue()); super(token, bureauId, new BasicRunQueue());
}
/**
* Runs the event loop.
*/
protected void run ()
{
((SimpleRunQueue)_runQueue).run();
} }
// overridden - creates a simple director // overridden - creates a simple director
@@ -126,12 +84,9 @@ public class TestClient extends BureauClient
// just use our test agent exclusively - in the real world, the agent created would depend // just use our test agent exclusively - in the real world, the agent created would depend
// on the object's type and/or properties // on the object's type and/or properties
return new BureauDirector(_ctx) { return new BureauDirector(_ctx) {
@Override @Override public Agent createAgent (AgentObject agentObj) {
public Agent createAgent (AgentObject agentObj) {
return new TestAgent(); return new TestAgent();
} }
}; };
} }
} }
@@ -24,7 +24,6 @@ package com.threerings.crowd.client;
import java.io.IOException; import java.io.IOException;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
@@ -50,7 +49,7 @@ import com.threerings.crowd.util.CrowdContext;
* extended context implementation. * extended context implementation.
*/ */
public class JabberClient public class JabberClient
implements RunQueue, SessionObserver implements SessionObserver
{ {
/** /**
* Initializes a new client and provides it with a frame in which to * Initializes a new client and provides it with a frame in which to
@@ -74,8 +73,7 @@ public class JabberClient
// log off when they close the window // log off when they close the window
_frame.addWindowListener(new WindowAdapter() { _frame.addWindowListener(new WindowAdapter() {
@Override @Override public void windowClosing (WindowEvent evt) {
public void windowClosing (WindowEvent evt) {
// if we're logged on, log off // if we're logged on, log off
if (_client.isLoggedOn()) { if (_client.isLoggedOn()) {
_client.logoff(true); _client.logoff(true);
@@ -153,7 +151,7 @@ public class JabberClient
throws IOException throws IOException
{ {
// create the handles on our various services // create the handles on our various services
_client = new Client(null, this); _client = new Client(null, RunQueue.AWT);
// create our managers and directors // create our managers and directors
_locdir = new LocationDirector(_ctx); _locdir = new LocationDirector(_ctx);
@@ -162,19 +160,6 @@ public class JabberClient
_chatdir = new ChatDirector(_ctx, _msgmgr, null); _chatdir = new ChatDirector(_ctx, _msgmgr, null);
} }
// documentation inherited from interface RunQueue
public void postRunnable (Runnable run)
{
// queue it on up on the awt thread
EventQueue.invokeLater(run);
}
// documentation inherited from interface RunQueue
public boolean isDispatchThread ()
{
return EventQueue.isDispatchThread();
}
/** /**
* The context implementation. This provides access to all of the * The context implementation. This provides access to all of the
* objects and services that are needed by the operating client. * objects and services that are needed by the operating client.
@@ -21,9 +21,8 @@
package com.threerings.crowd.client; package com.threerings.crowd.client;
import com.samskivert.util.BasicRunQueue;
import com.samskivert.util.Config; import com.samskivert.util.Config;
import com.samskivert.util.Queue;
import com.samskivert.util.RunQueue;
import com.threerings.util.MessageManager; import com.threerings.util.MessageManager;
import com.threerings.util.Name; import com.threerings.util.Name;
@@ -39,7 +38,7 @@ import com.threerings.crowd.util.CrowdContext;
import static com.threerings.crowd.Log.log; import static com.threerings.crowd.Log.log;
public class TestClient public class TestClient
implements RunQueue, ClientObserver implements ClientObserver
{ {
public TestClient (String username) public TestClient (String username)
{ {
@@ -47,8 +46,7 @@ public class TestClient
_ctx = createContext(); _ctx = createContext();
// create the handles on our various services // create the handles on our various services
_client = new Client( _client = new Client(new UsernamePasswordCreds(new Name(username), "test"), _rqueue);
new UsernamePasswordCreds(new Name(username), "test"), this);
_locdir = new LocationDirector(_ctx); _locdir = new LocationDirector(_ctx);
_occdir = new OccupantDirector(_ctx); _occdir = new OccupantDirector(_ctx);
_chatdir = new ChatDirector(_ctx, new MessageManager("rsrc"), "global"); _chatdir = new ChatDirector(_ctx, new MessageManager("rsrc"), "global");
@@ -62,27 +60,11 @@ public class TestClient
public void run () public void run ()
{ {
_main = Thread.currentThread();
// log on // log on
_client.logon(); _client.logon();
// loop over our queue, running the runnables // loop over our queue, running the runnables
while (true) { _rqueue.run();
Runnable run = _queue.get();
run.run();
}
}
public void postRunnable (Runnable run)
{
// queue it on up
_queue.append(run);
}
public boolean isDispatchThread ()
{
return _main == Thread.currentThread();
} }
public void clientWillLogon (Client client) public void clientWillLogon (Client client)
@@ -193,6 +175,5 @@ public class TestClient
protected OccupantDirector _occdir; protected OccupantDirector _occdir;
protected ChatDirector _chatdir; protected ChatDirector _chatdir;
protected Thread _main; protected BasicRunQueue _rqueue = new BasicRunQueue();
protected Queue<Runnable> _queue = new Queue<Runnable>();
} }
@@ -23,9 +23,7 @@ package com.threerings.presents.client;
import java.util.ArrayList; import java.util.ArrayList;
import com.samskivert.util.Queue; import com.samskivert.util.BasicRunQueue;
import com.samskivert.util.RunQueue;
import com.threerings.util.Name; import com.threerings.util.Name;
import com.threerings.presents.data.TestObject; import com.threerings.presents.data.TestObject;
@@ -42,7 +40,7 @@ import static com.threerings.presents.Log.log;
* A standalone test client. * A standalone test client.
*/ */
public class TestClient public class TestClient
implements RunQueue, SessionObserver, Subscriber<TestObject>, EventListener, implements SessionObserver, Subscriber<TestObject>, EventListener,
TestService.TestOidListener, TestReceiver TestService.TestOidListener, TestReceiver
{ {
public void setClient (Client client) public void setClient (Client client)
@@ -50,30 +48,6 @@ public class TestClient
_client = client; _client = client;
} }
// from interface RunQueue
public void postRunnable (Runnable run)
{
// queue it on up
_queue.append(run);
}
// from interface RunQueue
public boolean isDispatchThread ()
{
return _main == Thread.currentThread();
}
public void run ()
{
_main = Thread.currentThread();
// loop over our queue, running the runnables
while (true) {
Runnable run = _queue.get();
run.run();
}
}
// from interface SessionObserver // from interface SessionObserver
public void clientWillLogon (Client client) public void clientWillLogon (Client client)
{ {
@@ -203,16 +177,15 @@ public class TestClient
TestClient tclient = new TestClient(); TestClient tclient = new TestClient();
UsernamePasswordCreds creds = UsernamePasswordCreds creds =
new UsernamePasswordCreds(new Name("test"), "test"); new UsernamePasswordCreds(new Name("test"), "test");
Client client = new Client(creds, tclient); BasicRunQueue rqueue = new BasicRunQueue();
Client client = new Client(creds, rqueue);
tclient.setClient(client); tclient.setClient(client);
client.addClientObserver(tclient); client.addClientObserver(tclient);
client.setServer("localhost", Client.DEFAULT_SERVER_PORTS); client.setServer("localhost", Client.DEFAULT_SERVER_PORTS);
client.logon(); client.logon();
// start up our event processing loop // start up our event processing loop
tclient.run(); rqueue.run();
} }
protected Thread _main;
protected Queue<Runnable> _queue = new Queue<Runnable>();
protected Client _client; protected Client _client;
} }