proxy progress

This commit is contained in:
rusefi 2020-07-12 18:18:59 -04:00
parent 677d5888ed
commit 177c5a235b
7 changed files with 92 additions and 79 deletions

View File

@ -3,8 +3,6 @@ package com.rusefi.server;
import org.json.simple.JSONObject;
public class UserDetails {
public static final String USER_ID = "user_id";
public static final String USERNAME = "username";
private final String userName;

View File

@ -0,0 +1,51 @@
package com.rusefi.tools.online;
import com.rusefi.server.UserDetails;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jetbrains.annotations.NotNull;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ProxyClient {
public static final String LOCALHOST = "localhost";
public static final String LIST_PATH = "/list_online";
@NotNull
public static List<UserDetails> getOnlineUsers(int httpPort) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
String url = "http://" + LOCALHOST + ":" + httpPort + LIST_PATH;
System.out.println("Connecting to " + url);
HttpGet httpget = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpget);
HttpEntity entity = httpResponse.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
JSONParser parser = new JSONParser();
List<UserDetails> userLists = new ArrayList<>();
try {
JSONArray array = (JSONArray) parser.parse(responseString);
for (int i = 0; i < array.size(); i++) {
JSONObject element = (JSONObject) array.get(i);
userLists.add(UserDetails.valueOf(element));
}
System.out.println("object=" + array);
} catch (ParseException e) {
throw new IllegalStateException(e);
}
return userLists;
}
}

View File

@ -1,4 +0,0 @@
package com.rusefi.tools.online;
public class ProxyClient {
}

View File

@ -12,7 +12,7 @@ import com.rusefi.server.SessionDetails;
import java.io.IOException;
import java.net.Socket;
import static com.rusefi.io.TcpCommunicationIntegrationTest.LOCALHOST;
import static com.rusefi.tools.online.ProxyClient.LOCALHOST;
import static com.rusefi.io.tcp.BinaryProtocolServer.getPacketLength;
import static com.rusefi.io.tcp.BinaryProtocolServer.readPromisedBytes;

View File

@ -1,33 +1,18 @@
package com.rusefi;
import com.opensr5.Logger;
import com.rusefi.io.TcpCommunicationIntegrationTest;
import com.rusefi.io.tcp.BinaryProtocolServer;
import com.rusefi.server.Backend;
import com.rusefi.server.ClientConnectionState;
import com.rusefi.server.UserDetails;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jetbrains.annotations.NotNull;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.rusefi.tools.online.ProxyClient;
import org.junit.Test;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import static com.rusefi.server.Backend.LIST_PATH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -51,7 +36,7 @@ public class ServerTest {
CountDownLatch onConnected = new CountDownLatch(2);
int httpPort = 8000;
Backend backend = new Backend(userDetailsResolver, httpPort) {
Backend backend = new Backend(userDetailsResolver, httpPort, logger) {
@Override
public void register(ClientConnectionState clientConnectionState) {
super.register(clientConnectionState);
@ -66,25 +51,7 @@ public class ServerTest {
}
};
BinaryProtocolServer.tcpServerSocket(serverPort, "Server", new Function<Socket, Runnable>() {
@Override
public Runnable apply(Socket clientSocket) {
return new Runnable() {
@Override
public void run() {
ClientConnectionState clientConnectionState = new ClientConnectionState(clientSocket, logger, backend.getUserDetailsResolver());
try {
clientConnectionState.requestControllerInfo();
backend.register(clientConnectionState);
clientConnectionState.runEndlessLoop();
} catch (IOException e) {
backend.close(clientConnectionState);
}
}
};
}
}, logger, parameter -> serverCreated.countDown());
Backend.runProxy(serverPort, serverCreated, backend);
assertTrue(serverCreated.await(30, TimeUnit.SECONDS));
assertEquals(0, backend.getCount());
@ -98,38 +65,11 @@ public class ServerTest {
List<ClientConnectionState> clients = backend.getClients();
assertEquals(2, clients.size());
List<UserDetails> onlineUsers = getOnlineUsers(httpPort);
List<UserDetails> onlineUsers = ProxyClient.getOnlineUsers(httpPort);
assertEquals(2, onlineUsers.size());
assertTrue(allClientsDisconnected.await(30, TimeUnit.SECONDS));
}
@NotNull
private List<UserDetails> getOnlineUsers(int httpPort) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
String url = "http://" + TcpCommunicationIntegrationTest.LOCALHOST + ":" + httpPort + LIST_PATH;
System.out.println("Connecting to " + url);
HttpGet httpget = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpget);
HttpEntity entity = httpResponse.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
JSONParser parser = new JSONParser();
List<UserDetails> userLists = new ArrayList<>();
try {
JSONArray array = (JSONArray) parser.parse(responseString);
for (int i = 0; i < array.size(); i++) {
JSONObject element = (JSONObject) array.get(i);
userLists.add(UserDetails.valueOf(element));
}
System.out.println("object=" + array);
} catch (ParseException e) {
throw new IllegalStateException(e);
}
return userLists;
}
}

