org.xsocket.connection
Class ConnectionUtils

Package class diagram package ConnectionUtils
java.lang.Object
  extended by org.xsocket.connection.ConnectionUtils

public final class ConnectionUtils
extends Object

utility class

Author:
grro@xsocket.org

Field Summary
static String DEFAULT_DOMAIN
           
static String SERVER_TRHREAD_PREFIX
           
 
Method Summary
static String getImplementationDate()
          get the implementation date
static String getImplementationVersion()
          get the implementation version
static String getVersionInfo()
          Deprecated. use getImplementationVersion() instead
static
<T> Map<Class,T>
newMapCache(int maxSize)
          creates a thread-safe new bound cache
static ObjectName registerMBean(IConnectionPool pool)
          creates and registers a mbean for the given connection pool on the platform MBeanServer
static ObjectName registerMBean(IConnectionPool pool, String domain)
          creates and registers a mbean for the given connection pool on the platform MBeanServer under the given domain name
static ObjectName registerMBean(IConnectionPool pool, String domain, MBeanServer mbeanServer)
          creates and registers a mbean for the given pool on the given MBeanServer under the given domain name
static ObjectName registerMBean(IServer server)
          creates and registers a mbean for the given server on the platform MBeanServer
static ObjectName registerMBean(IServer server, String domain)
          creates and registers a mbean for the given server on the platform MBeanServer under the given domain name
static ObjectName registerMBean(IServer server, String domain, MBeanServer mbeanServer)
          creates and registers a mbean for the given server on the given MBeanServer under the given domain name
static void start(IServer server)
          starts the given server within a dedicated thread.
static void start(IServer server, int timeoutSec)
          starts the given server within a dedicated thread.
static int validateSufficientDatasizeByIntLengthField(INonBlockingConnection connection)
          validate, based on a leading int length field.
static int validateSufficientDatasizeByIntLengthField(INonBlockingConnection connection, boolean removeLengthField)
          validate, based on a leading int length field, that enough data (getNumberOfAvailableBytes() >= length) is available.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_DOMAIN

public static final String DEFAULT_DOMAIN
See Also:
Constant Field Values

SERVER_TRHREAD_PREFIX

public static final String SERVER_TRHREAD_PREFIX
See Also:
Constant Field Values
Method Detail

validateSufficientDatasizeByIntLengthField

public static int validateSufficientDatasizeByIntLengthField(INonBlockingConnection connection)
                                                      throws IOException,
                                                             BufferUnderflowException
validate, based on a leading int length field. The length field will be removed

Parameters:
connection - the connection
Returns:
the length
Throws:
IOException - if an exception occurs
BufferUnderflowException - if not enough data is available

validateSufficientDatasizeByIntLengthField

public static int validateSufficientDatasizeByIntLengthField(INonBlockingConnection connection,
                                                             boolean removeLengthField)
                                                      throws IOException,
                                                             BufferUnderflowException
validate, based on a leading int length field, that enough data (getNumberOfAvailableBytes() >= length) is available. If not, an BufferUnderflowException will been thrown. Example:
 //client
 connection.setAutoflush(false);  // avoid immediate write
 ...
 connection.markWritePosition();  // mark current position
 connection.write((int) 0);       // write "emtpy" length field
  
 // write and count written size
 int written = connection.write(CMD_PUT);
 written += ...
  
 connection.resetToWriteMark();  // return to length field position
 connection.write(written);      // and update it
 connection.flush(); // flush (marker will be removed implicit)
 ...
 
 
 // server
 class MyHandler implements IDataHandler {
    ...
    public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException {
       int length = ConnectionUtils.validateSufficientDatasizeByIntLengthField(connection);
       
       // enough data (BufferUnderflowException hasn`t been thrown)
       byte cmd = connection.readByte();
       ...
    }
  }      
 

Parameters:
connection - the connection
removeLengthField - true, if length field should be removed
Returns:
the length
Throws:
IOException - if an exception occurs
BufferUnderflowException - if not enough data is available

start

public static void start(IServer server)
                  throws SocketTimeoutException
starts the given server within a dedicated thread. This method blocks until the server is open. If the server hasn't been started within 60 sec a timeout exception will been thrown.

Parameters:
server - the server to start
Throws:
SocketTimeoutException - is the timeout has been reached

start

public static void start(IServer server,
                         int timeoutSec)
                  throws SocketTimeoutException
starts the given server within a dedicated thread. This method blocks until the server is open.

Parameters:
server - the server to start
timeoutSec - the maximum time to wait
Throws:
SocketTimeoutException - is the timeout has been reached

registerMBean

public static ObjectName registerMBean(IServer server)
                                throws JMException
creates and registers a mbean for the given server on the platform MBeanServer

Parameters:
server - the server to register
Returns:
the objectName
Throws:
JMException - if an jmx exception occurs

registerMBean

public static ObjectName registerMBean(IServer server,
                                       String domain)
                                throws JMException
creates and registers a mbean for the given server on the platform MBeanServer under the given domain name

Parameters:
server - the server to register
domain - the domain name to use
Returns:
the objectName
Throws:
JMException - if an jmx exception occurs

registerMBean

public static ObjectName registerMBean(IServer server,
                                       String domain,
                                       MBeanServer mbeanServer)
creates and registers a mbean for the given server on the given MBeanServer under the given domain name

Parameters:
mbeanServer - the mbean server to use
server - the server to register
domain - the domain name to use
Returns:
the objectName
Throws:
JMException - if an jmx exception occurs

registerMBean

public static ObjectName registerMBean(IConnectionPool pool)
                                throws JMException
creates and registers a mbean for the given connection pool on the platform MBeanServer

Parameters:
pool - the pool to register
Returns:
the objectName
Throws:
JMException - if an jmx exception occurs

registerMBean

public static ObjectName registerMBean(IConnectionPool pool,
                                       String domain)
                                throws JMException
creates and registers a mbean for the given connection pool on the platform MBeanServer under the given domain name

Parameters:
pool - the pool to register
domain - the domain name to use
Returns:
the objectName
Throws:
JMException - if an jmx exception occurs

registerMBean

public static ObjectName registerMBean(IConnectionPool pool,
                                       String domain,
                                       MBeanServer mbeanServer)
                                throws JMException
creates and registers a mbean for the given pool on the given MBeanServer under the given domain name

Parameters:
mbeanServer - the mbean server to use
pool - the pool to register
domain - the domain name to use
Returns:
the objectName
Throws:
JMException - if an jmx exception occurs

getVersionInfo

public static String getVersionInfo()
Deprecated. use getImplementationVersion() instead


getImplementationVersion

public static String getImplementationVersion()
get the implementation version

Returns:
the implementation version

getImplementationDate

public static String getImplementationDate()
get the implementation date

Returns:
the implementation date

newMapCache

public static <T> Map<Class,T> newMapCache(int maxSize)
creates a thread-safe new bound cache

Type Parameters:
T - the map value type
Parameters:
maxSize - the max size of the cache
Returns:
the new map cache


Copyright 2008 xSocket.org