Class Service
- java.lang.Object
-
- org.processmining.framework.util.socket.Service
-
public class Service extends java.lang.Object
This is a wrapper for a server socket. It has an arbitrary number of registered ServiceHandler-s. Whenever a client connects to the service, all registered handlers are notified. Each handler can further communucate with the client.- Author:
- christian
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
Service.ConnectionHandlerRunnable
A simple class that enables notification of ServiceHandler-s in a new thread.
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<ServiceHandler>
handlers
protected java.net.ServerSocket
serverSocket
-
Constructor Summary
Constructors Constructor Description Service(int port)
The only constructor, which simply sets a port to be used by the server socket.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.
-
-
-
Field Detail
-
serverSocket
protected java.net.ServerSocket serverSocket
-
handlers
protected java.util.List<ServiceHandler> handlers
-
-
Method Detail
-
addServiceHandler
public void addServiceHandler(ServiceHandler handler)
Registers a new handler. From this moment, the handler will be notified about new client connections.- Parameters:
handler
- to be registered.
-
removeServiceHandler
public void removeServiceHandler(ServiceHandler handler)
Unregister the handler. From this moment, the handler will not longer be notified about new client connections.- Parameters:
handler
- to be unregistered.
-
start
public void start(ServiceEnvironment environment, java.util.concurrent.Executor executor) throws java.io.IOException
Opens the server socked on the given port. Periodically notifies the environment that the service is alive. Notifies all registered handlers about every new client connection. It keeps on listening on the socket until the environment is canceled, upon which the socket is closed.- Parameters:
environment
- is the environment of this service.executor
- enables notification of handlers in a new thread.- Throws:
java.io.IOException
-
handleConnection
protected void handleConnection(java.net.Socket socket, ServiceEnvironment environment, java.util.concurrent.Executor executor)
Notifies all registered handlers about every new client connection.- Parameters:
socket
- is the client socket.environment
- is the environment of this service.executor
- enables notification of handlers in a new thread.
-
getPort
public int getPort()
Get the server port.- Returns:
- the port.
-
-