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.manager;
18  
19  import java.io.IOException;
20  
21  import net.sf.asterisk.manager.action.EventGeneratingAction;
22  import net.sf.asterisk.manager.action.ManagerAction;
23  import net.sf.asterisk.manager.event.ManagerEvent;
24  import net.sf.asterisk.manager.response.ManagerResponse;
25  
26  /***
27   * The main interface to talk to an Asterisk server via the Asterisk Manager
28   * API.<br>
29   * The ManagerConnection repesents a connection to an Asterisk Server and is
30   * capable to send Actions and receive Responses and Events. It does not add any
31   * valuable functionality but rather provides a Java view to Asterisk's Manager
32   * API (freeing you from TCP/IP connection and parsing stuff).<br>
33   * It is used as the foundation for higher leveled interfaces like the
34   * AsteriskManager.<br>
35   * A concrete implementation of this interface can be obtained from a
36   * ManagerConnectionFactory.
37   * 
38   * @see net.sf.asterisk.manager.ManagerConnectionFactory
39   * @author srt
40   * @version $Id: ManagerConnection.java,v 1.12 2005/10/10 20:33:55 srt Exp $
41   */
42  public interface ManagerConnection
43  {
44      /***
45       * Registers a new user event type.<br>
46       * Asterisk allows you to send custom events via the UserEvent application.
47       * If you choose to send such events you can extend the abstract class
48       * UserEvent provide a name for your new event and optionally add your own
49       * attributes. After registering a user event type Asterisk-Java will handle
50       * such events the same way it handles the internal events and inform your
51       * registered event handlers.<br>
52       * Note: If you write your own Asterisk applications that use Asterisk's
53       * <code>manager_event()</code> function directly and don't use the
54       * channel and uniqueid attributes provided by UserEvent you can also
55       * register events that directly subclass ManagerEvent.
56       * 
57       * @see net.sf.asterisk.manager.event.UserEvent
58       * @see ManagerEvent
59       * @param userEventClass the class of the user event to register.
60       */
61      void registerUserEventClass(Class userEventClass);
62  
63      /***
64       * The timeout to use when connecting the the Asterisk server.<br>
65       * Default is 0, that is using Java's built-in default.
66       * 
67       * @param socketTimeout the timeout value to be used in milliseconds.
68       * @see java.net.Socket#connect(java.net.SocketAddress, int)
69       * @since 0.2
70       */
71      public void setSocketTimeout(int socketTimeout);
72  
73      /***
74       * Logs in to the Asterisk server with the username and password specified
75       * when this connection was created.
76       * 
77       * @throws IOException if the network connection is disrupted.
78       * @throws AuthenticationFailedException if the username and/or password are
79       *             incorrect or the ChallengeResponse could not be built.
80       * @throws TimeoutException if no response is received within the default
81       *             timeout period. If the implementation uses challenge/response
82       *             this can either be a timeout of the ChallengeAction or the
83       *             LoginAction; otherwise it is always a timeout of the
84       *             LoginAction.
85       * @see net.sf.asterisk.manager.action.LoginAction
86       * @see net.sf.asterisk.manager.action.ChallengeAction
87       */
88      void login() throws IOException, AuthenticationFailedException,
89              TimeoutException;
90  
91      /***
92       * Sends a LogoffAction to the Asterisk server and disconnects.
93       * 
94       * @throws IOException if the network connection is disrupted.
95       * @throws TimeoutException if no response to the logoff action is received
96       *             within the default timeout period.
97       * @see net.sf.asterisk.manager.action.LogoffAction
98       */
99      void logoff() throws IOException, TimeoutException;
100 
101     /***
102      * Returns the protocol identifier, i.e. a string like "Asterisk Call
103      * Manager/1.0".
104      * 
105      * @return the protocol identifier of the Asterisk Manager Interface in use
106      *         if it has already been received; <code>null</code> otherwise
107      */
108     String getProtocolIdentifier();
109 
110     /***
111      * Sends a ManagerAction to the Asterisk server and waits for the
112      * corresponding ManagerResponse.
113      * 
114      * @param action the action to send to the Asterisk server
115      * @return the corresponding response received from the Asterisk server
116      * @throws IOException if the network connection is disrupted.
117      * @throws TimeoutException if no response is received within the default
118      *             timeout period.
119      * @throws IllegalArgumentException if the action is <code>null</code>.
120      * @throws IllegalStateException if you are not connected to an Asterisk
121      *             server.
122      * @see #sendAction(ManagerAction, long)
123      * @see #sendAction(ManagerAction, ManagerResponseHandler)
124      */
125     ManagerResponse sendAction(ManagerAction action) throws IOException,
126             TimeoutException, IllegalArgumentException, IllegalStateException;
127 
128     /***
129      * Sends a ManagerAction to the Asterisk server and waits for the
130      * corresponding ManagerResponse.
131      * 
132      * @param action the action to send to the Asterisk server
133      * @param timeout milliseconds to wait for the response before throwing a
134      *            TimeoutException
135      * @return the corresponding response received from the Asterisk server
136      * @throws IOException if the network connection is disrupted.
137      * @throws TimeoutException if no response is received within the given
138      *             timeout period.
139      * @throws IllegalArgumentException if the action is <code>null</code>.
140      * @throws IllegalStateException if you are not connected to an Asterisk
141      *             server.
142      * @see #sendAction(ManagerAction, ManagerResponseHandler)
143      */
144     ManagerResponse sendAction(ManagerAction action, long timeout)
145             throws IOException, TimeoutException, IllegalArgumentException,
146             IllegalStateException;
147 
148     /***
149      * Sends a ManagerAction to the Asterisk server and registers a callback
150      * handler to be called when the corresponding ManagerResponse is received.
151      * 
152      * @param action the action to send to the Asterisk server
153      * @param callbackHandler the callback handler to call when the response is
154      *            received or <code>null</code> if you are not interested in
155      *            the response
156      * @throws IOException if the network connection is disrupted.
157      * @throws IllegalArgumentException if the action is <code>null</code>.
158      * @throws IllegalStateException if you are not connected to an Asterisk
159      *             server.
160      */
161     void sendAction(ManagerAction action, ManagerResponseHandler callbackHandler)
162             throws IOException, IllegalArgumentException, IllegalStateException;
163 
164     /***
165      * Sends a EventGeneratingAction to the Asterisk server and waits for the
166      * corresponding ManagerResponse and the ResponseEvents.<br>
167      * EventGeneratingActions are ManagerActions that don't return their
168      * response in the corresponding ManagerResponse but send a series of events
169      * that contain the payload.<br>
170      * Examples for EventGeneratingActions are the
171      * {@link net.sf.asterisk.manager.action.StatusAction}, the
172      * {@link net.sf.asterisk.manager.action.QueueAction} or the
173      * {@link net.sf.asterisk.manager.action.AgentsAction}.
174      * 
175      * @param action the action to send to the Asterisk server
176      * @return a ResponseEvents that contains the corresponding response and
177      *         response events received from the Asterisk server
178      * @throws IOException if the network connection is disrupted.
179      * @throws EventTimeoutException if no response or not all response events are
180      *             received within the given timeout period.
181      * @throws IllegalArgumentException if the action is <code>null</code>,
182      *             the actionCompleteEventClass property of the action is
183      *             <code>null</code> or if actionCompleteEventClass is not a
184      *             ResponseEvent.
185      * @throws IllegalStateException if you are not connected to an Asterisk
186      *             server.
187      * @see EventGeneratingAction
188      * @see net.sf.asterisk.manager.event.ResponseEvent
189      * @since 0.2
190      */
191     ResponseEvents sendEventGeneratingAction(EventGeneratingAction action)
192             throws IOException, EventTimeoutException, IllegalArgumentException,
193             IllegalStateException;
194 
195     /***
196      * Sends a EventGeneratingAction to the Asterisk server and waits for the
197      * corresponding ManagerResponse and the ResponseEvents.
198      * EventGeneratingActions are ManagerActions that don't return their
199      * response in the corresponding ManagerResponse but send a series of events
200      * that contain the payload.<br>
201      * Examples for EventGeneratingActions are the
202      * {@link net.sf.asterisk.manager.action.StatusAction}, the
203      * {@link net.sf.asterisk.manager.action.QueueAction} or the
204      * {@link net.sf.asterisk.manager.action.AgentsAction}.
205      * 
206      * @param action the action to send to the Asterisk server
207      * @param timeout milliseconds to wait for the response and the response
208      *            events before throwing a TimeoutException
209      * @return a ResponseEvents that contains the corresponding response and
210      *         response events received from the Asterisk server
211      * @throws IOException if the network connection is disrupted.
212      * @throws EventTimeoutException if no response or not all response events are
213      *             received within the given timeout period.
214      * @throws IllegalArgumentException if the action is <code>null</code>,
215      *             the actionCompleteEventClass property of the action is
216      *             <code>null</code> or if actionCompleteEventClass is not a
217      *             ResponseEvent.
218      * @throws IllegalStateException if you are not connected to an Asterisk
219      *             server.
220      * @see EventGeneratingAction
221      * @see net.sf.asterisk.manager.event.ResponseEvent
222      * @since 0.2
223      */
224     ResponseEvents sendEventGeneratingAction(EventGeneratingAction action,
225             long timeout) throws IOException, EventTimeoutException,
226             IllegalArgumentException, IllegalStateException;
227 
228     /***
229      * Registers an event handler to be called whenever an
230      * {@link net.sf.asterisk.manager.event.ManagerEvent} is receiced from the
231      * Asterisk server.<br>
232      * Event handlers are notified about new events in the same order as they
233      * were registered via addEventHandler.
234      * 
235      * @param eventHandler the handler to call whenever a manager event is
236      *            received
237      * @see #removeEventHandler(ManagerEventHandler)
238      */
239     void addEventHandler(ManagerEventHandler eventHandler);
240 
241     /***
242      * Unregisters a previously registered event handler.<br>
243      * Does nothing if the given event handler hasn't be been regiered before.
244      * 
245      * @param eventHandler the event handle to unregister
246      * @see #addEventHandler(ManagerEventHandler)
247      */
248     void removeEventHandler(ManagerEventHandler eventHandler);
249 
250     /***
251      * Returns the Asterisk server of this connection.
252      * 
253      * @return the Asterisk server of this connection.
254      */
255     AsteriskServer getAsteriskServer();
256 
257     /***
258      * Checks if the connection to the Asterisk server is established.
259      * 
260      * @return <code>true</code> if the connection is established,
261      *         <code>false</code> otherwise.
262      * @since 0.2
263      */
264     boolean isConnected();
265 }