public static List listRunningInstancesPorts()

in java/com/jetbrains/cef/remote/ProcessLister.java [21:114]


    public static List<RunningServerInfo> listRunningInstancesPorts() {
        final ArrayList<RunningServerInfo> result = new ArrayList<>();

        if (OS.isLinux() || OS.isMacintosh()) {
            final String cmd = "ps -Af | grep -E 'cef_server .*'";
            RunningServerInfo defaultArgsServer = null;

            try {
                Process process = new ProcessBuilder("bash", "-c", cmd).redirectErrorStream(true).start();
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
                    //   UID PID   PPID ....
                    //   501 80516 80489   0 Fri07AM ??         1:52.31 /Users/..../Contents/Frameworks/cef_server.app/Contents/MacOS/cef_server --port=6188 --logfile=/Users/.../jcef_80489.log --loglevel=5 --params=/var/folders/1k/hmmg06wx2bn4dwfq53wy6c_c0000gn/T/cef_server_params.txt
                    String line;
                    while ((line = reader.readLine()) != null) {
                        if (line.contains("grep -E"))
                            continue;

                        line = line.trim();
                        final int posSp0 = line.indexOf(" ");
                        int posSp0End = posSp0 + 1;
                        while (posSp0End < line.length() && line.charAt(posSp0End) == ' ') ++posSp0End;
                        final int posSp1 = line.indexOf(" ", posSp0End);
                        int posSp1End = posSp1 + 1;
                        while (posSp1End < line.length() && line.charAt(posSp1End) == ' ') ++posSp1End;
                        final int posSp2 = line.indexOf(" ", posSp1End);
                        int posSp2End = posSp2 + 1;
                        while (posSp2End < line.length() && line.charAt(posSp2End) == ' ') ++posSp2End;

                        final int pid = parseIntSafe(line.substring(posSp0End, posSp1), -1);
                        final int ppid = parseIntSafe(line.substring(posSp1End, posSp2), -1);

                        final String cmdPrefix = OS.isMacintosh() ? "MacOS/cef_server" : "bin/cef_server";
                        final int prefixPos = line.indexOf(cmdPrefix);
                        final String cmdLine = prefixPos >= 0 ? line.substring(prefixPos + cmdPrefix.length()) : "";

                        final int pos0 = line.indexOf("--port=");
                        if (pos0 < 0) {
                            if (OS.isMacintosh() && line.contains("cef_server Helper"))
                                continue;
                            if (OS.isLinux() && line.contains("--type="))
                                continue;
                            defaultArgsServer = new RunningServerInfo(new ThriftTransport(9999), pid, ppid, cmdLine);
                        } else {
                            final int pos1 = line.indexOf(" ", pos0 + 7);
                            String sport = line.substring(pos0 + 7, pos1);
                            try {
                                result.add(new RunningServerInfo(new ThriftTransport(Integer.parseInt(sport)), pid, ppid, cmdLine));
                            } catch (NumberFormatException e) {
                                CefLog.Error("Can't parse port number: " + sport);
                            }
                        }
                    }
                }

                process.waitFor();
            } catch (IOException | InterruptedException e) {
                CefLog.Error("Failed to execute command: " + e.getMessage());
                e.printStackTrace();
            }

            if (defaultArgsServer != null)
                result.add(defaultArgsServer);

            return result;
        }

        // Windows
        Pattern p = Pattern.compile("--type=[^ ]+");
        List<WindowsProcessInfo> processes = null;
        try {
            processes = listWindowsProcesses(".*cef_server.exe", s -> !p.matcher(s).find());
            for (WindowsProcessInfo pi : processes) {
                final int pos0 = pi.commandLine.indexOf("--port=");
                if (pos0 >= 0) {
                    final int pos1 = pi.commandLine.indexOf(" ", pos0 + 7);
                    String sport = pi.commandLine.substring(pos0 + 7, pos1);
                    try {
                        result.add(new RunningServerInfo(new ThriftTransport(Integer.parseInt(sport)), pi.pid, pi.parentPid == null ? -1 : pi.parentPid, pi.commandLine, pi.parentName, pi.parentCommandLine));
                    } catch (NumberFormatException e) {
                        CefLog.Error("Can't parse port number: " + sport);
                    }
                } else {
                    CefLog.Debug("Found cef_server instance without --port parameter.");
                    result.add(new RunningServerInfo(new ThriftTransport(9999), pi.pid, -1, pi.commandLine, pi.parentName, pi.parentCommandLine));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return result;
    }