com.sun.sgs.impl.protocol.simple
Class SimpleSgsProtocolImpl

java.lang.Object
  extended by com.sun.sgs.impl.protocol.simple.SimpleSgsProtocolImpl
All Implemented Interfaces:
SessionProtocol, Closeable, Channel
Direct Known Subclasses:
SimpleSgsRelocationProtocolImpl

public class SimpleSgsProtocolImpl
extends Object
implements SessionProtocol

Implements the protocol specified in SimpleSgsProtocol. The implementation uses a wrapper channel, AsynchronousMessageChannel, that reads and writes complete messages by framing messages with a 2-byte message length, and masking (and re-issuing) partial I/O operations. Also enforces a fixed buffer size when reading.


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.sun.sgs.protocol.SessionProtocol
SessionProtocol.DisconnectReason
 
Field Summary
protected  SimpleSgsProtocolAcceptor acceptor
          This protocol's acceptor.
protected  Set<Delivery> deliverySet
          The set of supported delivery requirements.
protected  ProtocolListener listener
          The protocol listener.
protected  LoggerWrapper logger
          The logger for this instance.
protected  SessionProtocolHandler protocolHandler
          The protocol handler.
protected  byte[] reconnectKey
          The reconnect key.
 
Constructor Summary
(package private) SimpleSgsProtocolImpl(ProtocolListener listener, SimpleSgsProtocolAcceptor acceptor, AsynchronousByteChannel byteChannel, int readBufferSize)
          Creates a new instance of this class.
protected SimpleSgsProtocolImpl(ProtocolListener listener, SimpleSgsProtocolAcceptor acceptor, AsynchronousByteChannel byteChannel, int readBufferSize, LoggerWrapper logger)
          Constructs a new instance of this class.
 
Method Summary
 void channelJoin(String name, BigInteger channelId, Delivery delivery)
          Notifies the associated client that it is joined to the channel with the specified name and channelId.
 void channelLeave(BigInteger channelId)
          Notifies the associated client that it is no longer a member of the channel with the specified channelId.
 void channelMessage(BigInteger channelId, ByteBuffer message, Delivery delivery)
          Sends the associated client the specified channel message for the channel with the specified channelId in a manner that satisfies the specified delivery guarantee.
 void close()
          
 void disconnect(SessionProtocol.DisconnectReason reason)
          Disconnects the associated session for the specified reason.
 Set<Delivery> getDeliveries()
          Returns a set containing the delivery guarantees supported by this protocol.
protected  Identity getIdentity()
          Returns the associated identity, or null if the client has not yet authenticated.
 int getMaxMessageLength()
          Returns the maximum length, in bytes, of the buffers passed as the message parameters to the sessionMessage and channelMessage methods.
protected  byte getProtocolVersion()
          Returns the SimpleSgsProtocol version supported by this implementation.
protected  void handleMessageReceived(byte opcode, MessageBuffer msg)
          Processes the received message.
 boolean isOpen()
          
protected  void loginSuccess()
          Notifies the associated client that the previous login attempt was successful.
protected  void monitorDisconnection()
          Monitors the client's disconnection and closes this instance's underlying connection if the client hasn't closed the connection in a timely fashion.
protected  void readNow()
          Resumes reading from the underlying connection.
protected  void scheduleRead()
          Schedules an asynchronous task to resume reading.
 void sessionMessage(ByteBuffer message, Delivery delivery)
          Sends the associated client the specified message in a manner that satisfies the specified delivery guarantee.
 String toString()
          
protected  void write(ByteBuffer buf)
          Writes a message to the underlying connection if login has been handled, otherwise enqueues the message to be sent when the login has not yet been handled.
protected  void writeBuffer(ByteBuffer buf, Delivery delivery)
          Writes the specified buffer, satisfying the specified delivery requirement.
protected  void writeNow(ByteBuffer message, boolean flush)
          Writes a message to the underlying connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

protocolHandler

protected volatile SessionProtocolHandler protocolHandler
The protocol handler.


acceptor

protected final SimpleSgsProtocolAcceptor acceptor
This protocol's acceptor.


logger

protected final LoggerWrapper logger
The logger for this instance.


listener

protected final ProtocolListener listener
The protocol listener.


reconnectKey

protected final byte[] reconnectKey
The reconnect key.


deliverySet

protected final Set<Delivery> deliverySet
The set of supported delivery requirements.

Constructor Detail

SimpleSgsProtocolImpl

SimpleSgsProtocolImpl(ProtocolListener listener,
                      SimpleSgsProtocolAcceptor acceptor,
                      AsynchronousByteChannel byteChannel,
                      int readBufferSize)
Creates a new instance of this class.

Parameters:
listener - a protocol listener
acceptor - the SimpleSgsProtocol acceptor
byteChannel - a byte channel for the underlying connection
readBufferSize - the read buffer size

SimpleSgsProtocolImpl

protected SimpleSgsProtocolImpl(ProtocolListener listener,
                                SimpleSgsProtocolAcceptor acceptor,
                                AsynchronousByteChannel byteChannel,
                                int readBufferSize,
                                LoggerWrapper logger)
Constructs a new instance of this class. The subclass should invoke scheduleRead after constructing the instance to commence reading.

Parameters:
listener - a protocol listener
acceptor - the SimpleSgsProtocol acceptor
byteChannel - a byte channel for the underlying connection
readBufferSize - the read buffer size
logger - a logger for this instance
Method Detail

getProtocolVersion

protected byte getProtocolVersion()
Returns the SimpleSgsProtocol version supported by this implementation.

Returns:
the SimpleSgsProtocol version supported by this implementation

getIdentity

protected Identity getIdentity()
Returns the associated identity, or null if the client has not yet authenticated.

Returns:
the associated identity, or null

