Asterisk-Java

org.asteriskjava.live
Class DefaultAsteriskServer

java.lang.Object
  extended by org.asteriskjava.live.DefaultAsteriskServer
All Implemented Interfaces:
AsteriskServer
Direct Known Subclasses:
SecureAsteriskServer

public class DefaultAsteriskServer
extends java.lang.Object
implements AsteriskServer

Default implementation of the AsteriskServer interface.

Version:
$Id: DefaultAsteriskServer.java 599 2007-02-03 16:35:22Z srt $
Author:
srt
See Also:
AsteriskServer

Constructor Summary
DefaultAsteriskServer()
          Creates a new instance without a ManagerConnection.
DefaultAsteriskServer(ManagerConnection eventConnection)
          Creates a new instance that uses the given ManagerConnection.
DefaultAsteriskServer(java.lang.String hostname, int port, java.lang.String username, java.lang.String password)
          Creates a new instance and a new ManagerConnection with the given connection data.
DefaultAsteriskServer(java.lang.String hostname, java.lang.String username, java.lang.String password)
          Creates a new instance and a new ManagerConnection with the given connection data.
 
Method Summary
 void addAsteriskServerListener(AsteriskServerListener listener)
           
protected  DefaultManagerConnection createManagerConnection(java.lang.String hostname, int port, java.lang.String username, java.lang.String password)
           
 AsteriskChannel getChannelById(java.lang.String id)
          Returns a channel by its unique id.
 AsteriskChannel getChannelByName(java.lang.String name)
          Returns a channel by its name.
 java.util.Collection<AsteriskChannel> getChannels()
          Returns the active channels of the Asterisk server.
 java.lang.String getGlobalVariable(java.lang.String variable)
          Returns the value of the given global variable.
 ManagerConnection getManagerConnection()
          Returns the underlying ManagerConnection.
 MeetMeRoom getMeetMeRoom(java.lang.String name)
          Returns the MeetMe room with the given number, if the room does not yet exist a new MeetMeRoom object is created.
 java.util.Collection<MeetMeRoom> getMeetMeRooms()
          Returns the acitve MeetMe rooms on the Asterisk server.
 java.util.Collection<AsteriskQueue> getQueues()
          Returns the queues served by the Asterisk server.
 java.lang.String getVersion()
          Returns the exact version string of this Asterisk server.
 int[] getVersion(java.lang.String file)
          Returns the CVS revision of a given source file of this Asterisk server.
 java.util.Collection<Voicemailbox> getVoicemailboxes()
          Returns a collection of all voicemailboxes configured for this Asterisk server with the number of new and old messages they contain.
 void initialize()
           
 AsteriskChannel originateToApplication(java.lang.String channel, java.lang.String application, java.lang.String data, long timeout)
          Generates an outgoing channel to an application.
 AsteriskChannel originateToApplication(java.lang.String channel, java.lang.String application, java.lang.String data, long timeout, CallerId callerId, java.util.Map<java.lang.String,java.lang.String> variables)
          Generates an outgoing channel to an application and sets an optional map of channel variables.
 void originateToApplicationAsync(java.lang.String channel, java.lang.String application, java.lang.String data, long timeout, CallerId callerId, java.util.Map<java.lang.String,java.lang.String> variables, OriginateCallback cb)
          Asynchronously generates an outgoing channel to an application and sets an optional map of channel variables.
 void originateToApplicationAsync(java.lang.String channel, java.lang.String application, java.lang.String data, long timeout, OriginateCallback cb)
          Asynchronously generates an outgoing channel to an application.
 AsteriskChannel originateToExtension(java.lang.String channel, java.lang.String context, java.lang.String exten, int priority, long timeout)
          Generates an outgoing channel to a dialplan entry (extension, context, priority).
 AsteriskChannel originateToExtension(java.lang.String channel, java.lang.String context, java.lang.String exten, int priority, long timeout, CallerId callerId, java.util.Map<java.lang.String,java.lang.String> variables)
          Generates an outgoing channel to a dialplan entry (extension, context, priority) and sets an optional map of channel variables.
 void originateToExtensionAsync(java.lang.String channel, java.lang.String context, java.lang.String exten, int priority, long timeout, CallerId callerId, java.util.Map<java.lang.String,java.lang.String> variables, OriginateCallback cb)
          Asynchronously generates an outgoing channel to a dialplan entry (extension, context, priority) and sets an optional map of channel variables.
 void originateToExtensionAsync(java.lang.String channel, java.lang.String context, java.lang.String exten, int priority, long timeout, OriginateCallback cb)
          Asynchronously generates an outgoing channel to a dialplan entry (extension, context, priority).
 void removeAsteriskServerListener(AsteriskServerListener listener)
           
 void setGlobalVariable(java.lang.String variable, java.lang.String value)
          Sets the value of the given global variable.
 void setManagerConnection(ManagerConnection eventConnection)
           
 void setSkipQueues(boolean skipQueues)
          Determines if queue status is retrieved at startup.
 void shutdown()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultAsteriskServer

