Holger's Java API

## com.antelmann.net
Class NetConnectionServer

    java.lang.Object
      java.lang.Thread
          com.antelmann.net.NetConnectionServer

All Implemented Interfaces:
  Enabled, Runnable

    public class NetConnectionServer

extends Thread
implements Enabled

A NetConnectionServer - once run() is called - listens to incoming requests from NetConnection objects with a signature provided by the ConnectionDispatcher. The server creates and starts a new Thread for each NetConnection with the right signature; the Thread is initialized with a Runnable object provided by the ConnectionDispatcher. The NetConnection objects used by the NetConnectionServer are SocketConnection objects.

Author:
  Holger Antelmann
See Also:
  `NetConnectionHandler`, `ConnectionDispatcher`, `MessageDelegator`, `NetConnection`, `SocketConnection`

Nested Class Summary
`static class` `NetConnectionServer.DefaultDispatcher` The DefaultDispatcher provides a default ConnectionDispatcher that is used when the NetConnectionServer constructor in the enclosing class is called with a NetConnectionHandler

Nested classes/interfaces inherited from class java.lang.Thread
`Thread.State, Thread.UncaughtExceptionHandler`

Field Summary

Fields inherited from class java.lang.Thread
`MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY`

Fields inherited from interface com.antelmann.util.Enabled
`FILTER`

Constructor Summary
`NetConnectionServer(int port, ConnectionDispatcher factory)` the factory is responsible for providing the runnable objects that handle each connection in a separate thread
`NetConnectionServer(int port, ConnectionDispatcher factory, Logger logger)`
`NetConnectionServer(int port, NetConnectionHandler handler, Handshake handshake)`
`NetConnectionServer(int port, NetConnectionHandler handler, Handshake handshake, Logger logger)`
`NetConnectionServer(int port, NetConnectionHandler handler, Handshake handshake, Logger logger, Logger delegateLogger)` each connection made will run in a separate thread that delegates every message received to the given handler

Method Summary
`void` `closeAllConnections()`
`protected void` `finalize()`
`Map<NetConnection,MessageDelegator>` `getConnectionMap()` returns an unmodifiable view of all NetConnections and their MessageDelegators that have been created with this server (minus those that have been removed through `removeInactiveConnections()`)
`MessageDelegator[]` `getDelegators()` returns a snapshot of the currently known delegators
`int` `getLocalPort()`
`Logger` `getLogger()`
`NetConnection[]` `getNetConnections()` returns a snapshot of the currently known connections
`ServerSocket` `getServerSocket()`
`boolean` `isEnabled()` determines whether this object is currently enabled
`boolean` `isRequiresLicense()`
`void` `removeInactiveConnections()` removes inactive connections from the maintained list of connections
`void` `run()` starts listening and creates a new Thread for every connection made.
`void` `setEnabled(boolean enabled)` allows to enable/disable this instance
`void` `setRequiredLicensee(Object licensee)`
`void` `setRequiresLicense(boolean requiresLicense)`
`void` `setThreadFactory(ThreadFactory tf)` allows to customize Thread creation for a client connection
`void` `shutdown()` shutdown() does not close the connections that have already been established
`static NetConnection` `waitForConnection(int port, Handshake handshake, int timeout)` listens for a single connection to be made and returns a NetConnection if the connection made matches the signature

Methods inherited from class java.lang.Thread

`activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield`

Methods inherited from class java.lang.Object
`equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Constructor Detail

### NetConnectionServer

    public NetConnectionServer(int port,
                               NetConnectionHandler handler,
                               Handshake handshake)
                        throws IOException

Throws:
  `IOException`

### NetConnectionServer

    public NetConnectionServer(int port,
                               NetConnectionHandler handler,
                               Handshake handshake,
                               Logger logger)
                        throws IOException

Throws:
  `IOException`

### NetConnectionServer

    public NetConnectionServer(int port,
                               NetConnectionHandler handler,
                               Handshake handshake,
                               Logger logger,
                               Logger delegateLogger)
                        throws IOException

  each connection made will run in a separate thread that delegates every message received to the given handler

  Parameters:
    `port` - the local port this server is to listen to
    `handler` - the handler that is to handle messages from all incoming connections
    `handshake` - the handshake to be used when establishing a new connection
    `logger` - the logger for this server that logs connection attempts
    `delegateLogger` - the logger used by the MessageDelegator created to handle incoming connections
  Throws:
    `IOException`

### NetConnectionServer

    public NetConnectionServer(int port,
                               ConnectionDispatcher factory)
                        throws IOException

  the factory is responsible for providing the runnable objects that handle each connection in a separate thread

  Throws:
    `IOException`

### NetConnectionServer

    public NetConnectionServer(int port,
                               ConnectionDispatcher factory,
                               Logger logger)
                        throws IOException

Throws:
  `IOException`

Method Detail

### isRequiresLicense

    public boolean isRequiresLicense()

### setRequiresLicense

    public void setRequiresLicense(boolean requiresLicense)

### setRequiredLicensee

    public void setRequiredLicensee(Object licensee)

### setThreadFactory

    public void setThreadFactory(ThreadFactory tf)

  allows to customize Thread creation for a client connection

### waitForConnection

    public static NetConnection waitForConnection(int port,
                                                  Handshake handshake,
                                                  int timeout)
                                           throws IOException

  listens for a single connection to be made and returns a NetConnection if the connection made matches the signature

  Parameters:
    `timeout` - specified in milliseconds; if 0, the method waits indefinately until a connection is made or the serverSocket is closed
  Throws:
    `IOException`
  See Also:
    `SocketConnection.createServerConnection(ServerSocket, Handshake)`

### run

    @Licensed
    public void run()

  starts listening and creates a new Thread for every connection made. That thread will use a NetConnectionHandler produced by the ConnectionDispatcher to handle the connection created.

  Specified by:
    `run` in interface `Runnable`
  Overrides:
    `run` in class `Thread`

### getLogger

    public Logger getLogger()

### getConnectionMap

    public Map<NetConnection,MessageDelegator> getConnectionMap()

  returns an unmodifiable view of all NetConnections and their MessageDelegators that have been created with this server (minus those that have been removed through `removeInactiveConnections()`)

  See Also:
    `removeInactiveConnections()`

### getDelegators

    public MessageDelegator[] getDelegators()

  returns a snapshot of the currently known delegators

### getNetConnections

    public NetConnection[] getNetConnections()

  returns a snapshot of the currently known connections

### removeInactiveConnections

    public void removeInactiveConnections()

  removes inactive connections from the maintained list of connections

  See Also:
    `getNetConnections()`

### shutdown

    public void shutdown()

  shutdown() does not close the connections that have already been established

### closeAllConnections

    public void closeAllConnections()

### setEnabled

    public void setEnabled(boolean enabled)

  Description copied from interface: `Enabled`
  allows to enable/disable this instance

  Specified by:
    `setEnabled` in interface `Enabled`

### isEnabled

    public boolean isEnabled()

  Description copied from interface: `Enabled`
  determines whether this object is currently enabled

  Specified by:
    `isEnabled` in interface `Enabled`

### getLocalPort

    public int getLocalPort()

### getServerSocket

    public ServerSocket getServerSocket()

### finalize

    protected void finalize()

  Overrides:
    `finalize` in class `Object`

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads