use of org.jivesoftware.smack.SmackException.NotConnectedException in project openhab1-addons by openhab.
the class XMPP method chatXMPP.
/**
* Sends a message to an XMPP multi user chat.
*
* @param message the message to send
*
* @return <code>true</code>, if sending the message has been successful and
* <code>false</code> in all other cases.
*/
@ActionDoc(text = "Sends a message to an XMPP multi user chat.")
public static boolean chatXMPP(@ParamDoc(name = "message") String message) {
boolean success = false;
try {
MultiUserChat chat = XMPPConnect.getChat();
try {
while (message.length() >= 2000) {
chat.sendMessage(message.substring(0, 2000));
message = message.substring(2000);
}
chat.sendMessage(message);
logger.debug("Sent message '{}' to multi user chat.", message);
success = true;
} catch (XMPPException e) {
logger.warn("Error Delivering block", e);
} catch (NotConnectedException e) {
logger.warn("Error Delivering block", e);
}
} catch (NotInitializedException e) {
logger.warn("Could not send XMPP message as connection is not correctly initialized!");
}
return success;
}
use of org.jivesoftware.smack.SmackException.NotConnectedException in project openhab1-addons by openhab.
the class XMPPConnect method establishConnection.
private static void establishConnection() {
if (servername == null) {
return;
}
ConnectionConfiguration config;
// Create a connection to the jabber server on the given port
if (proxy != null) {
config = new ConnectionConfiguration(servername, port, proxy);
} else {
config = new ConnectionConfiguration(servername, port);
}
config.setSecurityMode(securityMode);
if (tlsPin != null) {
try {
SSLContext sc = JavaPinning.forPin(tlsPin);
config.setCustomSSLContext(sc);
} catch (KeyManagementException | NoSuchAlgorithmException e) {
logger.error("Could not create TLS Pin for XMPP connection", e);
}
}
if (connection != null && connection.isConnected()) {
try {
connection.disconnect();
} catch (NotConnectedException e) {
logger.debug("Already disconnected", e);
}
}
connection = new XMPPTCPConnection(config);
try {
connection.connect();
connection.login(username, password, null);
if (consoleUsers.length > 0) {
ChatManager.getInstanceFor(connection).addChatListener(new XMPPConsole(consoleUsers));
connection.addConnectionListener(new XMPPConnectionListener());
}
logger.info("Connection to XMPP as '{}' has been established. Is secure/encrypted: {}", connection.getUser(), connection.isSecureConnection());
initialized = true;
} catch (Exception e) {
logger.error("Could not establish connection to XMPP server '" + servername + ":" + port + "': {}", e.getMessage());
}
}
Aggregations