| Method from org.apache.tomcat.util.net.PoolTcpEndpoint Detail: |
Socket acceptSocket() {
if( !running || serverSocket==null ) return null;
Socket accepted = null;
try {
if(factory==null) {
accepted = serverSocket.accept();
} else {
accepted = factory.acceptSocket(serverSocket);
}
if (null == accepted) {
log.warn(sm.getString("endpoint.warn.nullSocket"));
} else {
if (!running) {
accepted.close(); // rude, but unlikely!
accepted = null;
} else if (factory != null) {
factory.initSocket( accepted );
}
}
}
catch(InterruptedIOException iioe) {
// normal part -- should happen regularly so
// that the endpoint can release if the server
// is shutdown.
}
catch (AccessControlException ace) {
// When using the Java SecurityManager this exception
// can be thrown if you are restricting access to the
// socket with SocketPermission's.
// Log the unauthorized access and continue
String msg = sm.getString("endpoint.warn.security",
serverSocket, ace);
log.warn(msg);
}
catch (IOException e) {
String msg = null;
if (running) {
msg = sm.getString("endpoint.err.nonfatal",
serverSocket, e);
log.error(msg, e);
}
if (accepted != null) {
try {
accepted.close();
} catch(Throwable ex) {
msg = sm.getString("endpoint.err.nonfatal",
accepted, ex);
log.warn(msg, ex);
}
accepted = null;
}
if( ! running ) return null;
reinitializing = true;
// Restart endpoint when getting an IOException during accept
synchronized (threadSync) {
if (reinitializing) {
reinitializing = false;
// 1) Attempt to close server socket
closeServerSocket();
initialized = false;
// 2) Reinit endpoint (recreate server socket)
try {
msg = sm.getString("endpoint.warn.reinit");
log.warn(msg);
initEndpoint();
} catch (Throwable t) {
msg = sm.getString("endpoint.err.nonfatal",
serverSocket, t);
log.error(msg, t);
}
// 3) If failed, attempt to restart endpoint
if (!initialized) {
msg = sm.getString("endpoint.warn.restart");
log.warn(msg);
try {
stopEndpoint();
initEndpoint();
startEndpoint();
} catch (Throwable t) {
msg = sm.getString("endpoint.err.fatal",
serverSocket, t);
log.error(msg, t);
}
// Current thread is now invalid: kill it
throw new ThreadDeath();
}
}
}
}
return accepted;
}
|
protected void closeServerSocket() {
if (!paused)
unlockAccept();
try {
if( serverSocket!=null)
serverSocket.close();
} catch(Exception e) {
log.error(sm.getString("endpoint.err.close"), e);
}
serverSocket = null;
}
|
public InetAddress getAddress() {
return inet;
}
|
public int getBacklog() {
return backlog;
}
|
public TcpConnectionHandler getConnectionHandler() {
return handler;
}
|
public int getCurrentThreadCount() {
return curThreads;
}
|
public int getCurrentThreadsBusy() {
return curThreads - workerThreads.size();
}
|
public int getMaxSpareThreads() {
return tp.getMaxSpareThreads();
}
|
public int getMaxThreads() {
return tp.getMaxThreads();
}
|
public int getMinSpareThreads() {
return tp.getMinSpareThreads();
}
|
public int getPort() {
return port;
}
|
public int getServerSoTimeout() {
return serverTimeout;
}
|
ServerSocketFactory getServerSocketFactory() {
return factory;
}
|
public int getSoLinger() {
return linger;
}
|
public int getSoTimeout() {
return socketTimeout;
}
|
public String getStrategy() {
if (lf) {
return "lf";
} else {
return "ms";
}
}
|
public boolean getTcpNoDelay() {
return tcpNoDelay;
}
|
public int getThreadPriority() {
return tp.getThreadPriority();
}
|
public void initEndpoint() throws IOException, InstantiationException {
try {
if(factory==null)
factory=ServerSocketFactory.getDefault();
if(serverSocket==null) {
try {
if (inet == null) {
serverSocket = factory.createSocket(port, backlog);
} else {
serverSocket = factory.createSocket(port, backlog, inet);
}
} catch (BindException orig) {
String msg;
if (inet == null)
msg = orig.getMessage() + "< null >:" + port;
else
msg = orig.getMessage() + " " +
inet.toString() + ":" + port;
BindException be = new BindException(msg);
be.initCause(orig);
throw be;
}
}
if( serverTimeout >= 0 )
serverSocket.setSoTimeout( serverTimeout );
} catch( IOException ex ) {
throw ex;
} catch( InstantiationException ex1 ) {
throw ex1;
}
initialized = true;
}
|
public boolean isPaused() {
return paused;
}
|
public boolean isRunning() {
return running;
}
|
public void pauseEndpoint() {
if (running && !paused) {
paused = true;
unlockAccept();
}
}
|
void processSocket(Socket s,
TcpConnection con,
Object[] threadData) {
// Process the connection
int step = 1;
try {
// 1: Set socket options: timeout, linger, etc
setSocketOptions(s);
// 2: SSL handshake
step = 2;
if (getServerSocketFactory() != null) {
getServerSocketFactory().handshake(s);
}
// 3: Process the connection
step = 3;
con.setEndpoint(this);
con.setSocket(s);
getConnectionHandler().processConnection(con, threadData);
} catch (SocketException se) {
log.debug(sm.getString("endpoint.err.socket", s.getInetAddress()),
se);
// Try to close the socket
try {
s.close();
} catch (IOException e) {
}
} catch (Throwable t) {
if (step == 2) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.err.handshake"), t);
}
} else {
log.error(sm.getString("endpoint.err.unexpected"), t);
}
// Try to close the socket
try {
s.close();
} catch (IOException e) {
}
} finally {
if (con != null) {
con.recycle();
}
}
}
|
void recycleWorkerThread(MasterSlaveWorkerThread workerThread) {
workerThreads.push(workerThread);
}
Recycle the specified Processor so that it can be used again. |
public void resumeEndpoint() {
if (running) {
paused = false;
}
}
|
public void run() {
// Loop until we receive a shutdown command
while (running) {
// Loop if endpoint is paused
while (paused) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Ignore
}
}
// Allocate a new worker thread
MasterSlaveWorkerThread workerThread = createWorkerThread();
if (workerThread == null) {
try {
// Wait a little for load to go down: as a result,
// no accept will be made until the concurrency is
// lower than the specified maxThreads, and current
// connections will wait for a little bit instead of
// failing right away.
Thread.sleep(100);
} catch (InterruptedException e) {
// Ignore
}
continue;
}
// Accept the next incoming connection from the server socket
Socket socket = acceptSocket();
// Hand this socket off to an appropriate processor
workerThread.assign(socket);
// The processor will recycle itself when it finishes
}
// Notify the threadStop() method that we have shut ourselves down
synchronized (threadSync) {
threadSync.notifyAll();
}
}
The background thread that listens for incoming TCP/IP connections and
hands them off to an appropriate processor. |
public void setAddress(InetAddress inet) {
this.inet=inet;
}
|
public void setBacklog(int backlog) {
if( backlog >0)
this.backlog = backlog;
}
Allows the server developer to specify the backlog that
should be used for server sockets. By default, this value
is 100. |
public void setConnectionHandler(TcpConnectionHandler handler) {
this.handler=handler;
}
|
public void setMaxSpareThreads(int maxThreads) {
if(maxThreads > 0)
tp.setMaxSpareThreads(maxThreads);
}
|
public void setMaxThreads(int maxThreads) {
if( maxThreads > 0)
tp.setMaxThreads(maxThreads);
}
|
public void setMinSpareThreads(int minThreads) {
if(minThreads > 0)
tp.setMinSpareThreads(minThreads);
}
|
public void setPort(int port) {
this.port=port;
}
|
public void setServerSoTimeout(int i) {
serverTimeout=i;
}
|
public void setServerSocket(ServerSocket ss) {
serverSocket = ss;
}
|
public void setServerSocketFactory(ServerSocketFactory factory) {
this.factory=factory;
}
|
public void setServerTimeout(int timeout) {
this.serverTimeout = timeout;
}
|
public void setSoLinger(int i) {
linger=i;
}
|
public void setSoTimeout(int i) {
socketTimeout=i;
}
|
void setSocketOptions(Socket socket) throws SocketException {
if(linger >= 0 )
socket.setSoLinger( true, linger);
if( tcpNoDelay )
socket.setTcpNoDelay(tcpNoDelay);
if( socketTimeout > 0 )
socket.setSoTimeout( socketTimeout );
}
|
public void setStrategy(String strategy) {
if ("ms".equals(strategy)) {
lf = false;
} else {
lf = true;
}
}
|
public void setTcpNoDelay(boolean b) {
tcpNoDelay=b;
}
|
public void setThreadPriority(int threadPriority) {
tp.setThreadPriority(threadPriority);
}
|
public void startEndpoint() throws IOException, InstantiationException {
if (!initialized) {
initEndpoint();
}
if (lf) {
tp.start();
}
running = true;
paused = false;
if (lf) {
listener = new LeaderFollowerWorkerThread(this);
tp.runIt(listener);
} else {
maxThreads = getMaxThreads();
threadStart();
}
}
|
public void stopEndpoint() {
if (running) {
if (lf) {
tp.shutdown();
}
running = false;
if (serverSocket != null) {
closeServerSocket();
}
if (!lf) {
threadStop();
}
initialized=false ;
}
}
|
protected void unlockAccept() {
Socket s = null;
try {
// Need to create a connection to unlock the accept();
if (inet == null) {
s = new Socket(InetAddress.getByName("localhost").getHostAddress(), port);
} else {
s = new Socket(inet, port);
// setting soLinger to a small value will help shutdown the
// connection quicker
s.setSoLinger(true, 0);
}
} catch(Exception e) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.debug.unlock", "" + port), e);
}
} finally {
if (s != null) {
try {
s.close();
} catch (Exception e) {
// Ignore
}
}
}
}
|