/*
 * Copyright 2014-2018 Real Logic Ltd.
 *
 * 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 io.aeron.samples.raw;

import io.aeron.driver.Configuration;
import org.agrona.nio.NioSelectedKeySet;
import org.agrona.IoUtil;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.channels.DatagramChannel;
import java.nio.channels.FileChannel;
import java.nio.channels.Selector;

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.READ;
import static java.nio.file.StandardOpenOption.WRITE;

public class Common
{
    public static final int NUM_MESSAGES = 10_000;

    public static final int PONG_PORT = 40123;
    public static final int PING_PORT = 50123;

    static final Field SELECTED_KEYS_FIELD;
    static final Field PUBLIC_SELECTED_KEYS_FIELD;

    static
    {
        Field selectKeysField = null;
        Field publicSelectKeysField = null;

        try
        {
            final Class<?> clazz = Class.forName("sun.nio.ch.SelectorImpl", false, ClassLoader.getSystemClassLoader());

            if (clazz.isAssignableFrom(Selector.open().getClass()))
            {
                selectKeysField = clazz.getDeclaredField("selectedKeys");
                selectKeysField.setAccessible(true);

                publicSelectKeysField = clazz.getDeclaredField("publicSelectedKeys");
                publicSelectKeysField.setAccessible(true);
            }
        }
        catch (final Exception ignore)
        {
        }

        SELECTED_KEYS_FIELD = selectKeysField;
        PUBLIC_SELECTED_KEYS_FIELD = publicSelectKeysField;
    }

    public static void init(final DatagramChannel channel) throws IOException
    {
        channel.configureBlocking(false);
        channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    }

    public static void init(final DatagramChannel channel, final InetSocketAddress sendAddress)
        throws IOException
    {
        channel.configureBlocking(false);
        channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        channel.connect(sendAddress);
    }

    public static NioSelectedKeySet keySet(final Selector selector)
    {
        NioSelectedKeySet tmpSet = null;

        if (null != PUBLIC_SELECTED_KEYS_FIELD)
        {
            try
            {
                tmpSet = new NioSelectedKeySet();

                SELECTED_KEYS_FIELD.set(selector, tmpSet);
                PUBLIC_SELECTED_KEYS_FIELD.set(selector, tmpSet);
            }
            catch (final Exception ignore)
            {
                tmpSet = null;
            }
        }

        return tmpSet;
    }

    public static FileChannel createTmpFileChannel() throws IOException
    {
        final File file = File.createTempFile("buffer-", ".dat");
        file.deleteOnExit();

        final FileChannel fileChannel = FileChannel.open(file.toPath(), CREATE, READ, WRITE);
        IoUtil.fill(fileChannel, 0, Configuration.MTU_LENGTH_DEFAULT, (byte)0);

        return fileChannel;
    }
}

@@ -635,127 +635,42 @@
 ort 
-io.aeron.driver.Configuration;%0Aimport org.agrona.nio.NioSelectedKeySet;%0Aimport org.agrona.IoUtil;%0A%0Aimport java.io.File;
+org.agrona.nio.NioSelectedKeySet;%0A
 %0Aimp
@@ -871,217 +871,16 @@
 els.
-FileChannel;%0Aimport java.nio.channels.Selector;%0A%0Aimport static java.nio.file.StandardOpenOption.CREATE;%0Aimport static java.nio.file.StandardOpenOption.READ;%0Aimport static java.nio.file.StandardOpenOption.WRITE
+Selector
 ;%0A%0Ap
@@ -2965,391 +2965,7 @@
    %7D
-%0A%0A    public static FileChannel createTmpFileChannel() throws IOException%0A    %7B%0A        final File file = File.createTempFile(%22buffer-%22, %22.dat%22);%0A        file.deleteOnExit();%0A%0A        final FileChannel fileChannel = FileChannel.open(file.toPath(), CREATE, READ, WRITE);%0A        IoUtil.fill(fileChannel, 0, Configuration.MTU_LENGTH_DEFAULT, (byte)0);%0A%0A        return fileChannel;%0A    %7D
 %0A%7D%0A

Remove unused code.