// 
// Decompiled by Procyon v0.6.0
// 

package io.netty.channel.socket.oio;

import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.channel.socket.ServerSocketChannelConfig;
import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelOutboundBuffer;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.List;
import io.netty.util.internal.SocketUtils;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import io.netty.util.internal.ObjectUtil;
import io.netty.channel.Channel;
import java.io.IOException;
import io.netty.channel.ChannelException;
import java.net.ServerSocket;
import io.netty.channel.ChannelMetadata;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.oio.AbstractOioMessageChannel;

@Deprecated
public class OioServerSocketChannel extends AbstractOioMessageChannel implements ServerSocketChannel
{
    private static final InternalLogger logger;
    private static final ChannelMetadata METADATA;
    final ServerSocket socket;
    private final OioServerSocketChannelConfig config;
    
    private static ServerSocket newServerSocket() {
        try {
            return new ServerSocket();
        }
        catch (final IOException e) {
            throw new ChannelException("failed to create a server socket", e);
        }
    }
    
    public OioServerSocketChannel() {
        this(newServerSocket());
    }
    
    public OioServerSocketChannel(final ServerSocket socket) {
        super(null);
        ObjectUtil.checkNotNull(socket, "socket");
        boolean success = false;
        try {
            socket.setSoTimeout(1000);
            success = true;
        }
        catch (final IOException e) {
            throw new ChannelException("Failed to set the server socket timeout.", e);
        }
        finally {
            if (!success) {
                try {
                    socket.close();
                }
                catch (final IOException e2) {
                    if (OioServerSocketChannel.logger.isWarnEnabled()) {
                        OioServerSocketChannel.logger.warn("Failed to close a partially initialized socket.", e2);
                    }
                }
            }
        }
        this.socket = socket;
        this.config = new DefaultOioServerSocketChannelConfig(this, socket);
    }
    
    @Override
    public InetSocketAddress localAddress() {
        return (InetSocketAddress)super.localAddress();
    }
    
    @Override
    public ChannelMetadata metadata() {
        return OioServerSocketChannel.METADATA;
    }
    
    @Override
    public OioServerSocketChannelConfig config() {
        return this.config;
    }
    
    @Override
    public InetSocketAddress remoteAddress() {
        return null;
    }
    
    @Override
    public boolean isOpen() {
        return !this.socket.isClosed();
    }
    
    @Override
    public boolean isActive() {
        return this.isOpen() && this.socket.isBound();
    }
    
    @Override
    protected SocketAddress localAddress0() {
        return SocketUtils.localSocketAddress(this.socket);
    }
    
    @Override
    protected void doBind(final SocketAddress localAddress) throws Exception {
        this.socket.bind(localAddress, this.config.getBacklog());
    }
    
    @Override
    protected void doClose() throws Exception {
        this.socket.close();
    }
    
    @Override
    protected int doReadMessages(final List<Object> buf) throws Exception {
        if (this.socket.isClosed()) {
            return -1;
        }
        try {
            final Socket s = this.socket.accept();
            try {
                buf.add(new OioSocketChannel(this, s));
                return 1;
            }
            catch (final Throwable t) {
                OioServerSocketChannel.logger.warn("Failed to create a new channel from an accepted socket.", t);
                try {
                    s.close();
                }
                catch (final Throwable t2) {
                    OioServerSocketChannel.logger.warn("Failed to close a socket.", t2);
                }
            }
        }
        catch (final SocketTimeoutException ex) {}
        return 0;
    }
    
    @Override
    protected void doWrite(final ChannelOutboundBuffer in) throws Exception {
        throw new UnsupportedOperationException();
    }
    
    @Override
    protected Object filterOutboundMessage(final Object msg) throws Exception {
        throw new UnsupportedOperationException();
    }
    
    @Override
    protected void doConnect(final SocketAddress remoteAddress, final SocketAddress localAddress) throws Exception {
        throw new UnsupportedOperationException();
    }
    
    @Override
    protected SocketAddress remoteAddress0() {
        return null;
    }
    
    @Override
    protected void doDisconnect() throws Exception {
        throw new UnsupportedOperationException();
    }
    
    @Deprecated
    @Override
    protected void setReadPending(final boolean readPending) {
        super.setReadPending(readPending);
    }
    
    final void clearReadPending0() {
        super.clearReadPending();
    }
    
    static {
        logger = InternalLoggerFactory.getInstance(OioServerSocketChannel.class);
        METADATA = new ChannelMetadata(false, 1);
    }
}
