use of org.asteriskjava.manager.ManagerConnectionFactory in project openhab1-addons by openhab.
the class AsteriskBinding method connect.
/**
* Connects to <code>host</code> and opens a ManagerConnection by using the
* given <code>username</code> and <code>password</code>. Note: The Asterisk
* ManagerInterface on your Asterisk PBX is deactivated by default. Please
* refer to the documentation how to activate the ManagerInterface (AMI).
*
* @param host where to find the Asterisk PBX
* @param username username to login to Asterisk ManagerInterface
* @param password password to login to Asterisk ManagerInterface
*/
private void connect(String host, String username, String password) {
ManagerConnectionFactory factory = new ManagerConnectionFactory(host, username, password);
AsteriskBinding.managerConnection = factory.createManagerConnection();
AsteriskBinding.managerConnection.addEventListener(new AsteriskEventManager());
try {
AsteriskBinding.managerConnection.login();
} catch (AuthenticationFailedException afe) {
logger.error("Authentication failed. Please verify username and password.");
} catch (IOException ioe) {
logger.error("Could not connect to manager interface on host {}: {}", host, ioe);
} catch (Exception e) {
logger.error("Login to Asterisk manager interface on host {} threw an exception: {}", host, e);
}
try {
AsteriskBinding.managerConnection.sendAction(new StatusAction());
} catch (Exception e) {
logger.error("Registering for status update threw an exception: {}", e);
}
}
use of org.asteriskjava.manager.ManagerConnectionFactory in project camel by apache.
the class AsteriskConnection method connect.
private void connect() {
if (managerConnection == null) {
LOG.debug("asterisk connection attempt to {} username: {}", host, username);
ManagerConnectionFactory factory = new ManagerConnectionFactory(host, username, password);
managerConnection = factory.createManagerConnection();
LOG.debug("asterisk connection established!");
}
}
use of org.asteriskjava.manager.ManagerConnectionFactory in project opennms by OpenNMS.
the class AsteriskSIPPeerMonitor method poll.
/**
* {@inheritDoc}
*
* <P>
* Run the service monitor and return the poll status
* </P>
*/
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
// read configuration parameters
String sipPeer = ParameterMap.getKeyedString(parameters, "sip-peer", DEFAULT_SIPPEER);
if (sipPeer.equals(DEFAULT_SIPPEER)) {
LOG.error("AsteriskMonitor: No sip-peer parameter in poller configuration");
throw new RuntimeException("AsteriskMonitor: required parameter 'sip-peer' is not present in supplied properties.");
}
TimeoutTracker timeoutTracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
AmiPeerFactory amiPeerFactory = AmiPeerFactory.getInstance();
AmiAgentConfig amiConfig = amiPeerFactory.getAgentConfig(svc.getAddress());
// setting up AMI connection
LOG.debug("{}: Creating new AMI-Connection: {}:{}, {}/{}", svc.getSvcName(), svc.getIpAddr(), amiConfig.getPort(), amiConfig.getUsername(), amiConfig.getPassword());
ManagerConnectionFactory factory = new ManagerConnectionFactory(svc.getIpAddr(), amiConfig.getPort().orElse(null), amiConfig.getUsername().orElse(null), amiConfig.getPassword().orElse(null));
ManagerConnection managerConnection;
if (amiConfig.getUseTls().orElse(false)) {
managerConnection = factory.createSecureManagerConnection();
} else {
managerConnection = factory.createManagerConnection();
}
managerConnection.setSocketTimeout(new Long(timeoutTracker.getTimeoutInMillis()).intValue());
// start with polling
while (timeoutTracker.shouldRetry()) {
timeoutTracker.nextAttempt();
LOG.debug("{}: Attempt {}", svc.getSvcName(), timeoutTracker.getAttempt());
try {
LOG.debug("{}: AMI login", svc.getSvcName());
managerConnection.login();
LOG.debug("{}: AMI sendAction SipShowPeer", svc.getSvcName());
ManagerResponse response = managerConnection.sendAction(new SipShowPeerAction(sipPeer));
if (response.getAttribute("Status") == null) {
LOG.debug("{}: service status down", svc.getSvcName());
return PollStatus.decode("Down", "State of SIP Peer is unknown, because it was not found on the Asterisk server");
}
LOG.debug("{}: Response: {}", svc.getSvcName(), response.getAttribute("Status"));
LOG.debug("{}: AMI logoff", svc.getSvcName());
managerConnection.logoff();
if (response.getAttribute("Status").startsWith("OK")) {
LOG.debug("{}: service status up", svc.getSvcName());
return PollStatus.decode("Up", "OK");
} else {
LOG.debug("{}: service status down", svc.getSvcName());
return PollStatus.decode("Down", "State of SIP Peer is " + response.getAttribute("Status") + " and not OK");
}
} catch (AuthenticationFailedException e) {
LOG.debug("{}: AMI AuthenticationError.", svc.getSvcName(), e);
return PollStatus.decode("Down", "Could not get the state of SIP Peer: AMI AuthenticationError");
} catch (TimeoutException e) {
LOG.debug("{}: TimeOut reached.", svc.getSvcName(), e);
} catch (SocketTimeoutException e) {
LOG.debug("{}: TimeOut reached.", svc.getSvcName(), e);
} catch (Exception e) {
LOG.error("{}: An Unknown Exception Occurred.", svc.getSvcName(), e);
return PollStatus.decode("Down", "Could not get the state of SIP Peer: " + e.toString());
}
}
// If none of the retries worked
return PollStatus.decode("Down", "Could not get the state of SIP Peer: Timeout exceeded");
}
Aggregations