/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.
 *
 */

package org.snmp4j.mina;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoBuffer;
import org.apache.mina.common.IoHandler;
import org.apache.mina.common.IoSession;
import org.apache.mina.transport.socket.nio.NioDatagramConnector;
import org.snmp4j.log.*;
import org.snmp4j.transport.UdpTransportMapping;
import org.snmp4j.smi.*;

/**
 * An Apache MINA based transport
 * Works with Apache MINA 2.0M2 
 * 
 * @author Julien Vermillard
 */
public class MinaUdpTransportMapping extends UdpTransportMapping {

    private static final LogAdapter logger =
            LogFactory.getLogger(MinaUdpTransportMapping.class);
    protected IoSession session = null;
    protected NioDatagramConnector connector = new NioDatagramConnector();
    protected IoHandler handler = new Handler();

    /**
     * Creates a UDP transport with an arbitrary local port on all local
     * interfaces.
     *
     * @throws IOException
     *    if socket binding fails.
     */
    public MinaUdpTransportMapping() throws IOException {
        super(new UdpAddress(InetAddress.getLocalHost(), 0));
    }

    /**
     * Creates a UDP transport with optional reusing the address if is currently
     * in timeout state (TIME_WAIT) after the connection is closed.
     *
     * @param udpAddress
     *    the local address for sending and receiving of UDP messages.
     * @param reuseAddress
     *    if <code>true</code> addresses are reused which provides faster socket
     *    binding if an application is restarted for instance.
     * @throws IOException
     *    if socket binding fails.
     */
    public MinaUdpTransportMapping(UdpAddress udpAddress,
            boolean reuseAddress) throws IOException {
        super(udpAddress);
        connector.getSessionConfig().setReuseAddress(reuseAddress);
    }

    /**
     * Creates a UDP transport on the specified address. The address will not be
     * reused if it is currently in timeout state (TIME_WAIT).
     *
     * @param udpAddress
     *    the local address for sending and receiving of UDP messages.
     * @throws IOException
     *    if socket binding fails.
     */
    public MinaUdpTransportMapping(UdpAddress udpAddress) throws IOException {
        super(udpAddress);
    }

    public void sendMessage(Address targetAddress, byte[] message)
            throws java.io.IOException {
        InetSocketAddress targetSocketAddress =
                new InetSocketAddress(((UdpAddress) targetAddress).getInetAddress(),
                ((UdpAddress) targetAddress).getPort());
    if (logger.isDebugEnabled()) {
        logger.debug("Sending message to " + targetAddress + " with length " +
                message.length + ": " +
                new OctetString(message).toHexString());
        }
        session.write(IoBuffer.wrap(message), targetSocketAddress);
    }

    /**
     * Closes the socket and stops the listener thread.
     *
     * @throws IOException
     */
    public void close() throws IOException {
        session.close();
    }

    /**
     * Starts the listener thread that accepts incoming messages. The thread is
     * started in daemon mode and thus it will not block application terminated.
     * Nevertheless, the {@link #close()} method should be called to stop the
     * listen thread gracefully and free associated ressources.
     *
     * @throws IOException
     */
    public synchronized void listen() throws IOException {
        connector.setHandler(handler);
        ConnectFuture cf = connector.connect(new InetSocketAddress(getAddress().getInetAddress(), getAddress().getPort()));
        cf.awaitUninterruptibly();
        session = cf.getSession();
    }


    public int getSocketTimeout() {
        return ((int) connector.getConnectTimeoutMillis()) / 1000;
    }

    /**
     * Gets the requested receive buffer size for the underlying UDP socket.
     * This size might not reflect the actual size of the receive buffer, which
     * is implementation specific.
     * @return
     *    <=0 if the default buffer size of the OS is used, or a value >0 if the
     *    user specified a buffer size.
     */
    public int getReceiveBufferSize() {
        return session.getConfig().getReadBufferSize();
    }

    /**
     * Sets the receive buffer size, which should be > the maximum inbound message
     * size. This method has to be called before {@link #listen()} to be
     * effective.
     * @param receiveBufferSize
     *    an integer value >0 and > {@link #getMaxInboundMessageSize()}.
     */
    public void setReceiveBufferSize(int receiveBufferSize) {
        if (receiveBufferSize <= 0) {
            throw new IllegalArgumentException("Receive buffer size must be > 0");
        }
        connector.getSessionConfig().setReadBufferSize(receiveBufferSize);
    }

    public void setSocketTimeout(int socketTimeout) {
        connector.setConnectTimeoutMillis(socketTimeout*1000);
    }

    public boolean isListening() {
        return session != null && session.isConnected();
    }

    class Handler implements IoHandler {

        public void sessionCreated(IoSession session) throws Exception {
        }

        public void sessionOpened(IoSession session) throws Exception {
            if (logger.isDebugEnabled()) {
                logger.debug("session opened " + session);
            }
        }

        public void sessionClosed(IoSession session) throws Exception {
            if (logger.isDebugEnabled()) {
                logger.debug("session closed");
            }
        }

        public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        }

        public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
            logger.error("Network exception", cause);
        }

        public void messageReceived(IoSession session, Object message) throws Exception {
            if (logger.isDebugEnabled()) {
                logger.debug("received : " + message);
            }
            
            fireProcessMessage(new UdpAddress(((InetSocketAddress) session.getRemoteAddress()).getAddress(), ((InetSocketAddress) session.getRemoteAddress()).getPort()),
                    ((IoBuffer) message).buf());
        }

        public void messageSent(IoSession session, Object message) throws Exception {
            if (logger.isDebugEnabled()) {
                logger.debug("sent : " + message);
            }
        }
    }
}