[Asterisk-java-cvs] CVS: asterisk-java/src/java/net/sf/asterisk/fastagi MappingStrategy.java,NONE,1.
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-03-10 16:36:57
|
Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27555/src/java/net/sf/asterisk/fastagi Modified Files: AGIConnectionHandler.java DefaultAGIServer.java Added Files: MappingStrategy.java Log Message: Added MappingStrategy --- NEW FILE: MappingStrategy.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; /** * A MappingStrategy determines which AGIScript has to be called to service a * given AGIRequest. * * @author srt * @version $Id: MappingStrategy.java,v 1.1 2005/03/10 16:36:43 srt Exp $ */ public interface MappingStrategy { /** * Returns the AGIScript that is responsible to handle the given request. * * @param request the request to lookup. * @return the responsible script. */ AGIScript determineScript(AGIRequest request); } Index: AGIConnectionHandler.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi/AGIConnectionHandler.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -p -r1.1 -r1.2 --- AGIConnectionHandler.java 10 Mar 2005 13:45:23 -0000 1.1 +++ AGIConnectionHandler.java 10 Mar 2005 16:36:43 -0000 1.2 @@ -35,35 +35,68 @@ import org.apache.commons.logging.LogFac public class AGIConnectionHandler implements Runnable { private final Log logger = LogFactory.getLog(getClass()); + + /** + * The socket connection. + */ private SocketConnectionFacade socket; - private AGIReader reader; - private AGIWriter writer; /** + * The strategy to use to determine which script to run. + */ + private MappingStrategy mappingStrategy; + + /** * Creates a new AGIConnectionHandler to handle the given socket connection. + * * @param socket the socket connection to handle. + * @param mappingStrategy the strategy to use to determine which script to + * run. */ - public AGIConnectionHandler(SocketConnectionFacade socket) + public AGIConnectionHandler(SocketConnectionFacade socket, + MappingStrategy mappingStrategy) { this.socket = socket; - this.reader = new AGIReaderImpl(socket); - this.writer = new AGIWriterImpl(socket); + this.mappingStrategy = mappingStrategy; } - + + protected AGIReader createReader() + { + return new AGIReaderImpl(socket); + } + + protected AGIWriter createWriter() + { + return new AGIWriterImpl(socket); + } + public void run() { - AGIRequest request; - AGIResponse response; - AGIScript script; - try { + AGIReader reader; + AGIWriter writer; + AGIRequest request; + AGIResponse response; + AGIScript script; + + reader = createReader(); + writer = createWriter(); + request = reader.readRequest(); response = new AGIResponseImpl(writer, reader); - - //TODO find appropriate AGIScript - //TODO run AGIScript - //script.service(request, response); + + script = mappingStrategy.determineScript(request); + + if (script != null) + { + script.service(request, response); + } + else + { + logger.error("Unable to determine which script to run for " + + request.getRequestURL()); + } } catch (IOException e) { @@ -75,7 +108,7 @@ public class AGIConnectionHandler implem { socket.close(); } - catch(IOException e) + catch (IOException e) { } } Index: DefaultAGIServer.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi/DefaultAGIServer.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -p -r1.1 -r1.2 --- DefaultAGIServer.java 10 Mar 2005 13:44:32 -0000 1.1 +++ DefaultAGIServer.java 10 Mar 2005 16:36:43 -0000 1.2 @@ -28,7 +28,14 @@ import net.sf.asterisk.util.ThreadPool; public class DefaultAGIServer implements AGIServer { - private static final int DEFAULT_PORT = 4573; + /** + * The default bind port. + */ + private static final int DEFAULT_BIND_PORT = 4573; + + /** + * The default thread pool size. + */ private static final int DEFAULT_POOL_SIZE = 10; /** @@ -41,7 +48,7 @@ public class DefaultAGIServer implements /** * The port to listen on. */ - protected int port = DEFAULT_PORT; + private int bindPort; /** * The thread pool that contains the worker threads to process incoming @@ -53,18 +60,65 @@ public class DefaultAGIServer implements * 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 int poolSize; + /** + * True while this server is shut down. + */ private boolean die; + /** + * The strategy to use for mapping AGIRequests to AGIScripts that serve + * them. + */ + private MappingStrategy mappingStrategy; + + /** + * Creates a new DefaultAGIServer. + * + */ public DefaultAGIServer() { - this.pool = new ThreadPool("AGIServer", poolSize); + this.bindPort = DEFAULT_BIND_PORT; + this.poolSize = DEFAULT_POOL_SIZE; + } + + /** + * Sets the number of worker threads in the thread pool.<br> + * This equals the maximum number of concurrent requests this AGIServer can + * serve. + * + * @param poolSize the size of the worker thread pool. + */ + public void setPoolSize(int poolSize) + { + this.poolSize = poolSize; + } + + /** + * Sets the TCP port to listen on for new connections. + * + * @param bindPort the port to bind to. + */ + public void setBindPort(int bindPort) + { + this.bindPort = bindPort; + } + + /** + * Sets the strategy to use for mapping AGIRequests to AGIScripts that serve + * them. + * + * @param mappingStrategy the mapping strategy to use. + */ + public void setMappingStrategy(MappingStrategy mappingStrategy) + { + this.mappingStrategy = mappingStrategy; } protected ServerSocketFacade createServerSocket() throws IOException { - return new ServerSocketFacadeImpl(port); + return new ServerSocketFacadeImpl(bindPort, 0, null); } public void startup() throws IOException, IllegalStateException @@ -78,6 +132,8 @@ public class DefaultAGIServer implements AGIConnectionHandler connectionHandler; die = false; + pool = new ThreadPool("AGIServer", poolSize); + logger.info("Thread pool started."); try { serverSocket = createServerSocket(); @@ -86,29 +142,35 @@ public class DefaultAGIServer implements while ((socket = serverSocket.accept()) != null) { logger.info("Received connection."); - connectionHandler = new AGIConnectionHandler(socket); + connectionHandler = new AGIConnectionHandler(socket, + mappingStrategy); pool.addJob(connectionHandler); } } catch (IOException e) { // swallow if shutdown - if (die) + if (!die) { logger.error("IOException while waiting for connections.", e); } } finally { - try - { - serverSocket.close(); - } - catch (IOException e) + if (serverSocket != null) { - // swallow + try + { + serverSocket.close(); + } + catch (IOException e) + { + // swallow + } } serverSocket = null; + pool.shutdown(); + logger.info("AGIServer shut down."); } } @@ -120,12 +182,18 @@ public class DefaultAGIServer implements { serverSocket.close(); } - logger.info("AGIServer shut down."); } public void shutdown() throws IOException, IllegalStateException { - pool.shutdown(); die(); } + + public static void main(String[] args) throws Exception + { + AGIServer server; + + server = new DefaultAGIServer(); + server.startup(); + } } |