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 ByteBuffer[] copy(ByteBuffer[] buffers)
          (deep) copy of the byte buffer array
static String getImplementationDate()
          get the implementation date
static String getImplementationVersion()
          get the implementation version
static boolean isDispatcherThread()
          returns if current thread is dispatcher thread
static boolean matchVersion(String currentVersion, String requiredVersion)
          checks if the current version matchs with the required version by using the maven version style
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 IBlockingConnection synchronizedConnection(IBlockingConnection con)
          Returns a synchronized (thread-safe) connection backed by the specified connection.
static INonBlockingConnection synchronizedConnection(INonBlockingConnection con)
          Returns a synchronized (thread-safe) connection backed by the specified connection.
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

copy

public static ByteBuffer[] copy(ByteBuffer[] buffers)
(deep) copy of the byte buffer array

Parameters:
buffers - the byte buffer array
Returns:
the copy

synchronizedConnection

public static INonBlockingConnection synchronizedConnection(INonBlockingConnection con)
Returns a synchronized (thread-safe) connection backed by the specified connection. All methods of the wapper are synchronized based on the underyling connection.

Parameters:
con - the connection to be "wrapped" in a synchronized connection
Returns:
the synchronized (thread-safe) connection

synchronizedConnection

public static IBlockingConnection synchronizedConnection(IBlockingConnection con)
Returns a synchronized (thread-safe) connection backed by the specified connection. All methods of the wapper are synchronized based on the underyling connection.

Parameters:
con - the connection to be "wrapped" in a synchronized connection
Returns:
the synchronized (thread-safe) connection

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

matchVersion

public static boolean matchVersion(String currentVersion,
                                   String requiredVersion)
checks if the current version matchs with the required version by using the maven version style

Parameters:
currentVersion - the current version
requiredVersion - the required version
Returns:
true if the version matchs

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

isDispatcherThread

public static boolean isDispatcherThread()
returns if current thread is dispatcher thread

Returns:
true, if current thread is a dispatcher thread


Copyright 2010 xSocket.org