View File

@ -11,6 +11,7 @@ import com.rusefi.config.generated.Fields;
import com.rusefi.io.tcp.BinaryProtocolProxy;
import com.rusefi.io.tcp.BinaryProtocolServer;
import com.rusefi.io.tcp.TcpIoStream;
import com.rusefi.tools.online.ProxyClient;
import com.rusefi.tune.xml.Constant;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
@ -26,7 +27,6 @@ import static org.junit.Assert.assertTrue;
public class TcpCommunicationIntegrationTest {
private static final Logger LOGGER = Logger.CONSOLE;
public static final String LOCALHOST = "localhost";
// todo: implement & test TCP connector restart!
@Test
@ -68,7 +68,7 @@ public class TcpCommunicationIntegrationTest {
// todo: remove CONFIGURATION_RUSEFI_BINARY or nicer API to disable local file load
LinkManager clientManager = new LinkManager(LOGGER);
clientManager.startAndConnect(LOCALHOST + ":" + port, new ConnectionStateListener() {
clientManager.startAndConnect(ProxyClient.LOCALHOST + ":" + port, new ConnectionStateListener() {
@Override
public void onConnectionEstablished() {
connectionEstablishedCountDownLatch.countDown();
@ -105,16 +105,16 @@ public class TcpCommunicationIntegrationTest {
IoStream targetEcuSocket;
try {
targetEcuSocket = new TcpIoStream(LOGGER, new Socket(LOCALHOST, controllerPort));
targetEcuSocket = new TcpIoStream(LOGGER, new Socket(ProxyClient.LOCALHOST, controllerPort));
} catch (IOException e) {
throw new IllegalStateException("Failed to connect to controller " + LOCALHOST + ":" + controllerPort);
throw new IllegalStateException("Failed to connect to controller " + ProxyClient.LOCALHOST + ":" + controllerPort);
}
BinaryProtocolProxy.createProxy(targetEcuSocket, proxyPort);
CountDownLatch connectionEstablishedCountDownLatch = new CountDownLatch(1);
LinkManager clientManager = new LinkManager(LOGGER);
clientManager.startAndConnect(LOCALHOST + ":" + proxyPort, new ConnectionStateListener() {
clientManager.startAndConnect(ProxyClient.LOCALHOST + ":" + proxyPort, new ConnectionStateListener() {
@Override
public void onConnectionEstablished() {
connectionEstablishedCountDownLatch.countDown();

View File

@ -1,5 +1,8 @@
package com.rusefi.server;
import com.opensr5.Logger;
import com.rusefi.io.tcp.BinaryProtocolServer;
import com.rusefi.tools.online.ProxyClient;
import org.jetbrains.annotations.NotNull;
import org.takes.Take;
import org.takes.facets.fork.FkRegex;
@ -12,21 +15,44 @@ import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.function.Function;
public class Backend {
public static final String LIST_PATH = "/list_online";
public static final String VERSION_PATH = "/version";
public static final String BACKEND_VERSION = "0.0001";
private final FkRegex showOnlineUsers = new FkRegex(LIST_PATH,
private final FkRegex showOnlineUsers = new FkRegex(ProxyClient.LIST_PATH,
(Take) req -> getUsersOnline()
);
public static void runProxy(int serverPort, CountDownLatch serverCreated, Backend backend) {
BinaryProtocolServer.tcpServerSocket(serverPort, "Server", new Function<Socket, Runnable>() {
@Override
public Runnable apply(Socket clientSocket) {
return new Runnable() {
@Override
public void run() {
ClientConnectionState clientConnectionState = new ClientConnectionState(clientSocket, backend.logger, backend.getUserDetailsResolver());
try {
clientConnectionState.requestControllerInfo();
backend.register(clientConnectionState);
clientConnectionState.runEndlessLoop();
} catch (IOException e) {
backend.close(clientConnectionState);
}
}
};
}
}, backend.logger, parameter -> serverCreated.countDown());
}
@NotNull
private RsJson getUsersOnline() throws IOException {
JsonArrayBuilder builder = Json.createArrayBuilder();
@ -47,10 +73,12 @@ public class Backend {
private final Set<ClientConnectionState> clients = new HashSet<>();
// private final int clientTimeout;
private final Function<String, UserDetails> userDetailsResolver;
private final Logger logger;
public Backend(Function<String, UserDetails> userDetailsResolver, int httpPort) {
public Backend(Function<String, UserDetails> userDetailsResolver, int httpPort, Logger logger) {
// this.clientTimeout = clientTimeout;
this.userDetailsResolver = userDetailsResolver;
this.logger = logger;
new Thread(new Runnable() {