public class ManagerConnectionImpl extends Object implements ManagerConnection
ManagerConnectionFactory
Modifier and Type | Field and Description |
---|---|
protected String |
password
The password to use for login as defined in Asterisk's
manager.conf . |
protected ManagerConnectionState |
state |
protected String |
username
The username to use for login as defined in Asterisk's
manager.conf . |
Constructor and Description |
---|
ManagerConnectionImpl()
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
void |
addEventListener(ManagerEventListener listener)
Registers an event listener that is called whenever an
ManagerEvent is receiced from the
Asterisk server. |
protected void |
connect() |
protected ManagerReader |
createReader(org.asteriskjava.manager.internal.Dispatcher dispatcher,
Object source) |
protected SocketConnectionFacade |
createSocket() |
protected ManagerWriter |
createWriter() |
void |
deregisterEventClass(Class<? extends ManagerEvent> eventClass) |
protected AsteriskVersion |
determineVersion() |
protected void |
disconnect()
Closes the socket connection.
|
void |
dispatchEvent(ManagerEvent event)
This method is called by the reader whenever a ManagerEvent is received.
|
void |
dispatchResponse(ManagerResponse response)
This method is called by the reader whenever a
ManagerResponse is
received. |
protected void |
doLogin(long timeout,
String eventMask)
Does the real login, following the steps outlined below.
|
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".
|
protected String |
getRawVersion() |
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 eventMask)
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 listener)
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)
Implements synchronous sending of "simple" actions.
|
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 |
setDefaultEventTimeout(long defaultEventTimeout)
Sets the time in milliseconds the synchronous method
sendEventGeneratingAction(EventGeneratingAction) will wait for a
response and the last response event before throwing a TimeoutException. |
void |
setDefaultResponseTimeout(long defaultResponseTimeout)
Sets the time in milliseconds the synchronous method
sendAction(ManagerAction) will wait for a response before
throwing a TimeoutException. |
void |
setEncoding(Charset encoding)
Sets the encoding to use to connect to the Asterisk server (eg.
|
void |
setHostname(String hostname)
Sets the hostname of the asterisk server to connect to.
|
void |
setKeepAliveAfterAuthenticationFailure(boolean keepAliveAfterAuthenticationFailure)
Set to
true to try reconnecting to ther asterisk serve even
if the reconnection attempt threw an AuthenticationFailedException. |
void |
setPassword(String password)
Sets the password to use to connect to the asterisk server.
|
void |
setPort(int port)
Sets the port to use to connect to the asterisk server.
|
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.
|
void |
setSsl(boolean ssl)
Sets whether to use SSL.
|
void |
setUsername(String username)
Sets the username to use to connect to the asterisk server.
|
String |
toString() |
protected String username
manager.conf
.protected String password
manager.conf
.protected ManagerConnectionState state
protected ManagerReader createReader(org.asteriskjava.manager.internal.Dispatcher dispatcher, Object source)
protected ManagerWriter createWriter()
public void setHostname(String hostname)
localhost
.hostname
- the hostname to connect topublic void setPort(int port)
manager.conf
file.
Default is 5038.port
- the port to connect topublic void setSsl(boolean ssl)
ssl
- true
to use SSL for the connection,
false
for a plain text connection.public void setUsername(String username)
manager.conf
file.username
- the username to use for loginpublic void setPassword(String password)
manager.conf
file.password
- the password to use for loginpublic void setEncoding(Charset encoding)
ManagerConnection
setEncoding
in interface ManagerConnection
encoding
- the encoding to use for manager/user events etc.public void setDefaultResponseTimeout(long defaultResponseTimeout)
sendAction(ManagerAction)
will wait for a response before
throwing a TimeoutException. defaultResponseTimeout
- default response timeout in millisecondspublic void setDefaultEventTimeout(long defaultEventTimeout)
sendEventGeneratingAction(EventGeneratingAction)
will wait for a
response and the last response event before throwing a TimeoutException.
defaultEventTimeout
- default event timeout in millisecondspublic 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.public String getUsername()
ManagerConnection
manager.conf
file.getUsername
in interface ManagerConnection
public String getPassword()
ManagerConnection
manager.conf
file.getPassword
in interface ManagerConnection
public Charset getEncoding()
ManagerConnection
getEncoding
in interface ManagerConnection
public AsteriskVersion getVersion()
ManagerConnection
getVersion
in interface ManagerConnection
public String getHostname()
ManagerConnection
getHostname
in interface ManagerConnection
public int getPort()
ManagerConnection
getPort
in interface ManagerConnection
public boolean isSsl()
ManagerConnection
Default is false.
isSsl
in interface ManagerConnection
true
if SSL is used for the connection,
false
for a plain text connection.public InetAddress getLocalAddress()
ManagerConnection
getLocalAddress
in interface ManagerConnection
public int getLocalPort()
ManagerConnection
getLocalPort
in interface ManagerConnection
public InetAddress getRemoteAddress()
ManagerConnection
getRemoteAddress
in interface ManagerConnection
public int getRemotePort()
ManagerConnection
getRemotePort
in interface ManagerConnection
public void registerUserEventClass(Class<? extends ManagerEvent> userEventClass)
ManagerConnection
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).
registerUserEventClass
in interface ManagerConnection
userEventClass
- the class representing the user event to register.UserEvent
,
ManagerEvent
public void setSocketTimeout(int socketTimeout)
ManagerConnection
Default is 0, that is using Java's built-in default.
setSocketTimeout
in interface ManagerConnection
socketTimeout
- the timeout value to be used in milliseconds.Socket.connect(java.net.SocketAddress, int)
public void setSocketReadTimeout(int socketReadTimeout)
ManagerConnection
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.
setSocketReadTimeout
in interface ManagerConnection
socketReadTimeout
- the read timeout value to be used in
milliseconds.Socket.setSoTimeout(int)
public void login() throws IOException, AuthenticationFailedException, TimeoutException
ManagerConnection
login
in interface ManagerConnection
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
public void login(String eventMask) throws IOException, AuthenticationFailedException, TimeoutException
ManagerConnection
login
in interface ManagerConnection
eventMask
- 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.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
protected void doLogin(long timeout, String eventMask) throws IOException, AuthenticationFailedException, TimeoutException
connect()
if not
already connected
ChallengeAction
requesting a challenge for authType
MD5.
ChallengeResponse
is received a LoginAction
is sent using the calculated key (MD5 hash of the password appended to
the received challenge).
timeout
- the maximum time to wait for the protocol identifier (in
ms)eventMask
- 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.IOException
- if there is an i/o problem.AuthenticationFailedException
- if username or password are
incorrect and the login action returns an error or if the MD5
hash cannot be computed. The connection is closed in this
case.TimeoutException
- if a timeout occurs while waiting for the
protocol identifier. The connection is closed in this case.protected AsteriskVersion determineVersion() throws IOException, TimeoutException
IOException
TimeoutException
protected String getRawVersion()
protected void connect() throws IOException
IOException
protected SocketConnectionFacade createSocket() throws IOException
IOException
public void logoff() throws IllegalStateException
ManagerConnection
logoff
in interface ManagerConnection
IllegalStateException
- if not in state CONNECTED or RECONNECTING.LogoffAction
protected void disconnect()
public ManagerResponse sendAction(ManagerAction action) throws IOException, TimeoutException, IllegalArgumentException, IllegalStateException
ManagerConnection
sendAction
in interface ManagerConnection
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.ManagerConnection.sendAction(ManagerAction, long)
,
ManagerConnection.sendAction(ManagerAction, SendActionCallback)
public ManagerResponse sendAction(ManagerAction action, long timeout) throws IOException, TimeoutException, IllegalArgumentException, IllegalStateException
sendAction
in interface ManagerConnection
timeout
- - in millisecondsaction
- the action to send to the Asterisk serverIOException
- 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.ManagerConnection.sendAction(ManagerAction, SendActionCallback)
public void sendAction(ManagerAction action, SendActionCallback callback) throws IOException, IllegalArgumentException, IllegalStateException
ManagerConnection
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.sendAction
in interface ManagerConnection
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.public ResponseEvents sendEventGeneratingAction(EventGeneratingAction action) throws IOException, EventTimeoutException, IllegalArgumentException, IllegalStateException
ManagerConnection
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
.
sendEventGeneratingAction
in interface ManagerConnection
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
public ResponseEvents sendEventGeneratingAction(EventGeneratingAction action, long timeout) throws IOException, EventTimeoutException, IllegalArgumentException, IllegalStateException
ManagerConnection
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
.
sendEventGeneratingAction
in interface ManagerConnection
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
public void addEventListener(ManagerEventListener listener)
ManagerConnection
ManagerEvent
is receiced from the
Asterisk server.
Event listeners are notified about new events in the same order as they were registered.
addEventListener
in interface ManagerConnection
listener
- the listener to call whenever a manager event is
receivedManagerConnection.removeEventListener(ManagerEventListener)
public void removeEventListener(ManagerEventListener listener)
ManagerConnection
Does nothing if the given event listener hasn't be been regiered before.
removeEventListener
in interface ManagerConnection
listener
- the listener to removeManagerConnection.addEventListener(ManagerEventListener)
public String getProtocolIdentifier()
ManagerConnection
getProtocolIdentifier
in interface ManagerConnection
null
otherwisepublic ManagerConnectionState getState()
ManagerConnection
getState
in interface ManagerConnection
public void dispatchResponse(ManagerResponse response)
ManagerResponse
is
received. The response is dispatched to the associated
SendActionCallback
.response
- the response received by the readerManagerReader
public void dispatchEvent(ManagerEvent event)
event
- the event received by the readeraddEventListener(ManagerEventListener)
,
removeEventListener(ManagerEventListener)
,
ManagerReader
public void deregisterEventClass(Class<? extends ManagerEvent> eventClass)
deregisterEventClass
in interface ManagerConnection
Copyright © 2004–2017. All rights reserved.