privatevoid acceptConnections() {
logger().log(INFO, "Server is accepting connections at port {0}",
getPort()); try {
beforeAcceptingConnections(); while (isRunning()) {
Socket socket = serverSocket.accept();
logger().log(INFO, "Accepted new connection at {0}", socket); synchronized (lock) { // Recheck if the server is still running // as someone has to close the `socket` if (isRunning()) {
socketList.add(socket);
} else {
closeSilently(socket);
}
}
connectionsPool.submit(() -> handleConnection(socket));
}
} catch (Throwable t) { if (isRunning()) { thrownew RuntimeException( "Unexpected exception while accepting connections", t);
}
} finally {
logger().log(INFO, "Server stopped accepting connections at port {0}",
getPort());
}
}
/* *A"TemplateMethod"describinghowaconnection(representedbyasocket) *ishandled. * *Thesocketisclosedimmediatelybeforethemethodreturns(normallyor *abruptly).
*/ privatevoid handleConnection(Socket socket) { // No need to close socket's streams separately, they will be closed // automatically when `socket.close()` is called
beforeConnectionHandled(socket);
ConnWrapper connWrapper = new ConnWrapper(socket); try (socket) {
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream(); byte[] inBuffer = newbyte[1024]; int count; byte[] request;
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int msgLen = -1;
// As inBuffer.length > 0, at least 1 byte is read while ((count = in.read(inBuffer)) > 0) {
buffer.write(inBuffer, 0, count); if (msgLen <= 0) {
msgLen = LdapMessage.getMessageLength(buffer.toByteArray());
}
/* *Startsthisserver.Maybecalledonlyonce.
*/ public BaseLdapServer start() { synchronized (lock) { if (state != State.NEW) { thrownew IllegalStateException(state.toString());
}
state = State.STARTED;
logger().log(INFO, "Starting server at port {0}", getPort());
acceptingThread.start(); returnthis;
}
}
/* *Stopsthisserver. * *Maybecalledatanytime,evenbeforeacallto`start()`.Inthelatter *casethesubsequentcallto`start()`willthrowanexception.Repeated *callstothismethodhavenoeffect. * *Stopsacceptingnewconnections,interruptsthethreadsservingalready *acceptedconnectionsandclosesallthesockets.
*/
@Override publicvoid close() { synchronized (lock) { if (state == State.STOPPED) { return;
}
state = State.STOPPED;
logger().log(INFO, "Stopping server at port {0}", getPort());
acceptingThread.interrupt();
closeSilently(serverSocket); // It's important to signal an interruption so that overridden // methods have a chance to return if they use // interruption-sensitive blocking operations. However, blocked I/O // operations on the socket will NOT react on that, hence the socket // also has to be closed to propagate shutting down.
connectionsPool.shutdownNow();
socketList.forEach(BaseLdapServer.this::closeSilently);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.