proxy progress - I am getting tired :(
This commit is contained in:
parent
fdf7281261
commit
d7f9a2b81f
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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> T getJsonResponse(HttpResponse response) throws IOException, ParseException {
|
||||
String responseString = getResponse(response);
|
||||
public static <T> 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) {
|
||||
|
|
|
@ -32,11 +32,11 @@ public class ProxyClient {
|
|||
|
||||
@NotNull
|
||||
public static List<PublicSession> getOnlineApplications(String url) throws IOException {
|
||||
HttpResponse httpResponse = HttpUtil.executeGet(Logger.CONSOLE, url);
|
||||
String responseString = HttpUtil.executeGet(Logger.CONSOLE, url);
|
||||
|
||||
List<PublicSession> 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);
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue