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.impl;
18  
19  import net.sf.asterisk.fastagi.AGIChannel;
20  import net.sf.asterisk.fastagi.AGIException;
21  import net.sf.asterisk.fastagi.AGIReader;
22  import net.sf.asterisk.fastagi.AGIWriter;
23  import net.sf.asterisk.fastagi.InvalidCommandSyntaxException;
24  import net.sf.asterisk.fastagi.InvalidOrUnknownCommandException;
25  import net.sf.asterisk.fastagi.command.AGICommand;
26  import net.sf.asterisk.fastagi.command.AnswerCommand;
27  import net.sf.asterisk.fastagi.command.ChannelStatusCommand;
28  import net.sf.asterisk.fastagi.command.ExecCommand;
29  import net.sf.asterisk.fastagi.command.GetDataCommand;
30  import net.sf.asterisk.fastagi.command.GetFullVariableCommand;
31  import net.sf.asterisk.fastagi.command.GetOptionCommand;
32  import net.sf.asterisk.fastagi.command.GetVariableCommand;
33  import net.sf.asterisk.fastagi.command.HangupCommand;
34  import net.sf.asterisk.fastagi.command.SayAlphaCommand;
35  import net.sf.asterisk.fastagi.command.SayDateTimeCommand;
36  import net.sf.asterisk.fastagi.command.SayDigitsCommand;
37  import net.sf.asterisk.fastagi.command.SayNumberCommand;
38  import net.sf.asterisk.fastagi.command.SayPhoneticCommand;
39  import net.sf.asterisk.fastagi.command.SayTimeCommand;
40  import net.sf.asterisk.fastagi.command.SetAutoHangupCommand;
41  import net.sf.asterisk.fastagi.command.SetCallerIdCommand;
42  import net.sf.asterisk.fastagi.command.SetContextCommand;
43  import net.sf.asterisk.fastagi.command.SetExtensionCommand;
44  import net.sf.asterisk.fastagi.command.SetMusicOffCommand;
45  import net.sf.asterisk.fastagi.command.SetMusicOnCommand;
46  import net.sf.asterisk.fastagi.command.SetPriorityCommand;
47  import net.sf.asterisk.fastagi.command.SetVariableCommand;
48  import net.sf.asterisk.fastagi.command.StreamFileCommand;
49  import net.sf.asterisk.fastagi.command.WaitForDigitCommand;
50  import net.sf.asterisk.fastagi.reply.AGIReply;
51  import net.sf.asterisk.io.SocketConnectionFacade;
52  
53  /***
54   * Default implementation of the AGIChannel interface.
55   * 
56   * @author srt
57   * @version $Id: AGIChannelImpl.java,v 1.8 2005/11/27 17:31:05 srt Exp $
58   */
59  public class AGIChannelImpl implements AGIChannel
60  {
61      private AGIWriter agiWriter;
62      private AGIReader agiReader;
63  
64      public AGIChannelImpl(SocketConnectionFacade socket)
65      {
66          this.agiWriter = new AGIWriterImpl(socket);
67          this.agiReader = new AGIReaderImpl(socket);
68      }
69  
70      public AGIChannelImpl(AGIWriter agiWriter, AGIReader agiReader)
71      {
72          this.agiWriter = agiWriter;
73          this.agiReader = agiReader;
74      }
75  
76      public synchronized AGIReply sendCommand(AGICommand command)
77              throws AGIException
78      {
79          AGIReply reply;
80  
81          agiWriter.sendCommand(command);
82          reply = agiReader.readReply();
83  
84          if (reply.getStatus() == AGIReply.SC_INVALID_OR_UNKNOWN_COMMAND)
85          {
86              throw new InvalidOrUnknownCommandException(command.buildCommand());
87          }
88          if (reply.getStatus() == AGIReply.SC_INVALID_COMMAND_SYNTAX)
89          {
90              throw new InvalidCommandSyntaxException(reply.getSynopsis(), reply
91                      .getUsage());
92          }
93  
94          return reply;
95      }
96      
97      public void answer() throws AGIException
98      {
99          sendCommand(new AnswerCommand());
100     }
101 
102     public void hangup() throws AGIException
103     {
104         sendCommand(new HangupCommand());
105     }
106 
107     public void setAutoHangup(int time) throws AGIException
108     {
109         sendCommand(new SetAutoHangupCommand(time));
110     }
111 
112     public void setCallerId(String callerId) throws AGIException
113     {
114         sendCommand(new SetCallerIdCommand(callerId));
115     }
116 
117     public void playMusicOnHold() throws AGIException
118     {
119         sendCommand(new SetMusicOnCommand());
120     }
121 
122     public void playMusicOnHold(String musicOnHoldClass) throws AGIException
123     {
124         sendCommand(new SetMusicOnCommand(musicOnHoldClass));
125     }
126 
127     public void stopMusicOnHold() throws AGIException
128     {
129         sendCommand(new SetMusicOffCommand());
130     }
131 
132     public int getChannelStatus() throws AGIException
133     {
134         AGIReply reply;
135 
136         reply = sendCommand(new ChannelStatusCommand());
137         return reply.getResultCode();
138     }
139 
140     public String getData(String file) throws AGIException
141     {
142         AGIReply reply;
143 
144         reply = sendCommand(new GetDataCommand(file));
145         return reply.getResult();
146     }
147 
148     public String getData(String file, long timeout) throws AGIException
149     {
150         AGIReply reply;
151 
152         reply = sendCommand(new GetDataCommand(file, timeout));
153         return reply.getResult();
154     }
155 
156     public String getData(String file, long timeout, int maxDigits)
157             throws AGIException
158     {
159         AGIReply reply;
160 
161         reply = sendCommand(new GetDataCommand(file, timeout, maxDigits));
162         return reply.getResult();
163     }
164 
165     public char getOption(String file, String escapeDigits)
166             throws AGIException
167     {
168         AGIReply reply;
169 
170         reply = sendCommand(new GetOptionCommand(file, escapeDigits));
171         return reply.getResultCodeAsChar();
172     }
173 
174     public char getOption(String file, String escapeDigits, int timeout)
175             throws AGIException
176     {
177         AGIReply reply;
178 
179         reply = sendCommand(new GetOptionCommand(file, escapeDigits, timeout));
180         return reply.getResultCodeAsChar();
181     }
182 
183     public int exec(String application) throws AGIException
184     {
185         AGIReply reply;
186 
187         reply = sendCommand(new ExecCommand(application));
188         return reply.getResultCode();
189     }
190 
191     public int exec(String application, String options) throws AGIException
192     {
193         AGIReply reply;
194 
195         reply = sendCommand(new ExecCommand(application, options));
196         return reply.getResultCode();
197     }
198 
199     public void setContext(String context) throws AGIException
200     {
201         sendCommand(new SetContextCommand(context));
202     }
203 
204     public void setExtension(String extension) throws AGIException
205     {
206         sendCommand(new SetExtensionCommand(extension));
207     }
208 
209     public void setPriority(String priority) throws AGIException
210     {
211         sendCommand(new SetPriorityCommand(priority));
212     }
213 
214     public void streamFile(String file) throws AGIException
215     {
216         sendCommand(new StreamFileCommand(file));
217     }
218 
219     public char streamFile(String file, String escapeDigits)
220             throws AGIException
221     {
222         AGIReply reply;
223 
224         reply = sendCommand(new StreamFileCommand(file, escapeDigits));
225         return reply.getResultCodeAsChar();
226     }
227 
228     public void sayDigits(String digits) throws AGIException
229     {
230         sendCommand(new SayDigitsCommand(digits));
231     }
232 
233     public char sayDigits(String digits, String escapeDigits)
234             throws AGIException
235     {
236         AGIReply reply;
237 
238         reply = sendCommand(new SayDigitsCommand(digits, escapeDigits));
239         return reply.getResultCodeAsChar();
240     }
241 
242     public void sayNumber(String number) throws AGIException
243     {
244         sendCommand(new SayNumberCommand(number));
245     }
246 
247     public char sayNumber(String number, String escapeDigits)
248             throws AGIException
249     {
250         AGIReply reply;
251 
252         reply = sendCommand(new SayNumberCommand(number, escapeDigits));
253         return reply.getResultCodeAsChar();
254     }
255 
256     public void sayPhonetic(String text) throws AGIException
257     {
258         sendCommand(new SayPhoneticCommand(text));
259     }
260 
261     public char sayPhonetic(String text, String escapeDigits)
262             throws AGIException
263     {
264         AGIReply reply;
265 
266         reply = sendCommand(new SayPhoneticCommand(text, escapeDigits));
267         return reply.getResultCodeAsChar();
268     }
269 
270     public void sayAlpha(String text) throws AGIException
271     {
272         sendCommand(new SayAlphaCommand(text));
273     }
274 
275     public char sayAlpha(String text, String escapeDigits)
276             throws AGIException
277     {
278         AGIReply reply;
279 
280         reply = sendCommand(new SayAlphaCommand(text, escapeDigits));
281         return reply.getResultCodeAsChar();
282     }
283 
284     public void sayTime(long time) throws AGIException
285     {
286         sendCommand(new SayTimeCommand(time));
287     }
288 
289     public char sayTime(long time, String escapeDigits) throws AGIException
290     {
291         AGIReply reply;
292 
293         reply = sendCommand(new SayTimeCommand(time, escapeDigits));
294         return reply.getResultCodeAsChar();
295     }
296 
297     public String getVariable(String name) throws AGIException
298     {
299         AGIReply reply;
300 
301         reply = sendCommand(new GetVariableCommand(name));
302         if (reply.getResultCode() != 1)
303         {
304             return null;
305         }
306         return reply.getExtra();
307     }
308 
309     public void setVariable(String name, String value) throws AGIException
310     {
311         sendCommand(new SetVariableCommand(name, value));
312     }
313 
314     public char waitForDigit(int timeout) throws AGIException
315     {
316         AGIReply reply;
317 
318         reply = sendCommand(new WaitForDigitCommand(timeout));
319         return reply.getResultCodeAsChar();
320     }
321 
322     public String getFullVariable(String name) throws AGIException
323     {
324         AGIReply reply;
325 
326         reply = sendCommand(new GetFullVariableCommand(name));
327         if (reply.getResultCode() != 1)
328         {
329             return null;
330         }
331         return reply.getExtra();
332     }
333 
334     public String getFullVariable(String name, String channel) throws AGIException
335     {
336         AGIReply reply;
337 
338         reply = sendCommand(new GetFullVariableCommand(name, channel));
339         if (reply.getResultCode() != 1)
340         {
341             return null;
342         }
343         return reply.getExtra();
344     }
345 
346     public char sayDateTime(long time, String escapeDigits, String format, String timezone) throws AGIException
347     {
348         AGIReply reply;
349 
350         reply = sendCommand(new SayDateTimeCommand(time, escapeDigits, format, timezone));
351         return reply.getResultCodeAsChar();
352     }
353 
354     public char sayDateTime(long time, String escapeDigits, String format) throws AGIException
355     {
356         AGIReply reply;
357 
358         reply = sendCommand(new SayDateTimeCommand(time, escapeDigits, format));
359         return reply.getResultCodeAsChar();
360     }
361 
362     public char sayDateTime(long time, String escapeDigits) throws AGIException
363     {
364         AGIReply reply;
365 
366         reply = sendCommand(new SayDateTimeCommand(time, escapeDigits));
367         return reply.getResultCodeAsChar();
368     }
369 
370     public void sayDateTime(long time) throws AGIException
371     {
372         sendCommand(new SayDateTimeCommand(time));
373     }
374     
375     
376 }