getDeliveries

public Set<Delivery> getDeliveries()
Returns a set containing the delivery guarantees supported by this protocol. The returned set is serializable.

Specified by:
getDeliveries in interface SessionProtocol
Returns:
a set containing the supported delivery guarantees

getMaxMessageLength

public int getMaxMessageLength()
Returns the maximum length, in bytes, of the buffers passed as the message parameters to the sessionMessage and channelMessage methods.

Specified by:
getMaxMessageLength in interface SessionProtocol
Returns:
the maximum message length

sessionMessage

public void sessionMessage(ByteBuffer message,
                           Delivery delivery)
Sends the associated client the specified message in a manner that satisfies the specified delivery guarantee.

When possible, the message should be delivered using the most efficient means (e.g., protocol and transport) to satisfy the delivery guarantee. However, a stronger delivery guarantee may be used to deliver the message if this protocol only supports stronger delivery guarantees. If this protocol is not able to satisfy the specified delivery guarantee (e.g., only supports weaker delivery guarantees than the one specified), then a DeliveryNotSupportedException will be thrown.

The ByteBuffer is not modified and may be reused immediately after this method returns. Changes made to the buffer after this method returns will have no effect on the message sent to the client by this invocation.

Specified by:
sessionMessage in interface SessionProtocol
Parameters:
message - a message
delivery - the delivery guarantee

channelJoin

public void channelJoin(String name,
                        BigInteger channelId,
                        Delivery delivery)
Notifies the associated client that it is joined to the channel with the specified name and channelId. This notification to the client must be delivered reliably.

Specified by:
channelJoin in interface SessionProtocol
Parameters:
name - a channel name
channelId - the channel's ID
delivery - the channel's delivery guarantee

channelLeave

public void channelLeave(BigInteger channelId)
Notifies the associated client that it is no longer a member of the channel with the specified channelId. This notification to the client must be delivered reliably.

Specified by:
channelLeave in interface SessionProtocol
Parameters:
channelId - a channel ID

channelMessage

public void channelMessage(BigInteger channelId,
                           ByteBuffer message,
                           Delivery delivery)
Sends the associated client the specified channel message for the channel with the specified channelId in a manner that satisfies the specified delivery guarantee.

When possible, the message should be delivered using the most efficient means (e.g., protocol and transport) to satisfy the delivery guarantee. However, a stronger delivery guarantee may be used to deliver the message if this protocol only supports stronger delivery guarantees. If this protocol is not able to satisfy the specified delivery guarantee (e.g., only supports weaker delivery guarantees than the one specified), then a DeliveryNotSupportedException will be thrown.

The ByteBuffer is not modified and may be reused immediately after this method returns. Changes made to the buffer after this method returns will have no effect on the message sent to the client by this invocation.

This implementation invokes the protected method writeBuffer with the channel protocol message (a ByteBuffer) and the specified delivery requirement. A subclass can override the writeBuffer method if it supports other delivery guarantees and can make use of alternate transports for those other delivery requirements.

Specified by:
channelMessage in interface SessionProtocol
Parameters:
channelId - a channel ID
message - a channel message
delivery - the channel's delivery guarantee

disconnect

public void disconnect(SessionProtocol.DisconnectReason reason)
                throws IOException
Disconnects the associated session for the specified reason. The protocol may send a message to the associated client indicating the reason for the disconnection, or the protocol may close the connection immediately. Any underlying connection(s) should be closed in a timely fashion.

Specified by:
disconnect in interface SessionProtocol
Parameters:
reason - the reason for disconnection
Throws:
IOException - if an I/O error occurs

loginSuccess

protected void loginSuccess()
Notifies the associated client that the previous login attempt was successful.


isOpen

public boolean isOpen()

Specified by:
isOpen in interface Channel

close

public void close()

Specified by:
close in interface Closeable
Specified by:
close in interface Channel

toString

public String toString()

Overrides:
toString in class Object

scheduleRead

protected final void scheduleRead()
Schedules an asynchronous task to resume reading.


readNow

protected final void readNow()
Resumes reading from the underlying connection.


write

protected final void write(ByteBuffer buf)
Writes a message to the underlying connection if login has been handled, otherwise enqueues the message to be sent when the login has not yet been handled.

Parameters:
buf - a buffer containing a complete protocol message

writeNow

protected final void writeNow(ByteBuffer message,
                              boolean flush)
Writes a message to the underlying connection.

Parameters:
message - a buffer containing a complete protocol message
flush - if true, then set the loginHandled flag to true and flush the message queue

writeBuffer

protected void writeBuffer(ByteBuffer buf,
                           Delivery delivery)
Writes the specified buffer, satisfying the specified delivery requirement.

This implementation writes the buffer reliably, because this protocol only supports reliable delivery.

A subclass can override the writeBuffer method if it supports other delivery guarantees and can make use of alternate transports for those other delivery requirements.

Parameters:
buf - a byte buffer containing a protocol message
delivery - a delivery requirement

handleMessageReceived

protected void handleMessageReceived(byte opcode,
                                     MessageBuffer msg)
Processes the received message. This implementation processes opcodes for SimpleSgsProtocol version 0x04. A subclass can override this implementation to process additional opcodes, and then delegate to this implementation to process the version 0x04 opcodes.

Parameters:
opcode - the message opcode
msg - a message buffer containing the entire message, but with the position advanced to the payload (just after the opcode)

monitorDisconnection

protected void monitorDisconnection()
Monitors the client's disconnection and closes this instance's underlying connection if the client hasn't closed the connection in a timely fashion.


RedDwarf, Version 0.10.1
2010-03-14 10:56:12

Copyright © 2010 The RedDwarf Authors. All rights reserved
Copyright © 2007-2010 Sun Microsystems, Inc. All rights reserved