SK.gnome.dwarf.tcpip
Class TCPIPServer

java.lang.Object
  extended bySK.gnome.dwarf.GenericService
      extended bySK.gnome.dwarf.GenericServer
          extended bySK.gnome.dwarf.thread.ThreadServer
              extended bySK.gnome.dwarf.thread.MultiThreadServer
                  extended bySK.gnome.dwarf.tcpip.TCPIPServer
All Implemented Interfaces:
Reportable, java.lang.Runnable, Server, Service
Direct Known Subclasses:
QuoteServer

public class TCPIPServer
extends MultiThreadServer

Provides generic TCP/IP server.

This is just a convenience class. It extends the MultiThreadServer by setting some default initial values for the queue size, number of active handlers and the handler timeout. The real servers may directly use an instance of this class, or they may extend it to implement the additional protocol-related functionality.

This class also provides a means to configure a SSL/TLS context, from which a client or server sockets may be created without an effort. Use the setKeyStoreURL(URL), setKeyStorePwd(String), setKeyPwd(String) and setSSLProtocol(String) methods to initialize the SSL/TLS context.


Field Summary
protected  javax.net.ssl.SSLContext sslContext
          SSL context associated with this server.
 
Fields inherited from class SK.gnome.dwarf.thread.MultiThreadServer
counter
 
Fields inherited from class SK.gnome.dwarf.thread.ThreadServer
daemon, thread
 
Fields inherited from class SK.gnome.dwarf.GenericService
initParameters, parent
 
Fields inherited from interface SK.gnome.dwarf.Service
INITIALIZED, LOG_DEBUG, LOG_ERROR, LOG_FATAL, LOG_INFO, LOG_TRACE, LOG_WARN, LOG_XFER, SHUTDOWN, STARTED, STOPPED
 
Constructor Summary
TCPIPServer(java.lang.String name)
          Creates a new TCPIPServer.
 
Method Summary
 java.net.ServerSocket createSSLServerSocket(int port, int backlog)
          Creates a SSL server socket.
 java.net.ServerSocket createSSLServerSocket(int port, int backlog, java.net.InetAddress address)
          Creates a SSL server socket.
 java.net.Socket createSSLSocket(java.net.Socket socket, boolean autoClose)
          Creates a SSL socket.
 boolean enqueue(java.lang.Object obj)
          Puts the client request to the queue.
 void finish(java.lang.Object obj)
          Returns the processed request to the server.
protected  int getRequestReadTimeout()
          Returns the request read timeout.
protected  TCPRequest getTCPRequest()
          Returns a TCPRequest implementation.
 void init(Server parent)
          Initializes the service.
protected  boolean isBlocking()
          Returns whether this server is configured as blocking.
 boolean isSSLConfigured()
          Whether the SSL sockets may be created.
protected  void loop()
          Main server thread loop.
 java.lang.String report()
          Returns the service report.
 void setKeyPwd(java.lang.String pwd)
          Sets the key password.
 void setKeyStorePwd(java.lang.String pwd)
          Sets the keystore password.
 void setKeyStoreURL(java.net.URL url)
          Sets the keystore URL.
 void setRegistrationQueueSize(int size)
          Sets the max size of channel registration queue.
 void setRequestClass(java.lang.Class cl)
          Sets the class of non-blocking request implementation.
 void setSSLProtocol(java.lang.String name)
          Sets the requested SSL/TLS protocol name.
 void shutdown()
          Shuts down the service.
 void start()
          Starts the service.
 
Methods inherited from class SK.gnome.dwarf.thread.MultiThreadServer
addService, dequeue, enableThread, getHandlerTimeout, setHandlerClass, setHandlerTimeout, setMaxHandlers, setMinHandlers, setQueueSize, setSingleRequest, wakeUpHandlers
 
Methods inherited from class SK.gnome.dwarf.thread.ThreadServer
finish, run, setDaemon, stop
 
Methods inherited from class SK.gnome.dwarf.GenericServer
addService, getAuthenticator, getLogger, getParameters, getService, getServices, getServices, removeService
 
Methods inherited from class SK.gnome.dwarf.GenericService
getAuthFacility, getFullName, getInitParameter, getInitParameterNames, getLogFacility, getName, getPrincipal, getShutdownTimeout, getState, log, log, login, logout, setAuthFacility, setInitParameters, setLogFacility, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface SK.gnome.dwarf.Service
getFullName, getName, getState, log, log, login, logout
 

Field Detail

sslContext

protected javax.net.ssl.SSLContext sslContext
SSL context associated with this server.

Constructor Detail

TCPIPServer

public TCPIPServer(java.lang.String name)
Creates a new TCPIPServer.

The inherited attributes are initialized to some default values as follows, so an instance of this class may be created and used without any further effort:

 setQueueSize(TCPIPConstants.TCPIP_DEFAULT_QUEUE_SIZE);
 setReqistrationQueueSize(TCPIPConstants.TCPIP_DEFAULT_REGISTRATION_QUEUE_SIZE);
 setHandlerTimeout(TCPIPConstants.TCPIP_DEFAULT_HANDLER_TIMEOUT);
 setMinHandlers(TCPIPConstants.TCPIP_DEFAULT_MIN_HANDLERS);
 setMaxHandlers(TCPIPConstants.TCPIP_DEFAULT_MAX_HANDLERS);
 

Method Detail

setRegistrationQueueSize

public void setRegistrationQueueSize(int size)
Sets the max size of channel registration queue.

Parameters:
size - the queue size limit

setRequestClass

