/*******************************************************************************
 * Copyright (c) 2015-2018 Skymind, Inc.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Apache License, Version 2.0 which is available at
 * https://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.
 *
 * SPDX-License-Identifier: Apache-2.0
 ******************************************************************************/

package org.nd4j.parameterserver.background;


import lombok.extern.slf4j.Slf4j;
import org.nd4j.parameterserver.ParameterServerSubscriber;
import org.zeroturnaround.exec.ProcessExecutor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * Start background daemons for tests
 * Credit to:
 * https://stackoverflow.com/questions/636367/executing-a-java-application-in-a-separate-process
 * @author Adam Gibson
 */
@Slf4j
public class BackgroundDaemonStarter {


    /**
     *  Start a slave daemon with
     *  the specified master url with the form of:
     *  hostname:port:streamId
     * @param parameterLength the length of the parameters to
     *                        be averaging
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static int startSlave(int parameterLength, String masterUrl, String mediaDriverDirectory) throws Exception {
        return exec(ParameterServerSubscriber.class, mediaDriverDirectory, "-s", "1," + String.valueOf(parameterLength),
                        "-p", "40126", "-h", "localhost", "-id", "10", "-pm", masterUrl, "-sp", "9500", "--updatesPerEpoch", "1");
    }

    /**
     *
     * Start a slave daemon with a default url of:
     * localhost:40123:11
     * where the url is:
     * hostname:port:streamId
     * @param parameterLength the parameter length of the ndarrays
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static int startSlave(int parameterLength, String mediaDriverDirectory) throws Exception {
        return startSlave(parameterLength, "localhost:40123:11", mediaDriverDirectory);
    }


    public static String slaveConnectionUrl() {
        return "localhost:40126:10";
    }

    /**
     * Master connection url
     * @return
     */
    public static String masterResponderUrl() {
        return "localhost:40124:12";
    }

    /**
     * Master connection url
     * @return
     */
    public static String masterConnectionUrl() {
        return "localhost:40123:11";
    }

    /**
     *
     * @param parameterLength
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static int startMaster(int parameterLength, String mediaDriverDirectory) throws Exception {
        return exec(ParameterServerSubscriber.class, mediaDriverDirectory, "-m", "true", "-s",
                        "1," + String.valueOf(parameterLength), "-p", "40123", "-h", "localhost", "-id", "11", "-sp",
                        "9200", "--updatesPerEpoch", "1");
    }


    /**
     * Exec a java process in the background
     * @param klass the main class to run
     * @param mediaDriverDirectory the media driver directory to use
     * @param args the args to use (can be null)
     * @return the process exit code
     * @throws IOException
     * @throws InterruptedException
     */
    public static int exec(Class klass, String mediaDriverDirectory, String... args) throws Exception {
        String javaHome = System.getProperty("java.home");
        String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
        String classpath = System.getProperty("java.class.path");
        String className = klass.getCanonicalName();
        if (args == null || args.length < 1) {
            try {
                return new ProcessExecutor().command(javaBin, "-cp", classpath, className).readOutput(true)
                                .redirectOutput(System.out).destroyOnExit().redirectError(System.err).execute()
                                .getExitValue();
            } catch (TimeoutException e) {
                log.error("",e);
            }
        } else {
            List<String> args2 = new ArrayList<>(
                            Arrays.asList(javaBin, "-cp", classpath, className, "-md", mediaDriverDirectory));
            args2.addAll(Arrays.asList(args));
            try {
                new ProcessExecutor().command(args2).destroyOnExit().readOutput(true).redirectOutput(System.out)
                                .redirectError(System.err).execute().getExitValue();
            } catch (TimeoutException e) {
                log.error("",e);
            }
        }


        return 1;
    }


}