public DefaultAsteriskServer()
Creates a new instance without a ManagerConnection. The ManagerConnection must be set using setManagerConnection(ManagerConnection) before you can use this AsteriskServer.


DefaultAsteriskServer

public DefaultAsteriskServer(java.lang.String hostname,
                             java.lang.String username,
                             java.lang.String password)
Creates a new instance and a new ManagerConnection with the given connection data.

Parameters:
hostname - the hostname of the Asterisk server to connect to.
username - the username to use for login
password - the password to use for login

DefaultAsteriskServer

public DefaultAsteriskServer(java.lang.String hostname,
                             int port,
                             java.lang.String username,
                             java.lang.String password)
Creates a new instance and a new ManagerConnection with the given connection data.

Parameters:
hostname - the hostname of the Asterisk server to connect to.
port - the port where Asterisk listens for incoming Manager API connections, usually 5038.
username - the username to use for login
password - the password to use for login

DefaultAsteriskServer

public DefaultAsteriskServer(ManagerConnection eventConnection)
Creates a new instance that uses the given ManagerConnection.

Parameters:
eventConnection - the ManagerConnection to use for receiving events from Asterisk.
Method Detail

createManagerConnection

protected DefaultManagerConnection createManagerConnection(java.lang.String hostname,
                                                           int port,
                                                           java.lang.String username,
                                                           java.lang.String password)

setSkipQueues

public void setSkipQueues(boolean skipQueues)
Determines if queue status is retrieved at startup. If you don't need queue information and still run Asterisk 1.0.x you can set this to true to circumvent the startup delay caused by the missing QueueStatusComplete event.

Default is false.

Parameters:
skipQueues - true to skip queue initialization, false to not skip.
Since:
0.2

setManagerConnection

public void setManagerConnection(ManagerConnection eventConnection)

initialize

public void initialize()
                throws ManagerCommunicationException
Throws:
ManagerCommunicationException

getManagerConnection

public ManagerConnection getManagerConnection()
Description copied from interface: AsteriskServer
Returns the underlying ManagerConnection.

Specified by:
getManagerConnection in interface AsteriskServer
Returns:
the underlying ManagerConnection.

originateToExtension

public AsteriskChannel originateToExtension(java.lang.String channel,
                                            java.lang.String context,
                                            java.lang.String exten,
                                            int priority,
                                            long timeout)
                                     throws ManagerCommunicationException,
                                            NoSuchChannelException
Description copied from interface: AsteriskServer
Generates an outgoing channel to a dialplan entry (extension, context, priority).

Specified by:
originateToExtension in interface AsteriskServer
Parameters:
channel - channel name to call, for example "SIP/1310".
context - context to connect to
exten - extension to connect to
priority - priority to connect to
timeout - how long to wait for the channel to be answered before its considered to have failed (in ms)
Returns:
the generated channel
Throws:
ManagerCommunicationException - if the originate action cannot be sent to Asterisk
NoSuchChannelException - if the channel is not available on the Asterisk server, for example because you used "SIP/1310" and 1310 is not a valid SIP user, the SIP channel module hasn't been loaded or the SIP or IAX peer is not registered currently.

