org.jdesktop.wonderland.client.comms
Class BaseConnection

java.lang.Object
  extended by org.jdesktop.wonderland.client.comms.BaseConnection
All Implemented Interfaces:
ClientConnection
Direct Known Subclasses:
CellCacheConnection, CellChannelConnection, CellEditChannelConnection, WonderlandSessionImpl.SessionInternalHandler

public abstract class BaseConnection
extends java.lang.Object
implements ClientConnection

A basic Wonderland Connection that can be extended to develop real connections.

Author:
jkaplan

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jdesktop.wonderland.client.comms.ClientConnection
ClientConnection.Status
 
Constructor Summary
BaseConnection()
           
 
Method Summary
protected  void addResponseListener(MessageID messageID, ResponseListener listener)
          Add a response listener
 void connect(WonderlandSession session)
          Connect this client to the given session.
 void connect(WonderlandSession session, java.util.Properties properties)
          Connect this client to the given session with the given properties.
 void connected(WonderlandSession session)
          Notify this client that it is connected to given session
 void disconnect()
          Disconnect from the current session
 void disconnected()
          Notify this client that it is disconnected from the current session
 WonderlandSession getSession()
          Get the session this client is connected to
 ClientConnection.Status getStatus()
          Get the status of this client
abstract  void handleMessage(Message message)
          Handle a message sent from the server.
 void messageReceived(Message message)
          When a message is received, do the basic processing needed to determine if it is a response to a message.
protected  boolean notifyResponseListener(ResponseMessage response)
          Notify any registered response listeners that a response to the given message was received.
protected  void send(Message message)
          Send a message to the server over the session channel on behalf of this client.
protected  void send(Message message, ResponseListener listener)
          Send a message to the server over the session channel and provide a listener.
protected  ResponseMessage sendAndWait(Message message)
          Send a message to the server and wait for the response.
protected  void setStatus(ClientConnection.Status status)
          Set the status
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.jdesktop.wonderland.client.comms.ClientConnection
getConnectionType
 

Constructor Detail

BaseConnection

public BaseConnection()
Method Detail

getSession

public WonderlandSession getSession()
Description copied from interface: ClientConnection
Get the session this client is connected to

Specified by:
getSession in interface ClientConnection
Returns:
the session this client is connected to, or null if the client is not connected to a session.

getStatus

public ClientConnection.Status getStatus()
Description copied from interface: ClientConnection
Get the status of this client

Specified by:
getStatus in interface ClientConnection
Returns:
the status of the client

setStatus

protected void setStatus(ClientConnection.Status status)
Set the status

Parameters:
status - the new status

connect

public void connect(WonderlandSession session)
             throws ConnectionFailureException
Connect this client to the given session. Identical to calling session.connect(this)

Parameters:
session - the session to connect to
Throws:
ConnectionFailureException - if there is a problem connecting

connect

public void connect(WonderlandSession session,
                    java.util.Properties properties)
             throws ConnectionFailureException
Connect this client to the given session with the given properties. Identical to calling session.connect(this, properties)

Parameters:
session - the session to connect to
properties - the properties to use while connecting
Throws:
ConnectionFailureException - if there is a problem connecting

connected

public void connected(WonderlandSession session)
Description copied from interface: ClientConnection
Notify this client that it is connected to given session

Specified by:
connected in interface ClientConnection
Parameters:
session - the session the client is now connected to

disconnect

public void disconnect()
Disconnect from the current session


disconnected

public void disconnected()
Description copied from interface: ClientConnection
Notify this client that it is disconnected from the current session

Specified by:
disconnected in interface ClientConnection

messageReceived

public void messageReceived(Message message)
When a message is received, do the basic processing needed to determine if it is a response to a message. If it is a response, notify the relevant listeners. If not, forward it on to the handleMessage() method.

Specified by:
messageReceived in interface ClientConnection
Parameters:
message - the message to handle

handleMessage

public abstract void handleMessage(Message message)
Handle a message sent from the server. This method is called when a message is received that is not a response message.

Parameters:
message - the message to handle

send

protected void send(Message message)
Send a message to the server over the session channel on behalf of this client. Identical to calling send(message, null).

Parameters:
message - the message to send
Throws:
MessageException - if there is an error getting the bytes for the message
java.lang.IllegalStateException - if this client is not in the CONNECTED state or the session this client is connected to is not in the CONNECTED state

send

protected void send(Message message,
                    ResponseListener listener)
Send a message to the server over the session channel and provide a listener. The message is sent on behalf of this client. The client must be successfully connected to this session in order for the send to work.

The given listener will be notified when a response is received. If the listener is not null, it is critical that the server guarantees that either a ResponseMessage or an ErrorMessage will be sent in response to this message, otherwise this will cause a memory leak.

Note that listeners are cleared as soon as the given client disconnects from the session, so responses are not guaranteed in this case.

Parameters:
message - the message to send
listener - the message response listener to notify when a response is received
Throws:
MessageException - if there is an error getting the bytes for the message
java.lang.IllegalStateException - if this client is not in the CONNECTED state or the session this client is connected to is not in the CONNECTED state

sendAndWait

protected ResponseMessage sendAndWait(Message message)
                               throws java.lang.InterruptedException
Send a message to the server and wait for the response. This method blocks until the response is received.

If the given client disconnects before a response is received from the server, this method should throw an InterruptedException.

Parameters:
message - the message to send
Returns:
the response to the given message
Throws:
MessageException - if there is an error getting the bytes for the message
java.lang.InterruptedException - if the wait is interrupted
MessageException - if there is an error getting the bytes for the message
java.lang.IllegalStateException - if this client is not in the CONNECTED state or the session this client is connected to is not in the CONNECTED state

addResponseListener

protected void addResponseListener(MessageID messageID,
                                   ResponseListener listener)
Add a response listener

Parameters:
messageID - the id of the message to listen for
listener - the response listener

notifyResponseListener

protected boolean notifyResponseListener(ResponseMessage response)
Notify any registered response listeners that a response to the given message was received.

Parameters:
response - the response message
Returns:
true if a listener was found to respond to this message, or false if not

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Open Wonderland - http://openwonderland.org