View Javadoc

1   /*
2    * Copyright  2004-2005 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package net.sf.asterisk.fastagi.command;
18  
19  /***
20   * Plays the given file, and waits for the user to press one of the given
21   * digits. If none of the esacpe digits is pressed while streaming the file this
22   * command waits for the specified timeout still waiting for the user to press a
23   * digit. Streaming always begins at the beginning.<br>
24   * Returns 0 if no digit being pressed, or the ASCII numerical value of the
25   * digit if one was pressed, or -1 on error or if the channel was disconnected.
26   * <br>
27   * Remember, the file extension must not be included in the filename.
28   * 
29   * @see net.sf.asterisk.fastagi.command.StreamFileCommand
30   * @author srt
31   * @version $Id: GetOptionCommand.java,v 1.1 2005/08/01 18:38:43 srt Exp $
32   */
33  public class GetOptionCommand extends AGICommand
34  {
35      /***
36       * Serial version identifier.
37       */
38      private static final long serialVersionUID = 3978141041352128820L;
39  
40      /***
41       * The name of the file to stream.
42       */
43      private String file;
44  
45      /***
46       * When one of these digits is pressed while streaming the command returns.
47       */
48      private String escapeDigits;
49  
50      /***
51       * The timeout in seconds.
52       */
53      private int timeout;
54  
55      /***
56       * Creates a new GetOptionCommand with a default timeout of 5 seconds.
57       * 
58       * @param file the name of the file to stream, must not include extension.
59       * @param escapeDigits contains the digits that the user is expected to
60       *            press.
61       */
62      public GetOptionCommand(String file, String escapeDigits)
63      {
64          this.file = file;
65          this.escapeDigits = escapeDigits;
66          this.timeout = -1;
67      }
68  
69      /***
70       * Creates a new GetOptionCommand with the given timeout.
71       * 
72       * @param file the name of the file to stream, must not include extension.
73       * @param escapeDigits contains the digits that the user is expected to
74       *            press.
75       * @param timeout the timeout in seconds to wait if none of the defined
76       *            esacpe digits was presses while streaming.
77       */
78      public GetOptionCommand(String file, String escapeDigits, int timeout)
79      {
80          this.file = file;
81          this.escapeDigits = escapeDigits;
82          this.timeout = timeout;
83      }
84  
85      /***
86       * Returns the name of the file to stream.
87       * 
88       * @return the name of the file to stream.
89       */
90      public String getFile()
91      {
92          return file;
93      }
94  
95      /***
96       * Sets the name of the file to stream.
97       * 
98       * @param file the name of the file to stream, must not include extension.
99       */
100     public void setFile(String file)
101     {
102         this.file = file;
103     }
104 
105     /***
106      * Returns the digits that the user is expected to press.
107      * 
108      * @return the digits that the user is expected to press.
109      */
110     public String getEscapeDigits()
111     {
112         return escapeDigits;
113     }
114 
115     /***
116      * Sets the digits that the user is expected to press.
117      * 
118      * @param escapeDigits the digits that the user is expected to press.
119      */
120     public void setEscapeDigits(String escapeDigits)
121     {
122         this.escapeDigits = escapeDigits;
123     }
124 
125     /***
126      * Returns the timeout to wait if none of the defined esacpe digits was
127      * presses while streaming.
128      * 
129      * @return the timeout in seconds.
130      */
131     public int getTimeout()
132     {
133         return timeout;
134     }
135 
136     /***
137      * Sets the timeout to wait if none of the defined esacpe digits was presses
138      * while streaming.
139      * 
140      * @param timeout the timeout in seconds.
141      */
142     public void setTimeout(int timeout)
143     {
144         this.timeout = timeout;
145     }
146 
147     public String buildCommand()
148     {
149         return "GET OPTION " + escapeAndQuote(file) + " "
150                 + escapeAndQuote(escapeDigits)
151                 + (timeout < 0 ? "" : " " + timeout);
152     }
153 }