com.threerings.presents.server
Class ClientManager

java.lang.Object
  extended by com.threerings.presents.server.ClientManager
All Implemented Interfaces:
Lifecycle.BaseComponent, Lifecycle.Component, Lifecycle.InitComponent, Lifecycle.ShutdownComponent, ClientResolutionListener, ReportManager.Reporter

public class ClientManager
extends Object
implements ClientResolutionListener, ReportManager.Reporter, Lifecycle.Component

The client manager is responsible for managing client sessions which are slightly more than just connections. Clients persist in the absence of connections in case a user goes bye bye unintentionally and wants to reconnect and continue their session.

The client manager operates with thread safety because it is called both from the conmgr thread (to notify of connections showing up or going away) and from the dobjmgr thread (when clients are given the boot for application-defined reasons).


Nested Class Summary
static interface ClientManager.ClientObserver
          Used by entities that wish to track when clients initiate and end sessions on this server.
static interface ClientManager.ClientOp
          Used by applyToClient(com.threerings.util.Name, com.threerings.presents.server.ClientManager.ClientOp).
static interface ClientManager.DetailedClientObserver
          Methods for observing additional events in the session lifecycle.
 
Field Summary
protected  ObserverList<ClientManager.ClientObserver> _clobservers
          Tracks registered ClientManager.ClientObservers.
protected  Map<Connection,PresentsSession> _conmap
          A mapping from connections to session instances.
protected  ObserverList<ClientManager.DetailedClientObserver> _dclobservers
          Tracks registered ClientManager.DetailedClientObservers.
protected  List<SessionFactory> _factories
          Lets us know what sort of session classes to use.
protected  com.google.inject.Injector _injector
          Used to resolve dependencies in PresentsSession instances that we create.
protected  Map<Name,ClientObject> _objmap
          A mapping from usernames to client object instances.
protected  PresentsDObjectMgr _omgr
           
protected  Map<Name,ClientResolver> _penders
          A mapping of pending client resolvers.
protected  Map<Name,PresentsSession> _usermap
          A mapping from auth username to session instances.
protected static long SESSION_FLUSH_INTERVAL
          The frequency with which we check for expired sessions.
 
Constructor Summary
ClientManager(ReportManager repmgr, Lifecycle cycle)
          Constructs a client manager that will interact with the supplied connection manager.
 
Method Summary
 void addClientObserver(ClientManager.ClientObserver observer)
          Registers an observer that will be notified when clients start and end their sessions.
 void addSessionFactory(SessionFactory factory)
          Adds a session factory to the chain.
 void appendReport(StringBuilder report, long now, long sinceLast, boolean reset)
          Requests that this reporter append its report to the supplied string buffer.
 void applyToClient(Name username, ClientManager.ClientOp clop)
          Resolves the specified client, applies the supplied client operation to them and releases the client.
protected  void clearSession(PresentsSession session)
          Called by PresentsSession to let us know that we can clear it entirely out of the system.
 Iterable<ClientObject> clientObjects()
          Returns an iterable over all active client objects.
 void clientResolved(Name username, ClientObject clobj)
          Called when resolution completed successfully.
protected  void clientSessionDidEnd(PresentsSession session)
          Called by PresentsSession when it has ended its session.
protected  void clientSessionDidStart(PresentsSession session)
          Called by PresentsSession when it has started its session.
protected  void clientSessionWillEnd(PresentsSession session)
          Called by PresentsSession when it is about to end its session.
 void connectionClosed(Connection conn)
          Called by the connection manager to let us know when a connection has been closed.
 void connectionEstablished(PresentsConnection conn, Name authname, AuthRequest req, AuthResponse rsp)
          Called by the connection manager to let us know when a new connection has been established.
 void connectionFailed(Connection conn, IOException fault)
          Called by the connection manager to let us know when a connection has failed.
 Iterator<ClientObject> enumerateClientObjects()
          Enumerates all active client objects.
