// Copyright 2019 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config;

import com.yahoo.config.UrlReference;
import com.yahoo.jrt.Request;
import com.yahoo.jrt.Spec;
import com.yahoo.jrt.StringValue;
import com.yahoo.jrt.Supervisor;
import com.yahoo.jrt.Target;
import com.yahoo.jrt.Transport;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.defaults.Defaults;

import java.io.File;
import java.util.logging.Logger;

/**
 * @author lesters
 */
public class UrlDownloader {

    private static final Logger log = Logger.getLogger(UrlDownloader.class.getName());

    private static final int BASE_ERROR_CODE = 0x10000;
    public  static final int DOES_NOT_EXIST = BASE_ERROR_CODE + 1;
    public  static final int INTERNAL_ERROR = BASE_ERROR_CODE + 2;
    public  static final int HTTP_ERROR = BASE_ERROR_CODE + 3;

    private final Supervisor supervisor = new Supervisor(new Transport());
    private final Spec spec;
    private Target target;

    public UrlDownloader() {
        spec = new Spec(Defaults.getDefaults().vespaHostname(), Defaults.getDefaults().vespaConfigProxyRpcPort());
        connect();
    }

    public void shutdown() {
        supervisor.transport().shutdown().join();
    }

    private void connect() {
        int timeRemaining = 5000;
        try {
            while (timeRemaining > 0) {
                target = supervisor.connectSync(spec);
                if (target.isValid()) {
                    log.log(LogLevel.DEBUG, "Successfully connected to '" + spec + "', this = " + System.identityHashCode(this));
                    return;
                }
                Thread.sleep(500);
                timeRemaining -= 500;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public boolean isValid() {
        return target.isValid();
    }

    private boolean temporaryError(Request req) {
        return false;  // Currently, none of the errors are considered temporary
    }

    public File waitFor(UrlReference urlReference, long timeout) {
        long start = System.currentTimeMillis() / 1000;
        long timeLeft = timeout;
        do {
            Request request = new Request("url.waitFor");
            request.parameters().add(new StringValue(urlReference.value()));

            double rpcTimeout = Math.min(timeLeft, 60.0);
            log.log(LogLevel.DEBUG, "InvokeSync waitFor " + urlReference + " with " + rpcTimeout + " seconds timeout");
            target.invokeSync(request, rpcTimeout);

            if (request.checkReturnTypes("s")) {
                return new File(request.returnValues().get(0).asString());
            } else if (!request.isError()) {
                throw new RuntimeException("Invalid response: " + request.returnValues());
            } else if (temporaryError(request)) {
                log.log(LogLevel.INFO, "Retrying waitFor for " + urlReference + ": " + request.errorCode() + " -- " + request.errorMessage());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException("Interrupted sleep between retries of waitFor", e);
                }
            } else {
                 throw new RuntimeException("Wait for " + urlReference + " failed:" + request.errorMessage() + " (" + request.errorCode() + ")");
            }
            timeLeft = start + timeout - System.currentTimeMillis() / 1000;
        } while (timeLeft > 0);

        throw new RuntimeException("Timed out waiting for " + urlReference + " after " + timeout);
    }

}

@@ -2416,16 +2416,20 @@
 imeLeft,
+ 5 *
  60.0);%0A

Increase url download timeout to 5 minutes