com.threerings.presents.client
Class ClientDObjectMgr

java.lang.Object
  extended by com.threerings.presents.client.ClientDObjectMgr
All Implemented Interfaces:
DObjectManager, Runnable

public class ClientDObjectMgr
extends Object
implements DObjectManager, Runnable

The client distributed object manager manages a set of proxy objects which mirror the distributed objects maintained on the server. Requests for modifications, etc. are forwarded to the server and events are dispatched from the server to this client for objects to which this client is subscribed.


Nested Class Summary
protected static class ClientDObjectMgr.FlushRecord
          Used to manage pending object flushes.
protected static class ClientDObjectMgr.ObjectAction<T extends DObject>
          The object action is used to queue up a subscribe or unsubscribe request.
protected static class ClientDObjectMgr.PendingRequest<T extends DObject>
          Represents a pending subscription request.
 
Field Summary
protected  Queue<Object> _actions
          Our primary dispatch queue.
protected  Client _client
          A reference to our client instance.
protected  Communicator _comm
          A reference to the communicator that sends and receives messages for this client.
protected  HashIntMap<DObject> _dead
          Objects that have been marked for death.
protected  HashMap<Class<?>,Long> _delays
          A mapping from distributed object class to flush delay.
protected  Interval _flusher
          Periodically calls flushObject(com.threerings.presents.dobj.DObject).
protected  HashIntMap<ClientDObjectMgr.FlushRecord> _flushes
          A set of objects waiting to be flushed.
protected  HashIntMap<DObject> _ocache
          All of the distributed objects that are active on this client.
protected  HashIntMap<ClientDObjectMgr.PendingRequest<?>> _penders
          Pending object subscriptions.
protected static int DUMP_OTABLE_KEYCODE
          The key code for our dump table debug hook (o).
protected static int DUMP_OTABLE_MODMASK
          The modifiers for our dump table debug hook (Alt+Shift).
protected static long FLUSH_INTERVAL
          Flush expired objects every 30 seconds.
 
Constructor Summary
ClientDObjectMgr(Communicator comm, Client client)
          Constructs a client distributed object manager.
 
Method Summary
 void cleanup()
          Called when the client is cleaned up due to having disconnected from the server.
protected  void dispatchAction(Object obj)
           
protected  void dispatchEvent(DEvent event)
          Called when a new event arrives from the server that should be dispatched to subscribers here on the client.
protected  void dispatchEvent(int remoteOid, DObject target, DEvent event)
          Dispatches an event on an already resolved target object.
protected
<T extends DObject>
void
doSubscribe(ClientDObjectMgr.ObjectAction<T> action)
          This is guaranteed to be invoked via the invoker and can safely do main thread type things like call back to the subscriber.
protected  void doUnsubscribe(int oid, Subscriber<?> target)
          This is guaranteed to be invoked via the invoker and can safely do main thread type things like call back to the subscriber.
protected  void flushObject(DObject obj)
          Flushes a distributed object subscription, issuing an unsubscribe request to the server.
protected  void flushObjects()
          Called periodically to flush any objects that have been lingering due to a previously enacted flush delay.
 boolean isManager(DObject object)
          Returns true if this distributed object manager is the authoritative manager for the specified distributed object, or false if we are only providing a proxy to the object.
protected  void notifyFailure(int oid, String message)
          Notifies the subscribers that had requested this object (for subscription) that it is not available.
 void postEvent(DEvent event)
          Posts a distributed object event into the system.
 void processMessage(Message msg)
          Called by the communicator when a message arrives from the network layer.
protected
<T extends DObject>
void
queueAction(int oid, Subscriber<T> target, boolean subscribe)
           
 void registerFlushDelay(Class<?> objclass, long delay)
          Registers an object flush delay.
protected
<T extends DObject>
void
registerObjectAndNotify(ObjectResponse<T> orsp)
          Registers this object in our proxy cache and notifies the subscribers that were waiting for subscription to this object.
 void removedLastSubscriber(DObject obj, boolean deathWish)
          When a distributed object removes its last subscriber, it will call this function to let the object manager know.
 void run()
          Invoked on the main client thread to process any newly arrived messages that we have waiting in our queue.
<T extends DObject>
void
subscribeToObject(int oid, Subscriber<T> target)
          Requests that the specified subscriber be subscribed to the object identified by the supplied object id.
<T extends DObject>
void
unsubscribeFromObject(int oid, Subscriber<T> target)
          Requests that the specified subscriber be unsubscribed from the object identified by the supplied object id.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_comm

protected Communicator _comm
A reference to the communicator that sends and receives messages for this client.


_client

protected Client _client
A reference to our client instance.


_flusher

protected Interval _flusher
Periodically calls flushObject(com.threerings.presents.dobj.DObject).


_actions

protected Queue<Object> _actions
Our primary dispatch queue.


_ocache

protected HashIntMap<DObject> _ocache
All of the distributed objects that are active on this client.


_dead

protected HashIntMap<DObject> _dead
Objects that have been marked for death.


_penders

protected HashIntMap<ClientDObjectMgr.PendingRequest<?>> _penders
Pending object subscriptions.


_delays

protected HashMap<Class<?>,Long> _delays
A mapping from distributed object class to flush delay.


_flushes