public void setRequestClass(java.lang.Class cl)
Sets the class of non-blocking request implementation.

Parameters:
cl - the request class

setKeyStoreURL

public void setKeyStoreURL(java.net.URL url)
Sets the keystore URL.

The keystore must be in the "JKS" format.

Parameters:
url - URL of the keystore

setKeyStorePwd

public void setKeyStorePwd(java.lang.String pwd)
Sets the keystore password.

Parameters:
pwd - keystore password

setKeyPwd

public void setKeyPwd(java.lang.String pwd)
Sets the key password.

Parameters:
pwd - key password

setSSLProtocol

public void setSSLProtocol(java.lang.String name)
Sets the requested SSL/TLS protocol name.

The default protocol used is "TLSv1".

Parameters:
name - the protocol name

init

public void init(Server parent)
          throws ServiceException
Initializes the service.

If the key store URL has been specified, the corresponding key store is loaded and a key manager is created and configured from it. Then a sslContext is initialized with the given key manager.

Specified by:
init in interface Service
Overrides:
init in class MultiThreadServer
Throws:
ServiceException

start

public void start()
           throws ServiceException
Starts the service.

It calls super.start() and then logs an information about the current package's implementation title and version.

Specified by:
start in interface Service
Overrides:
start in class ThreadServer
Throws:
ServiceException

shutdown

public void shutdown()
Description copied from class: ThreadServer
Shuts down the service.

It interrupts the ThreadServer.thread via its interrupt() method and waits for it to finish by calling the ThreadServer.finish() method.

Specified by:
shutdown in interface Service
Overrides:
shutdown in class ThreadServer

enqueue

public boolean enqueue(java.lang.Object obj)
Description copied from class: MultiThreadServer
Puts the client request to the queue.

The request is represented by an instance of the java.lang.Object, which is added to the server's internal queue. The idle handlers (which are blocked on the queue) are then notified that a client request is waiting for the processing. After that, if the current queue size is still greater than zero, the server also tries to start some of the temporarily stopped handlers - this process is actually preformed in the protected MultiThreadServer.loop() method. The method returns true if the client request was accepted by the server, otherwise returns false - it probably means either that the overall server's processing capacity has been reached, or an object equal to obj is already processed by a handler.

Overrides:
enqueue in class MultiThreadServer
Parameters:
obj - the client request
Returns:
true if object was accepted by the server, false otherwise

finish

public void finish(java.lang.Object obj)
Description copied from class: MultiThreadServer
Returns the processed request to the server.

This method just adjusts the MultiThreadServer.counter value.

Overrides:
finish in class MultiThreadServer
Parameters:
obj - the request object

createSSLServerSocket

public java.net.ServerSocket createSSLServerSocket(int port,
                                                   int backlog)
                                            throws java.io.IOException
Creates a SSL server socket.

Uses the Sun JSSE implementation to create a SSL server socket by using the system default socket factory. The socket is created according to the current sslContext value.

The created socket will be bound to all available addresses.

Parameters:
port - the port to listen on
backlog - the size of server backlog
Returns:
SSL server socket
Throws:
java.io.IOException - if socket could not be created

createSSLServerSocket

public java.net.ServerSocket createSSLServerSocket(int port,
                                                   int backlog,
                                                   java.net.InetAddress address)
                                            throws java.io.IOException
Creates a SSL server socket.

Uses the Sun JSSE implementation to create a SSL server socket by using the system default socket factory. The socket is created according to the current sslContext value.

The created socket will be bound to the given address.

Parameters:
port - the port to listen on
backlog - the size of server backlog
address - the address to listen on
Returns:
SSL server socket
Throws:
java.io.IOException - if socket could not be created

createSSLSocket

public java.net.Socket createSSLSocket(java.net.Socket socket,
                                       boolean autoClose)
                                throws java.io.IOException
Creates a SSL socket.

Uses the Sun JSSE implementation to create a SSL socket by tunneling the existing normal (insecure) socket. The socket is created according to the current sslContext value.

Parameters:
socket - the normal socket
autoClose - whether to close the underlying socket if closing the SSL socket
Returns:
the SSL client socket
Throws:
ServiceException - if the socket could not be created
java.io.IOException

isSSLConfigured

public boolean isSSLConfigured()
Whether the SSL sockets may be created.

It returns true is the sslContext has been initialized properly and the SSL client and server sockets may be created by this server. Application code may consult the result of this method to advertise some SSL-based capabilities to remote clients.

Returns:
true if the SSL sockets may be created, false otherwise

report

public java.lang.String report()
Description copied from class: MultiThreadServer
Returns the service report.

Returns report from the superclass plus the statistics of the handlers' activity and the internal client request queue.

Specified by:
report in interface Reportable
Overrides:
report in class MultiThreadServer

isBlocking

protected boolean isBlocking()
Returns whether this server is configured as blocking.

Returns:
true if the server is blocking, false otherwise

getRequestReadTimeout

protected int getRequestReadTimeout()
Returns the request read timeout.

This timeout is used by the non-blocking servers only.

Returns:
the read timeout in milliseconds

loop

protected void loop()
Description copied from class: MultiThreadServer
Main server thread loop.

This method calls the MultiThreadServer.wakeUpHandlers() method and then waits blocked until notified.

Overrides:
loop in class MultiThreadServer

getTCPRequest

protected TCPRequest getTCPRequest()
Returns a TCPRequest implementation.

The default implementation just creates and returns a new request object.

Returns:
the request object
See Also:
setRequestClass(Class)


Copyright (c) 1999-2005, Gnome Ltd. All rights reserved.