We love to revamp! Created a set of listener interfaces which are used

with distributed objects rather than having a single handleEvent() by
which all subscribers are forced to hear about all events. Now one
subscribes separately and then adds onesself as any of a few types of
listener once they have access to the subscribed object reference.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@439 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-10-12 00:03:03 +00:00
parent de8d9dabd1
commit 804505890f
34 changed files with 534 additions and 323 deletions
@@ -1,5 +1,5 @@
// //
// $Id: ChatDirector.java,v 1.10 2001/10/11 04:07:51 mdb Exp $ // $Id: ChatDirector.java,v 1.11 2001/10/12 00:03:02 mdb Exp $
package com.threerings.crowd.chat; package com.threerings.crowd.chat;
@@ -19,7 +19,8 @@ import com.threerings.crowd.util.CrowdContext;
* messaging. * messaging.
*/ */
public class ChatDirector public class ChatDirector
implements LocationObserver, Subscriber, InvocationReceiver, ChatCodes implements LocationObserver, MessageListener, InvocationReceiver,
ChatCodes
{ {
/** /**
* Creates a chat director and initializes it with the supplied * Creates a chat director and initializes it with the supplied
@@ -110,54 +111,39 @@ public class ChatDirector
return ChatService.tell(_ctx.getClient(), target, message, this); return ChatService.tell(_ctx.getClient(), target, message, this);
} }
// documentation inherited
public boolean locationMayChange (int placeId) public boolean locationMayChange (int placeId)
{ {
// we accept all location change requests // we accept all location change requests
return true; return true;
} }
// documentation inherited
public void locationDidChange (PlaceObject place) public void locationDidChange (PlaceObject place)
{ {
if (_place != null) { if (_place != null) {
// unsubscribe from our old object // unlisten to our old object
_place.removeSubscriber(this); _place.removeListener(this);
} }
// subscribe to the new object // listen to the new object
_place = place; _place = place;
_place.addSubscriber(this); _place.addListener(this);
} }
// documentation inherited
public void locationChangeFailed (int placeId, String reason) public void locationChangeFailed (int placeId, String reason)
{ {
// nothing we care about // nothing we care about
} }
public void objectAvailable (DObject object) // documentation inherited
public void messageReceived (MessageEvent event)
{ {
// nothing to do here String name = event.getName();
}
public void requestFailed (int oid, ObjectAccessException cause)
{
// nothing to do here
}
public boolean handleEvent (DEvent event, DObject target)
{
// we only care about message events
if (!(event instanceof MessageEvent)) {
return true;
}
// and only those of proper name
MessageEvent mevt = (MessageEvent)event;
String name = mevt.getName();
if (name.equals(ChatService.SPEAK_NOTIFICATION)) { if (name.equals(ChatService.SPEAK_NOTIFICATION)) {
handleSpeakMessage(mevt.getArgs()); handleSpeakMessage(event.getArgs());
} }
return true;
} }
/** /**
@@ -216,6 +202,5 @@ public class ChatDirector
protected CrowdContext _ctx; protected CrowdContext _ctx;
protected PlaceObject _place; protected PlaceObject _place;
protected ArrayList _displays = new ArrayList(); protected ArrayList _displays = new ArrayList();
} }
@@ -1,5 +1,5 @@
// //
// $Id: LocationDirector.java,v 1.11 2001/10/11 04:07:51 mdb Exp $ // $Id: LocationDirector.java,v 1.12 2001/10/12 00:03:02 mdb Exp $
package com.threerings.crowd.client; package com.threerings.crowd.client;
@@ -201,11 +201,6 @@ public class LocationDirector
"object; all has gone horribly wrong" + "object; all has gone horribly wrong" +
"[cause=" + cause + "]."); "[cause=" + cause + "].");
} }
public boolean handleEvent (DEvent event, DObject target)
{
return false;
}
}; };
int cloid = client.getClientOid(); int cloid = client.getClientOid();
client.getDObjectManager().subscribeToObject(cloid, sub); client.getDObjectManager().subscribeToObject(cloid, sub);
@@ -326,12 +321,6 @@ public class LocationDirector
} }
} }
public boolean handleEvent (DEvent event, DObject target)
{
// nothing to do here, but remain subscribed
return true;
}
protected void notifyFailure (int placeId, String reason) protected void notifyFailure (int placeId, String reason)
{ {
for (int i = 0; i < _observers.size(); i++) { for (int i = 0; i < _observers.size(); i++) {
@@ -1,5 +1,5 @@
// //
// $Id: OccupantManager.java,v 1.6 2001/10/11 04:07:51 mdb Exp $ // $Id: OccupantManager.java,v 1.7 2001/10/12 00:03:02 mdb Exp $
package com.threerings.crowd.client; package com.threerings.crowd.client;
@@ -36,7 +36,7 @@ import com.threerings.crowd.util.CrowdContext;
* what's in the cache. * what's in the cache.
*/ */
public class OccupantManager public class OccupantManager
implements LocationObserver, Subscriber implements LocationObserver, OidListListener
{ {
/** /**
* Constructs a new occupant manager with the supplied context. * Constructs a new occupant manager with the supplied context.
@@ -73,16 +73,16 @@ public class OccupantManager
// inherit documentation // inherit documentation
public void locationDidChange (PlaceObject place) public void locationDidChange (PlaceObject place)
{ {
// unsubscribe from the old place object if there was one // unlisten to the old place object if there was one
if (_place != null) { if (_place != null) {
_place.removeSubscriber(this); _place.removeListener(this);
// clear out the occupant cache for the previous location // clear out the occupant cache for the previous location
_ocache.clear(); _ocache.clear();
} }
// subscribe to the new one // listen to the new one
_place = place; _place = place;
_place.addSubscriber(this); _place.addListener(this);
// cache the occupant info for the occupants in this room // cache the occupant info for the occupants in this room
Iterator iter = _place.occupantInfo.elements(); Iterator iter = _place.occupantInfo.elements();
@@ -98,43 +98,17 @@ public class OccupantManager
// nothing to do here either // nothing to do here either
} }
// inherit documentation
public void objectAvailable (DObject object)
{
// nothing doing
}
// inherit documentation
public void requestFailed (int oid, ObjectAccessException cause)
{
// nothing doing
}
// inherit documentation
public boolean handleEvent (DEvent event, DObject target)
{
// we care about occupant added and removed events only
if (event instanceof ObjectAddedEvent) {
ObjectAddedEvent oe = (ObjectAddedEvent)event;
if (oe.getName().equals(PlaceObject.OCCUPANTS)) {
handleOccupantAdded(oe.getOid());
}
} else if (event instanceof ObjectRemovedEvent) {
ObjectRemovedEvent oe = (ObjectRemovedEvent)event;
if (oe.getName().equals(PlaceObject.OCCUPANTS)) {
handleOccupantRemoved(oe.getOid());
}
}
return true;
}
/** /**
* Deals with all of the processing when an occupant shows up. * Deals with all of the processing when an occupant shows up.
*/ */
protected void handleOccupantAdded (int bodyOid) public void objectAdded (ObjectAddedEvent event)
{ {
// bail if this isn't for the OCCUPANTS field
if (!event.getName().equals(PlaceObject.OCCUPANTS)) {
return;
}
int bodyOid = event.getOid();
Object key = new Integer(bodyOid); Object key = new Integer(bodyOid);
// get the occupant info from the place object // get the occupant info from the place object
@@ -158,8 +132,14 @@ public class OccupantManager
/** /**
* Deals with all of the processing when an occupant leaves. * Deals with all of the processing when an occupant leaves.
*/ */
protected void handleOccupantRemoved (int bodyOid) public void objectRemoved (ObjectRemovedEvent event)
{ {
// bail if this isn't for the OCCUPANTS field
if (!event.getName().equals(PlaceObject.OCCUPANTS)) {
return;
}
int bodyOid = event.getOid();
// see if we have an occupant object for this body // see if we have an occupant object for this body
OccupantInfo info = (OccupantInfo)_ocache.get(bodyOid); OccupantInfo info = (OccupantInfo)_ocache.get(bodyOid);
if (info == null) { if (info == null) {
@@ -1,5 +1,5 @@
// //
// $Id: PlaceManager.java,v 1.17 2001/10/11 04:07:51 mdb Exp $ // $Id: PlaceManager.java,v 1.18 2001/10/12 00:03:02 mdb Exp $
package com.threerings.crowd.server; package com.threerings.crowd.server;
@@ -28,9 +28,10 @@ import com.threerings.crowd.data.*;
* <p> A derived class is expected to handle initialization, cleanup and * <p> A derived class is expected to handle initialization, cleanup and
* operational functionality via the calldown functions {@link * operational functionality via the calldown functions {@link
* #didStartup}, {@link #willShutdown}, and {@link #didShutdown} as well * #didStartup}, {@link #willShutdown}, and {@link #didShutdown} as well
* as through additions to {@link #handleEvent}. * as through event listeners.
*/ */
public class PlaceManager implements Subscriber public class PlaceManager
implements MessageListener, OidListListener
{ {
/** /**
* Returns a reference to our place configuration object. * Returns a reference to our place configuration object.
@@ -101,11 +102,8 @@ public class PlaceManager implements Subscriber
// configure the occupant info set // configure the occupant info set
plobj.occupantInfo.setElementType(getOccupantInfoClass()); plobj.occupantInfo.setElementType(getOccupantInfoClass());
// we'll want to be included among the place object's subscribers; // we'll need to hear about place object events
// we know that we can call addSubscriber() directly because the plobj.addListener(this);
// place manager is doing all of our initialization on the dobjmgr
// thread
plobj.addSubscriber(this);
// let our derived classes do their thang // let our derived classes do their thang
didStartup(); didStartup();
@@ -225,47 +223,48 @@ public class PlaceManager implements Subscriber
_msghandlers.put(name, handler); _msghandlers.put(name, handler);
} }
// nothing doing /**
public void objectAvailable (DObject object) * Dispatches message events to registered message handlers. Derived
{ * classes should probably register message handlers rather than
} * override this method directly.
*/
// nothing doing public void messageReceived (MessageEvent event)
public void requestFailed (int oid, ObjectAccessException cause)
{ {
MessageHandler handler = null;
if (_msghandlers != null) {
handler = (MessageHandler)_msghandlers.get(event.getName());
}
if (handler != null) {
handler.handleEvent(event, _plobj);
}
} }
/** /**
* Derived classes can override this to handle events, but they must * Handles occupant arrival into the place. Derived classes may need
* be sure to pass unknown events up to their super class. * to override this method to handle other oid lists in their derived
* place objects. They should be sure to call
* <code>super.objectAdded</code> if the event is one they don't
* explicitly handle.
*/ */
public boolean handleEvent (DEvent event, DObject target) public void objectAdded (ObjectAddedEvent event)
{ {
// if this is a message event, see if we have a handler for it if (event.getName().equals(PlaceObject.OCCUPANTS)) {
if (event instanceof MessageEvent) { bodyEntered(event.getOid());
MessageEvent mevt = (MessageEvent)event;
MessageHandler handler = null;
if (_msghandlers != null) {
handler = (MessageHandler)_msghandlers.get(mevt.getName());
}
if (handler != null) {
handler.handleEvent(mevt, (PlaceObject)target);
}
} else if (event instanceof ObjectAddedEvent) {
ObjectAddedEvent oae = (ObjectAddedEvent)event;
if (oae.getName().equals(PlaceObject.OCCUPANTS)) {
bodyEntered(oae.getOid());
}
} else if (event instanceof ObjectRemovedEvent) {
ObjectRemovedEvent ore = (ObjectRemovedEvent)event;
if (ore.getName().equals(PlaceObject.OCCUPANTS)) {
bodyLeft(ore.getOid());
}
} }
}
return true; /**
* Handles occupant departure from the place. Derived classes may need
* to override this method to handle other oid lists in their derived
* place objects. They should be sure to call
* <code>super.objectRemoved</code> if the event is one they don't
* explicitly handle.
*/
public void objectRemoved (ObjectRemovedEvent event)
{
if (event.getName().equals(PlaceObject.OCCUPANTS)) {
bodyLeft(event.getOid());
}
} }
/** /**
@@ -1,5 +1,5 @@
// //
// $Id: PlaceRegistry.java,v 1.11 2001/10/11 04:07:51 mdb Exp $ // $Id: PlaceRegistry.java,v 1.12 2001/10/12 00:03:02 mdb Exp $
package com.threerings.crowd.server; package com.threerings.crowd.server;
@@ -173,13 +173,6 @@ public class PlaceRegistry implements Subscriber
", cause=" + cause + "]."); ", cause=" + cause + "].");
} }
public boolean handleEvent (DEvent event, DObject target)
{
// this shouldn't be called because we don't subscribe to
// anything, we just want to hear about object creation
return false;
}
protected Queue _createq = new Queue(); protected Queue _createq = new Queue();
protected HashIntMap _pmgrs = new HashIntMap(); protected HashIntMap _pmgrs = new HashIntMap();
} }
@@ -1,5 +1,5 @@
// //
// $Id: GameController.java,v 1.7 2001/10/11 21:08:21 mdb Exp $ // $Id: GameController.java,v 1.8 2001/10/12 00:03:03 mdb Exp $
package com.threerings.parlor.game; package com.threerings.parlor.game;
@@ -27,7 +27,8 @@ import com.threerings.parlor.util.ParlorContext;
* distributed object events. * distributed object events.
*/ */
public abstract class GameController public abstract class GameController
extends PlaceController implements Subscriber, GameCodes extends PlaceController
implements AttributeChangeListener, GameCodes
{ {
/** /**
* Initializes this game controller with the game configuration that * Initializes this game controller with the game configuration that
@@ -51,7 +52,11 @@ public abstract class GameController
super.init(ctx, config); super.init(ctx, config);
} }
// documentation inherited /**
* Adds this controller as a listener to the game object (thus derived
* classes need not do so) and lets the game manager know that we are
* now ready to go.
*/
public void willEnterPlace (PlaceObject plobj) public void willEnterPlace (PlaceObject plobj)
{ {
super.willEnterPlace(plobj); super.willEnterPlace(plobj);
@@ -59,8 +64,8 @@ public abstract class GameController
// obtain a casted reference // obtain a casted reference
_gobj = (GameObject)plobj; _gobj = (GameObject)plobj;
// and add ourselves as a subscriber // and add ourselves as a listener
_gobj.addSubscriber(this); _gobj.addListener(this);
// finally let the game manager know that we're ready to roll // finally let the game manager know that we're ready to roll
MessageEvent mevt = new MessageEvent( MessageEvent mevt = new MessageEvent(
@@ -68,13 +73,16 @@ public abstract class GameController
_ctx.getDObjectManager().postEvent(mevt); _ctx.getDObjectManager().postEvent(mevt);
} }
// documentation inherited /**
* Removes our listener registration from the game object and cleans
* house.
*/
public void didLeavePlace (PlaceObject plobj) public void didLeavePlace (PlaceObject plobj)
{ {
super.willEnterPlace(plobj); super.didLeavePlace(plobj);
// unsubscribe from the game object // unlisten to the game object
_gobj.removeSubscriber(this); _gobj.removeListener(this);
_gobj = null; _gobj = null;
} }
@@ -89,47 +97,27 @@ public abstract class GameController
} }
// documentation inherited // documentation inherited
public void objectAvailable (DObject object) public void attributeChanged (AttributeChangedEvent event)
{ {
Log.warning("Got call to objectAvailable()?! " + // deal with game state changes
"[object=" + object + "]."); if (event.getName().equals(GameObject.STATE)) {
} switch (event.getIntValue()) {
case GameObject.IN_PLAY:
// documentation inherited gameDidStart();
public void requestFailed (int oid, ObjectAccessException cause) break;
{ case GameObject.GAME_OVER:
Log.warning("Got call to requestFailed()?! " + gameDidEnd();
"[oid=" + oid + ", cause=" + cause + "]."); break;
} case GameObject.CANCELLED:
gameWasCancelled();
// documentation inherited break;
public boolean handleEvent (DEvent event, DObject target) default:
{ Log.warning("Game transitioned to unknown state " +
if (event instanceof AttributeChangedEvent) { "[gobj=" + _gobj +
AttributeChangedEvent ace = (AttributeChangedEvent)event; ", state=" + event.getIntValue() + "].");
break;
// deal with game state changes
if (ace.getName().equals(GameObject.STATE)) {
switch (ace.getIntValue()) {
case GameObject.IN_PLAY:
gameDidStart();
break;
case GameObject.GAME_OVER:
gameDidEnd();
break;
case GameObject.CANCELLED:
gameWasCancelled();
break;
default:
Log.warning("Game transitioned to unknown state " +
"[gobj=" + _gobj +
", state=" + ace.getIntValue() + "].");
break;
}
} }
} }
return true;
} }
/** /**
@@ -1,5 +1,5 @@
// //
// $Id: GameManager.java,v 1.8 2001/10/11 21:08:21 mdb Exp $ // $Id: GameManager.java,v 1.9 2001/10/12 00:03:03 mdb Exp $
package com.threerings.parlor.game; package com.threerings.parlor.game;
@@ -65,6 +65,8 @@ public class GameManager
// documentation inherited // documentation inherited
protected void didStartup () protected void didStartup ()
{ {
super.didStartup();
// obtain a casted reference to our game object // obtain a casted reference to our game object
_gameobj = (GameObject)_plobj; _gameobj = (GameObject)_plobj;
@@ -0,0 +1,27 @@
//
// $Id: MathUtil.java,v 1.1 2001/10/12 00:03:03 mdb Exp $
package com.threerings.parlor.util;
import java.util.Random;
/**
* Contains math-related utility functions that are pertinent to game
* development.
*/
public class MathUtil
{
/**
* Returns a pseudo-random integer in the range from zero (inclusive)
* to <code>maxValue</code> (exclusive). This presently uses an
* instance of {@link Random} which uses a 48-bit seed, which is
* modified using a linear congruential formula. (See Donald Knuth,
* The Art of Computer Programming, Volume 2, Section 3.2.1.)
*/
public static int random (int maxValue)
{
return _random.nextInt(maxValue);
}
protected static Random _random = new Random();
}
@@ -1,5 +1,5 @@
// //
// $Id: ClientDObjectMgr.java,v 1.7 2001/10/11 04:07:52 mdb Exp $ // $Id: ClientDObjectMgr.java,v 1.8 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.client; package com.threerings.presents.client;
@@ -173,10 +173,9 @@ public class ClientDObjectMgr
_ocache.remove(target.getOid()); _ocache.remove(target.getOid());
} }
// have the object pass this event on to its subscribers if // have the object pass this event on to its listeners
// desired
if (notify) { if (notify) {
target.notifySubscribers(event); target.notifyListeners(event);
} }
} catch (Exception e) { } catch (Exception e) {
@@ -1,5 +1,5 @@
// //
// $Id: InvocationDirector.java,v 1.13 2001/10/11 04:07:52 mdb Exp $ // $Id: InvocationDirector.java,v 1.14 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.client; package com.threerings.presents.client;
@@ -35,7 +35,7 @@ import com.threerings.presents.util.ClassUtil;
* notifications at any time from the server. * notifications at any time from the server.
*/ */
public class InvocationDirector public class InvocationDirector
implements Subscriber implements Subscriber, MessageListener
{ {
/** /**
* Initializes the invocation director with the specified invocation * Initializes the invocation director with the specified invocation
@@ -121,13 +121,18 @@ public class InvocationDirector
_receivers.put(module, receiver); _receivers.put(module, receiver);
} }
// documentation inherited
public void objectAvailable (DObject object) public void objectAvailable (DObject object)
{ {
// add ourselves as a message listener
object.addListener(this);
// let the client know that we're ready to go now that we've got // let the client know that we're ready to go now that we've got
// our subscription to the client object // our subscription to the client object
_client.invocationDirectorReady((ClientObject)object); _client.invocationDirectorReady((ClientObject)object);
} }
// documentation inherited
public void requestFailed (int oid, ObjectAccessException cause) public void requestFailed (int oid, ObjectAccessException cause)
{ {
// aiya! we were unable to subscribe to the client object. we're // aiya! we were unable to subscribe to the client object. we're
@@ -139,23 +144,14 @@ public class InvocationDirector
/** /**
* Process incoming message requests on user object. * Process incoming message requests on user object.
*/ */
public boolean handleEvent (DEvent event, DObject target) public void messageReceived (MessageEvent event)
{ {
// we only care about message events String name = event.getName();
if (!(event instanceof MessageEvent)) {
return true;
}
// and only those of proper name
MessageEvent mevt = (MessageEvent)event;
String name = mevt.getName();
if (name.equals(InvocationObject.RESPONSE_NAME)) { if (name.equals(InvocationObject.RESPONSE_NAME)) {
handleInvocationResponse(mevt.getArgs()); handleInvocationResponse(event.getArgs());
} else if (name.equals(InvocationObject.NOTIFICATION_NAME)) { } else if (name.equals(InvocationObject.NOTIFICATION_NAME)) {
handleInvocationNotification(mevt.getArgs()); handleInvocationNotification(event.getArgs());
} }
return true;
} }
/** /**
@@ -0,0 +1,23 @@
//
// $Id: AttributeChangeListener.java,v 1.1 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj;
/**
* Implemented by entites which wish to hear about attribute changes that
* take place for a particular distributed object.
*
* @see DObject#addListener
*/
public interface AttributeChangeListener
{
/**
* Called when an attribute changed event has been dispatched on an
* object. This will be called <em>after</em> the event has been
* applied to the object. So fetching the attribute during this call
* will provide the new value for the attribute.
*
* @param event The event that was dispatched on the object.
*/
public void attributeChanged (AttributeChangedEvent event);
}
@@ -1,5 +1,5 @@
// //
// $Id: AttributeChangedEvent.java,v 1.9 2001/10/11 04:07:52 mdb Exp $ // $Id: AttributeChangedEvent.java,v 1.10 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -121,11 +121,13 @@ public class AttributeChangedEvent extends TypedEvent
return true; return true;
} }
// documentation inherited
public short getType () public short getType ()
{ {
return TYPE; return TYPE;
} }
// documentation inherited
public void writeTo (DataOutputStream out) public void writeTo (DataOutputStream out)
throws IOException throws IOException
{ {
@@ -134,6 +136,7 @@ public class AttributeChangedEvent extends TypedEvent
ValueMarshaller.writeTo(out, _value); ValueMarshaller.writeTo(out, _value);
} }
// documentation inherited
public void readFrom (DataInputStream in) public void readFrom (DataInputStream in)
throws IOException throws IOException
{ {
@@ -142,6 +145,15 @@ public class AttributeChangedEvent extends TypedEvent
_value = ValueMarshaller.readFrom(in); _value = ValueMarshaller.readFrom(in);
} }
// documentation inherited
protected void notifyListener (Object listener)
{
if (listener instanceof AttributeChangeListener) {
((AttributeChangeListener)listener).attributeChanged(this);
}
}
// documentation inherited
protected void toString (StringBuffer buf) protected void toString (StringBuffer buf)
{ {
buf.append("CHANGE:"); buf.append("CHANGE:");
@@ -1,5 +1,5 @@
// //
// $Id: DEvent.java,v 1.6 2001/10/11 04:07:52 mdb Exp $ // $Id: DEvent.java,v 1.7 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -63,6 +63,18 @@ public abstract class DEvent
_toid = targetOid; _toid = targetOid;
} }
/**
* Events with associated listener interfaces should implement this
* function and notify the supplied listener if it implements their
* event listening interface. For example, the {@link
* AttributeChangedEvent} will notify listeners that implement
* {@AttributeChangeListener}.
*/
protected void notifyListener (Object listener)
{
// the default is to do nothing
}
/** /**
* Constructs and returns a string representation of this event. * Constructs and returns a string representation of this event.
*/ */
@@ -1,5 +1,5 @@
// //
// $Id: DObject.java,v 1.27 2001/10/11 04:07:52 mdb Exp $ // $Id: DObject.java,v 1.28 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -99,7 +99,7 @@ public class DObject
* instead to ensure that everything is done on the proper thread. * instead to ensure that everything is done on the proper thread.
* This function can only safely be called directly when you know you * This function can only safely be called directly when you know you
* are operating on the omgr thread (you are in the middle of a call * are operating on the omgr thread (you are in the middle of a call
* to <code>objectAvailable</code> or <code>handleEvent</code>). * to <code>objectAvailable</code> or to a listener callback).
* *
* @see DObjectManager#subscribeToObject * @see DObjectManager#subscribeToObject
*/ */
@@ -118,7 +118,7 @@ public class DObject
* instead to ensure that everything is done on the proper thread. * instead to ensure that everything is done on the proper thread.
* This function can only safely be called directly when you know you * This function can only safely be called directly when you know you
* are operating on the omgr thread (you are in the middle of a call * are operating on the omgr thread (you are in the middle of a call
* to <code>objectAvailable</code> or <code>handleEvent</code>). * to <code>objectAvailable</code> or to a listener callback).
* *
* @see DObjectManager#unsubscribeFromObject * @see DObjectManager#unsubscribeFromObject
*/ */
@@ -135,6 +135,38 @@ public class DObject
} }
} }
/**
* Adds an event listener to this object. The listener will be
* notified when any events are dispatched on this object that match
* their particular listener interface.
*
* @param listener the listener to be added.
*
* @see EventListener
* @see AttributeChangeListener
* @see SetListener
* @see OidListListener
*/
public void addListener (Object listener)
{
// only add the listener if they're not already there
Object[] els = ListUtil.testAndAdd(_listeners, listener);
if (els != null) {
_listeners = els;
}
}
/**
* Removes an event listener from this object. The listener will no
* longer be notified when events are dispatched on this object.
*
* @param listener the listener to be removed.
*/
public void removeListener (Object listener)
{
ListUtil.clear(_listeners, listener);
}
/** /**
* At times, an entity on the server may need to ensure that events it * At times, an entity on the server may need to ensure that events it
* has queued up have made it through the event queue and are applied * has queued up have made it through the event queue and are applied
@@ -257,36 +289,33 @@ public class DObject
* *
* @param event the event that was just applied. * @param event the event that was just applied.
*/ */
public void notifySubscribers (DEvent event) public void notifyListeners (DEvent event)
{ {
// Log.info("Dispatching event to " + _scount + // iterate over the listener list, performing the necessary
// " subscribers: " + event); // notifications
int llength = _listeners.length;
for (int i = 0; i < llength; i++) {
Object listener = _listeners[i];
// nothing to do if we've no subscribers // skip empty slots
if (_subs == null) { if (listener == null) {
return;
}
// otherwise iterate over the subscribers array and notify
int slength = _subs.length;
for (int i = 0; i < slength; i++) {
Subscriber sub = (Subscriber)_subs[i];
// skip empty spots
if (sub == null) {
continue; continue;
} }
// notify the subscriber try {
if (!sub.handleEvent(event, this)) { // do any event specific notifications
// if they return false, we need to remove them from the event.notifyListener(listener);
// subscriber list
_subs[i] = null;
// if we just removed our last subscriber, we need to let // and notify them if they are listening for all events
// the omgr know about it if (listener instanceof EventListener) {
if (--_scount == 0) { ((EventListener)listener).eventReceived(event);
_mgr.removedLastSubscriber(this);
} }
} catch (Exception e) {
Log.warning("Listener choked during notification " +
"[listener=" + listener +
", event=" + event + "].");
Log.logStackTrace(e);
} }
} }
} }
@@ -469,6 +498,9 @@ public class DObject
/** Our subscribers list. */ /** Our subscribers list. */
protected Object[] _subs; protected Object[] _subs;
/** Our event listeners list. */
protected Object[] _listeners;
/** Our subscriber count. */ /** Our subscriber count. */
protected int _scount; protected int _scount;
} }
@@ -1,5 +1,5 @@
// //
// $Id: EntryAddedEvent.java,v 1.4 2001/10/11 04:07:52 mdb Exp $ // $Id: EntryAddedEvent.java,v 1.5 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -89,11 +89,13 @@ public class ElementAddedEvent extends TypedEvent
return true; return true;
} }
// documentation inherited
public short getType () public short getType ()
{ {
return TYPE; return TYPE;
} }
// documentation inherited
public void writeTo (DataOutputStream out) public void writeTo (DataOutputStream out)
throws IOException throws IOException
{ {
@@ -102,6 +104,7 @@ public class ElementAddedEvent extends TypedEvent
ElementUtil.flatten(out, _elem); ElementUtil.flatten(out, _elem);
} }
// documentation inherited
public void readFrom (DataInputStream in) public void readFrom (DataInputStream in)
throws IOException throws IOException
{ {
@@ -116,6 +119,15 @@ public class ElementAddedEvent extends TypedEvent
in.readFully(_bytes, 0, bcount); in.readFully(_bytes, 0, bcount);
} }
// documentation inherited
protected void notifyListener (Object listener)
{
if (listener instanceof SetListener) {
((SetListener)listener).elementAdded(this);
}
}
// documentation inherited
protected void toString (StringBuffer buf) protected void toString (StringBuffer buf)
{ {
buf.append("ELADD:"); buf.append("ELADD:");
@@ -1,5 +1,5 @@
// //
// $Id: EntryRemovedEvent.java,v 1.5 2001/10/11 04:07:52 mdb Exp $ // $Id: EntryRemovedEvent.java,v 1.6 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -77,11 +77,13 @@ public class ElementRemovedEvent extends TypedEvent
} }
// documentation inherited
public short getType () public short getType ()
{ {
return TYPE; return TYPE;
} }
// documentation inherited
public void writeTo (DataOutputStream out) public void writeTo (DataOutputStream out)
throws IOException throws IOException
{ {
@@ -90,6 +92,7 @@ public class ElementRemovedEvent extends TypedEvent
ValueMarshaller.writeTo(out, _key); ValueMarshaller.writeTo(out, _key);
} }
// documentation inherited
public void readFrom (DataInputStream in) public void readFrom (DataInputStream in)
throws IOException throws IOException
{ {
@@ -98,6 +101,15 @@ public class ElementRemovedEvent extends TypedEvent
_key = ValueMarshaller.readFrom(in); _key = ValueMarshaller.readFrom(in);
} }
// documentation inherited
protected void notifyListener (Object listener)
{
if (listener instanceof SetListener) {
((SetListener)listener).elementRemoved(this);
}
}
// documentation inherited
protected void toString (StringBuffer buf) protected void toString (StringBuffer buf)
{ {
buf.append("ELREM:"); buf.append("ELREM:");
@@ -1,5 +1,5 @@
// //
// $Id: EntryUpdatedEvent.java,v 1.2 2001/10/11 04:07:52 mdb Exp $ // $Id: EntryUpdatedEvent.java,v 1.3 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -95,11 +95,13 @@ public class ElementUpdatedEvent extends TypedEvent
return true; return true;
} }
// documentation inherited
public short getType () public short getType ()
{ {
return TYPE; return TYPE;
} }
// documentation inherited
public void writeTo (DataOutputStream out) public void writeTo (DataOutputStream out)
throws IOException throws IOException
{ {
@@ -108,6 +110,7 @@ public class ElementUpdatedEvent extends TypedEvent
ElementUtil.flatten(out, _elem); ElementUtil.flatten(out, _elem);
} }
// documentation inherited
public void readFrom (DataInputStream in) public void readFrom (DataInputStream in)
throws IOException throws IOException
{ {
@@ -122,6 +125,15 @@ public class ElementUpdatedEvent extends TypedEvent
in.readFully(_bytes, 0, bcount); in.readFully(_bytes, 0, bcount);
} }
// documentation inherited
protected void notifyListener (Object listener)
{
if (listener instanceof SetListener) {
((SetListener)listener).elementUpdated(this);
}
}
// documentation inherited
protected void toString (StringBuffer buf) protected void toString (StringBuffer buf)
{ {
buf.append("ELUPD:"); buf.append("ELUPD:");
@@ -0,0 +1,25 @@
//
// $Id: EventListener.java,v 1.1 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj;
/**
* Implemented by entites which wish to hear about all events being
* dispatched on a particular distributed object.
*
* @see DObject#addListener
*/
public interface EventListener
{
/**
* Called when any event has been dispatched on an object. The event
* will be of the derived class that corresponds to the kind of event
* that occurred on the object. This will be called <em>after</em> the
* event has been applied to the object. So fetching an attribute upon
* receiving an attribute changed event will provide the new value for
* the attribute.
*
* @param event The event that was dispatched on the object.
*/
public void eventReceived (DEvent event);
}
@@ -1,5 +1,5 @@
// //
// $Id: MessageEvent.java,v 1.6 2001/10/11 04:07:52 mdb Exp $ // $Id: MessageEvent.java,v 1.7 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -86,11 +86,13 @@ public class MessageEvent extends TypedEvent
return true; return true;
} }
// documentation inherited
public short getType () public short getType ()
{ {
return TYPE; return TYPE;
} }
// documentation inherited
public void writeTo (DataOutputStream out) public void writeTo (DataOutputStream out)
throws IOException throws IOException
{ {
@@ -106,6 +108,7 @@ public class MessageEvent extends TypedEvent
} }
} }
// documentation inherited
public void readFrom (DataInputStream in) public void readFrom (DataInputStream in)
throws IOException throws IOException
{ {
@@ -120,6 +123,15 @@ public class MessageEvent extends TypedEvent
} }
} }
// documentation inherited
protected void notifyListener (Object listener)
{
if (listener instanceof MessageListener) {
((MessageListener)listener).messageReceived(this);
}
}
// documentation inherited
protected void toString (StringBuffer buf) protected void toString (StringBuffer buf)
{ {
buf.append("MSG:"); buf.append("MSG:");
@@ -0,0 +1,20 @@
//
// $Id: MessageListener.java,v 1.1 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj;
/**
* Implemented by entites which wish to hear about message events that are
* dispatched on a particular distributed object.
*
* @see DObject#addListener
*/
public interface MessageListener
{
/**
* Called when an message event has been dispatched on an object.
*
* @param event The event that was dispatched on the object.
*/
public void messageReceived (MessageEvent event);
}
@@ -1,5 +1,5 @@
// //
// $Id: ObjectAddedEvent.java,v 1.5 2001/10/11 04:07:52 mdb Exp $ // $Id: ObjectAddedEvent.java,v 1.6 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -73,11 +73,13 @@ public class ObjectAddedEvent extends TypedEvent
return true; return true;
} }
// documentation inherited
public short getType () public short getType ()
{ {
return TYPE; return TYPE;
} }
// documentation inherited
public void writeTo (DataOutputStream out) public void writeTo (DataOutputStream out)
throws IOException throws IOException
{ {
@@ -86,6 +88,7 @@ public class ObjectAddedEvent extends TypedEvent
out.writeInt(_oid); out.writeInt(_oid);
} }
// documentation inherited
public void readFrom (DataInputStream in) public void readFrom (DataInputStream in)
throws IOException throws IOException
{ {
@@ -94,6 +97,15 @@ public class ObjectAddedEvent extends TypedEvent
_oid = in.readInt(); _oid = in.readInt();
} }
// documentation inherited
protected void notifyListener (Object listener)
{
if (listener instanceof OidListListener) {
((OidListListener)listener).objectAdded(this);
}
}
// documentation inherited
protected void toString (StringBuffer buf) protected void toString (StringBuffer buf)
{ {
buf.append("OBJADD:"); buf.append("OBJADD:");
@@ -1,5 +1,5 @@
// //
// $Id: ObjectRemovedEvent.java,v 1.5 2001/10/11 04:07:52 mdb Exp $ // $Id: ObjectRemovedEvent.java,v 1.6 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -74,11 +74,13 @@ public class ObjectRemovedEvent extends TypedEvent
return true; return true;
} }
// documentation inherited
public short getType () public short getType ()
{ {
return TYPE; return TYPE;
} }
// documentation inherited
public void writeTo (DataOutputStream out) public void writeTo (DataOutputStream out)
throws IOException throws IOException
{ {
@@ -87,6 +89,7 @@ public class ObjectRemovedEvent extends TypedEvent
out.writeInt(_oid); out.writeInt(_oid);
} }
// documentation inherited
public void readFrom (DataInputStream in) public void readFrom (DataInputStream in)
throws IOException throws IOException
{ {
@@ -95,6 +98,15 @@ public class ObjectRemovedEvent extends TypedEvent
_oid = in.readInt(); _oid = in.readInt();
} }
// documentation inherited
protected void notifyListener (Object listener)
{
if (listener instanceof OidListListener) {
((OidListListener)listener).objectRemoved(this);
}
}
// documentation inherited
protected void toString (StringBuffer buf) protected void toString (StringBuffer buf)
{ {
buf.append("OBJREM:"); buf.append("OBJREM:");
@@ -0,0 +1,31 @@
//
// $Id: OidListListener.java,v 1.1 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj;
/**
* Implemented by entites which wish to hear about changes that occur to
* oid list attributes of a particular distributed object.
*
* @see DObject#addListener
*/
public interface OidListListener
{
/**
* Called when an object added event has been dispatched on an
* object. This will be called <em>after</em> the event has been
* applied to the object.
*
* @param event The event that was dispatched on the object.
*/
public void objectAdded (ObjectAddedEvent event);
/**
* Called when an object removed event has been dispatched on an
* object. This will be called <em>after</em> the event has been
* applied to the object.
*
* @param event The event that was dispatched on the object.
*/
public void objectRemoved (ObjectRemovedEvent event);
}
@@ -0,0 +1,40 @@
//
// $Id: SetListener.java,v 1.1 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj;
/**
* Implemented by entites which wish to hear about changes that occur to
* set attributes of a particular distributed object.
*
* @see DObject#addListener
*/
public interface SetListener
{
/**
* Called when an element added event has been dispatched on an
* object. This will be called <em>after</em> the event has been
* applied to the object.
*
* @param event The event that was dispatched on the object.
*/
public void elementAdded (ElementAddedEvent event);
/**
* Called when an element updated event has been dispatched on an
* object. This will be called <em>after</em> the event has been
* applied to the object.
*
* @param event The event that was dispatched on the object.
*/
public void elementUpdated (ElementUpdatedEvent event);
/**
* Called when an element removed event has been dispatched on an
* object. This will be called <em>after</em> the event has been
* applied to the object.
*
* @param event The event that was dispatched on the object.
*/
public void elementRemoved (ElementRemovedEvent event);
}
@@ -1,5 +1,5 @@
// //
// $Id: Subscriber.java,v 1.6 2001/10/11 04:07:52 mdb Exp $ // $Id: Subscriber.java,v 1.7 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.dobj; package com.threerings.presents.dobj;
@@ -10,16 +10,24 @@ package com.threerings.presents.dobj;
* registering as a subscriber to an object, an entity can react to * registering as a subscriber to an object, an entity can react to
* changes made to an object and ensure that their object is kept up to * changes made to an object and ensure that their object is kept up to
* date. * date.
*
* <p> To actually receive callbacks when events are dispatched on a
* distributed object, an entity should register itself as a listener on
* the object once it has received its object reference.
*
* @see EventListener
* @see AttributeChangeListener
* @see SetListener
* @see OidListListener
*/ */
public interface Subscriber public interface Subscriber
{ {
/** /**
* Called when a subscription request has succeeded and the object is * Called when a subscription request has succeeded and the object is
* available. If the object was requested for subscription, the * available. If the object was requested for subscription, the
* subscriber will subsequently receive notifications via * subscriber can subsequently receive notifications by registering
* <code>handleEvent</code>. If the object was requested as a one-time * itself as a listener of some sort (see {@link
* read-only copy, these updates will not occur and the subscriber * DObject#addListener}).
* should not attempt to modify the object.
* *
* @see DObjectManager#subscribeToObject * @see DObjectManager#subscribeToObject
*/ */
@@ -33,23 +41,4 @@ public interface Subscriber
* @see DObjectManager#subscribeToObject * @see DObjectManager#subscribeToObject
*/ */
public void requestFailed (int oid, ObjectAccessException cause); public void requestFailed (int oid, ObjectAccessException cause);
/**
* Called when an event has been dispatched on an object. The event
* will be of the derived class that corresponds to the kind of event
* that occurred on the object. <code>handleEvent</code> will be
* called <em>after</em> the event has been applied to the object. So
* fetching an attribute upon receiving an attribute changed event
* will provide the new value for the attribute.
*
* <p> A subscriber should return true from <code>handleEvent</code>
* unless they wish their subscription to be terminated.
*
* @param event The event dispatched on the object.
* @param target The object on which the event was dispatched.
*
* @return true if the subscriber wishes to remain subscribed to the
* target object, false if they do not.
*/
public boolean handleEvent (DEvent event, DObject target);
} }
@@ -1,5 +1,5 @@
// //
// $Id: InvocationManager.java,v 1.8 2001/10/11 04:07:53 mdb Exp $ // $Id: InvocationManager.java,v 1.9 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.server; package com.threerings.presents.server;
@@ -32,7 +32,7 @@ import com.threerings.presents.util.ClassUtil;
* the client. * the client.
*/ */
public class InvocationManager public class InvocationManager
implements Subscriber implements Subscriber, MessageListener
{ {
public InvocationManager (DObjectManager omgr) public InvocationManager (DObjectManager omgr)
{ {
@@ -93,6 +93,9 @@ public class InvocationManager
{ {
// this must be our invocation object // this must be our invocation object
_invoid = object.getOid(); _invoid = object.getOid();
// add ourselves as a message listener
object.addListener(this);
} }
public void requestFailed (int oid, ObjectAccessException cause) public void requestFailed (int oid, ObjectAccessException cause)
@@ -104,23 +107,15 @@ public class InvocationManager
_invoid = -1; _invoid = -1;
} }
public boolean handleEvent (DEvent event, DObject target) public void messageReceived (MessageEvent event)
{ {
// we shouldn't be getting non-message events, but check just to
// be sure
if (!(event instanceof MessageEvent)) {
Log.warning("Got non-message event!? [evt=" + event + "].");
return true;
}
// make sure the name is proper just for sanity's sake // make sure the name is proper just for sanity's sake
MessageEvent mevt = (MessageEvent)event; if (!event.getName().equals(InvocationObject.REQUEST_NAME)) {
if (!mevt.getName().equals(InvocationObject.REQUEST_NAME)) { return;
return true;
} }
// we've got an invocation request, so we process it // we've got an invocation request, so we process it
Object[] args = mevt.getArgs(); Object[] args = event.getArgs();
String module = (String)args[0]; String module = (String)args[0];
String procedure = (String)args[1]; String procedure = (String)args[1];
Integer invid = (Integer)args[2]; Integer invid = (Integer)args[2];
@@ -130,20 +125,20 @@ public class InvocationManager
(InvocationProvider)_providers.get(module); (InvocationProvider)_providers.get(module);
if (provider == null) { if (provider == null) {
Log.warning("No provider registered for invocation request " + Log.warning("No provider registered for invocation request " +
"[evt=" + mevt + "]."); "[evt=" + event + "].");
return true; return;
} }
// prune the method arguments from the full message arguments // prune the method arguments from the full message arguments
Object[] margs = new Object[args.length-1]; Object[] margs = new Object[args.length-1];
int cloid = mevt.getSourceOid(); int cloid = event.getSourceOid();
margs[0] = PresentsServer.omgr.getObject(cloid); margs[0] = PresentsServer.omgr.getObject(cloid);
// make sure the client is still around // make sure the client is still around
if (margs[0] == null) { if (margs[0] == null) {
Log.warning("Client no longer around for invocation provider " + Log.warning("Client no longer around for invocation provider " +
"request [module=" + module + "request [module=" + module +
", proc=" + procedure + ", cloid=" + cloid + "]."); ", proc=" + procedure + ", cloid=" + cloid + "].");
return true; return;
} }
System.arraycopy(args, 2, margs, 1, args.length-2); System.arraycopy(args, 2, margs, 1, args.length-2);
@@ -154,7 +149,7 @@ public class InvocationManager
Log.warning("Unable to resolve provider procedure " + Log.warning("Unable to resolve provider procedure " +
"[provider=" + provider.getClass().getName() + "[provider=" + provider.getClass().getName() +
", method=" + mname + "]."); ", method=" + mname + "].");
return true; return;
} }
// and invoke it // and invoke it
@@ -166,8 +161,6 @@ public class InvocationManager
", method=" + procmeth + "]."); ", method=" + procmeth + "].");
Log.logStackTrace(e); Log.logStackTrace(e);
} }
return true;
} }
protected DObjectManager _omgr; protected DObjectManager _omgr;
@@ -1,5 +1,5 @@
// //
// $Id: PresentsClient.java,v 1.20 2001/10/11 04:07:53 mdb Exp $ // $Id: PresentsClient.java,v 1.21 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.server; package com.threerings.presents.server;
@@ -75,11 +75,6 @@ public class PresentsClient implements Subscriber, MessageHandler
"[client=" + PresentsClient.this + "[client=" + PresentsClient.this +
", error=" + cause + "]."); ", error=" + cause + "].");
} }
public boolean handleEvent (DEvent event, DObject target)
{
return false;
}
}; };
Class clobjClass = _cmgr.getClientObjectClass(); Class clobjClass = _cmgr.getClientObjectClass();
PresentsServer.omgr.createObject(clobjClass, sub, false); PresentsServer.omgr.createObject(clobjClass, sub, false);
@@ -204,7 +199,13 @@ public class PresentsClient implements Subscriber, MessageHandler
*/ */
public synchronized void unmapSubscrip (int oid) public synchronized void unmapSubscrip (int oid)
{ {
_subscrips.remove(oid); DObject object = (DObject)_subscrips.remove(oid);
if (object != null) {
object.removeListener(this);
} else {
Log.warning("Requested to unmap non-existent subscription " +
"[oid=" + oid + "].");
}
} }
/** /**
@@ -409,6 +410,8 @@ public class PresentsClient implements Subscriber, MessageHandler
// queue up an object response // queue up an object response
Connection conn = getConnection(); Connection conn = getConnection();
if (conn != null) { if (conn != null) {
// add ourselves as an event listener
object.addListener(this);
// pass the successful subscrip on to the client // pass the successful subscrip on to the client
conn.postMessage(new ObjectResponse(object)); conn.postMessage(new ObjectResponse(object));
// make a note of this new subscription // make a note of this new subscription
@@ -434,7 +437,7 @@ public class PresentsClient implements Subscriber, MessageHandler
} }
// documentation inherited from interface // documentation inherited from interface
public boolean handleEvent (DEvent event, DObject target) public void eventReceived (DEvent event)
{ {
// forward the event to the client // forward the event to the client
Connection conn = getConnection(); Connection conn = getConnection();
@@ -447,8 +450,6 @@ public class PresentsClient implements Subscriber, MessageHandler
Log.info("Dropped event forward notification " + Log.info("Dropped event forward notification " +
"[client=" + this + ", event=" + event + "]."); "[client=" + this + ", event=" + event + "].");
} }
return true;
} }
public String toString () public String toString ()
@@ -1,5 +1,5 @@
// //
// $Id: PresentsDObjectMgr.java,v 1.17 2001/10/11 04:07:53 mdb Exp $ // $Id: PresentsDObjectMgr.java,v 1.18 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.server; package com.threerings.presents.server;
@@ -173,9 +173,9 @@ public class PresentsDObjectMgr implements DObjectManager
// if the event returns false from applyToObject, this // if the event returns false from applyToObject, this
// means it's a silent event and we shouldn't notify the // means it's a silent event and we shouldn't notify the
// subscribers // listeners
if (notify) { if (notify) {
target.notifySubscribers(event); target.notifyListeners(event);
} }
} catch (Exception e) { } catch (Exception e) {
+1 -1
View File
@@ -1,4 +1,4 @@
#!/bin/sh #!/bin/sh
bindir=`dirname $0` bindir=`dirname $0`
$bindir/runjava $* com.threerings.parlor.test.TestServer $bindir/runjava $* com.threerings.presents.server.PresentsServer
@@ -1,5 +1,5 @@
// //
// $Id: TestClient.java,v 1.10 2001/10/11 04:07:52 mdb Exp $ // $Id: TestClient.java,v 1.11 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.client.test; package com.threerings.presents.client.test;
@@ -16,7 +16,7 @@ import com.threerings.presents.server.test.TestObject;
* A standalone test client. * A standalone test client.
*/ */
public class TestClient public class TestClient
implements Client.Invoker, ClientObserver, Subscriber implements Client.Invoker, ClientObserver, Subscriber, EventListener
{ {
public void setClient (Client client) public void setClient (Client client)
{ {
@@ -74,6 +74,7 @@ public class TestClient
public void objectAvailable (DObject object) public void objectAvailable (DObject object)
{ {
object.addListener(this);
Log.info("Object available: " + object); Log.info("Object available: " + object);
((TestObject)object).setBar("lawl!"); ((TestObject)object).setBar("lawl!");
} }
@@ -86,17 +87,18 @@ public class TestClient
_client.logoff(true); _client.logoff(true);
} }
public boolean handleEvent (DEvent event, DObject target) public void eventReceived (DEvent event)
{ {
Log.info("Got event [event=" + event + ", target=" + target + "]."); Log.info("Got event [event=" + event + "].");
if (event instanceof AttributeChangedEvent) { if (event instanceof AttributeChangedEvent) {
// request to destroy the object // request to destroy the object
target.destroy(); _client.getDObjectManager().destroyObject(event.getTargetOid());
} else { } else {
// request that we log off // request that we log off
_client.logoff(true); _client.logoff(true);
} }
return true;
} }
public void handleTestSucceeded (int invid, String one, int two) public void handleTestSucceeded (int invid, String one, int two)
@@ -1,5 +1,5 @@
// //
// $Id: DOMTest.java,v 1.2 2001/10/11 04:07:53 mdb Exp $ // $Id: DOMTest.java,v 1.3 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.server.test; package com.threerings.presents.server.test;
@@ -10,10 +10,13 @@ import com.threerings.presents.server.*;
/** /**
* A simple test case for the dobjmgr. * A simple test case for the dobjmgr.
*/ */
public class DOMTest implements Subscriber public class DOMTest implements Subscriber, AttributeChangeListener
{ {
public void objectAvailable (DObject object) public void objectAvailable (DObject object)
{ {
// add ourselves as a listener
object.addListener(this);
Log.info("Object available: " + object); Log.info("Object available: " + object);
// set some values // set some values
TestObject to = (TestObject)object; TestObject to = (TestObject)object;
@@ -27,17 +30,14 @@ public class DOMTest implements Subscriber
omgr.shutdown(); omgr.shutdown();
} }
public boolean handleEvent (DEvent event, DObject target) public void attributeChanged (AttributeChangedEvent event)
{ {
Log.info("Got event [event=" + event + ", target=" + target + "]."); Log.info("Got event [event=" + event + "].");
// if this is the second event, request a shutdown // if this is the second event, request a shutdown
AttributeChangedEvent ace = (AttributeChangedEvent)event; if (event.getName().equals(TestObject.BAR)) {
if (ace.getName().equals(TestObject.BAR)) {
omgr.shutdown(); omgr.shutdown();
} }
return true;
} }
public static PresentsDObjectMgr omgr = new PresentsDObjectMgr(); public static PresentsDObjectMgr omgr = new PresentsDObjectMgr();
@@ -1,5 +1,5 @@
// //
// $Id: DestroyedRefTest.java,v 1.2 2001/10/11 04:07:53 mdb Exp $ // $Id: DestroyedRefTest.java,v 1.3 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.server.test; package com.threerings.presents.server.test;
@@ -12,7 +12,7 @@ import com.threerings.presents.server.PresentsServer;
* an oid list. * an oid list.
*/ */
public class DestroyedRefTest public class DestroyedRefTest
implements Runnable, Subscriber implements Runnable, Subscriber, EventListener
{ {
public void run () public void run ()
{ {
@@ -23,6 +23,9 @@ public class DestroyedRefTest
public void objectAvailable (DObject object) public void objectAvailable (DObject object)
{ {
// add ourselves as an event listener
object.addListener(this);
// keep references to our test objects // keep references to our test objects
if (_objone == null) { if (_objone == null) {
_objone = (TestObject)object; _objone = (TestObject)object;
@@ -51,14 +54,16 @@ public class DestroyedRefTest
Log.warning("Ack. Unable to create object [cause=" + cause + "]."); Log.warning("Ack. Unable to create object [cause=" + cause + "].");
} }
public boolean handleEvent (DEvent event, DObject target) public void eventReceived (DEvent event)
{ {
int toid = event.getTargetOid();
// when we get the attribute change, we can exit // when we get the attribute change, we can exit
if (event instanceof ObjectDestroyedEvent) { if (event instanceof ObjectDestroyedEvent) {
Log.info("The upcoming object added event should be rejected."); Log.info("The upcoming object added event should be rejected.");
} else if (event instanceof ObjectAddedEvent && } else if (event instanceof ObjectAddedEvent &&
target == _objtwo) { toid == _objtwo.getOid()) {
Log.info("list should contain only one oid: " + _objtwo.list); Log.info("list should contain only one oid: " + _objtwo.list);
} else if (event instanceof AttributeChangedEvent) { } else if (event instanceof AttributeChangedEvent) {
@@ -68,8 +73,6 @@ public class DestroyedRefTest
} else { } else {
Log.info("Got unexpected event: " + event); Log.info("Got unexpected event: " + event);
} }
return true;
} }
protected TestObject _objone; protected TestObject _objone;
@@ -1,5 +1,5 @@
// //
// $Id: RefTest.java,v 1.2 2001/10/11 04:07:53 mdb Exp $ // $Id: RefTest.java,v 1.3 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.server.test; package com.threerings.presents.server.test;
@@ -11,7 +11,7 @@ import com.threerings.presents.server.PresentsServer;
* Tests the oid list reference tracking code. * Tests the oid list reference tracking code.
*/ */
public class RefTest public class RefTest
implements Runnable, Subscriber implements Runnable, Subscriber, EventListener
{ {
public void run () public void run ()
{ {
@@ -22,6 +22,9 @@ public class RefTest
public void objectAvailable (DObject object) public void objectAvailable (DObject object)
{ {
// add ourselves as an event listener to our subscribed object
object.addListener(this);
// keep references to our test objects // keep references to our test objects
if (_objone == null) { if (_objone == null) {
_objone = (TestObject)object; _objone = (TestObject)object;
@@ -40,18 +43,20 @@ public class RefTest
Log.warning("Ack. Unable to create object [cause=" + cause + "]."); Log.warning("Ack. Unable to create object [cause=" + cause + "].");
} }
public boolean handleEvent (DEvent event, DObject target) public void eventReceived (DEvent event)
{ {
// Log.info("Got event: " + event); // Log.info("Got event: " + event);
int toid = event.getTargetOid();
// once we receive the second object added we can destroy the // once we receive the second object added we can destroy the
// target object to see if the reference is cleaned up // target object to see if the reference is cleaned up
if (event instanceof ObjectAddedEvent && target == _objtwo) { if (event instanceof ObjectAddedEvent &&
toid == _objtwo.getOid()) {
Log.info("Destroying object two " + _objtwo + "."); Log.info("Destroying object two " + _objtwo + ".");
_objtwo.destroy(); _objtwo.destroy();
} else if (event instanceof ObjectDestroyedEvent) { } else if (event instanceof ObjectDestroyedEvent) {
if (target == _objtwo) { if (toid == _objtwo.getOid()) {
Log.info("List won't yet be empty: " + _objone.list); Log.info("List won't yet be empty: " + _objone.list);
} else { } else {
Log.info("Other object destroyed."); Log.info("Other object destroyed.");
@@ -64,8 +69,6 @@ public class RefTest
// finally destroy the other object to complete the circle // finally destroy the other object to complete the circle
_objone.destroy(); _objone.destroy();
} }
return true;
} }
protected TestObject _objone; protected TestObject _objone;
@@ -1,5 +1,5 @@
// //
// $Id: TestServer.java,v 1.4 2001/10/11 04:07:53 mdb Exp $ // $Id: TestServer.java,v 1.5 2001/10/12 00:03:03 mdb Exp $
package com.threerings.presents.server.test; package com.threerings.presents.server.test;
@@ -38,11 +38,6 @@ public class TestServer extends PresentsServer
Log.warning("Unable to create test object " + Log.warning("Unable to create test object " +
"[error=" + cause + "]."); "[error=" + cause + "].");
} }
public boolean handleEvent (DEvent event, DObject target)
{
return false;
}
}; };
omgr.createObject(TestObject.class, sub, false); omgr.createObject(TestObject.class, sub, false);
} }