Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
//
// $Id: DObject.java 6662 2011-06-21 19:33:06Z ray $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/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.presents.dobj;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.samskivert.util.ArrayUtil;
import com.samskivert.util.ListUtil;
import com.samskivert.util.StringUtil;
import com.threerings.io.Streamable;
import com.threerings.presents.net.Transport;
import static com.threerings.presents.Log.log;
/**
* The distributed object forms the foundation of the Presents system. All information shared among
* users of the system is done via distributed objects. A distributed object has a set of
* listeners. These listeners have access to the object or a proxy of the object and therefore have
* access to the data stored in the object's members at all times.
*
*
Additionally, an object has a set of subscribers. Subscribers manage the lifespan of the
* object; while a subscriber is subscribed, the listeners registered with an object will be
* notified of events. When the subscriber unsubscribes, the object becomes non-live and the
* listeners are no longer notified. Note: on the server, object subscription is merely a
* formality as all objects remain live all the time, so do not depend on event
* notifications ceasing when a subscriber has relinquished its subscription. Always unregister all
* listeners when they no longer need to hear from an object.
*
*
When there is any change to the the object's fields data, an event is generated which is
* dispatched to all listeners of the object, notifying them of that change and effecting that
* change to the copy of the object maintained at each client. In this way, both a repository of
* shared information and a mechanism for asynchronous notification are made available as a
* fundamental application building blocks.
*
*
To define what information is shared, an application creates a distributed object
* declaration which is much like a class declaration except that it is transformed into a proper
* derived class of DObject by a script. A declaration looks something like this:
*
*
* public dclass RoomObject
* {
* public String description;
* public int[] occupants;
* }
*
*
* which is converted into an actual Java class that looks like this:
*
*
* public class RoomObject extends DObject
* {
* public String getDescription ()
* {
* // ...
* }
*
* public void setDescription (String description)
* {
* // ...
* }
*
* public int[] getOccupants ()
* {
* // ...
* }
*
* public void setOccupants (int[] occupants)
* {
* // ...
* }
*
* public void setOccupantsAt (int index, int value)
* {
* // ...
* }
* }
*
*
* These method calls on the actual distributed object will result in the proper attribute change
* events being generated and dispatched.
*
*
Note that distributed object fields can be any of the following set of primitive types:
*
*
*
* Fields of type {@link Streamable} can also be used.
*/
public class DObject
implements Streamable
{
public DObject ()
{
_accessors = _atable.get(getClass());
if (_accessors == null) {
_accessors = createAccessors();
Arrays.sort(_accessors);
_atable.put(getClass(), _accessors);
}
}
/**
* Returns the object id of this object. All objects in the system have a unique object id.
*/
public int getOid ()
{
return _oid;
}
/**
* Returns the dobject manager under the auspices of which this object operates. This could be
* null if the object is not active.
*/
public DObjectManager getManager ()
{
return _omgr;
}
/**
* Don't call this function! Go through the distributed object manager instead to ensure that
* everything is done on the proper thread. 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 to
* objectAvailable or to a listener callback).
*
* @see DObjectManager#subscribeToObject
*/
public void addSubscriber (Subscriber> sub)
{
// only add the subscriber if they're not already there
Object[] subs = ListUtil.testAndAddRef(_subs, sub);
if (subs != null) {
// Log.info("Adding subscriber " + which() + ": " + sub + ".");
_subs = subs;
_scount++;
} else {
log.warning("Refusing subscriber that's already in the list", "dobj", which(),
"subscriber", sub, new Exception());
}
}
/**
* Don't call this function! Go through the distributed object manager instead to ensure that
* everything is done on the proper thread. 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 to
* objectAvailable or to a listener callback).
*
* @see DObjectManager#unsubscribeFromObject
*/
public void removeSubscriber (Subscriber> sub)
{
if (ListUtil.clearRef(_subs, sub) != null) {
// if we removed something, check to see if we just removed the last subscriber from
// our list; we also want to be sure that we're still active otherwise there's no need
// to notify our objmgr because we don't have one
if (--_scount == 0 && _omgr != null) {
_omgr.removedLastSubscriber(this, _deathWish);
}
}
}
/**
* Instructs this object to request to have a fork stuck in it when its last subscriber is
* removed.
*/
public void setDestroyOnLastSubscriberRemoved (boolean deathWish)
{
_deathWish = deathWish;
}
/**
* 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.
*
*
Note that the entity adding itself as a listener should have obtained the object
* reference by subscribing to it or should be acting on behalf of some other entity that
* subscribed to the object, and that it must be sure to remove itself from the
* listener list (via {@link #removeListener}) when it is done because unsubscribing from the
* object (done by whatever entity subscribed in the first place) is not guaranteed to result
* in the listeners added through that subscription being automatically removed (in most cases,
* they definitely will not be removed).
*
* @param listener the listener to be added.
*
* @see EventListener
* @see AttributeChangeListener
* @see SetListener
* @see OidListListener
*/
public void addListener (ChangeListener listener)
{
addListener(listener, false);
}
/**
* 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.
*
*
Note that the entity adding itself as a listener should have obtained the object
* reference by subscribing to it or should be acting on behalf of some other entity that
* subscribed to the object, and that it must be sure to remove itself from the
* listener list (via {@link #removeListener}) when it is done because unsubscribing from the
* object (done by whatever entity subscribed in the first place) is not guaranteed to result
* in the listeners added through that subscription being automatically removed (in most cases,
* they definitely will not be removed).
*
* @param listener the listener to be added.
* @param weak if true, retain only a weak reference to the listener (do not prevent the
* listener from being garbage-collected).
*
* @see EventListener
* @see AttributeChangeListener
* @see SetListener
* @see OidListListener
*/
public void addListener (ChangeListener listener, boolean weak)
{
// only add the listener if they're not already there
int idx = getListenerIndex(listener);
if (idx == -1) {
_listeners = ListUtil.add(_listeners,
weak ? new WeakReference