use of org.jivesoftware.openfire.Connection in project Openfire by igniterealtime.
the class ClientConnectionHandler method sessionIdle.
/**
* In addition to the functionality provided by the parent class, this
* method will send XMPP ping requests to the remote entity on every first
* invocation of this method (which will occur after a period of half the
* allowed connection idle time has passed, without any IO).
*
* XMPP entities must respond with either an IQ result or an IQ error
* (feature-unavailable) stanza upon receiving the XMPP ping stanza. Both
* responses will be received by Openfire and will cause the connection idle
* count to be reset.
*
* Entities that do not respond to the IQ Ping stanzas can be considered
* dead, and their connection will be closed by the parent class
* implementation on the second invocation of this method.
*
* Note that whitespace pings that are sent by XMPP entities will also cause
* the connection idle count to be reset.
*
* @see ConnectionHandler#sessionIdle(IoSession, IdleStatus)
*/
@Override
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
super.sessionIdle(session, status);
final boolean doPing = ConnectionSettings.Client.KEEP_ALIVE_PING_PROPERTY.getValue();
if (doPing && session.getIdleCount(status) == 1) {
final ClientStanzaHandler handler = (ClientStanzaHandler) session.getAttribute(HANDLER);
final JID entity = handler.getAddress();
if (entity != null) {
// Ping the connection to see if it is alive.
final IQ pingRequest = new IQ(Type.get);
pingRequest.setChildElement("ping", IQPingHandler.NAMESPACE);
pingRequest.setFrom(XMPPServer.getInstance().getServerInfo().getXMPPDomain());
pingRequest.setTo(entity);
// Get the connection for this session
final Connection connection = (Connection) session.getAttribute(CONNECTION);
if (Log.isDebugEnabled()) {
Log.debug("ConnectionHandler: Pinging connection that has been idle: " + connection);
}
// OF-1497: Ensure that data sent to the client is processed through LocalClientSession, to avoid
// synchronisation issues with stanza counts related to Stream Management (XEP-0198)!
LocalClientSession ofSession = (LocalClientSession) SessionManager.getInstance().getSession(entity);
if (ofSession == null) {
Log.warn("Trying to ping a MINA connection that's idle, but has no corresponding Openfire session. MINA Connection: " + connection);
} else {
ofSession.deliver(pingRequest);
}
}
}
}
use of org.jivesoftware.openfire.Connection in project Openfire by igniterealtime.
the class ConnectionHandler method messageReceived.
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
// Get the stanza handler for this session
StanzaHandler handler = (StanzaHandler) session.getAttribute(HANDLER);
// Get the parser to use to process stanza. For optimization there is going
// to be a parser for each running thread. Each Filter will be executed
// by the Executor placed as the first Filter. So we can have a parser associated
// to each Thread
final XMPPPacketReader parser = PARSER_CACHE.get();
// Update counter of read btyes
updateReadBytesCounter(session);
// Let the stanza handler process the received stanza
try {
handler.process((String) message, parser);
} catch (Throwable e) {
// Make sure to catch Throwable, not (only) Exception! See OF-2367
Log.error("Closing connection due to error while processing message: {}", message, e);
final Connection connection = (Connection) session.getAttribute(CONNECTION);
if (connection != null) {
connection.close();
}
}
}
use of org.jivesoftware.openfire.Connection in project Openfire by igniterealtime.
the class ConnectionHandler method exceptionCaught.
@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
Log.warn("Closing connection due to exception in session: " + session, cause);
try {
// OF-524: Determine stream:error message.
final StreamError error;
if (cause != null && (cause instanceof XMLNotWellFormedException || (cause.getCause() != null && cause.getCause() instanceof XMLNotWellFormedException))) {
error = new StreamError(StreamError.Condition.not_well_formed);
} else {
error = new StreamError(StreamError.Condition.internal_server_error);
}
final Connection connection = (Connection) session.getAttribute(CONNECTION);
// OF-1784: Don't write an error when the source problem is an issue with writing data.
if (JiveGlobals.getBooleanProperty("xmpp.skip-error-delivery-on-write-error.disable", false) || !(cause instanceof WriteException)) {
connection.deliverRawText(error.toXML());
}
} finally {
final Connection connection = (Connection) session.getAttribute(CONNECTION);
if (connection != null) {
connection.close();
}
}
}
Aggregations