proxy progress - I am getting tired :(
This commit is contained in:
parent
fdf7281261
commit
d7f9a2b81f
|
@ -31,8 +31,7 @@ public class LocalApplicationProxy {
|
||||||
* @param jsonHttpPort
|
* @param jsonHttpPort
|
||||||
*/
|
*/
|
||||||
public static ServerHolder startAndRun(Logger logger, int serverPortForRemoteUsers, ApplicationRequest applicationRequest, int localApplicationPort, int jsonHttpPort) throws IOException {
|
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.executeGet(logger,ProxyClient.getHttpAddress(jsonHttpPort) + ProxyClient.VERSION_PATH);
|
||||||
String version = HttpUtil.getResponse(httpResponse);
|
|
||||||
logger.info("Server says version=" + version);
|
logger.info("Server says version=" + version);
|
||||||
if (!version.contains(ProxyClient.BACKEND_VERSION))
|
if (!version.contains(ProxyClient.BACKEND_VERSION))
|
||||||
throw new IOException("Unexpected backend version " + version + " while we want " + 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.Logger;
|
||||||
import com.opensr5.io.DataListener;
|
import com.opensr5.io.DataListener;
|
||||||
import com.rusefi.config.generated.Fields;
|
import com.rusefi.config.generated.Fields;
|
||||||
|
import com.rusefi.io.tcp.TcpIoStream;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@ -10,7 +11,7 @@ import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
public interface ByteReader {
|
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!
|
* 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));
|
listener.onDataArrived(Arrays.copyOf(inputBuffer, result));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("TcpIoStream: End of connection " + e);
|
logger.error("TcpIoStream: End of connection " + e);
|
||||||
|
disconnectListener.onDisconnect();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ public class TcpIoStream extends AbstractIoStream {
|
||||||
private final OutputStream output;
|
private final OutputStream output;
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private final String loggingPrefix;
|
private final String loggingPrefix;
|
||||||
|
private final DisconnectListener disconnectListener;
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Socket socket;
|
private final Socket socket;
|
||||||
private final IncomingDataBuffer dataBuffer;
|
private final IncomingDataBuffer dataBuffer;
|
||||||
|
@ -32,7 +33,12 @@ public class TcpIoStream extends AbstractIoStream {
|
||||||
}
|
}
|
||||||
|
|
||||||
public TcpIoStream(String loggingPrefix, Logger logger, Socket socket) throws IOException {
|
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.loggingPrefix = loggingPrefix;
|
||||||
|
this.disconnectListener = disconnectListener;
|
||||||
if (socket == null)
|
if (socket == null)
|
||||||
throw new NullPointerException("socket");
|
throw new NullPointerException("socket");
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
|
@ -75,7 +81,13 @@ public class TcpIoStream extends AbstractIoStream {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setInputListener(final DataListener listener) {
|
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 String RUSEFI_ONLINE_JSON_API_PREFIX = "https://rusefi.com/online/api.php?method=";
|
||||||
|
|
||||||
public static <T> T getJsonResponse(HttpResponse response) throws IOException, ParseException {
|
public static <T> T getJsonResponse(String responseString) throws ParseException {
|
||||||
String responseString = getResponse(response);
|
|
||||||
|
|
||||||
JSONParser parser = new JSONParser();
|
JSONParser parser = new JSONParser();
|
||||||
return (T) parser.parse(responseString);
|
return (T) parser.parse(responseString);
|
||||||
|
@ -40,7 +39,7 @@ public class HttpUtil {
|
||||||
return responseString;
|
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();
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
HttpParams httpParameters = httpclient.getParams();
|
HttpParams httpParameters = httpclient.getParams();
|
||||||
// HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
|
// HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
|
||||||
|
@ -49,7 +48,15 @@ public class HttpUtil {
|
||||||
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
|
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
|
||||||
logger.info("GET " + url);
|
logger.info("GET " + url);
|
||||||
HttpGet httpget = new HttpGet(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) {
|
public static JSONObject parse(String jsonString) {
|
||||||
|
|
|
@ -32,11 +32,11 @@ public class ProxyClient {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static List<PublicSession> getOnlineApplications(String url) throws IOException {
|
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<>();
|
List<PublicSession> userLists = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
JSONArray array = HttpUtil.getJsonResponse(httpResponse);
|
JSONArray array = HttpUtil.getJsonResponse(responseString);
|
||||||
|
|
||||||
for (int i = 0; i < array.size(); i++) {
|
for (int i = 0; i < array.size(); i++) {
|
||||||
JSONObject element = (JSONObject) array.get(i);
|
JSONObject element = (JSONObject) array.get(i);
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class Online {
|
||||||
System.out.println("response=" + response);
|
System.out.println("response=" + response);
|
||||||
System.out.println("code " + response.getStatusLine().getStatusCode());
|
System.out.println("code " + response.getStatusLine().getStatusCode());
|
||||||
|
|
||||||
JSONObject object = HttpUtil.getJsonResponse(response);
|
JSONObject object = HttpUtil.getJsonResponse(HttpUtil.getResponse(response));
|
||||||
|
|
||||||
System.out.println("object=" + object);
|
System.out.println("object=" + object);
|
||||||
JSONArray info = (JSONArray) object.get("info");
|
JSONArray info = (JSONArray) object.get("info");
|
||||||
|
|
|
@ -68,7 +68,7 @@ public class Backend implements Closeable {
|
||||||
public int serverPortForControllers;
|
public int serverPortForControllers;
|
||||||
|
|
||||||
public Backend(UserDetailsResolver userDetailsResolver, int httpPort, Logger logger) {
|
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) {
|
public Backend(UserDetailsResolver userDetailsResolver, int httpPort, Logger logger, int applicationTimeout) {
|
||||||
|
@ -305,6 +305,7 @@ public class Backend implements Closeable {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ApplicationConnectionState inactiveClient : inactiveApplications) {
|
for (ApplicationConnectionState inactiveClient : inactiveApplications) {
|
||||||
|
logger.error("Kicking out application " + inactiveClient);
|
||||||
close(inactiveClient);
|
close(inactiveClient);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,8 @@ public class JsonUserDetailsResolver implements UserDetailsResolver {
|
||||||
public UserDetails apply(String authToken) {
|
public UserDetails apply(String authToken) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
HttpResponse response = HttpUtil.executeGet(Logger.CONSOLE,HttpUtil.RUSEFI_ONLINE_JSON_API_PREFIX + "getUserByToken&rusefi_token=" + authToken);
|
String responseString = HttpUtil.executeGet(Logger.CONSOLE,HttpUtil.RUSEFI_ONLINE_JSON_API_PREFIX + "getUserByToken&rusefi_token=" + authToken);
|
||||||
JSONObject json = HttpUtil.getJsonResponse(response);
|
JSONObject json = HttpUtil.getJsonResponse(responseString);
|
||||||
System.out.println("String " + json);
|
System.out.println("String " + json);
|
||||||
Object getUserByToken = json.get("getUserByToken");
|
Object getUserByToken = json.get("getUserByToken");
|
||||||
if (getUserByToken instanceof String) {
|
if (getUserByToken instanceof String) {
|
||||||
|
|
Loading…
Reference in New Issue