sk.baka.ambient.commons
Class SocketServer

java.lang.Object
  extended by sk.baka.ambient.commons.SocketServer
All Implemented Interfaces:
Closeable, ThreadFactory
Direct Known Subclasses:
AmpacheServer, StreamerServer

public abstract class SocketServer
extends Object
implements ThreadFactory, Closeable

A server which listens on given port and executes tasks when the connection is established. The server is stopped by default.

Author:
Martin Vysny

Constructor Summary
SocketServer()
           
 
Method Summary
 void close()
           
protected abstract  void handleRequest(Socket socket, InputStream in, OutputStream out)
          Handles a request.
 boolean isStarted()
          Checks if the server is started.
 Thread newThread(Runnable r)
           
protected  void onStopping()
          Invoked when the server is being stopped.
 void start(int port, InetAddress bindTo)
          Starts the server.
 void stop()
          Stops the server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SocketServer

public SocketServer()
Method Detail

isStarted

public final boolean isStarted()
Checks if the server is started.

Returns:
true if the server is started, false otherwise.

start

public final void start(int port,
                        InetAddress bindTo)
                 throws IOException
Starts the server. Does nothing if the server is started.

Parameters:
port - the port to listen on.
bindTo - listen on this interface. See ServerSocketFactory.createServerSocket(int, int, InetAddress) for details.
Throws:
IOException - if server failed to start.

stop

public final void stop()
Stops the server. Does nothing if the server is not started.


onStopping

protected void onStopping()
Invoked when the server is being stopped.


close

public void close()
Specified by:
close in interface Closeable

newThread

public final Thread newThread(Runnable r)
Specified by:
newThread in interface ThreadFactory

handleRequest

protected abstract void handleRequest(Socket socket,
                                      InputStream in,
                                      OutputStream out)
                               throws IOException,
                                      ServerHttpException,
                                      InterruptedException
Handles a request. The communication is already opened and provided. Implementor executes in a new thread. Implementor should periodically check for Thread.isInterrupted(), terminating ASAP when interrupted.

Parameters:
socket - the socket for this request.
in - opened read pipe from the socket.
out - opened write pipe to the socket.
Throws:
IOException - if i/o error occurs.
ServerHttpException - if HTTP error occurs. This error is written as a HTTP response. If you decide to throw this error then make sure that nothing was written yet to out.
InterruptedException - if interrupted.


Copyright © 2007-2008. All Rights Reserved.