proxy progress
This commit is contained in:
parent
ebfd0bb0c3
commit
23fc41f3a7
|
@ -1,10 +1,9 @@
|
||||||
package com.rusefi.server;
|
package com.rusefi.server;
|
||||||
|
|
||||||
import javax.json.Json;
|
import org.json.simple.JSONObject;
|
||||||
import javax.json.JsonObject;
|
import org.json.simple.parser.JSONParser;
|
||||||
import javax.json.JsonReader;
|
import org.json.simple.parser.ParseException;
|
||||||
import javax.json.stream.JsonParsingException;
|
|
||||||
import java.io.StringReader;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class ApplicationRequest {
|
public class ApplicationRequest {
|
||||||
|
@ -28,26 +27,25 @@ public class ApplicationRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toJson() {
|
public String toJson() {
|
||||||
JsonObject jsonObject = Json.createObjectBuilder()
|
JSONObject jsonObject = new JSONObject();
|
||||||
.add(SESSION, sessionDetails.toJson())
|
jsonObject.put(SESSION, sessionDetails.toJson());
|
||||||
.add(USER_ID, targetUserId)
|
jsonObject.put(USER_ID, targetUserId);
|
||||||
.build();
|
return jsonObject.toJSONString();
|
||||||
return jsonObject.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ApplicationRequest valueOf(String jsonString) {
|
public static ApplicationRequest valueOf(String jsonString) {
|
||||||
JsonReader reader = Json.createReader(new StringReader(jsonString));
|
JSONParser parser = new JSONParser();
|
||||||
|
JSONObject jsonObject;
|
||||||
JsonObject jsonObject;
|
|
||||||
try {
|
try {
|
||||||
jsonObject = reader.readObject();
|
jsonObject = (JSONObject) parser.parse(jsonString);
|
||||||
} catch (JsonParsingException e) {
|
} catch (ParseException e) {
|
||||||
throw new IllegalStateException("While parsing [" + jsonString + "]", e);
|
throw new IllegalStateException(e);
|
||||||
}
|
}
|
||||||
int targetUserId = jsonObject.getInt(USER_ID);
|
|
||||||
|
|
||||||
SessionDetails session = SessionDetails.valueOf(jsonObject.getString(SESSION));
|
long targetUserId = (Long) jsonObject.get(USER_ID);
|
||||||
return new ApplicationRequest(session, targetUserId);
|
|
||||||
|
SessionDetails session = SessionDetails.valueOf((String) jsonObject.get(SESSION));
|
||||||
|
return new ApplicationRequest(session, (int)targetUserId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
|
@ -1,5 +1,6 @@
|
||||||
package com.rusefi.server;
|
package com.rusefi.server;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
import org.json.simple.parser.ParseException;
|
import org.json.simple.parser.ParseException;
|
||||||
|
@ -57,6 +58,7 @@ public class ControllerInfo {
|
||||||
return signature;
|
return signature;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static ControllerInfo valueOf(String jsonString) {
|
public static ControllerInfo valueOf(String jsonString) {
|
||||||
JSONParser parser = new JSONParser();
|
JSONParser parser = new JSONParser();
|
||||||
JSONObject jsonObject;
|
JSONObject jsonObject;
|
||||||
|
@ -65,6 +67,11 @@ public class ControllerInfo {
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
throw new IllegalStateException(e);
|
throw new IllegalStateException(e);
|
||||||
}
|
}
|
||||||
|
return valueOf(jsonObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static ControllerInfo valueOf(JSONObject jsonObject) {
|
||||||
String vehicleName = (String) jsonObject.get(VEHICLE_NAME);
|
String vehicleName = (String) jsonObject.get(VEHICLE_NAME);
|
||||||
String engineMake = (String) jsonObject.get(ENGINE_MAKE);
|
String engineMake = (String) jsonObject.get(ENGINE_MAKE);
|
||||||
String engineCode = (String) jsonObject.get(ENGINE_CODE);
|
String engineCode = (String) jsonObject.get(ENGINE_CODE);
|
||||||
|
|
|
@ -4,7 +4,7 @@ public class SignatureHelper {
|
||||||
|
|
||||||
public static final String PREFIX = "rusEFI ";
|
public static final String PREFIX = "rusEFI ";
|
||||||
|
|
||||||
static String getUrl(String signature) {
|
public static String getUrl(String signature) {
|
||||||
if (!signature.startsWith(PREFIX))
|
if (!signature.startsWith(PREFIX))
|
||||||
return null;
|
return null;
|
||||||
signature = signature.substring(PREFIX.length()).trim();
|
signature = signature.substring(PREFIX.length()).trim();
|
||||||
|
|
|
@ -27,6 +27,9 @@ import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See NetworkConnectorStartup
|
||||||
|
*/
|
||||||
public class Backend implements Closeable {
|
public class Backend implements Closeable {
|
||||||
public static final String VERSION_PATH = "/version";
|
public static final String VERSION_PATH = "/version";
|
||||||
public static final String BACKEND_VERSION = "0.0001";
|
public static final String BACKEND_VERSION = "0.0001";
|
||||||
|
@ -46,6 +49,8 @@ public class Backend implements Closeable {
|
||||||
private final UserDetailsResolver userDetailsResolver;
|
private final UserDetailsResolver userDetailsResolver;
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
public final static AtomicLong totalSessions = new AtomicLong();
|
public final static AtomicLong totalSessions = new AtomicLong();
|
||||||
|
public int serverPortForApplications;
|
||||||
|
public int serverPortForControllers;
|
||||||
|
|
||||||
public Backend(UserDetailsResolver userDetailsResolver, int httpPort, Logger logger) {
|
public Backend(UserDetailsResolver userDetailsResolver, int httpPort, Logger logger) {
|
||||||
// this.clientTimeout = clientTimeout;
|
// this.clientTimeout = clientTimeout;
|
||||||
|
@ -59,7 +64,7 @@ public class Backend implements Closeable {
|
||||||
try {
|
try {
|
||||||
new FtBasic(
|
new FtBasic(
|
||||||
new TkFork(showOnlineUsers,
|
new TkFork(showOnlineUsers,
|
||||||
Monitoring.showStatistics,
|
new Monitoring(this).showStatistics,
|
||||||
new FkRegex(VERSION_PATH, BACKEND_VERSION),
|
new FkRegex(VERSION_PATH, BACKEND_VERSION),
|
||||||
new FkRegex("/", new RsHtml("<html><body>\n" +
|
new FkRegex("/", new RsHtml("<html><body>\n" +
|
||||||
"<a href='https://rusefi.com/online/'>rusEFI Online</a>\n" +
|
"<a href='https://rusefi.com/online/'>rusEFI Online</a>\n" +
|
||||||
|
@ -92,6 +97,7 @@ public class Backend implements Closeable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void runApplicationConnector(int serverPortForApplications, Listener serverSocketCreationCallback) {
|
public void runApplicationConnector(int serverPortForApplications, Listener serverSocketCreationCallback) {
|
||||||
|
this.serverPortForApplications = serverPortForApplications;
|
||||||
// connection from authenticator app which proxies for Tuner Studio
|
// connection from authenticator app which proxies for Tuner Studio
|
||||||
// authenticator pushed hello packet on connect
|
// authenticator pushed hello packet on connect
|
||||||
System.out.println("Starting application connector at " + serverPortForApplications);
|
System.out.println("Starting application connector at " + serverPortForApplications);
|
||||||
|
@ -146,6 +152,7 @@ public class Backend implements Closeable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void runControllerConnector(int serverPortForControllers, Listener serverSocketCreationCallback) {
|
public void runControllerConnector(int serverPortForControllers, Listener serverSocketCreationCallback) {
|
||||||
|
this.serverPortForControllers = serverPortForControllers;
|
||||||
System.out.println("Starting controller connector at " + serverPortForControllers);
|
System.out.println("Starting controller connector at " + serverPortForControllers);
|
||||||
BinaryProtocolServer.tcpServerSocket(logger, new Function<Socket, Runnable>() {
|
BinaryProtocolServer.tcpServerSocket(logger, new Function<Socket, Runnable>() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -12,9 +12,14 @@ import java.lang.management.OperatingSystemMXBean;
|
||||||
|
|
||||||
public class Monitoring {
|
public class Monitoring {
|
||||||
public static final String STATUS = "/status";
|
public static final String STATUS = "/status";
|
||||||
static final FkRegex showStatistics = new FkRegex(STATUS,
|
final FkRegex showStatistics;
|
||||||
(Take) req -> Monitoring.getStatus());
|
private final Backend backend;
|
||||||
|
|
||||||
|
public Monitoring(Backend backend) {
|
||||||
|
this.backend = backend;
|
||||||
|
showStatistics = new FkRegex(STATUS,
|
||||||
|
(Take) req -> getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
private static String formatSize(long v) {
|
private static String formatSize(long v) {
|
||||||
if (v < 1024) return v + " B";
|
if (v < 1024) return v + " B";
|
||||||
|
@ -22,7 +27,7 @@ public class Monitoring {
|
||||||
return String.format("%.1f %sB", (double) v / (1L << (z * 10)), " KMGTPE".charAt(z));
|
return String.format("%.1f %sB", (double) v / (1L << (z * 10)), " KMGTPE".charAt(z));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static RsJson getStatus() throws IOException {
|
private RsJson getStatus() throws IOException {
|
||||||
JsonObjectBuilder builder = Json.createObjectBuilder();
|
JsonObjectBuilder builder = Json.createObjectBuilder();
|
||||||
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
|
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
|
||||||
builder.add("CPU_Load", operatingSystemMXBean.getSystemLoadAverage());
|
builder.add("CPU_Load", operatingSystemMXBean.getSystemLoadAverage());
|
||||||
|
@ -30,6 +35,8 @@ public class Monitoring {
|
||||||
builder.add("max_ram", formatSize(Runtime.getRuntime().maxMemory()));
|
builder.add("max_ram", formatSize(Runtime.getRuntime().maxMemory()));
|
||||||
builder.add("threads", Thread.getAllStackTraces().size());
|
builder.add("threads", Thread.getAllStackTraces().size());
|
||||||
builder.add("sessions", Backend.totalSessions.get());
|
builder.add("sessions", Backend.totalSessions.get());
|
||||||
|
builder.add("serverPortForApplications", backend.serverPortForApplications);
|
||||||
|
builder.add("serverPortForControllers", backend.serverPortForControllers);
|
||||||
|
|
||||||
return new RsJson(builder.build());
|
return new RsJson(builder.build());
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ public class SessionDetailsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testApplcationRequest() {
|
public void testApplicationRequest() {
|
||||||
ControllerInfo ci = new ControllerInfo("name", "make", "code", "sign");
|
ControllerInfo ci = new ControllerInfo("name", "make", "code", "sign");
|
||||||
SessionDetails sd = new SessionDetails(ci, "auth", 123);
|
SessionDetails sd = new SessionDetails(ci, "auth", 123);
|
||||||
ApplicationRequest ar = new ApplicationRequest(sd, 321);
|
ApplicationRequest ar = new ApplicationRequest(sd, 321);
|
||||||
|
|
Loading…
Reference in New Issue