org.jdesktop.wonderland.server.comms
Class WonderlandSessionListener

java.lang.Object
  extended by org.jdesktop.wonderland.server.comms.WonderlandSessionListener
All Implemented Interfaces:
com.sun.sgs.app.ClientSessionListener, com.sun.sgs.app.ManagedObject, com.sun.sgs.app.ManagedObjectRemoval, java.io.Serializable

public class WonderlandSessionListener
extends java.lang.Object
implements com.sun.sgs.app.ClientSessionListener, com.sun.sgs.app.ManagedObject, com.sun.sgs.app.ManagedObjectRemoval, java.io.Serializable

This is the default session listener is used by Wonderland clients. Clients can select this listener by specifiying "wonderland_client" in the protocol selection message.

The WonderlandSessionListener supports an extensible set of handlers. Hanlders can be registered to service for specific message types. These messages will be reported for all sessions connected to the handler. Note that handlers are not dynamic: handlers must be registered before the first session is created. Handlers added after a session is created will not be used in this session.

Other convenience methods exist to send messages to all clients.

Author:
jkaplan
See Also:
Serialized Form

Constructor Summary
WonderlandSessionListener(com.sun.sgs.app.ClientSession session)
          Create a new instance of WonderlandSessionListener for the given session
 
Method Summary
 void disconnected(boolean forced)
          Called when the delegate session is disconnected
protected  java.lang.String getBindingName()
          Get the binding name this manager is bound to
static ClientConnectionHandler getClientHandler(ConnectionType clientType)
          Get the handler for the given type
static java.util.Set<ClientConnectionHandler> getClientHandlers()
          Get all client handlers
protected  ClientConnectionHandler getHandler(java.lang.Short clientID)
          Get a client handler by client ID
static WonderlandClientSender getSender(ConnectionType type)
          Get a sender that can be used to send messages to all clients of a given ConnectionType
protected  com.sun.sgs.app.ClientSession getSession()
          Get the session this listener represents.
 java.math.BigInteger getSessionID()
          Get the session id of this session
protected  WonderlandClientID getWonderlandClientID()
          Get the WonderlandClientID for this session
protected  void handleAttach(MessageID messageID, ConnectionType type, java.util.Properties properties)
          Handle an attach request
protected  void handleDetach(short clientID, boolean disconnect)
          Handle a detach request
static void initialize()
          Initialize the session listener
 void receivedMessage(java.nio.ByteBuffer data)
          Called when the listener receives a message.
static void registerClientHandler(ClientConnectionHandler handler)
          Register a handler that will handle connections from a particular WonderlandClient type.
 void removingObject()
          Clean up when a session is destroyed
protected  void sendError(MessageID messageID, short clientID, java.lang.String error)
          Send an error to the session
protected  void sendError(MessageID messageID, short clientID, java.lang.String error, java.lang.Throwable cause)
          Send an error to the session
protected  void sendError(MessageID messageID, short clientID, java.lang.Throwable cause)
          Send an error to the session
protected  void sendToSession(short clientID, Message message)
          Send a message to the session channel using the given client ID.
static void unregisterClientHandler(ClientConnectionHandler handler)
          Unregister a client handler that was previously registered
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WonderlandSessionListener

public WonderlandSessionListener(com.sun.sgs.app.ClientSession session)
Create a new instance of WonderlandSessionListener for the given session

Parameters:
session - the session connected to this listener
Method Detail

initialize

public static void initialize()
Initialize the session listener


removingObject

public void removingObject()
Clean up when a session is destroyed

Specified by:
removingObject in interface com.sun.sgs.app.ManagedObjectRemoval

getSessionID

public java.math.BigInteger getSessionID()
Get the session id of this session

Returns:
this session listener's unique identifier

receivedMessage

public void receivedMessage(java.nio.ByteBuffer data)
Called when the listener receives a message. If the wrapped session has not yet been defined, look for ProtocolSelectionMessages, otherwise simply forward the data to the delegate session

Specified by:
receivedMessage in interface com.sun.sgs.app.ClientSessionListener
Parameters:
data - the message data

disconnected

public void disconnected(boolean forced)
Called when the delegate session is disconnected

Specified by:
disconnected in interface com.sun.sgs.app.ClientSessionListener
Parameters:
forced - true if the disconnect was forced

registerClientHandler

public static void registerClientHandler(ClientConnectionHandler handler)
Register a handler that will handle connections from a particular WonderlandClient type.

Parameters:
handler - the handler to register
See Also:
CommsManager.registerClientHandler(ClientConnectionHandler)

unregisterClientHandler

public static void unregisterClientHandler(ClientConnectionHandler handler)
Unregister a client handler that was previously registered

Parameters:
handler - the handler to unregister

getClientHandler

public static ClientConnectionHandler getClientHandler(ConnectionType clientType)
Get the handler for the given type

Parameters:
clientType - the type of client to get a handler for
Returns:
the handler for the given type, or null if no handler is registered for the given type

getClientHandlers

public static java.util.Set<ClientConnectionHandler> getClientHandlers()
Get all client handlers

Returns:
the set of all client handlers

getSender

public static WonderlandClientSender getSender(ConnectionType type)
Get a sender that can be used to send messages to all clients of a given ConnectionType

Parameters:
type - the type of client to get a channel to
Returns:
a sender for sending to all clients of the given type
Throws:
java.lang.IllegalStateException - if no handler is registered for the given type
See Also:
CommsManager.getSender(ConnectionType)

getSession

protected com.sun.sgs.app.ClientSession getSession()
Get the session this listener represents.

Returns:
the session connected to this listener

getWonderlandClientID

protected WonderlandClientID getWonderlandClientID()
Get the WonderlandClientID for this session

Returns:
the WonderlandClientID

getHandler

protected ClientConnectionHandler getHandler(java.lang.Short clientID)
Get a client handler by client ID

Parameters:
clientID - the id of the client to get
Returns:
the handler for the given ID, or null if there is no handler for the given ID

handleAttach

protected void handleAttach(MessageID messageID,
                            ConnectionType type,
                            java.util.Properties properties)
Handle an attach request

Parameters:
messageID - the ID of the message to respond to
type - the type of client to attach
properties - the message properties

handleDetach

protected void handleDetach(short clientID,
                            boolean disconnect)
Handle a detach request

Parameters:
clientID - the id of the client to detach
disconnect - if true, this is a disconnect. In that case, the ClientSession will be removed from the channel automatically, so there is no need for us to do it explicitly.

getBindingName

protected java.lang.String getBindingName()
Get the binding name this manager is bound to

Returns:
the binding name

sendError

protected void sendError(MessageID messageID,
                         short clientID,
                         java.lang.String error)
Send an error to the session

Parameters:
messageID - the source message's ID
clientID - the client ID to send to
error - the error to send

sendError

protected void sendError(MessageID messageID,
                         short clientID,
                         java.lang.Throwable cause)
Send an error to the session

Parameters:
messageID - the source message's ID
clientID - the client ID to send to
cause - the cause of the error

sendError

protected void sendError(MessageID messageID,
                         short clientID,
                         java.lang.String error,
                         java.lang.Throwable cause)
Send an error to the session

Parameters:
messageID - the messageID of the original error
clientID - the client ID to send to
error - the error message
cause - the underlying exception

sendToSession

protected void sendToSession(short clientID,
                             Message message)
Send a message to the session channel using the given client ID.

Parameters:
clientID - the client ID to use when sending
message - the message to send
Throws:
java.lang.IllegalArgumentException - if there is an error serializing the given message


Open Wonderland - http://openwonderland.org