// Path: src/java/us/kbase/common/utils/NetUtils.java
// public class NetUtils {
//     private static final Pattern IPADDRESS_PATTERN = Pattern.compile(
//             "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
//             "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
//             "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
//             "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
// 
//     public static List<String> findNetworkAddresses(String... networkNames)
//             throws SocketException {
//         Set<String> networkNameSet = new HashSet<String>(Arrays.asList(networkNames));
//         List<String> ret = new ArrayList<String>();
//         for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
//             NetworkInterface intf = en.nextElement();
//             // breaks linux, if required for windows needs to be os-dependent
//             // if (!intf.isUp()) continue;
//             if (networkNameSet.contains(intf.getName()) || networkNameSet.contains(intf.getDisplayName())) {
//                 for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
//                     String ip = enumIpAddr.nextElement().getHostAddress();
//                     if (IPADDRESS_PATTERN.matcher(ip).matches()) 
//                         ret.add(ip);
//                 }
//             }
//         }
//         return ret;
//     }
//     
//     public static int findFreePort() {
//         try (ServerSocket socket = new ServerSocket(0)) {
//             return socket.getLocalPort();
//         } catch (IOException e) {}
//         throw new IllegalStateException("Can not find available port in the system");
//     }
// }


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import us.kbase.auth.AuthException;
import us.kbase.auth.AuthToken;
import us.kbase.auth.ConfigurableAuthService;
import us.kbase.common.executionengine.CallbackServerConfigBuilder.CallbackServerConfig;
import us.kbase.common.service.JacksonTupleModule;
import us.kbase.common.service.JsonClientException;
import us.kbase.common.service.JsonServerMethod;
import us.kbase.common.service.JsonServerServlet;
import us.kbase.common.service.JsonServerSyslog;
import us.kbase.common.service.JsonTokenStream;
import us.kbase.common.service.RpcContext;
import us.kbase.common.service.UObject;
import us.kbase.common.utils.NetUtils;
import us.kbase.workspace.ProvenanceAction;
import us.kbase.workspace.SubAction;
    }

    protected abstract SubsequentCallRunner createJobRunner(
            final AuthToken token,
            final CallbackServerConfig config,
            final UUID jobId,
            final ModuleMethod modmeth,
            final String serviceVer)
            throws IOException, JsonClientException;
    
    @Override
    public void destroy() {
        cbLog("Shutting down executor service");
        final List<Runnable> failed = executor.shutdownNow();
        if (!failed.isEmpty()) {
            cbLog(String.format("Failed to stop %s tasks", failed.size()));
        }
    }
    
    public static URL getCallbackUrl(int callbackPort)
            throws SocketException {
        return getCallbackUrl(callbackPort, null);
    }

    public static URL getCallbackUrl(int callbackPort, String[] networkInterfaces)
            throws SocketException {
        if (networkInterfaces == null || networkInterfaces.length == 0) {
            networkInterfaces = new String[] {"docker0", "vboxnet0", "vboxnet1",
                    "VirtualBox Host-Only Ethernet Adapter", "en0"};
        }
// Path: src/java/us/kbase/common/utils/NetUtils.java
// public class NetUtils {
//     private static final Pattern IPADDRESS_PATTERN = Pattern.compile(
//             "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
//             "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
//             "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
//             "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
// 
//     public static List<String> findNetworkAddresses(String... networkNames)
//             throws SocketException {
//         Set<String> networkNameSet = new HashSet<String>(Arrays.asList(networkNames));
//         List<String> ret = new ArrayList<String>();
//         for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
//             NetworkInterface intf = en.nextElement();
//             // breaks linux, if required for windows needs to be os-dependent
//             // if (!intf.isUp()) continue;
//             if (networkNameSet.contains(intf.getName()) || networkNameSet.contains(intf.getDisplayName())) {
//                 for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
//                     String ip = enumIpAddr.nextElement().getHostAddress();
//                     if (IPADDRESS_PATTERN.matcher(ip).matches()) 
//                         ret.add(ip);
//                 }
//             }
//         }
//         return ret;
//     }
//     
//     public static int findFreePort() {
//         try (ServerSocket socket = new ServerSocket(0)) {
//             return socket.getLocalPort();
//         } catch (IOException e) {}
//         throw new IllegalStateException("Can not find available port in the system");
//     }
// }


// Path: src/java/us/kbase/common/executionengine/CallbackServer.java
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import us.kbase.auth.AuthException;
import us.kbase.auth.AuthToken;
import us.kbase.auth.ConfigurableAuthService;
import us.kbase.common.executionengine.CallbackServerConfigBuilder.CallbackServerConfig;
import us.kbase.common.service.JacksonTupleModule;
import us.kbase.common.service.JsonClientException;
import us.kbase.common.service.JsonServerMethod;
import us.kbase.common.service.JsonServerServlet;
import us.kbase.common.service.JsonServerSyslog;
import us.kbase.common.service.JsonTokenStream;
import us.kbase.common.service.RpcContext;
import us.kbase.common.service.UObject;
import us.kbase.common.utils.NetUtils;
import us.kbase.workspace.ProvenanceAction;
import us.kbase.workspace.SubAction;

    }

    protected abstract SubsequentCallRunner createJobRunner(
            final AuthToken token,
            final CallbackServerConfig config,
            final UUID jobId,
            final ModuleMethod modmeth,
            final String serviceVer)
            throws IOException, JsonClientException;
    
    @Override
    public void destroy() {
        cbLog("Shutting down executor service");
        final List<Runnable> failed = executor.shutdownNow();
        if (!failed.isEmpty()) {
            cbLog(String.format("Failed to stop %s tasks", failed.size()));
        }
    }
    
    public static URL getCallbackUrl(int callbackPort)
            throws SocketException {
        return getCallbackUrl(callbackPort, null);
    }

    public static URL getCallbackUrl(int callbackPort, String[] networkInterfaces)
            throws SocketException {
        if (networkInterfaces == null || networkInterfaces.length == 0) {
            networkInterfaces = new String[] {"docker0", "vboxnet0", "vboxnet1",
                    "VirtualBox Host-Only Ethernet Adapter", "en0"};
        }
        final List<String> hostIps = NetUtils.findNetworkAddresses(