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.core.session;
021
022import java.net.SocketAddress;
023
024import org.apache.mina.core.service.IoService;
025
026/**
027 * A connectionless transport can recycle existing sessions by assigning an
028 * {@link IoSessionRecycler} to an {@link IoService}.
029 *
030 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
031 * TODO More documentation
032 */
033public interface IoSessionRecycler {
034    /**
035     * A dummy recycler that doesn't recycle any sessions.  Using this recycler will
036     * make all session lifecycle events to be fired for every I/O for all connectionless
037     * sessions.
038     */
039    IoSessionRecycler NOOP = new IoSessionRecycler() {
040        /**
041         * {@inheritDoc}
042         */
043        public void put(IoSession session) {
044            // Do nothing
045        }
046
047        /**
048         * {@inheritDoc}
049         */
050        public IoSession recycle(SocketAddress remoteAddress) {
051            return null;
052        }
053
054        /**
055         * {@inheritDoc}
056         */
057        public void remove(IoSession session) {
058            // Do nothing
059        }
060    };
061
062    /**
063     * Called when the underlying transport creates or writes a new {@link IoSession}.
064     *
065     * @param session the new {@link IoSession}.
066     */
067    void put(IoSession session);
068
069    /**
070     * Attempts to retrieve a recycled {@link IoSession}.
071     *
072     * @param remoteAddress the remote socket address of the {@link IoSession} the transport wants to recycle.
073     * @return a recycled {@link IoSession}, or null if one cannot be found.
074     */
075    IoSession recycle(SocketAddress remoteAddress);
076
077    /**
078     * Called when an {@link IoSession} is explicitly closed.
079     *
080     * @param session the new {@link IoSession}.
081     */
082    void remove(IoSession session);
083}