/** 
 * 
 * Copyright 2004 Hiram Chirino
 * 
 * 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. 
 * 
 **/

package org.activeio.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

import org.activeio.Packet;
import org.activeio.SynchChannel;
import org.activeio.SynchChannelServer;
import org.activeio.Packet.ByteSequence;
import org.activeio.packet.ByteArrayPacket;
import org.activeio.packet.FilterPacket;

/**
 * A {@see org.activeio.SynchChannel}implementation that uses
 * TCP to talk to the network.
 * 
 * @version $Revision$
 */
final public class DatagramSocketSynchChannel implements SynchChannel {

    private final class UDPFilterPacket extends FilterPacket {
        private final DatagramPacket packet;

        private UDPFilterPacket(Packet next, DatagramPacket packet) {
            super(next);
            this.packet = packet;
        }

        public Object narrow(Class target) {
            if( target == DatagramContext.class ) {
                return new DatagramContext(packet);
            }
            return super.narrow(target);
        }

        public Packet filter(Packet packet) {
            return new UDPFilterPacket(packet, this.packet);
        }
    }

    private static final int DEFAULT_BUFFER_SIZE = 64 * 1024;

    private final DatagramSocket socket;

    private boolean disposed;

    private int curentSoTimeout;

    /**
     * Construct basic helpers
     * 
     * @param wireFormat
     * @throws IOException
     */
    protected DatagramSocketSynchChannel(DatagramSocket socket) throws IOException {
        this.socket = socket;
        socket.setReceiveBufferSize(DEFAULT_BUFFER_SIZE);
        socket.setSendBufferSize(DEFAULT_BUFFER_SIZE);
    }
    
    /**
     * @see org.activeio.SynchChannel#read(long)
     */
    public org.activeio.Packet read(long timeout) throws IOException {
        try {

            if (timeout == SynchChannelServer.WAIT_FOREVER_TIMEOUT)
                setSoTimeout(0);
            else if (timeout == SynchChannelServer.NO_WAIT_TIMEOUT)
                setSoTimeout(1);
            else
                setSoTimeout((int) timeout);

            // FYI: message data is truncated if biger than this buffer.
            final byte data[] = new byte[DEFAULT_BUFFER_SIZE];
            final DatagramPacket packet = new DatagramPacket(data, data.length);
            socket.receive(packet);
            
            // A FilterPacket is used to provide the UdpDatagramContext via narrow.
            return new UDPFilterPacket(new ByteArrayPacket(data, 0, packet.getLength()), packet);
            
        } catch (SocketTimeoutException e) {
            return null;
        }
    }

    private void setSoTimeout(int i) throws SocketException {
        if (curentSoTimeout != i) {
            socket.setSoTimeout(i);
            curentSoTimeout = i;
        }
    }

    /**
     * @see org.activeio.Channel#write(org.activeio.channel.Packet)
     */
    public void write(org.activeio.Packet packet) throws IOException {
        ByteSequence sequence = packet.asByteSequence();

        DatagramContext context = (DatagramContext) packet.narrow(DatagramContext.class);
        if( context!=null ) {
            socket.send(new DatagramPacket(sequence.getData(),sequence.getOffset(), sequence.getLength(), context.address, context.port.intValue()));
        } else {
            socket.send(new DatagramPacket(sequence.getData(),sequence.getOffset(), sequence.getLength()));
        }
    }

    /**
     * @see org.activeio.Channel#flush()
     */
    public void flush() throws IOException {
    }

    /**
     * @see org.activeio.Disposable#dispose()
     */
    public void dispose() {
        if (disposed)
            return;
        socket.close();
        disposed = true;
    }

    public void start() throws IOException {
    }

    public void stop(long timeout) throws IOException {
    }

    
    public Object narrow(Class target) {
        if( target.isAssignableFrom(getClass()) ) {
            return this;
        }
        return null;
    }

    public String toString() {
        return "Datagram Connection: "+socket.getLocalSocketAddress()+" -> "+socket.getRemoteSocketAddress();
    }
}