originateToExtension

public AsteriskChannel originateToExtension(java.lang.String channel,
                                            java.lang.String context,
                                            java.lang.String exten,
                                            int priority,
                                            long timeout,
                                            CallerId callerId,
                                            java.util.Map<java.lang.String,java.lang.String> variables)
                                     throws ManagerCommunicationException,
                                            NoSuchChannelException
Description copied from interface: AsteriskServer
Generates an outgoing channel to a dialplan entry (extension, context, priority) and sets an optional map of channel variables.

Specified by:
originateToExtension in interface AsteriskServer
Parameters:
channel - channel name to call, for example "SIP/1310".
context - context to connect to
exten - extension to connect to
priority - priority to connect to
timeout - how long to wait for the channel to be answered before its considered to have failed (in ms)
callerId - callerId to use for the outgoing channel, may be null.
variables - channel variables to set, may be null.
Returns:
the generated channel
Throws:
ManagerCommunicationException - if the originate action cannot be sent to Asterisk
NoSuchChannelException - if the channel is not available on the Asterisk server, for example because you used "SIP/1310" and 1310 is not a valid SIP user, the SIP channel module hasn't been loaded or the SIP or IAX peer is not registered currently.

originateToApplication

public AsteriskChannel originateToApplication(java.lang.String channel,
                                              java.lang.String application,
                                              java.lang.String data,
                                              long timeout)
                                       throws ManagerCommunicationException,
                                              NoSuchChannelException
Description copied from interface: AsteriskServer
Generates an outgoing channel to an application.

Specified by:
originateToApplication in interface AsteriskServer
Parameters:
channel - channel name to call, for example "SIP/1310".
application - application to connect to, for example "MeetMe"
data - data to pass to the application, for example "1000|d", may be null.
timeout - how long to wait for the channel to be answered before its considered to have failed (in ms)
Returns:
the generated channel
Throws:
ManagerCommunicationException - if the originate action cannot be sent to Asterisk
NoSuchChannelException - if the channel is not available on the Asterisk server, for example because you used "SIP/1310" and 1310 is not a valid SIP user, the SIP channel module hasn't been loaded or the SIP or IAX peer is not registered currently.

originateToApplication

public AsteriskChannel originateToApplication(java.lang.String channel,
                                              java.lang.String application,
                                              java.lang.String data,
                                              long timeout,
                                              CallerId callerId,
                                              java.util.Map<java.lang.String,java.lang.String> variables)
                                       throws ManagerCommunicationException,
                                              NoSuchChannelException
Description copied from interface: AsteriskServer
Generates an outgoing channel to an application and sets an optional map of channel variables.

Specified by:
originateToApplication in interface AsteriskServer
Parameters:
channel - channel name to call, for example "SIP/1310".
application - application to connect to, for example "MeetMe"
data - data to pass to the application, for example "1000|d", may be null.
timeout - how long to wait for the channel to be answered before its considered to have failed (in ms)
callerId - callerId to use for the outgoing channel, may be null.
variables - channel variables to set, may be null.
Returns:
the generated channel
Throws:
ManagerCommunicationException - if the originate action cannot be sent to Asterisk
NoSuchChannelException - if the channel is not available on the Asterisk server, for example because you used "SIP/1310" and 1310 is not a valid SIP user, the SIP channel module hasn't been loaded or the SIP or IAX peer is not registered currently.

originateToApplicationAsync

public void originateToApplicationAsync(java.lang.String channel,
                                        java.lang.String application,
                                        java.lang.String data,
                                        long timeout,
                                        CallerId callerId,
                                        java.util.Map<java.lang.String,java.lang.String> variables,
                                        OriginateCallback cb)
                                 throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Asynchronously generates an outgoing channel to an application and sets an optional map of channel variables.

