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