protected class Worker implements Runnable {
protected Thread thread = null;
protected boolean available = false;
protected long socket = 0;
protected boolean options = false;
protected synchronized void assign(long socket, boolean options) {
// Wait for the Processor to get the previous Socket
while (available) {
try {
wait();
} catch (InterruptedException e) {
}
}
// Store the newly available Socket and notify our thread
this.socket = socket;
this.options = options;
available = true;
notifyAll();
}
/**
* Await a newly assigned Socket from our Connector, or <code>null</code>
* if we are supposed to shut down.
*/
protected synchronized long await() {
// Wait for the Connector to provide a new Socket
while (!available) {
try {
wait();
} catch (InterruptedException e) {
}
}
// Notify the Connector that we have received this Socket
long socket = this.socket;
available = false;
notifyAll();
return (socket);
}
/**
* The background thread that listens for incoming TCP/IP connections and
* hands them off to an appropriate processor.
*/
public void run() {
// Process requests until we receive a shutdown signal
while (running) {
// Wait for the next socket to be assigned
long socket = await();
if (socket == 0)
continue;
// Process the request from this socket
if ((options && !setSocketOptions(socket)) || !handler.process(socket)) {
// Close socket and pool
Socket.destroy(socket);
socket = 0;
}
// Finish up this request
recycleWorkerThread(this);
}
}
/**
* Start the background processing thread.
*/
public void start() {
thread = new ThreadWithAttributes(AprEndpoint.this, this);
thread.setName(getName() + "-" + (++curThreads));
thread.setDaemon(true);
thread.start();
}
}