org.abstracthorizon.danube.service.server
Class ServerSocketChannelService

java.lang.Object
  extended by org.abstracthorizon.danube.service.Service
      extended by org.abstracthorizon.danube.service.server.ServerSocketChannelService
All Implemented Interfaces:
Runnable

public class ServerSocketChannelService
extends Service

This is server socket service. This service accepts connections from a server socket, creates new ServerSocketChannelService.ConnectionHandlerThread objects and executes them in a given Executor.

Author:
Daniel Sendula

Nested Class Summary
 class ServerSocketChannelService.ConnectionHandlerThread
          This class is executed in under the given executor.
 
Field Summary
protected  Set<ServerSocketChannelService.ConnectionHandlerThread> activeConnections
          Set of active connections
protected  ConnectionHandler connectionHandler
          Connection handler new connection to be handed with
protected  Executor executor
          Executor (thread pool) to be used
protected  int graceFinishPeriod
          Grace period for connections to finish after service state changes to STOPPING
protected  int newSocketTimeout
          New socket timeout
protected  int port
          Port this service is going to listen on
protected  ServerSocketChannel serverSocketChannel
          Reference to the server socket
protected  int serverSocketTimeout
          Server socket timeout
 
Fields inherited from class org.abstracthorizon.danube.service.Service
DESTROYED, INITIALIZED, listeners, logger, name, NOT_INITIALIZED, RUNNING, SERVICE_STATE_NAMES, STARTED, STOPPED, STOPPING, stopService
 
Constructor Summary
ServerSocketChannelService()
          Default constructor
 
Method Summary
 void create()
          Creates the socket
protected  ServerSocketChannel createServerSocket()
          Creates server socket
protected  Connection createSocketConnection(SocketChannel socketChannel)
          Creates new socket connection
 void destroy()
          Closes the server socket
 ConnectionHandler getConnectionHandler()
          Returns connection handler connections are handed to.
 Executor getExecutor()
          Return the executor which is used or connections to be handled with
 int getGraceFinishPeriod()
          Returns grace finish period
 int getNewSocketTimeout()
          Returns new socket timeout
 int getPort()
          Returns the port service is expecting connections on
 int getServerSocketTimeout()
          Returns initial socket timeout
protected  void processConnection(SocketChannel socket)
          Creates socket connection and new instance of ServerSocketChannelService.ConnectionHandlerThread to process socket under the given executor
 void run()
          Accepts connections from server socket and calls #processConnection(Socket) method
 void setConnectionHandler(ConnectionHandler connectionHandler)
          Sets connection handler
 void setExecutor(Executor executor)
          Sets the executor for connections to be handled with
 void setGraceFinishPeriod(int graceFinishPeriod)
          Sets grace finish period
 void setNewSocketTimeout(int socketTimeout)
          Sets new socket timeout
 void setPort(int port)
          Sets the port.
 void setServerSocketTimeout(int socketTimeout)
          Sets initial socket timeout
 void start()
          Starts the service
 void stop()
          Stops the service.
 
Methods inherited from class org.abstracthorizon.danube.service.Service
addListener, changeState, getName, getState, getStateName, removeListener, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serverSocketChannel

protected ServerSocketChannel serverSocketChannel
Reference to the server socket


port

protected int port
Port this service is going to listen on


serverSocketTimeout

protected int serverSocketTimeout
Server socket timeout


newSocketTimeout

protected int newSocketTimeout
New socket timeout


executor

protected Executor executor
Executor (thread pool) to be used


activeConnections

protected Set<ServerSocketChannelService.ConnectionHandlerThread> activeConnections
Set of active connections


connectionHandler

protected ConnectionHandler connectionHandler
Connection handler new connection to be handed with


graceFinishPeriod

protected int graceFinishPeriod
Grace period for connections to finish after service state changes to STOPPING

Constructor Detail

ServerSocketChannelService

public ServerSocketChannelService()
Default constructor

Method Detail

getPort

public int getPort()
Returns the port service is expecting connections on

Returns:
the port

setPort

public void setPort(int port)
Sets the port. It has to be set before create() method is called.

Parameters:
port - the port

getServerSocketTimeout

public int getServerSocketTimeout()
Returns initial socket timeout

Returns:
initial socket timeout

setServerSocketTimeout

public void setServerSocketTimeout(int socketTimeout)
Sets initial socket timeout

Parameters:
socketTimeout - initial socket timeout

getNewSocketTimeout

public int getNewSocketTimeout()
Returns new socket timeout

Returns:
new socket timeout

setNewSocketTimeout

public void setNewSocketTimeout(int socketTimeout)
Sets new socket timeout

Parameters:
socketTimeout - new socket timeout

getGraceFinishPeriod

public int getGraceFinishPeriod()
Returns grace finish period

Returns:
grace finish period

setGraceFinishPeriod

public void setGraceFinishPeriod(int graceFinishPeriod)
Sets grace finish period

Parameters:
graceFinishPeriod - grace finish period

getExecutor

public Executor getExecutor()
Return the executor which is used or connections to be handled with

Returns:
the executor

setExecutor

public void setExecutor(Executor executor)
Sets the executor for connections to be handled with

Parameters:
executor -

getConnectionHandler

public ConnectionHandler getConnectionHandler()
Returns connection handler connections are handed to.

Returns:
connection handler

setConnectionHandler

public void setConnectionHandler(ConnectionHandler connectionHandler)
Sets connection handler

Parameters:
connectionHandler - connection handler

create

public void create()
            throws ServiceException
Creates the socket

Overrides:
create in class Service
Throws:
ServiceException

start

public void start()
           throws ServiceException
Starts the service

Overrides:
start in class Service
Throws:
ServiceException

stop

public void stop()
          throws ServiceException
Stops the service.

Overrides:
stop in class Service
Throws:
ServiceException

destroy

public void destroy()
             throws ServiceException
Closes the server socket

Overrides:
destroy in class Service
Throws:
ServiceException

run

public void run()
Accepts connections from server socket and calls #processConnection(Socket) method


createServerSocket

protected ServerSocketChannel createServerSocket()
                                          throws ServiceException
Creates server socket

Returns:
server socket
Throws:
ServiceException

createSocketConnection

protected Connection createSocketConnection(SocketChannel socketChannel)
                                     throws IOException
Creates new socket connection

Parameters:
socket - socket
Returns:
socketChannel socket channel
Throws:
IOException
Exception

processConnection

protected void processConnection(SocketChannel socket)
Creates socket connection and new instance of ServerSocketChannelService.ConnectionHandlerThread to process socket under the given executor

Parameters:
socket - socket


Copyright © 2005-2009 Abstract Horizon. All Rights Reserved.