Specified by:
originateToApplicationAsync in interface AsteriskServer
Parameters:
channel - channel name to call, for example "SIP/1310".
application - application to connect to, for example "MeetMe"
data - data to pass to the application, for example "1000|d", may be null.
timeout - how long to wait for the channel to be answered before its considered to have failed (in ms)
callerId - callerId to use for the outgoing channel, may be null.
variables - channel variables to set, may be null.
cb - callback to inform about the result
Throws:
ManagerCommunicationException - if the originate action cannot be sent to Asterisk

originateToApplicationAsync

public void originateToApplicationAsync(java.lang.String channel,
                                        java.lang.String application,
                                        java.lang.String data,
                                        long timeout,
                                        OriginateCallback cb)
                                 throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Asynchronously generates an outgoing channel to an application.

Specified by:
originateToApplicationAsync in interface AsteriskServer
Parameters:
channel - channel name to call, for example "SIP/1310".
application - application to connect to, for example "MeetMe"
data - data to pass to the application, for example "1000|d", may be null.
timeout - how long to wait for the channel to be answered before its considered to have failed (in ms)
cb - callback to inform about the result
Throws:
ManagerCommunicationException - if the originate action cannot be sent to Asterisk

originateToExtensionAsync

public void originateToExtensionAsync(java.lang.String channel,
                                      java.lang.String context,
                                      java.lang.String exten,
                                      int priority,
                                      long timeout,
                                      CallerId callerId,
                                      java.util.Map<java.lang.String,java.lang.String> variables,
                                      OriginateCallback cb)
                               throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Asynchronously generates an outgoing channel to a dialplan entry (extension, context, priority) and sets an optional map of channel variables.

Specified by:
originateToExtensionAsync in interface AsteriskServer
Parameters:
channel - channel name to call, for example "SIP/1310".
context - context to connect to
exten - extension to connect to
priority - priority to connect to
timeout - how long to wait for the channel to be answered before its considered to have failed (in ms)
callerId - callerId to use for the outgoing channel, may be null.
variables - channel variables to set, may be null.
cb - callback to inform about the result
Throws:
ManagerCommunicationException - if the originate action cannot be sent to Asterisk

originateToExtensionAsync

public void originateToExtensionAsync(java.lang.String channel,
                                      java.lang.String context,
                                      java.lang.String exten,
                                      int priority,
                                      long timeout,
                                      OriginateCallback cb)
                               throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Asynchronously generates an outgoing channel to a dialplan entry (extension, context, priority).

Specified by:
originateToExtensionAsync in interface AsteriskServer
Parameters:
channel - channel name to call, for example "SIP/1310".
context - context to connect to
exten - extension to connect to
priority - priority to connect to
timeout - how long to wait for the channel to be answered before its considered to have failed (in ms)
cb - callback to inform about the result
Throws:
ManagerCommunicationException - if the originate action cannot be sent to Asterisk

getChannels

public java.util.Collection<AsteriskChannel> getChannels()
                                                  throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Returns the active channels of the Asterisk server.

Specified by:
getChannels in interface AsteriskServer
Returns:
a Collection of active channels.
Throws:
ManagerCommunicationException - if there is a problem communication with Asterisk

getChannelByName

public AsteriskChannel getChannelByName(java.lang.String name)
                                 throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Returns a channel by its name.

Specified by:
getChannelByName in interface AsteriskServer
Parameters:
name - name of the channel to return
Returns:
the channel with the given name or null if there is no such channel.
Throws:
ManagerCommunicationException - if there is a problem communication with Asterisk

getChannelById

public AsteriskChannel getChannelById(java.lang.String id)
                               throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Returns a channel by its unique id.

Specified by:
getChannelById in interface AsteriskServer
Parameters:
id - the unique id of the channel to return
Returns:
the channel with the given unique id or null if there is no such channel.
Throws:
ManagerCommunicationException - if there is a problem communication with Asterisk

getMeetMeRooms

public java.util.Collection<MeetMeRoom> getMeetMeRooms()
                                                throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Returns the acitve MeetMe rooms on the Asterisk server.

