/*
 * Copyright 2014 shevek.
 *
 * 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 com.turn.ttorrent.client.io;

import com.google.common.collect.Sets;
import com.turn.ttorrent.client.ClientEnvironment;
import com.turn.ttorrent.client.TorrentRegistry;
import com.turn.ttorrent.tracker.client.PeerAddressProvider;
import com.turn.ttorrent.protocol.TorrentUtils;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.socket.ServerSocketChannel;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.util.Collections;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author shevek
 */
public class PeerServer extends PeerEndpoint implements PeerAddressProvider {

    private static final Logger LOG = LoggerFactory.getLogger(PeerServer.class);
    public static final int PORT_RANGE_START = 6881;
    public static final int PORT_RANGE_END = 6999;
    public static final int CLIENT_KEEP_ALIVE_MINUTES = 3;
    private final ClientEnvironment environment;
    @CheckForNull
    private final TorrentRegistry torrents;
    private ChannelFuture future;

    // This can stop taking Client if we sort out where the peerId will come from.
    // One option is to make PeerAddressProvider NOT extend PeerIdentityProvider.
    public PeerServer(@Nonnull ClientEnvironment environment, @Nonnull TorrentRegistry torrents) {
        this.environment = environment;
        this.torrents = torrents;
    }

    public void start() throws Exception {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(environment.getEventService());
        bootstrap.channel(environment.getEventLoopType().getServerChannelType());
        bootstrap.option(ChannelOption.SO_BACKLOG, 128);
        bootstrap.option(ChannelOption.SO_REUSEADDR, true);
        bootstrap.option(ChannelOption.TCP_NODELAY, true);
        bootstrap.childHandler(new ChannelInitializer() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new PeerServerHandshakeHandler(torrents));
            }
        });
        bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
        bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
        // bootstrap.childOption(ChannelOption.SO_TIMEOUT, (int) TimeUnit.MINUTES.toMillis(CLIENT_KEEP_ALIVE_MINUTES));
        // TODO: Derive from PieceHandler.DEFAULT_BLOCK_SIZE
        // bootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 1024 * 1024);
        // bootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
        SocketAddress address = environment.getLocalPeerListenAddress();
        if (address != null) {
            future = bootstrap.bind(address).sync();
        } else {
            BIND:
            {
                Exception x = new IOException("No available port for the BitTorrent client!");
                for (int i = PORT_RANGE_START; i <= PORT_RANGE_END; i++) {
                    try {
                        future = bootstrap.bind(i).sync();
                        break BIND;
                    } catch (InterruptedException e) {
                        throw e;
                    } catch (Exception e) {
                        x = e;
                    }
                }
                throw new IOException("Failed to find an address to bind in range [" + PORT_RANGE_START + "," + PORT_RANGE_END + "]", x);
            }
        }
    }

    public void stop() throws InterruptedException {
        try {
            Channel channel = future.channel();
            channel.close().sync();
        } finally {
            future = null;
        }
    }

    @Override
    public byte[] getLocalPeerId() {
        return environment.getLocalPeerId();
    }

    @Override
    public String getLocalPeerName() {
        return environment.getLocalPeerName();
    }

    @Nonnull
    public InetSocketAddress getLocalAddress() {
        ServerSocketChannel channel = (ServerSocketChannel) future.channel();
        return channel.localAddress();
    }

    @Override
    public Set<? extends InetSocketAddress> getLocalAddresses() {
        try {
            // TODO: This call may be expensive on some operating systems. Cache it.
            return Sets.newHashSet(TorrentUtils.getSpecificAddresses(getLocalAddress()));
        } catch (SocketException e) {
            LOG.error("Failed to get specific addresses", e);
            return Collections.emptySet();
        }
    }
}

@@ -623,16 +623,61 @@
 nt.io;%0A%0A
+import com.google.common.base.Preconditions;%0A
 import c
@@ -707,16 +707,16 @@
 t.Sets;%0A
-
 import c
@@ -1396,46 +1396,8 @@
 et;%0A
-import javax.annotation.CheckForNull;%0A
 impo
@@ -1835,16 +1835,29 @@
 ES = 3;%0A
+    @Nonnull%0A
     priv
@@ -1906,17 +1906,12 @@
    @
-CheckForN
+Nonn
 ull%0A
@@ -2284,53 +2284,169 @@
 t = 
-environment;%0A        this.torrents = torrents
+Preconditions.checkNotNull(environment, %22ClientEnvironment was null.%22);%0A        this.torrents = Preconditions.checkNotNull(torrents, %22TorrentRegistry was null.%22)
 ;%0A  

Fix findbugs NPE warning.