001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.filter.keepalive;
021
022import org.apache.mina.core.session.IoSession;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026/**
027 * Tells {@link KeepAliveFilter} what to do when a keep-alive response message
028 * was not received within a certain timeout.
029 *
030 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
031 */
032public interface KeepAliveRequestTimeoutHandler {
033    /**
034     * Do nothing.
035     */
036    KeepAliveRequestTimeoutHandler NOOP = new KeepAliveRequestTimeoutHandler() {
037        public void keepAliveRequestTimedOut(KeepAliveFilter filter, IoSession session) throws Exception {
038            // Do nothing.
039        }
040    };
041
042    /**
043     * Logs a warning message, but doesn't do anything else.
044     */
045    KeepAliveRequestTimeoutHandler LOG = new KeepAliveRequestTimeoutHandler() {
046        private final Logger LOGGER = LoggerFactory.getLogger(KeepAliveFilter.class);
047
048        public void keepAliveRequestTimedOut(KeepAliveFilter filter, IoSession session) throws Exception {
049            LOGGER.warn("A keep-alive response message was not received within " + "{} second(s).",
050                    filter.getRequestTimeout());
051        }
052    };
053
054    /**
055     * Throws a {@link KeepAliveRequestTimeoutException}.
056     */
057    KeepAliveRequestTimeoutHandler EXCEPTION = new KeepAliveRequestTimeoutHandler() {
058        public void keepAliveRequestTimedOut(KeepAliveFilter filter, IoSession session) throws Exception {
059            throw new KeepAliveRequestTimeoutException("A keep-alive response message was not received within "
060                    + filter.getRequestTimeout() + " second(s).");
061        }
062    };
063
064    /**
065     * Closes the connection after logging.
066     */
067    KeepAliveRequestTimeoutHandler CLOSE = new KeepAliveRequestTimeoutHandler() {
068        private final Logger LOGGER = LoggerFactory.getLogger(KeepAliveFilter.class);
069
070        public void keepAliveRequestTimedOut(KeepAliveFilter filter, IoSession session) throws Exception {
071            LOGGER.warn("Closing the session because a keep-alive response "
072                    + "message was not received within {} second(s).", filter.getRequestTimeout());
073            session.closeNow();
074        }
075    };
076
077    /**
078     * A special handler for the 'deaf speaker' mode.
079     */
080    KeepAliveRequestTimeoutHandler DEAF_SPEAKER = new KeepAliveRequestTimeoutHandler() {
081        public void keepAliveRequestTimedOut(KeepAliveFilter filter, IoSession session) throws Exception {
082            throw new Error("Shouldn't be invoked.  Please file a bug report.");
083        }
084    };
085
086    /**
087     * Invoked when {@link KeepAliveFilter} couldn't receive the response for
088     * the sent keep alive message.
089     * 
090     * @param filter The filter to use
091     * @param session The current session
092     * @throws Exception If anything went wrong
093     */
094    void keepAliveRequestTimedOut(KeepAliveFilter filter, IoSession session) throws Exception;
095}