protected  void flushSessions()
          Called once per minute to check for sessions that have been disconnected too long and forcibly end their sessions.
 PresentsSession getClient(Name authUsername)
          Returns the client instance that manages the client session for the specified authentication username or null if that client is not currently connected to the server.
 int getClientCount()
          Returns the number of client sessions (some may be disconnected).
 ClientObject getClientObject(Name username)
          Returns the client object associated with the specified username.
 int getConnectionCount()
          Returns the number of connected clients.
 int getOutstandingResolutionCount()
          Return the number of client resolutions are currently happening.
 List<PresentsSession> getSessionsForAddress(byte[] addr)
          Returns all sessions logged in from the given IP in the form returned from InetAddress.getAddress().
 void init()
           
 void releaseClientObject(Name username)
          Releases a client object that was obtained via a call to resolveClientObject(com.threerings.util.Name, com.threerings.presents.server.ClientResolutionListener).
 void removeClientObserver(ClientManager.ClientObserver observer)
          Removes an observer previously registered with addClientObserver(com.threerings.presents.server.ClientManager.ClientObserver).
protected  boolean renameClientObject(Name oldname, Name newname)
          Renames a currently connected client from oldname to newname.
 void resolutionFailed(Name username, Exception reason)
          Called when resolution fails.
 void resolveClientObject(Name username, ClientResolutionListener listener)
          Requests that the client object for the specified user be resolved.
 void setDefaultSessionFactory(SessionFactory factory)
          Configures the default factory for creating PresentsSession and ClientResolver classes for authenticated client connections.
 void setInjector(com.google.inject.Injector injector)
          Configures the injector we'll use to resolve dependencies for PresentsSession instances.
 void shutdown()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_injector

protected com.google.inject.Injector _injector
Used to resolve dependencies in PresentsSession instances that we create.


_usermap

protected Map<Name,PresentsSession> _usermap
A mapping from auth username to session instances.


_conmap

protected Map<Connection,PresentsSession> _conmap
A mapping from connections to session instances.


_objmap

protected Map<Name,ClientObject> _objmap
A mapping from usernames to client object instances.


_penders

protected Map<Name,ClientResolver> _penders
A mapping of pending client resolvers.


_factories

protected List<SessionFactory> _factories
Lets us know what sort of session classes to use.


_clobservers

protected ObserverList<ClientManager.ClientObserver> _clobservers
Tracks registered ClientManager.ClientObservers.


_dclobservers

protected ObserverList<ClientManager.DetailedClientObserver> _dclobservers
Tracks registered ClientManager.DetailedClientObservers.


_omgr

@Inject
protected PresentsDObjectMgr _omgr

SESSION_FLUSH_INTERVAL

protected static final long SESSION_FLUSH_INTERVAL
The frequency with which we check for expired sessions.

See Also:
Constant Field Values
Constructor Detail

ClientManager

@Inject
public ClientManager(ReportManager repmgr,
                            Lifecycle cycle)
Constructs a client manager that will interact with the supplied connection manager.

Method Detail

setInjector

public void setInjector(com.google.inject.Injector injector)
Configures the injector we'll use to resolve dependencies for PresentsSession instances.


setDefaultSessionFactory

public void setDefaultSessionFactory(SessionFactory factory)
Configures the default factory for creating PresentsSession and ClientResolver classes for authenticated client connections. All factories added via addSessionFactory(com.threerings.presents.server.SessionFactory) will be offered a chance to handle sessions before this factory of last resort.


addSessionFactory

public void addSessionFactory(SessionFactory factory)
Adds a session factory to the chain. This factory will be offered a chance to resolve sessions before passing the buck to the next factory in the chain.


getOutstandingResolutionCount

public int getOutstandingResolutionCount()
Return the number of client resolutions are currently happening.


getClientCount

public int getClientCount()
Returns the number of client sessions (some may be disconnected).


getSessionsForAddress

public List<PresentsSession> getSessionsForAddress(byte[] addr)
Returns all sessions logged in from the given IP in the form returned from InetAddress.getAddress().


getConnectionCount

