|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.asterisk.manager.DefaultManagerConnection
public class DefaultManagerConnection
Default implemention of the ManagerConnection interface.
Generelly avoid direct use of this class. Use the ManagerConnectionFactory to
obtain a ManagerConnection instead.
When using a dependency injection framework like Spring direct usage for
wiring up beans that require a ManagerConnection property is fine though.
Note that the DefaultManagerConnection will create one new Thread
for reading data from Asterisk on the first call to on of the login()
methods.
ManagerConnectionFactory
Field Summary | |
---|---|
protected boolean |
keepAlive
Should we attempt to reconnect when the connection is lost? This is set to true after successful login and to
false after logoff or after an authentication failure when
keepAliveAfterAuthenticationFailure is false . |
protected java.lang.String |
password
The password to use for login as defined in Asterisk's manager.conf . |
protected java.lang.String |
username
The username to use for login as defined in Asterisk's manager.conf . |
Constructor Summary | |
---|---|
DefaultManagerConnection()
Creates a new instance. |
|
DefaultManagerConnection(java.lang.String hostname,
int port,
java.lang.String username,
java.lang.String password)
Creates a new instance with the given connection parameters. |
Method Summary | |
---|---|
void |
addEventHandler(ManagerEventHandler eventHandler)
Registers an event handler to be called whenever an ManagerEvent is receiced from the
Asterisk server.Event handlers are notified about new events in the same order as they were registered via addEventHandler. |
protected void |
connect()
|
protected ManagerReader |
createReader(Dispatcher dispatcher,
AsteriskServer server)
|
protected SocketConnectionFacade |
createSocket()
|
protected ManagerWriter |
createWriter()
|
protected AsteriskVersion |
determineVersion()
|
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. |
AsteriskServer |
getAsteriskServer()
Returns the Asterisk server of this connection. |
java.lang.String |
getProtocolIdentifier()
Returns the protocol identifier, i.e. |
boolean |
isConnected()
Returns true if there is a socket connection to the
asterisk server, false otherwise. |
void |
login()
Logs in to the asterisk manager using asterisk's MD5 based challenge/response protocol. |
void |
logoff()
Sends a LogoffAction and disconnects from the server. |
void |
registerUserEventClass(java.lang.Class userEventClass)
Registers a new user event type. Asterisk allows you to send custom events via the UserEvent application. |
void |
removeEventHandler(ManagerEventHandler eventHandler)
Unregisters a previously registered event handler. Does nothing if the given event handler hasn't be been regiered before. |
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,
ManagerResponseHandler callbackHandler)
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 a EventGeneratingAction to the Asterisk server and waits for the corresponding ManagerResponse and the ResponseEvents. EventGeneratingActions are ManagerActions that don't return their response in the corresponding ManagerResponse but send a series of events that contain the payload. Examples for EventGeneratingActions are the StatusAction , the
net.sf.asterisk.manager.action.QueueAction or the
AgentsAction . |
ResponseEvents |
sendEventGeneratingAction(EventGeneratingAction action,
long timeout)
Sends a EventGeneratingAction to the Asterisk server and waits for the corresponding ManagerResponse and the ResponseEvents. |
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.Default is 5000. |
void |
setDefaultResponseTimeout(long defaultResponseTimeout)
Sets the time in milliseconds the synchronous method sendAction(ManagerAction) will wait for a response before
throwing a TimeoutException.Default is 2000. |
void |
setDefaultTimeout(long defaultTimeout)
Deprecated. use setDefaultResponseTimeout(long) instead |
void |
setHostname(java.lang.String hostname)
Sets the hostname of the asterisk server to connect to. Default is localhost . |
void |
setKeepAliveAfterAuthenticationFailure(boolean keepAliveAfterAuthenticationFailure)
Set to true to try reconnecting to ther asterisk serve
even if the reconnection attempt threw an AuthenticationFailedException.Default is false . |
void |
setPassword(java.lang.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 |
setSleepTime(long sleepTime)
Deprecated. this has been replaced by an interrupt based response checking approach. |
void |
setSocketTimeout(int socketTimeout)
The timeout to use when connecting the the Asterisk server. Default is 0, that is using Java's built-in default. |
void |
setUsername(java.lang.String username)
Sets the username to use to connect to the asterisk server. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected java.lang.String username
manager.conf
.
protected java.lang.String password
manager.conf
.
protected boolean keepAlive
true
after successful login and to
false
after logoff or after an authentication failure when
keepAliveAfterAuthenticationFailure is false
.
Constructor Detail |
---|
public DefaultManagerConnection()
public DefaultManagerConnection(java.lang.String hostname, int port, java.lang.String username, java.lang.String password)
hostname
- the hosname 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 loginpassword
- the password to use for loginMethod Detail |
---|
protected ManagerReader createReader(Dispatcher dispatcher, AsteriskServer server)
protected ManagerWriter createWriter()
public void setHostname(java.lang.String hostname)
localhost
.
hostname
- the hostname to connect topublic void setPort(int port)
manager.conf
file.
port
- the port to connect topublic void setUsername(java.lang.String username)
manager.conf
file.
username
- the username to use for loginpublic void setPassword(java.lang.String password)
manager.conf
file.
password
- the password to use for loginpublic void setDefaultTimeout(long defaultTimeout)
setDefaultResponseTimeout(long)
instead
sendAction(ManagerAction)
will wait for a response before
throwing a TimeoutException.
defaultTimeout
- default timeout in millisecondspublic 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 setSleepTime(long sleepTime)
sendAction(ManagerAction)
and
sendAction(ManagerAction, long)
will sleep between two checks
for the arrival of a response. This value should be rather small.
sleepTime
- time in milliseconds to sleep between two checks for the
arrival of a response or the protocol identifierpublic void setKeepAliveAfterAuthenticationFailure(boolean keepAliveAfterAuthenticationFailure)
true
to try reconnecting to ther asterisk serve
even if the reconnection attempt threw an AuthenticationFailedException.false
.
public void registerUserEventClass(java.lang.Class userEventClass)
ManagerConnection
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.
registerUserEventClass
in interface ManagerConnection
userEventClass
- the class of the user event to register.UserEvent
,
ManagerEvent
public void setSocketTimeout(int socketTimeout)
ManagerConnection
setSocketTimeout
in interface ManagerConnection
socketTimeout
- the timeout value to be used in milliseconds.Socket.connect(java.net.SocketAddress, int)
public void login() throws java.io.IOException, AuthenticationFailedException, TimeoutException
login
in interface ManagerConnection
AuthenticationFailedException
- if the username and/or password are
incorrect
TimeoutException
- if no response is received within the specified
timeout period
java.io.IOException
- if the network connection is disrupted.ChallengeAction
,
LoginAction
protected AsteriskVersion determineVersion() throws java.io.IOException, TimeoutException
java.io.IOException
TimeoutException
protected void connect() throws java.io.IOException
java.io.IOException
protected SocketConnectionFacade createSocket() throws java.io.IOException
java.io.IOException
public boolean isConnected()
true
if there is a socket connection to the
asterisk server, false
otherwise.
isConnected
in interface ManagerConnection
true
if there is a socket connection to the
asterisk server, false
otherwise.public void logoff() throws java.io.IOException, TimeoutException
LogoffAction
and disconnects from the server.
logoff
in interface ManagerConnection
java.io.IOException
- if the network connection is disrupted.
TimeoutException
- if no response to the logoff action is received
within the default timeout period.LogoffAction
public ManagerResponse sendAction(ManagerAction action) throws java.io.IOException, TimeoutException, java.lang.IllegalArgumentException, java.lang.IllegalStateException
ManagerConnection
sendAction
in interface ManagerConnection
action
- the action to send to the Asterisk server
java.io.IOException
- if the network connection is disrupted.
TimeoutException
- if no response is received within the default
timeout period.
java.lang.IllegalArgumentException
- if the action is null
.
java.lang.IllegalStateException
- if you are not connected to an Asterisk
server.ManagerConnection.sendAction(ManagerAction, long)
,
ManagerConnection.sendAction(ManagerAction, ManagerResponseHandler)
public ManagerResponse sendAction(ManagerAction action, long timeout) throws java.io.IOException, TimeoutException, java.lang.IllegalArgumentException, java.lang.IllegalStateException
ManagerConnection
sendAction
in interface ManagerConnection
action
- the action to send to the Asterisk servertimeout
- milliseconds to wait for the response before throwing a
TimeoutException
java.io.IOException
- if the network connection is disrupted.
TimeoutException
- if no response is received within the given
timeout period.
java.lang.IllegalArgumentException
- if the action is null
.
java.lang.IllegalStateException
- if you are not connected to an Asterisk
server.ManagerConnection.sendAction(ManagerAction, ManagerResponseHandler)
public void sendAction(ManagerAction action, ManagerResponseHandler callbackHandler) throws java.io.IOException, java.lang.IllegalArgumentException, java.lang.IllegalStateException
ManagerConnection
sendAction
in interface ManagerConnection
action
- the action to send to the Asterisk servercallbackHandler
- the callback handler to call when the response is
received or null
if you are not interested in
the response
java.io.IOException
- if the network connection is disrupted.
java.lang.IllegalArgumentException
- if the action is null
.
java.lang.IllegalStateException
- if you are not connected to an Asterisk
server.public ResponseEvents sendEventGeneratingAction(EventGeneratingAction action) throws java.io.IOException, EventTimeoutException, java.lang.IllegalArgumentException, java.lang.IllegalStateException
ManagerConnection
StatusAction
, the
net.sf.asterisk.manager.action.QueueAction
or the
AgentsAction
.
sendEventGeneratingAction
in interface ManagerConnection
action
- the action to send to the Asterisk server
java.io.IOException
- if the network connection is disrupted.
EventTimeoutException
- if no response or not all response events are
received within the given timeout period.
java.lang.IllegalArgumentException
- if the action is null
,
the actionCompleteEventClass property of the action is
null
or if actionCompleteEventClass is not a
ResponseEvent.
java.lang.IllegalStateException
- if you are not connected to an Asterisk
server.EventGeneratingAction
,
ResponseEvent
public ResponseEvents sendEventGeneratingAction(EventGeneratingAction action, long timeout) throws java.io.IOException, EventTimeoutException, java.lang.IllegalArgumentException, java.lang.IllegalStateException
ManagerConnection
StatusAction
, the
net.sf.asterisk.manager.action.QueueAction
or the
AgentsAction
.
sendEventGeneratingAction
in interface ManagerConnection
action
- the action to send to the Asterisk servertimeout
- milliseconds to wait for the response and the response
events before throwing a TimeoutException
java.io.IOException
- if the network connection is disrupted.
EventTimeoutException
- if no response or not all response events are
received within the given timeout period.
java.lang.IllegalArgumentException
- if the action is null
,
the actionCompleteEventClass property of the action is
null
or if actionCompleteEventClass is not a
ResponseEvent.
java.lang.IllegalStateException
- if you are not connected to an Asterisk
server.EventGeneratingAction
,
ResponseEvent
public void addEventHandler(ManagerEventHandler eventHandler)
ManagerConnection
ManagerEvent
is receiced from the
Asterisk server.
addEventHandler
in interface ManagerConnection
eventHandler
- the handler to call whenever a manager event is
receivedManagerConnection.removeEventHandler(ManagerEventHandler)
public void removeEventHandler(ManagerEventHandler eventHandler)
ManagerConnection
removeEventHandler
in interface ManagerConnection
eventHandler
- the event handle to unregisterManagerConnection.addEventHandler(ManagerEventHandler)
public java.lang.String getProtocolIdentifier()
ManagerConnection
getProtocolIdentifier
in interface ManagerConnection
null
otherwisepublic AsteriskServer getAsteriskServer()
ManagerConnection
getAsteriskServer
in interface ManagerConnection
public void dispatchResponse(ManagerResponse response)
ManagerResponse
is
received. The response is dispatched to the associated
ManagerResponseHandler
.
dispatchResponse
in interface Dispatcher
response
- the response received by the readerManagerReader
public void dispatchEvent(ManagerEvent event)
dispatchEvent
in interface Dispatcher
event
- the event received by the readeraddEventHandler(ManagerEventHandler)
,
removeEventHandler(ManagerEventHandler)
,
ManagerReader
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |