[Asterisk-java-cvs] CVS: asterisk-java/src/java/net/sf/asterisk/fastagi BaseAGIScript.java,NONE,1.1
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-08-06 18:43:40
|
Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7780/src/java/net/sf/asterisk/fastagi Modified Files: AGIConnectionHandler.java AbstractAGIScript.java Added Files: BaseAGIScript.java Log Message: Desprecated AbstractAGIScript in favor of BaseAGIScript When using BaseAGIScript you don't have to pass the AGIChannel around to all methods for sending actions as it will always use the AGIChannel attached to the current thread. --- NEW FILE: BaseAGIScript.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 net.sf.asterisk.fastagi.command.AGICommand; import net.sf.asterisk.fastagi.command.AnswerCommand; import net.sf.asterisk.fastagi.command.ChannelStatusCommand; import net.sf.asterisk.fastagi.command.ExecCommand; import net.sf.asterisk.fastagi.command.GetDataCommand; import net.sf.asterisk.fastagi.command.GetOptionCommand; import net.sf.asterisk.fastagi.command.GetVariableCommand; import net.sf.asterisk.fastagi.command.HangupCommand; import net.sf.asterisk.fastagi.command.SayAlphaCommand; import net.sf.asterisk.fastagi.command.SayDigitsCommand; import net.sf.asterisk.fastagi.command.SayNumberCommand; import net.sf.asterisk.fastagi.command.SayPhoneticCommand; import net.sf.asterisk.fastagi.command.SayTimeCommand; import net.sf.asterisk.fastagi.command.SetAutoHangupCommand; import net.sf.asterisk.fastagi.command.SetCallerIdCommand; import net.sf.asterisk.fastagi.command.SetContextCommand; import net.sf.asterisk.fastagi.command.SetExtensionCommand; import net.sf.asterisk.fastagi.command.SetMusicOffCommand; import net.sf.asterisk.fastagi.command.SetMusicOnCommand; import net.sf.asterisk.fastagi.command.SetPriorityCommand; import net.sf.asterisk.fastagi.command.SetVariableCommand; import net.sf.asterisk.fastagi.command.StreamFileCommand; import net.sf.asterisk.fastagi.command.WaitForDigitCommand; import net.sf.asterisk.fastagi.reply.AGIReply; /** * The BaseAGIScript provides some convinience methods to make it easier to * write custom AGIScripts.<br> * Just extend it by your own AGIScripts. * * @since 0.2 * @author srt * @version $Id: BaseAGIScript.java,v 1.1 2005/08/06 18:43:24 srt Exp $ */ public abstract class BaseAGIScript implements AGIScript { /** * Answers the channel. */ protected void answer() throws AGIException { sendCommand(new AnswerCommand()); } /** * Hangs the channel up. */ protected void hangup() throws AGIException { sendCommand(new HangupCommand()); } /** * Cause the channel to automatically hangup at the given number of seconds * in the future. * * @param time the number of seconds before this channel is automatically * hung up.<br> * 0 disables the autohangup feature. */ protected void setAutoHangup(int time) throws AGIException { sendCommand(new SetAutoHangupCommand(time)); } /** * Sets the caller id on the current channel. * * @param callerId the raw caller id to set, for example "John Doe<1234>". */ protected void setCallerId(String callerId) throws AGIException { sendCommand(new SetCallerIdCommand(callerId)); } /** * Plays music on hold from the default music on hold class. */ protected void playMusicOnHold() throws AGIException { sendCommand(new SetMusicOnCommand()); } /** * Plays music on hold from the given music on hold class. * * @param musicOnHoldClass the music on hold class to play music from as * configures in Asterisk's <code><musiconhold.conf/code>. */ protected void playMusicOnHold(String musicOnHoldClass) throws AGIException { sendCommand(new SetMusicOnCommand(musicOnHoldClass)); } /** * Stops playing music on hold. */ protected void stopMusicOnHold() throws AGIException { sendCommand(new SetMusicOffCommand()); } /** * Returns the status of the channel.<br> * Return values: * <ul> * <li>0 Channel is down and available * <li>1 Channel is down, but reserved * <li>2 Channel is off hook * <li>3 Digits (or equivalent) have been dialed * <li>4 Line is ringing * <li>5 Remote end is ringing * <li>6 Line is up * <li>7 Line is busy * </ul> * * @return the status of the channel. */ protected int getChannelStatus() throws AGIException { AGIReply reply; reply = sendCommand(new ChannelStatusCommand()); return reply.getResultCode(); } /** * Plays the given file and waits for the user to enter DTMF digits until he * presses '#'. The user may interrupt the streaming by starting to enter * digits. * * @param file the name of the file to play * @return a String containing the DTMF the user entered */ protected String getData(String file) throws AGIException { AGIReply reply; reply = sendCommand(new GetDataCommand(file)); return reply.getResult(); } /** * Plays the given file and waits for the user to enter DTMF digits until he * presses '#' or the timeout occurs. The user may interrupt the streaming * by starting to enter digits. * * @param file the name of the file to play * @param timeout the timeout to wait for user input.<br> * 0 means standard timeout value, -1 means "ludicrous time" * (essentially never times out). * @return a String containing the DTMF the user entered */ protected String getData(String file, int timeout) throws AGIException { AGIReply reply; reply = sendCommand(new GetDataCommand(file, timeout)); return reply.getResult(); } /** * Plays the given file and waits for the user to enter DTMF digits until he * presses '#' or the timeout occurs or the maximum number of digits has * been entered. The user may interrupt the streaming by starting to enter * digits. * * @param file the name of the file to play * @param timeout the timeout to wait for user input.<br> * 0 means standard timeout value, -1 means "ludicrous time" * (essentially never times out). * @param maxDigits the maximum number of digits the user is allowed to * enter * @return a String containing the DTMF the user entered */ protected String getData(String file, int timeout, int maxDigits) throws AGIException { AGIReply reply; reply = sendCommand(new GetDataCommand(file, timeout, maxDigits)); return reply.getResult(); } // TODO add getOption() methods /** * Plays the given file, and waits for the user to press one of the given * digits. If none of the esacpe digits is pressed while streaming the file * it waits for the default timeout of 5 seconds still waiting for the user * to press a digit. * * @param file the name of the file to stream, must not include extension. * @param escapeDigits contains the digits that the user is expected to * press. * @return the DTMF digit pressed or 0x0 if none was pressed. */ protected char getOption(String file, String escapeDigits) throws AGIException { AGIReply reply; reply = sendCommand(new GetOptionCommand(file, escapeDigits)); return reply.getResultCodeAsChar(); } /** * Plays the given file, and waits for the user to press one of the given * digits. If none of the esacpe digits is pressed while streaming the file * it waits for the specified timeout still waiting for the user to press a * digit. * * @param file the name of the file to stream, must not include extension. * @param escapeDigits contains the digits that the user is expected to * press. * @param timeout the timeout in seconds to wait if none of the defined * esacpe digits was presses while streaming. * @return the DTMF digit pressed or 0x0 if none was pressed. */ protected char getOption(String file, String escapeDigits, int timeout) throws AGIException { AGIReply reply; reply = sendCommand(new GetOptionCommand(file, escapeDigits, timeout)); return reply.getResultCodeAsChar(); } /** * Executes the given command. * * @param application the name of the application to execute, for example * "Dial". * @return the return code of the application of -2 if the application was * not found. */ protected int exec(String application) throws AGIException { AGIReply reply; reply = sendCommand(new ExecCommand(application)); return reply.getResultCode(); } /** * Executes the given command. * * @param application the name of the application to execute, for example * "Dial". * @param options the parameters to pass to the application, for example * "SIP/123". * @return the return code of the application of -2 if the application was * not found. */ protected int exec(String application, String options) throws AGIException { AGIReply reply; reply = sendCommand(new ExecCommand(application, options)); return reply.getResultCode(); } /** * Sets the context for continuation upon exiting the application. * * @param context the context for continuation upon exiting the application. */ protected void setContext(String context) throws AGIException { sendCommand(new SetContextCommand(context)); } /** * Sets the extension for continuation upon exiting the application. * * @param extension the extension for continuation upon exiting the * application. */ protected void setExtension(String extension) throws AGIException { sendCommand(new SetExtensionCommand(extension)); } /** * Sets the priority for continuation upon exiting the application. * * @param priority the priority for continuation upon exiting the * application. */ protected void setPriority(int priority) throws AGIException { sendCommand(new SetPriorityCommand(priority)); } /** * Plays the given file. * * @param file name of the file to play. */ protected void streamFile(String file) throws AGIException { sendCommand(new StreamFileCommand(file)); } /** * Plays the given file and allows the user to escape by pressing one of the * given digit. * * @param file name of the file to play. * @param escapeDigits a String containing the DTMF digits that allow the * user to escape. * @return the DTMF digit pressed or 0x0 if none was pressed. */ protected char streamFile(String file, String escapeDigits) throws AGIException { AGIReply reply; reply = sendCommand(new StreamFileCommand(file, escapeDigits)); return reply.getResultCodeAsChar(); } /** * Says the given digit string. * * @param digits the digit string to say. */ protected void sayDigits(String digits) throws AGIException { sendCommand(new SayDigitsCommand(digits)); } /** * Says the given number, returning early if any of the given DTMF number * are received on the channel. * * @param digits the digit string to say. * @param escapeDigits a String containing the DTMF digits that allow the * user to escape. * @return the DTMF digit pressed or 0x0 if none was pressed. */ protected char sayDigits(String digits, String escapeDigits) throws AGIException { AGIReply reply; reply = sendCommand(new SayDigitsCommand(digits, escapeDigits)); return reply.getResultCodeAsChar(); } /** * Says the given number. * * @param number the number to say. */ protected void sayNumber(String number) throws AGIException { sendCommand(new SayNumberCommand(number)); } /** * Says the given number, returning early if any of the given DTMF number * are received on the channel. * * @param number the number to say. * @param escapeDigits a String containing the DTMF digits that allow the * user to escape. * @return the DTMF digit pressed or 0x0 if none was pressed. */ protected char sayNumber(String number, String escapeDigits) throws AGIException { AGIReply reply; reply = sendCommand(new SayNumberCommand(number, escapeDigits)); return reply.getResultCodeAsChar(); } /** * Says the given character string with phonetics. * * @param text the text to say. */ protected void sayPhonetic(String text) throws AGIException { sendCommand(new SayPhoneticCommand(text)); } /** * Says the given character string with phonetics, returning early if any of * the given DTMF number are received on the channel. * * @param text the text to say. * @param escapeDigits a String containing the DTMF digits that allow the * user to escape. * @return the DTMF digit pressed or 0x0 if none was pressed. */ protected char sayPhonetic(String text, String escapeDigits) throws AGIException { AGIReply reply; reply = sendCommand(new SayPhoneticCommand(text, escapeDigits)); return reply.getResultCodeAsChar(); } /** * Says the given character string. * * @param text the text to say. */ protected void sayAlpha(String text) throws AGIException { sendCommand(new SayAlphaCommand(text)); } /** * Says the given character string, returning early if any of the given DTMF * number are received on the channel. * * @param text the text to say. * @param escapeDigits a String containing the DTMF digits that allow the * user to escape. * @return the DTMF digit pressed or 0x0 if none was pressed. */ protected char sayAlpha(String text, String escapeDigits) throws AGIException { AGIReply reply; reply = sendCommand(new SayAlphaCommand(text, escapeDigits)); return reply.getResultCodeAsChar(); } /** * Says the given time. * * @param time the time to say in seconds since 00:00:00 on January 1, 1970. */ protected void sayTime(long time) throws AGIException { sendCommand(new SayTimeCommand(time)); } /** * Says the given time, returning early if any of the given DTMF number are * received on the channel. * * @param time the time to say in seconds since 00:00:00 on January 1, 1970. * @param escapeDigits a String containing the DTMF digits that allow the * user to escape. * @return the DTMF digit pressed or 0x0 if none was pressed. */ protected char sayTime(long time, String escapeDigits) throws AGIException { AGIReply reply; reply = sendCommand(new SayTimeCommand(time, escapeDigits)); return reply.getResultCodeAsChar(); } /** * Returns the value of the given channel variable. * * @param name the name of the variable to retrieve. * @return the value of the given variable or <code>null</code> if not * set. */ protected String getVariable(String name) throws AGIException { AGIReply reply; reply = sendCommand(new GetVariableCommand(name)); if (reply.getResultCode() != 1) { return null; } return reply.getExtra(); } /** * Sets the value of the given channel variable to a new value. * * @param name the name of the variable to retrieve. * @param value the new value to set. */ protected void setVariable(String name, String value) throws AGIException { sendCommand(new SetVariableCommand(name, value)); } /** * Waits up to 'timeout' milliseconds to receive a DTMF digit. * * @param timeout timeout the milliseconds to wait for the channel to * receive a DTMF digit, -1 will wait forever. * @return the DTMF digit pressed or 0x0 if none was pressed. */ protected char waitForDigit(int timeout) throws AGIException { AGIReply reply; reply = sendCommand(new WaitForDigitCommand(timeout)); return reply.getResultCodeAsChar(); } /** * Sends the given command to the channel attached to the current thread. * * @param command the command to send to Asterisk * @return the reply received from Asterisk * @throws AGIException if the command could not be processed properly */ private AGIReply sendCommand(AGICommand command) throws AGIException { AGIChannel channel; channel = AGIConnectionHandler.getChannel(); return channel.sendCommand(command); } } Index: AGIConnectionHandler.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi/AGIConnectionHandler.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -p -r1.11 -r1.12 --- AGIConnectionHandler.java 14 Jul 2005 12:54:41 -0000 1.11 +++ AGIConnectionHandler.java 6 Aug 2005 18:43:25 -0000 1.12 @@ -39,6 +39,7 @@ import net.sf.asterisk.util.LogFactory; public class AGIConnectionHandler implements Runnable { private final Log logger = LogFactory.getLog(getClass()); + private static final ThreadLocal channel = new ThreadLocal(); /** * The socket connection. @@ -83,6 +84,7 @@ public class AGIConnectionHandler implem AGIRequest request; AGIChannel channel; AGIScript script; + Thread thread; String threadName; reader = createReader(); @@ -93,7 +95,9 @@ public class AGIConnectionHandler implem script = mappingStrategy.determineScript(request); - threadName = Thread.currentThread().getName(); + thread = Thread.currentThread(); + threadName = thread.getName(); + AGIConnectionHandler.channel.set(channel); if (script != null) { @@ -122,6 +126,7 @@ public class AGIConnectionHandler implem } finally { + AGIConnectionHandler.channel.set(null); try { socket.close(); @@ -132,4 +137,15 @@ public class AGIConnectionHandler implem } } } + + /** + * Returns the AGIChannel associated with the current thread.<br> + * + * @return the AGIChannel associated with the current thread or + * <code>null</code> if none is associated. + */ + static AGIChannel getChannel() + { + return (AGIChannel) AGIConnectionHandler.channel.get(); + } } Index: AbstractAGIScript.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi/AbstractAGIScript.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -d -p -r1.15 -r1.16 --- AbstractAGIScript.java 1 Aug 2005 18:38:43 -0000 1.15 +++ AbstractAGIScript.java 6 Aug 2005 18:43:25 -0000 1.16 @@ -43,8 +43,9 @@ import net.sf.asterisk.fastagi.reply.AGI /** * The AbstractAGIScript provides some convinience methods to make it easier to * write custom AGIScripts.<br> - * Just extend it by your own AGIScripts. + * Just extend it by your own AGIScripts.<br> * + * @deprecated use {@see net.sf.asterisk.fastagi.BaseAGIScript} instead * @author srt * @version $Id$ */ |