Skip to content

Commit

Permalink
Add support of several endpoint address to ReverseProxy test utility
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Oct 15, 2024
1 parent 812726a commit 7db3aec
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
*******************************************************************************/
package org.eclipse.leshan.integration.tests.util;

import static org.eclipse.leshan.core.util.TestToolBox.uriHandler;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.leshan.core.endpoint.EndpointUri;
import org.eclipse.leshan.core.endpoint.Protocol;

public class LeshanProxyBuilder {

public static ReverseProxy givenReverseProxyFor(LeshanTestServer server, Protocol protocol) {
EndpointUri serverEndpointUri = server.getEndpoint(protocol).getURI();
return new ReverseProxy(new InetSocketAddress("localhost", 0),
new InetSocketAddress(serverEndpointUri.getHost(), serverEndpointUri.getPort()));
List<InetSocketAddress> endpoints = server.getEndpoints().stream() //
.map(e -> uriHandler.getSocketAddr(e.getURI())) //
.collect(Collectors.toList());

return new ReverseProxy(new InetSocketAddress("localhost", 0), endpoints);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -58,9 +61,12 @@ public class ReverseProxy {
private static final int BUFFER_SIZE = 2048;

private final InetSocketAddress clientSideProxyAddress;
private final InetSocketAddress serverAddress;
private InetSocketAddress serverAddress;
private InetSocketAddress clientAddress;

private final List<InetSocketAddress> possibleServerAddress;
private volatile int currentServerAddressIndex;

private DatagramChannel clientToProxyChannel;
private DatagramChannel proxyToServerChannel;
private Selector selector;
Expand All @@ -69,10 +75,17 @@ public class ReverseProxy {
private volatile boolean running = false; // true if reserver proxy is running
private volatile boolean stop = false; // true if we asked to stop reserve proxy
private volatile boolean changeServerSideProxyAddress = false; // true if we ask to change server side proxy address
private volatile int askedServerAddressIndex; // value of the requested server address index

public ReverseProxy(InetSocketAddress clientSideProxyAddress, InetSocketAddress serverAddress) {
this(clientSideProxyAddress, Collections.singletonList(serverAddress));
}

public ReverseProxy(InetSocketAddress clientSideProxyAddress, List<InetSocketAddress> possibleServerAddress) {
this.clientSideProxyAddress = clientSideProxyAddress;
this.serverAddress = serverAddress;
this.possibleServerAddress = Collections.unmodifiableList(new ArrayList<>(possibleServerAddress));
this.currentServerAddressIndex = 0;
this.serverAddress = possibleServerAddress.get(currentServerAddressIndex);
}

public void start() {
Expand Down Expand Up @@ -111,6 +124,12 @@ public void start() {
if (changeServerSideProxyAddress) {
reassignServerSideProxyAddress();
}
// change server address if needed
int addr = askedServerAddressIndex;
if (addr != currentServerAddressIndex) {
serverAddress = possibleServerAddress.get(addr);
currentServerAddressIndex = addr;
}
}
// proxy is stopped
LOGGER.trace("Stopping Reverse Proxy");
Expand Down Expand Up @@ -170,8 +189,8 @@ private void handlerServerPackets() throws IOException {
if (sourceAddress == null) {
return;
}
if (!sourceAddress.equals(serverAddress)) {
logAndRaiseException(String.format("We should only receive data from server %s", serverAddress));
if (!possibleServerAddress.contains(sourceAddress)) {
logAndRaiseException(String.format("We should only receive data from server %s", possibleServerAddress));
}
if (clientAddress == null) {
logAndRaiseException("Client should send data first before sever send data");
Expand Down Expand Up @@ -236,6 +255,27 @@ public InetSocketAddress getServerAddress() {
return serverAddress;
}

/**
* @return address of server to proxified
*/
public void useNextServerAddress() {
if (possibleServerAddress.size() == 1) {
throw new IllegalStateException("there is only 1 possible server address");
}

askedServerAddressIndex = (askedServerAddressIndex + 1) % possibleServerAddress.size();

selector.wakeup();
// Wait address effectively changed (we wait 10x100 ms max)
for (int i = 0; i < 10 && askedServerAddressIndex != currentServerAddressIndex; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
LOGGER.debug("Change Server Side Proxy Address was interrupted", e);
}
}
}

/**
* @return will assign a new server side proxy address which can be used to simulate client address change.
*/
Expand Down

0 comments on commit 7db3aec

Please sign in to comment.