From d7f9a2b81f9ae30ea9922e7c784b6e980923277a Mon Sep 17 00:00:00 2001 From: rusefi Date: Fri, 24 Jul 2020 00:52:33 -0400 Subject: [PATCH] proxy progress - I am getting tired :( --- .../java/com/rusefi/LocalApplicationProxy.java | 3 +-- .../src/main/java/com/rusefi/io/ByteReader.java | 4 +++- .../main/java/com/rusefi/io/tcp/TcpIoStream.java | 14 +++++++++++++- .../java/com/rusefi/tools/online/HttpUtil.java | 15 +++++++++++---- .../java/com/rusefi/tools/online/ProxyClient.java | 4 ++-- .../src/com/rusefi/tools/online/Online.java | 2 +- .../src/main/java/com/rusefi/server/Backend.java | 3 ++- .../rusefi/server/JsonUserDetailsResolver.java | 4 ++-- 8 files changed, 35 insertions(+), 14 deletions(-) diff --git a/java_console/io/src/main/java/com/rusefi/LocalApplicationProxy.java b/java_console/io/src/main/java/com/rusefi/LocalApplicationProxy.java index 8f50b1fd65..ee5e2f16a3 100644 --- a/java_console/io/src/main/java/com/rusefi/LocalApplicationProxy.java +++ b/java_console/io/src/main/java/com/rusefi/LocalApplicationProxy.java @@ -31,8 +31,7 @@ public class LocalApplicationProxy { * @param jsonHttpPort */ public static ServerHolder startAndRun(Logger logger, int serverPortForRemoteUsers, ApplicationRequest applicationRequest, int localApplicationPort, int jsonHttpPort) throws IOException { - HttpResponse httpResponse = HttpUtil.executeGet(logger,ProxyClient.getHttpAddress(jsonHttpPort) + ProxyClient.VERSION_PATH); - String version = HttpUtil.getResponse(httpResponse); + String version = HttpUtil.executeGet(logger,ProxyClient.getHttpAddress(jsonHttpPort) + ProxyClient.VERSION_PATH); logger.info("Server says version=" + version); if (!version.contains(ProxyClient.BACKEND_VERSION)) throw new IOException("Unexpected backend version " + version + " while we want " + ProxyClient.BACKEND_VERSION); diff --git a/java_console/io/src/main/java/com/rusefi/io/ByteReader.java b/java_console/io/src/main/java/com/rusefi/io/ByteReader.java index 07a2e25465..d121a1d40c 100644 --- a/java_console/io/src/main/java/com/rusefi/io/ByteReader.java +++ b/java_console/io/src/main/java/com/rusefi/io/ByteReader.java @@ -3,6 +3,7 @@ package com.rusefi.io; import com.opensr5.Logger; import com.opensr5.io.DataListener; import com.rusefi.config.generated.Fields; +import com.rusefi.io.tcp.TcpIoStream; import java.io.IOException; import java.util.Arrays; @@ -10,7 +11,7 @@ import java.util.concurrent.Executor; import java.util.concurrent.Executors; public interface ByteReader { - static void runReaderLoop(String loggingPrefix, DataListener listener, ByteReader reader, Logger logger) { + static void runReaderLoop(String loggingPrefix, DataListener listener, ByteReader reader, Logger logger, TcpIoStream.DisconnectListener disconnectListener) { /** * Threading of the whole input/output does not look healthy at all! * @@ -35,6 +36,7 @@ public interface ByteReader { listener.onDataArrived(Arrays.copyOf(inputBuffer, result)); } catch (IOException e) { logger.error("TcpIoStream: End of connection " + e); + disconnectListener.onDisconnect(); return; } } diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java b/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java index 2eb96a4363..f1b4b00377 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java @@ -23,6 +23,7 @@ public class TcpIoStream extends AbstractIoStream { private final OutputStream output; private final Logger logger; private final String loggingPrefix; + private final DisconnectListener disconnectListener; @NotNull private final Socket socket; private final IncomingDataBuffer dataBuffer; @@ -32,7 +33,12 @@ public class TcpIoStream extends AbstractIoStream { } public TcpIoStream(String loggingPrefix, Logger logger, Socket socket) throws IOException { + this(loggingPrefix, logger, socket, DisconnectListener.VOID); + } + + public TcpIoStream(String loggingPrefix, Logger logger, Socket socket, DisconnectListener disconnectListener) throws IOException { this.loggingPrefix = loggingPrefix; + this.disconnectListener = disconnectListener; if (socket == null) throw new NullPointerException("socket"); this.socket = socket; @@ -75,7 +81,13 @@ public class TcpIoStream extends AbstractIoStream { @Override public void setInputListener(final DataListener listener) { + ByteReader.runReaderLoop(loggingPrefix, listener, input::read, logger, disconnectListener); + } - ByteReader.runReaderLoop(loggingPrefix, listener, input::read, logger); + public interface DisconnectListener { + DisconnectListener VOID = () -> { + + }; + void onDisconnect(); } } diff --git a/java_console/io/src/main/java/com/rusefi/tools/online/HttpUtil.java b/java_console/io/src/main/java/com/rusefi/tools/online/HttpUtil.java index 04476e7adb..f0bc5c2f51 100644 --- a/java_console/io/src/main/java/com/rusefi/tools/online/HttpUtil.java +++ b/java_console/io/src/main/java/com/rusefi/tools/online/HttpUtil.java @@ -26,8 +26,7 @@ public class HttpUtil { public static String RUSEFI_ONLINE_JSON_API_PREFIX = "https://rusefi.com/online/api.php?method="; - public static T getJsonResponse(HttpResponse response) throws IOException, ParseException { - String responseString = getResponse(response); + public static T getJsonResponse(String responseString) throws ParseException { JSONParser parser = new JSONParser(); return (T) parser.parse(responseString); @@ -40,7 +39,7 @@ public class HttpUtil { return responseString; } - public static HttpResponse executeGet(Logger logger, String url) throws IOException { + public static String executeGet(Logger logger, String url) throws IOException { HttpClient httpclient = new DefaultHttpClient(); HttpParams httpParameters = httpclient.getParams(); // HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT); @@ -49,7 +48,15 @@ public class HttpUtil { HttpConnectionParams.setTcpNoDelay(httpParameters, true); logger.info("GET " + url); HttpGet httpget = new HttpGet(url); - return httpclient.execute(httpget); + + // in case of emergency + // -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog -Dorg.apache.commons.logging.simplelog.showdatetime=true -Dorg.apache.commons.logging.simplelog.log.org.apache.http=DEBUG -Dorg.apache.commons.logging.simplelog.log.org.apache.http.wire=ERROR + try { + HttpResponse httpResponse = httpclient.execute(httpget); + return HttpUtil.getResponse(httpResponse); + } finally { + httpget.releaseConnection(); + } } public static JSONObject parse(String jsonString) { diff --git a/java_console/io/src/main/java/com/rusefi/tools/online/ProxyClient.java b/java_console/io/src/main/java/com/rusefi/tools/online/ProxyClient.java index b145623a0f..df392b5024 100644 --- a/java_console/io/src/main/java/com/rusefi/tools/online/ProxyClient.java +++ b/java_console/io/src/main/java/com/rusefi/tools/online/ProxyClient.java @@ -32,11 +32,11 @@ public class ProxyClient { @NotNull public static List getOnlineApplications(String url) throws IOException { - HttpResponse httpResponse = HttpUtil.executeGet(Logger.CONSOLE, url); + String responseString = HttpUtil.executeGet(Logger.CONSOLE, url); List userLists = new ArrayList<>(); try { - JSONArray array = HttpUtil.getJsonResponse(httpResponse); + JSONArray array = HttpUtil.getJsonResponse(responseString); for (int i = 0; i < array.size(); i++) { JSONObject element = (JSONObject) array.get(i); diff --git a/java_console/shared_ui/src/com/rusefi/tools/online/Online.java b/java_console/shared_ui/src/com/rusefi/tools/online/Online.java index 8c4bdd48ed..496227ed64 100644 --- a/java_console/shared_ui/src/com/rusefi/tools/online/Online.java +++ b/java_console/shared_ui/src/com/rusefi/tools/online/Online.java @@ -44,7 +44,7 @@ public class Online { System.out.println("response=" + response); System.out.println("code " + response.getStatusLine().getStatusCode()); - JSONObject object = HttpUtil.getJsonResponse(response); + JSONObject object = HttpUtil.getJsonResponse(HttpUtil.getResponse(response)); System.out.println("object=" + object); JSONArray info = (JSONArray) object.get("info"); diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java b/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java index bf81caed5f..6759d01bd0 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java @@ -68,7 +68,7 @@ public class Backend implements Closeable { public int serverPortForControllers; public Backend(UserDetailsResolver userDetailsResolver, int httpPort, Logger logger) { - this(userDetailsResolver, httpPort, logger, Timeouts.READ_IMAGE_TIMEOUT); + this(userDetailsResolver, httpPort, logger, 600 * SECOND); } public Backend(UserDetailsResolver userDetailsResolver, int httpPort, Logger logger, int applicationTimeout) { @@ -305,6 +305,7 @@ public class Backend implements Closeable { } for (ApplicationConnectionState inactiveClient : inactiveApplications) { + logger.error("Kicking out application " + inactiveClient); close(inactiveClient); } } diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/server/JsonUserDetailsResolver.java b/java_tools/proxy_server/src/main/java/com/rusefi/server/JsonUserDetailsResolver.java index a0e4387730..2b50771f50 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/server/JsonUserDetailsResolver.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/server/JsonUserDetailsResolver.java @@ -15,8 +15,8 @@ public class JsonUserDetailsResolver implements UserDetailsResolver { public UserDetails apply(String authToken) { try { - HttpResponse response = HttpUtil.executeGet(Logger.CONSOLE,HttpUtil.RUSEFI_ONLINE_JSON_API_PREFIX + "getUserByToken&rusefi_token=" + authToken); - JSONObject json = HttpUtil.getJsonResponse(response); + String responseString = HttpUtil.executeGet(Logger.CONSOLE,HttpUtil.RUSEFI_ONLINE_JSON_API_PREFIX + "getUserByToken&rusefi_token=" + authToken); + JSONObject json = HttpUtil.getJsonResponse(responseString); System.out.println("String " + json); Object getUserByToken = json.get("getUserByToken"); if (getUserByToken instanceof String) {