Skip to content

Commit

Permalink
increase log verbosity, wrap tray notification in event dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Geosearchef committed Jul 16, 2021
1 parent 868547f commit a3c3ec7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.ice4j.Transport;
import org.ice4j.TransportAddress;
import org.ice4j.ice.*;
import org.ice4j.ice.harvest.StunCandidateHarvester;
import org.ice4j.ice.harvest.TurnCandidateHarvester;
import org.ice4j.security.LongTermCredential;

import javax.swing.text.html.Option;
import java.io.IOException;
import java.net.DatagramPacket;
import java.util.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand Down Expand Up @@ -136,11 +139,10 @@ private void gatherCandidates() {

int previousConnectivityAttempts = getConnectivityAttempsInThePast(FORCE_SRFLX_RELAY_INTERVAL);
CandidatesMessage localCandidatesMessage = CandidateUtil.packCandidates(IceAdapter.id, peer.getRemoteId(), agent, component, previousConnectivityAttempts < FORCE_SRFLX_COUNT && IceAdapter.ALLOW_HOST, previousConnectivityAttempts < FORCE_RELAY_COUNT && IceAdapter.ALLOW_REFLEXIVE, IceAdapter.ALLOW_RELAY);
log.debug(getLogPrefix() + "Sending own candidates to {}", peer.getRemoteId());
log.debug(getLogPrefix() + "Sending own candidates to {}, offered candidates: {}", peer.getRemoteId(), localCandidatesMessage.getCandidates().stream().map(it -> it.getType().toString() + "(" + it.getProtocol() + ")").collect(Collectors.joining(", ")));
setState(AWAITING_CANDIDATES);
RPCService.onIceMsg(localCandidatesMessage);

//TODO: is this a good fix for awaiting candidates loop????
//Make sure to abort the connection process and reinitiate when we haven't received an answer to our offer in 6 seconds, candidate packet was probably lost
final int currentacei = ++awaitingCandidatesEventId;
Executor.executeDelayed(6000, () -> {
Expand Down Expand Up @@ -172,6 +174,7 @@ private List<IceServer> getViableIceServers() {
.filter(IceServer::hasAcceptableLatency)
.collect(Collectors.toList());
if (!viableIceServers.isEmpty()) {
log.info("Using official ice servers: {}", viableIceServers.stream().map(it -> "[" + it.getTurnAddresses().stream().map(TransportAddress::toString).collect(Collectors.joining(", ")) + "]").collect(Collectors.joining(", ")));
return viableIceServers;
}

Expand All @@ -180,6 +183,7 @@ private List<IceServer> getViableIceServers() {
.filter(IceServer::hasAcceptableLatency)
.collect(Collectors.toList());
if (!viableIceServers.isEmpty()) {
log.info("Using all viable ice servers: {}", viableIceServers.stream().map(it -> "[" + it.getTurnAddresses().stream().map(TransportAddress::toString).collect(Collectors.joining(", ")) + "]").collect(Collectors.joining(", ")));
return viableIceServers;
}

Expand All @@ -188,12 +192,15 @@ private List<IceServer> getViableIceServers() {
.filter(server -> server.getRoundTripTime().join().isPresent())
.min(Comparator.comparing(server -> server.getRoundTripTime().join().getAsDouble()));
if (closestIceServer.isPresent()) {
log.info("Using closest ice server: {}", closestIceServer.get().getTurnAddresses().stream().map(TransportAddress::toString).collect(Collectors.joining(", ")));
viableIceServers.add(closestIceServer.get());
}
if (!viableIceServers.isEmpty()) {
log.info("Using all reachable ice servers: {}", viableIceServers.stream().map(it -> "[" + it.getTurnAddresses().stream().map(TransportAddress::toString).collect(Collectors.joining(", ")) + "]").collect(Collectors.joining(", ")));
return viableIceServers;
}

log.info("Using all ice servers: {}", viableIceServers.stream().map(it -> "[" + it.getTurnAddresses().stream().map(TransportAddress::toString).collect(Collectors.joining(", ")) + "]").collect(Collectors.joining(", ")));
return allIceServers;
}

Expand All @@ -209,7 +216,7 @@ public synchronized void onIceMessageReceived(CandidatesMessage remoteCandidates

//Start ICE async as it's blocking and this is the RPC thread
new Thread(() -> {
log.debug(getLogPrefix() + "Got IceMsg for peer");
log.debug(getLogPrefix() + "Got IceMsg for peer, offered candidates: {}", remoteCandidatesMessage.getCandidates().stream().map(it -> it.getType().toString() + "(" + it.getProtocol() + ")").collect(Collectors.joining(", ")));

if (peer.isLocalOffer()) {
if (iceState != AWAITING_CANDIDATES) {
Expand Down Expand Up @@ -269,7 +276,7 @@ private void startIce() {
}
}

log.debug(getLogPrefix() + "ICE terminated");
log.debug(getLogPrefix() + "ICE terminated, connected, selected candidate pair: " + component.getSelectedPair().getLocalCandidate().getType().toString() + " <-> " + component.getSelectedPair().getRemoteCandidate().getType().toString());

//We are connected
connected = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ public static CompletableFuture<Double> getLatency(String address, Integer count
if(fallbackServers.containsKey(address)) {
String fallback = fallbackServers.get(address);
log.info("Falling back to " + fallback + " for latency estimation");
TrayIcon.showMessage("Failed to estimate latency to " + address + ". Falling back to " + fallback + ".");
return getLatency(fallback, IceAdapter.PING_COUNT).get();
}
TrayIcon.showMessage("Unable to contact relay server!");
throw new RuntimeException("Failed to contact the host");
}
} catch (InterruptedException | IOException | RuntimeException | ExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
Expand All @@ -16,7 +17,7 @@
public class TrayIcon {

public static final String FAF_LOGO_URL = "https://faforever.com/images/faf-logo.png";
private static java.awt.TrayIcon trayIcon;
private static volatile java.awt.TrayIcon trayIcon;


public static void create() {
Expand Down Expand Up @@ -79,9 +80,11 @@ public void mouseExited(MouseEvent mouseEvent) {
}

public static void showMessage(String message) {
if (trayIcon != null) {
trayIcon.displayMessage("FAForever Connection ICE Adapter", message, java.awt.TrayIcon.MessageType.INFO);
}
SwingUtilities.invokeLater(() -> {
if (trayIcon != null) {
trayIcon.displayMessage("FAForever Connection ICE Adapter", message, java.awt.TrayIcon.MessageType.INFO);
}
});
}

public static void close() {
Expand Down

0 comments on commit a3c3ec7

Please sign in to comment.