Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16005/src/java/net/sf/asterisk/fastagi
Added Files:
DefaultAGIServer.java
Log Message:
Added DefaultAGIServer
--- NEW FILE: DefaultAGIServer.java ---
/*
* Copyright 2004-2005 Stefan Reuter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sf.asterisk.fastagi;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.asterisk.io.ServerSocketFacade;
import net.sf.asterisk.io.ServerSocketFacadeImpl;
import net.sf.asterisk.io.SocketConnectionFacade;
import net.sf.asterisk.util.ThreadPool;
public class DefaultAGIServer implements AGIServer
{
private static final int DEFAULT_PORT = 4573;
private static final int DEFAULT_POOL_SIZE = 10;
/**
* Instance logger.
*/
private final Log logger = LogFactory.getLog(DefaultAGIServer.class);
private ServerSocketFacade serverSocket;
/**
* The port to listen on.
*/
protected int port = DEFAULT_PORT;
/**
* The thread pool that contains the worker threads to process incoming
* requests.
*/
private ThreadPool pool;
/**
* The number of worker threads in the thread pool. This equals the maximum
* number of concurrent requests this AGIServer can serve.
*/
private int poolSize = DEFAULT_POOL_SIZE;
private boolean die;
public DefaultAGIServer()
{
this.pool = new ThreadPool("AGIServer", poolSize);
}
protected ServerSocketFacade createServerSocket() throws IOException
{
return new ServerSocketFacadeImpl(port);
}
public void startup() throws IOException, IllegalStateException
{
run();
}
public void run()
{
SocketConnectionFacade socket;
AGIConnectionHandler connectionHandler;
die = false;
try
{
serverSocket = createServerSocket();
logger.info("Waiting for new connections.");
while ((socket = serverSocket.accept()) != null)
{
logger.info("Received connection.");
connectionHandler = new AGIConnectionHandler(socket);
pool.addJob(connectionHandler);
}
}
catch (IOException e)
{
// swallow if shutdown
if (die)
{
logger.error("IOException while waiting for connections.", e);
}
}
finally
{
try
{
serverSocket.close();
}
catch (IOException e)
{
// swallow
}
serverSocket = null;
}
}
public void die() throws IOException
{
die = true;
if (serverSocket != null)
{
serverSocket.close();
}
logger.info("AGIServer shut down.");
}
public void shutdown() throws IOException, IllegalStateException
{
pool.shutdown();
die();
}
}
|