public int getConnectionCount()
Returns the number of connected clients.


clientObjects

public Iterable<ClientObject> clientObjects()
Returns an iterable over all active client objects.


enumerateClientObjects

public Iterator<ClientObject> enumerateClientObjects()
Enumerates all active client objects.


addClientObserver

public void addClientObserver(ClientManager.ClientObserver observer)
Registers an observer that will be notified when clients start and end their sessions.


removeClientObserver

public void removeClientObserver(ClientManager.ClientObserver observer)
Removes an observer previously registered with addClientObserver(com.threerings.presents.server.ClientManager.ClientObserver).


getClient

public PresentsSession getClient(Name authUsername)
Returns the client instance that manages the client session for the specified authentication username or null if that client is not currently connected to the server.


getClientObject

public ClientObject getClientObject(Name username)
Returns the client object associated with the specified username. This will return null unless the client object is resolved for some reason (like they are logged on).


applyToClient

public void applyToClient(Name username,
                          ClientManager.ClientOp clop)
Resolves the specified client, applies the supplied client operation to them and releases the client.


resolveClientObject

public void resolveClientObject(Name username,
                                ClientResolutionListener listener)
Requests that the client object for the specified user be resolved. Note: this must be paired with a call to releaseClientObject(com.threerings.util.Name) when the caller is finished with the client object.


releaseClientObject

public void releaseClientObject(Name username)
Releases a client object that was obtained via a call to resolveClientObject(com.threerings.util.Name, com.threerings.presents.server.ClientResolutionListener). If this caller is the last reference, the object will be flushed and destroyed.


init

public void init()
Specified by:
init in interface Lifecycle.InitComponent

shutdown

public void shutdown()
Specified by:
shutdown in interface Lifecycle.ShutdownComponent

renameClientObject

protected boolean renameClientObject(Name oldname,
                                     Name newname)
Renames a currently connected client from oldname to newname.

Returns:
true if the client was found and renamed.

clientResolved

public void clientResolved(Name username,
                           ClientObject clobj)
Description copied from interface: ClientResolutionListener
Called when resolution completed successfully.

Specified by:
clientResolved in interface ClientResolutionListener

resolutionFailed

public void resolutionFailed(Name username,
                             Exception reason)
Description copied from interface: ClientResolutionListener
Called when resolution fails.

Specified by:
resolutionFailed in interface ClientResolutionListener

connectionEstablished

public void connectionEstablished(PresentsConnection conn,
                                  Name authname,
                                  AuthRequest req,
                                  AuthResponse rsp)
Called by the connection manager to let us know when a new connection has been established.


connectionFailed

public void connectionFailed(Connection conn,
                             IOException fault)
Called by the connection manager to let us know when a connection has failed.


connectionClosed

public void connectionClosed(Connection conn)
Called by the connection manager to let us know when a connection has been closed.


appendReport

public void appendReport(StringBuilder report,
                         long now,
                         long sinceLast,
                         boolean reset)
Description copied from interface: ReportManager.Reporter
Requests that this reporter append its report to the supplied string buffer.

Specified by:
appendReport in interface ReportManager.Reporter
Parameters:
report - the string buffer to which the report text should be appended.
now - the time at which the report generation began, in epoch millis.
sinceLast - number of milliseconds since the last time we generated a report.
reset - if true, all accumulating stats should be reset, if false they should be allowed to continue to accumulate.

clientSessionDidStart

protected void clientSessionDidStart(PresentsSession session)
Called by PresentsSession when it has started its session.


clientSessionWillEnd

protected void clientSessionWillEnd(PresentsSession session)
Called by PresentsSession when it is about to end its session.


clientSessionDidEnd

protected void clientSessionDidEnd(PresentsSession session)
Called by PresentsSession when it has ended its session.


clearSession

protected void clearSession(PresentsSession session)
Called by PresentsSession to let us know that we can clear it entirely out of the system.


flushSessions

protected void flushSessions()
Called once per minute to check for sessions that have been disconnected too long and forcibly end their sessions.