public class Service
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
protected class |
Service.ConnectionHandlerRunnable
A simple class that enables notification of ServiceHandler-s in a new
thread.
|
Modifier and Type | Field and Description |
---|---|
protected java.util.List<ServiceHandler> |
handlers |
protected java.net.ServerSocket |
serverSocket |
Constructor and Description |
---|
Service(int port)
The only constructor, which simply sets a port to be used by the server
socket.
|
Modifier and Type | Method and Description |
---|---|
void |
addServiceHandler(ServiceHandler handler)
Registers a new handler.
|
int |
getPort()
Get the server port.
|
protected void |
handleConnection(java.net.Socket socket,
ServiceEnvironment environment,
java.util.concurrent.Executor executor)
Notifies all registered handlers about every new client connection.
|
void |
removeServiceHandler(ServiceHandler handler)
Unregister the handler.
|
void |
start(ServiceEnvironment environment,
java.util.concurrent.Executor executor)
Opens the server socked on the given port.
|
protected java.net.ServerSocket serverSocket
protected java.util.List<ServiceHandler> handlers
public Service(int port)
port
- for the server socket.public void addServiceHandler(ServiceHandler handler)
handler
- to be registered.public void removeServiceHandler(ServiceHandler handler)
handler
- to be unregistered.public void start(ServiceEnvironment environment, java.util.concurrent.Executor executor) throws java.io.IOException
environment
- is the environment of this service.executor
- enables notification of handlers in a new thread.java.io.IOException
protected void handleConnection(java.net.Socket socket, ServiceEnvironment environment, java.util.concurrent.Executor executor)
socket
- is the client socket.environment
- is the environment of this service.executor
- enables notification of handlers in a new thread.public int getPort()