public interface ManagerConnection
The ManagerConnection repesents a connection to an Asterisk server and is
capable of sending ManagerAction
s and
receiving ManagerResponse
s and
ManagerEvent
s. It does not add any
further functionality but rather provides a Java view to Asterisk's Manager
API (freeing you from TCP/IP connection and parsing stuff).
It is used as the foundation for higher leveled interfaces like the Asterisk-Java Live.
A concrete implementation of this interface can be obtained from a
ManagerConnectionFactory
.
ManagerConnectionFactory
Modifier and Type | Method and Description |
---|---|
void |
addEventListener(ManagerEventListener eventListener)
Registers an event listener that is called whenever an
ManagerEvent is receiced from the
Asterisk server. |
void |
deregisterEventClass(Class<? extends ManagerEvent> eventClass) |
Charset |
getEncoding()
Returns the encoding to use to connect to the Asterisk server (eg.
|
String |
getHostname()
Returns the hostname of the connected Asterisk server.
|
InetAddress |
getLocalAddress()
Returns the local address of the underlying socket connection.
|
int |
getLocalPort()
Returns the local port of the underlying socket connection.
|
String |
getPassword()
Returns the password to use to connect to the Asterisk server.
|
int |
getPort()
Returns the Manager API port of the connected Asterisk server.
|
String |
getProtocolIdentifier()
Returns the protocol identifier, that is a string like "Asterisk Call
Manager/1.0".
|
InetAddress |
getRemoteAddress()
Returns the remote address of the underlying socket connection.
|
int |
getRemotePort()
Returns the remote port of the underlying socket connection.
|
ManagerConnectionState |
getState()
Returns the lifecycle status of this connection.
|
String |
getUsername()
Returns the username to use to connect to the Asterisk server.
|
AsteriskVersion |
getVersion()
Returns the version of Asterisk running in the server.
|
boolean |
isSsl()
Returns whether to use SSL.
|
void |
login()
Logs in to the Asterisk server with the username and password specified
when this connection was created.
|
void |
login(String events)
Logs in to the Asterisk server with the username and password specified
when this connection was created and a given event mask.
|
void |
logoff()
Sends a LogoffAction to the Asterisk server and disconnects.
|
void |
registerUserEventClass(Class<? extends ManagerEvent> userEventClass)
Registers a new user event type.
|
void |
removeEventListener(ManagerEventListener eventListener)
Unregisters a previously registered event listener.
|
ManagerResponse |
sendAction(ManagerAction action)
Sends a ManagerAction to the Asterisk server and waits for the
corresponding ManagerResponse.
|
ManagerResponse |
sendAction(ManagerAction action,
long timeout)
Sends a ManagerAction to the Asterisk server and waits for the
corresponding
ManagerResponse . |
void |
sendAction(ManagerAction action,
SendActionCallback callback)
Sends a ManagerAction to the Asterisk server and registers a callback
handler to be called when the corresponding
ManagerResponse is
received. |
ResponseEvents |
sendEventGeneratingAction(EventGeneratingAction action)
Sends an
EventGeneratingAction to the Asterisk server and waits
for the corresponding ManagerResponse and the
ResponseEvent s |
ResponseEvents |
sendEventGeneratingAction(EventGeneratingAction action,
long timeout)
Sends an
EventGeneratingAction to the Asterisk server and waits
for the corresponding ManagerResponse and the
ResponseEvent s |
void |
sendEventGeneratingAction(EventGeneratingAction action,
SendEventGeneratingActionCallback callback)
Asynchronously sends an
EventGeneratingAction to the Asterisk
server. |
void |
setEncoding(Charset encoding)
Sets the encoding to use to connect to the Asterisk server (eg.
|
void |
setKeepAliveAfterAuthenticationFailure(boolean keepAliveAfterAuthenticationFailure)
Set to
true to try reconnecting to ther asterisk serve even
if the reconnection attempt threw an AuthenticationFailedException. |
void |
setSocketReadTimeout(int socketReadTimeout)
Connection is dropped (and restarted) if it stales on read longer than
the timeout.
|
void |
setSocketTimeout(int socketTimeout)
The timeout to use when connecting the the Asterisk server.
|
String getHostname()
int getPort()
String getUsername()
manager.conf
file.String getPassword()
manager.conf
file.AsteriskVersion getVersion()
void setEncoding(Charset encoding)
encoding
- the encoding to use for manager/user events etc.Charset getEncoding()
boolean isSsl()
Default is false.
true
if SSL is used for the connection,
false
for a plain text connection.InetAddress getLocalAddress()
int getLocalPort()
InetAddress getRemoteAddress()
int getRemotePort()
void registerUserEventClass(Class<? extends ManagerEvent> userEventClass)
Asterisk allows you to send custom events via the UserEvent application. If you choose to send such events you can extend the abstract class UserEvent provide a name for your new event and optionally add your own attributes. After registering a user event type Asterisk-Java will handle such events the same way it handles the internal events and inform your registered event handlers.
Note: If you write your own Asterisk applications that use Asterisk's
manager_event()
function directly and don't use the channel
and uniqueid attributes provided by UserEvent you can also register
events that directly subclass ManagerEvent
.
The event class must be a concrete class with a default constructor (one that takes no arguments).
userEventClass
- the class representing the user event to register.IllegalArgumentException
- if userEventClass is not a valid event
class.UserEvent
,
ManagerEvent
void setSocketTimeout(int socketTimeout)
Default is 0, that is using Java's built-in default.
socketTimeout
- the timeout value to be used in milliseconds.Socket.connect(java.net.SocketAddress, int)
void setSocketReadTimeout(int socketReadTimeout)
If you set this property to a non zero value be sure to also use a
PingThread
or somthing similar to make sure there is some network
traffic, otherwise you will encounter lots of unexpected reconnects. The
read timeout should be at least twice the interval set for the
PingThread.
Default is 0, that is no read timeout.
socketReadTimeout
- the read timeout value to be used in
milliseconds.Socket.setSoTimeout(int)
void setKeepAliveAfterAuthenticationFailure(boolean keepAliveAfterAuthenticationFailure)
true
to try reconnecting to ther asterisk serve even
if the reconnection attempt threw an AuthenticationFailedException. true
.keepAliveAfterAuthenticationFailure
- true
to try
reconnecting to ther asterisk serve even if the reconnection attempt
threw an AuthenticationFailedException, false
otherwise.void login() throws IllegalStateException, IOException, AuthenticationFailedException, TimeoutException
IllegalStateException
- if connection is not in state INITIAL or
DISCONNECTED.IOException
- if the network connection is disrupted.AuthenticationFailedException
- if the username and/or password are
incorrect or the ChallengeResponse could not be built.TimeoutException
- if a timeout occurs while waiting for the
protocol identifier. The connection is closed in this case.LoginAction
,
ChallengeAction
void login(String events) throws IllegalStateException, IOException, AuthenticationFailedException, TimeoutException
events
- the event mask. Set to "on" if all events should be send,
"off" if not events should be sent or a combination of
"system", "call" and "log" (separated by ',') to specify what
kind of events should be sent.IllegalStateException
- if connection is not in state INITIAL or
DISCONNECTED.IOException
- if the network connection is disrupted.AuthenticationFailedException
- if the username and/or password are
incorrect or the ChallengeResponse could not be built.TimeoutException
- if a timeout occurs while waiting for the
protocol identifier. The connection is closed in this case.LoginAction
,
ChallengeAction
void logoff() throws IllegalStateException
IllegalStateException
- if not in state CONNECTED or RECONNECTING.LogoffAction
String getProtocolIdentifier()
null
otherwiseManagerConnectionState getState()
ManagerResponse sendAction(ManagerAction action) throws IOException, TimeoutException, IllegalArgumentException, IllegalStateException
action
- the action to send to the Asterisk serverIOException
- if the network connection is disrupted.TimeoutException
- if no response is received within the default
timeout period.IllegalArgumentException
- if the action is null
.IllegalStateException
- if you are not connected to an Asterisk
server.sendAction(ManagerAction, long)
,
sendAction(ManagerAction, SendActionCallback)
ManagerResponse sendAction(ManagerAction action, long timeout) throws IOException, TimeoutException, IllegalArgumentException, IllegalStateException
ManagerResponse
.action
- the action to send to the Asterisk servertimeout
- milliseconds to wait for the response before throwing a
TimeoutExceptionIOException
- if the network connection is disrupted.TimeoutException
- if no response is received within the given
timeout period.IllegalArgumentException
- if the action is null
.IllegalStateException
- if you are not connected to an Asterisk
server.sendAction(ManagerAction, SendActionCallback)
void sendAction(ManagerAction action, SendActionCallback callback) throws IOException, IllegalArgumentException, IllegalStateException
ManagerResponse
is
received. Be very careful that your callbackHandler terminates very
quickly and does not do any fancy processing because it is called from
the reader thread which is blocked for the time it takes to execute your
callbackHandler.action
- the action to send to the Asterisk servercallback
- the callback handler to call when the response is
received or null
if you are not interested in the
responseIOException
- if the network connection is disrupted.IllegalArgumentException
- if the action is null
.IllegalStateException
- if you are not connected to the Asterisk
server.ResponseEvents sendEventGeneratingAction(EventGeneratingAction action) throws IOException, EventTimeoutException, IllegalArgumentException, IllegalStateException
EventGeneratingAction
to the Asterisk server and waits
for the corresponding ManagerResponse
and the
ResponseEvent
s
EventGeneratingActions are ManagerAction
s that don't return their
response in the corresponding ManagerResponse
but send a series
of events that contain the payload.
This method will block until the correpsonding action complete event has
been received. The action complete event is determined by
EventGeneratingAction.getActionCompleteEventClass()
.
Examples for EventGeneratingActions are
StatusAction
,
QueueStatusAction
or
AgentsAction
.
action
- the action to send to the Asterisk serverIOException
- if the network connection is disrupted.EventTimeoutException
- if no response or not all response events
are received within the given timeout period.IllegalArgumentException
- if the action is null
, the
actionCompleteEventClass property of the action is
null
or if actionCompleteEventClass is not a
ResponseEvent.IllegalStateException
- if you are not connected to an Asterisk
server.EventGeneratingAction
,
ResponseEvent
ResponseEvents sendEventGeneratingAction(EventGeneratingAction action, long timeout) throws IOException, EventTimeoutException, IllegalArgumentException, IllegalStateException
EventGeneratingAction
to the Asterisk server and waits
for the corresponding ManagerResponse
and the
ResponseEvent
s
EventGeneratingActions are ManagerAction
s that don't return their
response in the corresponding ManagerResponse
but send a series
of events that contain the payload.
This method will block until the correpsonding action complete event has
been received but no longer that timeout seconds. The action complete
event is determined by
EventGeneratingAction.getActionCompleteEventClass()
.
Examples for EventGeneratingActions are the
StatusAction
, the
QueueStatusAction
or the
AgentsAction
.
action
- the action to send to the Asterisk servertimeout
- milliseconds to wait for the response and the action
complete event before throwing a TimeoutExceptionIOException
- if the network connection is disrupted.EventTimeoutException
- if no response or not all response events
are received within the given timeout period.IllegalArgumentException
- if the action is null
, the
actionCompleteEventClass property of the action is
null
or if actionCompleteEventClass is not a
ResponseEvent.IllegalStateException
- if you are not connected to an Asterisk
server.EventGeneratingAction
,
ResponseEvent
void sendEventGeneratingAction(EventGeneratingAction action, SendEventGeneratingActionCallback callback) throws IOException, IllegalArgumentException, IllegalStateException
EventGeneratingAction
to the Asterisk
server. This is similar to
sendEventGeneratingAction(EventGeneratingAction, long)
except it
does NOT block to wait for the completion. Instead after the action
completes or fails the result is provided to the optional callback via
SendEventGeneratingActionCallback.onResponse(ResponseEvents)
.void addEventListener(ManagerEventListener eventListener)
ManagerEvent
is receiced from the
Asterisk server.
Event listeners are notified about new events in the same order as they were registered.
eventListener
- the listener to call whenever a manager event is
receivedremoveEventListener(ManagerEventListener)
void removeEventListener(ManagerEventListener eventListener)
Does nothing if the given event listener hasn't be been regiered before.
eventListener
- the listener to removeaddEventListener(ManagerEventListener)
void deregisterEventClass(Class<? extends ManagerEvent> eventClass)
Copyright © 2004–2020. All rights reserved.