Skip to content

Commit

Permalink
Fix preferred coturns
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikah45 committed Oct 31, 2023
1 parent e97dd6d commit 7d38c58
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 33 deletions.
8 changes: 6 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ dependencies {
implementation("com.neovisionaries:nv-i18n:1.29")
implementation("com.nativelibs4java:bridj:0.7.0")
implementation("org.luaj:luaj-jse:3.0.1")
implementation("commons-validator:commons-validator:1.7")
implementation("commons-validator:commons-validator:1.7") {
exclude module: 'commons-logging'
}
implementation("com.github.micheljung:JJsonRpc:01a7fba5f4")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
Expand All @@ -337,7 +339,9 @@ dependencies {
implementation("com.github.1-alex98:discord-rpc:1.6.2-jna")
implementation("org.controlsfx:controlsfx:11.1.2")
implementation("org.fxmisc.flowless:flowless:0.7.1")
implementation("de.codecentric.centerdevice:javafxsvg:1.3.0")
implementation("de.codecentric.centerdevice:javafxsvg:1.3.0") {
exclude module: 'commons-logging'
}

implementation("org.javassist:javassist:3.29.2-GA")

Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/faforever/client/api/FafApiAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -153,19 +154,18 @@ public int getMaxPageSize() {
}

public Mono<MeResult> getMe() {
return retrieveMonoWithErrorHandling(MeResult.class, apiWebClient.get().uri("/me"))
.doOnNext(object -> log.trace("Retrieved {} from /me with type MeResult", object));
return retrieveMonoWithErrorHandling(MeResult.class, apiWebClient.get()
.uri("/me")).doOnNext(object -> log.trace("Retrieved {} from /me with type MeResult", object));
}

public Flux<IceServer> getIceServers() {
return retrieveMonoWithErrorHandling(IceServerResponse.class, apiWebClient.get().uri("/ice/server"))
.flatMapIterable(IceServerResponse::servers)
.doOnNext(object -> log.trace("Retrieved {} from /ice/server with type IceServer", object));
public <T> Mono<T> getApiObject(String path, Class<T> clazz) {
RequestHeadersSpec<?> requestSpec = apiWebClient.get().uri(path);
return retrieveMonoWithErrorHandling(clazz, requestSpec).doOnNext(object -> log.trace("Retrieved {} from {} with type {}", object, path, clazz));
}

public Mono<IceSession> getIceSession(int gameId) {
return retrieveMonoWithErrorHandling(IceSession.class, apiWebClient.get().uri("/ice/session/game/" + gameId))
.doOnNext(object -> log.trace("Retrieved {} from /ice/session/game/{} with type IceSession", object, gameId));
public <T> Flux<T> getApiObjects(String path, Class<T> clazz) {
RequestHeadersSpec<?> requestSpec = apiWebClient.get().uri(path);
return retrieveFluxWithErrorHandling(clazz, requestSpec).doOnNext(object -> log.trace("Retrieved {} from {} with type {}", object, path, clazz));
}

public Mono<Void> uploadFile(String endpoint, Path file, ByteCountListener listener,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/faforever/client/api/IceServer.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.faforever.client.api;

public record IceServer(String name, String region) {
public record IceServer(String id, String region) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.faforever.client.api.FafApiAccessor;
import com.faforever.client.api.IceServer;
import com.faforever.client.api.IceServerResponse;
import com.faforever.client.api.IceSession;
import com.faforever.client.mapstruct.IceServerMapper;
import com.faforever.client.preferences.ForgedAlliancePrefs;
import com.faforever.commons.api.dto.CoturnServer;
import lombok.RequiredArgsConstructor;
Expand All @@ -21,18 +21,18 @@
public class CoturnService {

private final FafApiAccessor fafApiAccessor;
private final IceServerMapper iceServerMapper;
private final ForgedAlliancePrefs forgedAlliancePrefs;

public CompletableFuture<List<IceServer>> getActiveCoturns() {
return fafApiAccessor.getIceServers()
return fafApiAccessor.getApiObject("/ice/server", IceServerResponse.class)
.flatMapIterable(IceServerResponse::servers)
.switchIfEmpty(Flux.error(new IllegalStateException("No Coturn Servers Available")))
.collectList()
.toFuture();
}

public CompletableFuture<List<CoturnServer>> getSelectedCoturns(int gameId) {
Flux<CoturnServer> coturnServerFlux = fafApiAccessor.getIceSession(gameId)
Flux<CoturnServer> coturnServerFlux = fafApiAccessor.getApiObject("/ice/session/game/" + gameId, IceSession.class)
.flatMapIterable(IceSession::servers);

Set<String> preferredCoturnIds = forgedAlliancePrefs.getPreferredCoturnIds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ public class SettingsController implements Controller<Node> {

private final SimpleChangeListener<Theme> selectedThemeChangeListener = this::onThemeChanged;
private final SimpleChangeListener<Theme> currentThemeChangeListener = newValue -> themeComboBox.getSelectionModel().select(newValue);;
private SimpleInvalidationListener availableLanguagesListener = this::setAvailableLanguages;;
private final SimpleInvalidationListener availableLanguagesListener = this::setAvailableLanguages;
;

public void initialize() {
JavaFxUtil.bindManagedToVisible(vaultLocationWarningLabel);
Expand Down Expand Up @@ -344,18 +345,18 @@ private void initPreferredCoturnListView() {
.getForgedAlliance()
.getPreferredCoturnIds();
Map<String, IceServer> hostPortCoturnServerMap = coturnServers.stream()
.collect(Collectors.toMap(IceServer::name, Function.identity()));
.collect(Collectors.toMap(IceServer::id, Function.identity()));

preferredCoturnServers.stream()
.filter(hostPortCoturnServerMap::containsKey)
.map(hostPortCoturnServerMap::get)
.forEach(coturnServer -> preferredCoturnListView.getSelectionModel().select(coturnServer));

JavaFxUtil.addAndTriggerListener(preferredCoturnListView.getSelectionModel()
.getSelectedItems(), (InvalidationListener) observable -> {
.getSelectedItems(), observable -> {
List<IceServer> selectedCoturns = preferredCoturnListView.getSelectionModel().getSelectedItems();
preferredCoturnServers.clear();
selectedCoturns.stream().map(IceServer::name).forEach(preferredCoturnServers::add);
selectedCoturns.stream().map(IceServer::id).forEach(preferredCoturnServers::add);
});
}, fxApplicationThreadExecutor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ public void testCheckChatUserGameListener() {
when(uiService.getThemeImage(UiService.CHAT_LIST_STATUS_HOSTING)).thenReturn(new Image(InputStream.nullInputStream()));
when(mapService.loadPreview(game.getMapFolderName(), PreviewSize.SMALL)).thenReturn(new Image(InputStream.nullInputStream()));
when(mapService.getMapLocallyFromName(mapFolderName)).thenReturn(Optional.of(mapVersion));
when(mapService.convertMapFolderNameToHumanNameIfPossible(mapFolderName)).thenReturn("map name");
when(mapService.convertMapFolderNameToHumanNameIfPossible(mapFolderName)).thenReturn("map id");
when(i18n.get(eq("game.onMapFormat"), anyString())).thenReturn(mapVersion.getMap()
.getDisplayName(), "map name", "Neroxis Generated Map");
.getDisplayName(), "map id", "Neroxis Generated Map");

runOnFxThreadAndWait(() -> instance.setChatUser(defaultUser));
assertNotNull(instance.gameStatusImageView.getImage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.faforever.client.mapstruct.IceServerMapper;
import com.faforever.client.mapstruct.MapperSetup;
import com.faforever.client.preferences.ForgedAlliancePrefs;
import com.faforever.client.test.ElideMatchers;
import com.faforever.client.test.ServiceTest;
import com.faforever.commons.api.dto.CoturnServer;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -21,8 +20,8 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.endsWith;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -45,9 +44,9 @@ public void setUp() throws Exception {

@Test
public void testGetActiveCoturns() {
when(fafApiAccessor.getMany(any())).thenReturn(Flux.empty());
when(fafApiAccessor.getApiObjects("/ice/server", IceServer.class)).thenReturn(Flux.empty());
instance.getActiveCoturns();
verify(fafApiAccessor).getMany(argThat(ElideMatchers.hasDtoClass(CoturnServer.class)));
verify(fafApiAccessor).getApiObjects("/ice/server", IceServer.class);
}

@Test
Expand All @@ -56,26 +55,26 @@ public void TestGetSelectedCoturnsNoActiveSelected() {

CoturnServer otherServer = new CoturnServer();
otherServer.setId("0");
when(fafApiAccessor.getIceSession(anyInt())).thenReturn(Mono.just(new IceSession("someSessionId", List.of(otherServer))));
when(fafApiAccessor.getApiObject(any(), any())).thenReturn(Mono.just(new IceSession("someSessionId", List.of(otherServer))));

List<CoturnServer> servers = instance.getSelectedCoturns(123).join();

assertEquals(1, servers.size());
assertEquals("0", servers.get(0).getId());
verify(fafApiAccessor).getIceSession(123);
verify(fafApiAccessor).getApiObject(endsWith("123"), eq(IceSession.class));
}

@Test
public void testGetSelectedCoturnsNoneSelected() {
CoturnServer otherServer = new CoturnServer();
otherServer.setId("0");
when(fafApiAccessor.getIceSession(anyInt())).thenReturn(Mono.just(new IceSession("someSessionId", List.of(otherServer))));
when(fafApiAccessor.getApiObject(any(), any())).thenReturn(Mono.just(new IceSession("someSessionId", List.of(otherServer))));

List<CoturnServer> servers = instance.getSelectedCoturns(123).join();

assertEquals(1, servers.size());
assertEquals("0", servers.get(0).getId());
verify(fafApiAccessor).getIceSession(123);
verify(fafApiAccessor).getApiObject(endsWith("123"), eq(IceSession.class));
}

@Test
Expand All @@ -86,12 +85,12 @@ public void testGetSelectedCoturnsActiveSelected() {
otherServer.setId("0");
CoturnServer selectedServer = new CoturnServer();
otherServer.setId("1");
when(fafApiAccessor.getIceSession(anyInt())).thenReturn(Mono.just(new IceSession("someSessionId", List.of(otherServer, selectedServer))));
when(fafApiAccessor.getApiObject(any(), any())).thenReturn(Mono.just(new IceSession("someSessionId", List.of(otherServer, selectedServer))));

List<CoturnServer> servers = instance.getSelectedCoturns(123).join();

assertEquals(1, servers.size());
assertEquals("1", servers.get(0).getId());
verify(fafApiAccessor).getIceSession(123);
verify(fafApiAccessor).getApiObject(endsWith("123"), eq(IceSession.class));
}
}

0 comments on commit 7d38c58

Please sign in to comment.