More juicy goodness. Event dispatch seems to work now.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@20 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-06-01 20:35:39 +00:00
parent 45c2dc143f
commit 1929898dd8
5 changed files with 75 additions and 38 deletions
@@ -1,5 +1,5 @@
//
// $Id: AttributeChangedEvent.java,v 1.2 2001/06/01 19:56:13 mdb Exp $
// $Id: AttributeChangedEvent.java,v 1.3 2001/06/01 20:35:39 mdb Exp $
package com.threerings.cocktail.cher.dobj;
@@ -101,18 +101,17 @@ public class AttributeChangedEvent extends DEvent
public boolean applyToObject (DObject target)
throws ObjectAccessException
{
// look up the setter for this object
Method setter = DEventUtil.getSetter(target.getClass(), _name);
try {
setter.invoke(target, new Object[] { _value });
} catch (Exception e) {
throw new ObjectAccessException("Reflection error: " + e);
}
// pass the new value on to the object
target.setAttribute(_name, _value);
return true;
}
public String toString ()
{
return "[CHANGE:targetOid=" + _toid + ", name=" + _name +
", value=" + _value + "]";
}
protected String _name;
protected Object _value;
}
@@ -1,5 +1,5 @@
//
// $Id: DEventUtil.java,v 1.1 2001/06/01 19:56:13 mdb Exp $
// $Id: DEventUtil.java,v 1.2 2001/06/01 20:35:39 mdb Exp $
package com.threerings.cocktail.cher.dobj;
@@ -56,7 +56,7 @@ public class DEventUtil
protected static String setterName (String name)
{
StringBuffer sname = new StringBuffer();
sname.append("get");
sname.append("set");
sname.append(Character.toUpperCase(name.charAt(0)));
sname.append(name.substring(1));
return sname.toString();
@@ -1,8 +1,9 @@
//
// $Id: DObject.java,v 1.7 2001/06/01 19:56:13 mdb Exp $
// $Id: DObject.java,v 1.8 2001/06/01 20:35:39 mdb Exp $
package com.threerings.cocktail.cher.dobj;
import java.lang.reflect.Field;
import java.util.ArrayList;
/**
@@ -180,6 +181,26 @@ public class DObject
}
}
/**
* Sets the named attribute to the specified value. This is only used
* by the internals of the event dispatch mechanism and should not be
* called directly by users. Use the generated attribute setter
* methods instead.
*/
public void setAttribute (String name, Object value)
throws ObjectAccessException
{
try {
Field field = getClass().getField(name);
field.set(this, value);
} catch (Exception e) {
String errmsg = "Attribute setting failure [name=" + name +
", value=" + value + ", error=" + e + "].";
throw new ObjectAccessException(errmsg);
}
}
protected int _oid;
protected DObjectManager _mgr;
protected ArrayList _subscribers = new ArrayList();
@@ -1,5 +1,5 @@
//
// $Id: PresentsDObjectMgr.java,v 1.1 2001/06/01 19:56:13 mdb Exp $
// $Id: PresentsDObjectMgr.java,v 1.2 2001/06/01 20:35:39 mdb Exp $
package com.threerings.cocktail.cher.server;
@@ -24,6 +24,18 @@ import com.threerings.cocktail.cher.util.IntMap;
*/
public class CherDObjectMgr implements DObjectManager
{
/**
* Creates the dobjmgr and prepares it for operation.
*/
public CherDObjectMgr ()
{
// we create a dummy object to live as oid zero and we'll use that
// for some internal event trickery
DObject dummy = new DObject();
dummy.init(0, this);
_objects.put(0, new DObject());
}
// inherit documentation from the interface
public void createObject (Class dclass, Subscriber target,
boolean subscribe)
@@ -53,18 +65,6 @@ public class CherDObjectMgr implements DObjectManager
_evqueue.append(event);
}
/**
* Initializes the dobjmgr and prepares it for operation.
*/
public void init ()
{
// we create a dummy object to live as oid zero and we'll use that
// for some internal event trickery
DObject dummy = new DObject();
dummy.init(0, this);
_objects.put(0, new DObject());
}
/**
* Runs the dobjmgr event loop until it is requested to exit. This
* should be called from the main application thread.
@@ -104,6 +104,7 @@ public class CherDObjectMgr implements DObjectManager
} catch (Exception e) {
Log.warning("Failure applying event [event=" + event +
", target=" + target + ", error=" + e + "].");
Log.logStackTrace(e);
continue;
}
@@ -128,9 +129,10 @@ public class CherDObjectMgr implements DObjectManager
public void shutdown ()
{
_running = false;
// stick a bogus object on the event queue to ensure that the mgr
// wakes up and smells the coffee
_evqueue.append(this);
_evqueue.append(new ShutdownEvent());
}
protected synchronized boolean isRunning ()
@@ -269,4 +271,19 @@ public class CherDObjectMgr implements DObjectManager
protected Subscriber _target;
protected boolean _subscribe;
}
protected class ShutdownEvent extends DEvent
{
public ShutdownEvent ()
{
super(0); // target the bogus object
}
public boolean applyToObject (DObject target)
throws ObjectAccessException
{
// nothing doing!
return false;
}
}
}
@@ -1,9 +1,10 @@
//
// $Id: PresentsServer.java,v 1.2 2001/05/30 23:58:31 mdb Exp $
// $Id: PresentsServer.java,v 1.3 2001/06/01 20:35:39 mdb Exp $
package com.threerings.cocktail.cher.server;
import com.threerings.cocktail.cher.Log;
import com.threerings.cocktail.cher.dobj.DObjectManager;
import com.threerings.cocktail.cher.server.net.AuthManager;
import com.threerings.cocktail.cher.server.net.ConnectionManager;
@@ -19,6 +20,9 @@ public class CherServer
/** The manager of network connections. */
public static ConnectionManager cmgr;
/** The distributed object manager. */
public static DObjectManager omgr;
/**
* Initializes all of the server services and prepares for operation.
*/
@@ -29,6 +33,8 @@ public class CherServer
authmgr = new AuthManager(new DummyAuthenticator());
// create our connection manager
cmgr = new ConnectionManager(authmgr);
// create our distributed object manager
omgr = new CherDObjectMgr();
} catch (Exception e) {
Log.warning("Unable to initialize server.");
@@ -40,25 +46,19 @@ public class CherServer
* Starts up all of the server services and enters the main server
* event loop.
*/
public static void start ()
public static void run ()
{
// start up the auth manager
authmgr.start();
// start up the connection manager
cmgr.start();
// for now, just block because we've nothing to do
try {
synchronized (CherServer.class) {
CherServer.class.wait();
}
} catch (InterruptedException ie) {
}
// invoke the dobjmgr event loop
((CherDObjectMgr)omgr).run();
}
public static void main (String[] args)
{
init();
start();
run();
}
}