Specified by:
getMeetMeRooms in interface AsteriskServer
Returns:
a Collection of MeetMeRooms
Throws:
ManagerCommunicationException - if there is a problem communication with Asterisk

getMeetMeRoom

public MeetMeRoom getMeetMeRoom(java.lang.String name)
                         throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Returns the MeetMe room with the given number, if the room does not yet exist a new MeetMeRoom object is created.

Specified by:
getMeetMeRoom in interface AsteriskServer
Parameters:
name - the number of the room to return
Returns:
the MeetMe room with the given number.
Throws:
ManagerCommunicationException - if there is a problem communication with Asterisk

getQueues

public java.util.Collection<AsteriskQueue> getQueues()
                                              throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Returns the queues served by the Asterisk server.

Specified by:
getQueues in interface AsteriskServer
Returns:
a Collection of queues.
Throws:
ManagerCommunicationException - if there is a problem communication with Asterisk

getVersion

public java.lang.String getVersion()
                            throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Returns the exact version string of this Asterisk server.

This typically looks like "Asterisk 1.2.9.1-BRIstuffed-0.3.0-PRE-1q built by root @ pbx0 on a i686 running Linux on 2006-06-20 20:21:30 UTC".

Specified by:
getVersion in interface AsteriskServer
Returns:
the version of this Asterisk server
Throws:
ManagerCommunicationException - if the version cannot be retrieved from Asterisk

getVersion

public int[] getVersion(java.lang.String file)
                 throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Returns the CVS revision of a given source file of this Asterisk server.

For example getVersion("app_meetme.c") may return {1, 102} for CVS revision "1.102".

Note that this feature is not available with Asterisk 1.0.x.

You can use this feature if you need to write applications that behave different depending on specific modules being available in a specific version or not.

Specified by:
getVersion in interface AsteriskServer
Parameters:
file - the file for which to get the version like "app_meetme.c"
Returns:
the CVS revision of the file, or null if that file is not part of the Asterisk instance you are connected to (maybe due to a module that provides it has not been loaded) or if you are connected to an Astersion 1.0.x
Throws:
ManagerCommunicationException - if the version cannot be retrieved from Asterisk

getGlobalVariable

public java.lang.String getGlobalVariable(java.lang.String variable)
                                   throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Returns the value of the given global variable.

Specified by:
getGlobalVariable in interface AsteriskServer
Parameters:
variable - the name of the global variable to return.
Returns:
the value of the global variable or null if it is not set.
Throws:
ManagerCommunicationException - if the get variable action cannot be sent to Asterisk.

setGlobalVariable

public void setGlobalVariable(java.lang.String variable,
                              java.lang.String value)
                       throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Sets the value of the given global variable.

Specified by:
setGlobalVariable in interface AsteriskServer
Parameters:
variable - the name of the global variable to set.
value - the value of the global variable to set.
Throws:
ManagerCommunicationException - if the set variable action cannot be sent to Asterisk.

getVoicemailboxes

public java.util.Collection<Voicemailbox> getVoicemailboxes()
                                                     throws ManagerCommunicationException
Description copied from interface: AsteriskServer
Returns a collection of all voicemailboxes configured for this Asterisk server with the number of new and old messages they contain.

Specified by:
getVoicemailboxes in interface AsteriskServer
Returns:
a collection of all voicemailboxes configured for this Asterisk server
Throws:
ManagerCommunicationException - if the voicemailboxes can't be retrieved.

addAsteriskServerListener

public void addAsteriskServerListener(AsteriskServerListener listener)
                               throws ManagerCommunicationException
Specified by:
addAsteriskServerListener in interface AsteriskServer
Throws:
ManagerCommunicationException

removeAsteriskServerListener

public void removeAsteriskServerListener(AsteriskServerListener listener)
Specified by:
removeAsteriskServerListener in interface AsteriskServer

shutdown

public void shutdown()
Specified by:
shutdown in interface AsteriskServer

Asterisk-Java

Copyright © 2004-2007 Stefan Reuter. All Rights Reserved.