ClientConnectionHandler.java 4.44 KB
Newer Older
1 2 3 4
/**
 * $Revision: $
 * $Date: $
 *
5
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
18 19
 */

20
package org.jivesoftware.openfire.nio;
21

22 23
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
24
import org.jivesoftware.openfire.Connection;
25
import org.jivesoftware.openfire.XMPPServer;
26
import org.jivesoftware.openfire.handler.IQPingHandler;
27 28
import org.jivesoftware.openfire.net.ClientStanzaHandler;
import org.jivesoftware.openfire.net.StanzaHandler;
29
import org.jivesoftware.openfire.session.ConnectionSettings;
30
import org.jivesoftware.openfire.spi.ConnectionConfiguration;
31
import org.jivesoftware.util.JiveGlobals;
32 33 34 35
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.IQ;
import org.xmpp.packet.IQ.Type;
36
import org.xmpp.packet.JID;
37 38 39 40 41 42 43 44 45

/**
 * ConnectionHandler that knows which subclass of {@link StanzaHandler} should
 * be created and how to build and configure a {@link NIOConnection}.
 *
 * @author Gaston Dombiak
 */
public class ClientConnectionHandler extends ConnectionHandler {

46 47
	private static final Logger Log = LoggerFactory.getLogger(ClientConnectionHandler.class);

48 49
    public ClientConnectionHandler(ConnectionConfiguration configuration) {
        super(configuration);
50 51
    }

52 53
    @Override
	NIOConnection createNIOConnection(IoSession session) {
54
        return new NIOConnection(session, new OfflinePacketDeliverer(), configuration );
55 56
    }

57 58
    @Override
	StanzaHandler createStanzaHandler(NIOConnection connection) {
59
        return new ClientStanzaHandler(XMPPServer.getInstance().getPacketRouter(), connection);
60 61
    }

62 63
    @Override
	int getMaxIdleTime() {
64
        return JiveGlobals.getIntProperty(ConnectionSettings.Client.IDLE_TIMEOUT, 6 * 60 * 1000) / 1000;
65
    }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

	/**
	 * 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);
    	
91
    	final boolean doPing = JiveGlobals.getBooleanProperty(ConnectionSettings.Client.KEEP_ALIVE_PING, true);
92
        if (doPing && session.getIdleCount(status) == 1) {
93 94 95 96 97 98 99 100
            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);
101
				pingRequest.setFrom( XMPPServer.getInstance().getServerInfo().getXMPPDomain() );
102 103 104 105 106 107 108 109 110 111 112 113 114
				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);
		        }

		        connection.deliver(pingRequest);
            }
        }
    }
115
}