protected HashIntMap<ClientDObjectMgr.FlushRecord> _flushes
A set of objects waiting to be flushed.


DUMP_OTABLE_MODMASK

protected static int DUMP_OTABLE_MODMASK
The modifiers for our dump table debug hook (Alt+Shift).


DUMP_OTABLE_KEYCODE

protected static int DUMP_OTABLE_KEYCODE
The key code for our dump table debug hook (o).


FLUSH_INTERVAL

protected static final long FLUSH_INTERVAL
Flush expired objects every 30 seconds.

See Also:
Constant Field Values
Constructor Detail

ClientDObjectMgr

public ClientDObjectMgr(Communicator comm,
                        Client client)
Constructs a client distributed object manager.

Parameters:
comm - a communicator instance by which it can communicate with the server.
client - a reference to the client that is managing this whole communications and event dispatch business.
Method Detail

isManager

public boolean isManager(DObject object)
Description copied from interface: DObjectManager
Returns true if this distributed object manager is the authoritative manager for the specified distributed object, or false if we are only providing a proxy to the object.

Specified by:
isManager in interface DObjectManager

subscribeToObject

public <T extends DObject> void subscribeToObject(int oid,
                                                  Subscriber<T> target)
Description copied from interface: DObjectManager
Requests that the specified subscriber be subscribed to the object identified by the supplied object id. That subscriber will be notified when the object is available or if the subscription request failed.

Specified by:
subscribeToObject in interface DObjectManager
Parameters:
oid - The object id of the distributed object to which subscription is desired.
target - The subscriber to be subscribed.
See Also:
Subscriber.objectAvailable(T), Subscriber.requestFailed(int, com.threerings.presents.dobj.ObjectAccessException)

unsubscribeFromObject

public <T extends DObject> void unsubscribeFromObject(int oid,
                                                      Subscriber<T> target)
Description copied from interface: DObjectManager
Requests that the specified subscriber be unsubscribed from the object identified by the supplied object id.

Specified by:
unsubscribeFromObject in interface DObjectManager
Parameters:
oid - The object id of the distributed object from which unsubscription is desired.
target - The subscriber to be unsubscribed.

postEvent

public void postEvent(DEvent event)
Description copied from interface: DObjectManager
Posts a distributed object event into the system. Instead of requesting the modification of a distributed object attribute by calling the setter for that attribute on the object itself, an AttributeChangedEvent can be constructed and posted directly. This is true for all event types and is useful for situations where one doesn't have access to the object in question, but needs to affect some event.

This event will be forwarded to the ultimate manager of the object (on the client, this means it will be forwarded to the server) where it will be checked for validity and then applied to the object and dispatched to all its subscribers.

Specified by:
postEvent in interface DObjectManager
Parameters:
event - The event to be dispatched.

removedLastSubscriber

public void removedLastSubscriber(DObject obj,
                                  boolean deathWish)
Description copied from interface: DObjectManager
When a distributed object removes its last subscriber, it will call this function to let the object manager know. The manager might then choose to flush this object from the system or unregister from some upstream manager whose object it was proxying, for example.

Specified by:
removedLastSubscriber in interface DObjectManager

registerFlushDelay

public void registerFlushDelay(Class<?> objclass,
                               long delay)
Registers an object flush delay.

See Also:
Client.registerFlushDelay(java.lang.Class, long)

processMessage

public void processMessage(Message msg)
Called by the communicator when a message arrives from the network layer. We queue it up for processing and request some processing time on the main thread.


run

public void run()
Invoked on the main client thread to process any newly arrived messages that we have waiting in our queue.

Specified by:
run in interface Runnable

dispatchAction

protected void dispatchAction(Object obj)

cleanup

public void cleanup()
Called when the client is cleaned up due to having disconnected from the server.


queueAction

protected <T extends DObject> void queueAction(int oid,
                                               Subscriber<T> target,
                                               boolean subscribe)

dispatchEvent

protected void dispatchEvent(DEvent event)
Called when a new event arrives from the server that should be dispatched to subscribers here on the client.


dispatchEvent

protected void dispatchEvent(int remoteOid,
                             DObject target,
                             DEvent event)
Dispatches an event on an already resolved target object.

Parameters:
remoteOid - is specified explicitly because we will have already translated the event's target oid into our local object managers oid space if we're acting on behalf of the peer manager.

registerObjectAndNotify

protected <T extends DObject> void registerObjectAndNotify(ObjectResponse<T> orsp)
Registers this object in our proxy cache and notifies the subscribers that were waiting for subscription to this object.


notifyFailure

protected void notifyFailure(int oid,
                             String message)
Notifies the subscribers that had requested this object (for subscription) that it is not available.


doSubscribe

protected <T extends DObject> void doSubscribe(ClientDObjectMgr.ObjectAction<T> action)
This is guaranteed to be invoked via the invoker and can safely do main thread type things like call back to the subscriber.


doUnsubscribe

protected void doUnsubscribe(int oid,
                             Subscriber<?> target)
This is guaranteed to be invoked via the invoker and can safely do main thread type things like call back to the subscriber.


flushObject

protected void flushObject(DObject obj)
Flushes a distributed object subscription, issuing an unsubscribe request to the server.


flushObjects

protected void flushObjects()
Called periodically to flush any objects that have been lingering due to a previously enacted flush delay.