Remove logger and change initialization

This commit is contained in:
Jonttuuu 2023-07-30 19:34:49 +03:00
parent 5d6b95a161
commit 3500a2a2fe
6 changed files with 44 additions and 42 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>fi.flexplex</groupId> <groupId>fi.flexplex</groupId>
<artifactId>lib</artifactId> <artifactId>lib</artifactId>
<version>1.0.0</version> <version>1.1.0</version>
<name>FlexLib</name> <name>FlexLib</name>
<properties> <properties>
<java.version>17</java.version> <java.version>17</java.version>

View File

@ -7,18 +7,16 @@ import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.logging.Logger;
import com.neovisionaries.ws.client.WebSocketException; import com.neovisionaries.ws.client.WebSocketException;
public final class FlexLib { public final class FlexLib {
private final FlexLib instance; private final FlexLib instance;
private final String token; private String token = "";
private final Logger logger;
private final Timer timer; private final Timer timer;
private final TimerTask timerTask; private final TimerTask timerTask;
private final GraphQLApi graphQLApi; private final GraphQLApi graphQLApi = new GraphQLApi(this, "https://api.flexplex.fi/graphql");
private final Set<FlexLibAdapter> eventListeners = new HashSet<>(); private final Set<FlexLibAdapter> eventListeners = new HashSet<>();
private WebSocketClient webSocketClient; private WebSocketClient webSocketClient;
private final HashMap<String, PlayerFriends> playerFriends = new HashMap<>(); private final HashMap<String, PlayerFriends> playerFriends = new HashMap<>();
@ -36,17 +34,9 @@ public final class FlexLib {
/** /**
* Instantiates FlexLib API * Instantiates FlexLib API
* @param token token for FlexPlex API
*/ */
public FlexLib(final String token, final Logger logger) { public FlexLib() {
logger.info("Initializing FlexLib");
this.instance = this; this.instance = this;
this.token = token;
this.logger = logger;
this.graphQLApi = new GraphQLApi(this, "https://api.flexplex.fi/graphql");
new FlexLibAdapterImpl(this);
this.timer = new Timer(); this.timer = new Timer();
this.timerTask = new TimerTask() { this.timerTask = new TimerTask() {
@Override @Override
@ -61,15 +51,16 @@ public final class FlexLib {
} }
if (webSocketClient == null) { if (webSocketClient == null) {
try { try {
webSocketClient = new WebSocketClient(instance, "wss://api.flexplex.fi/flexlib", token); webSocketClient = new WebSocketClient(instance, "wss://api.flexplex.fi/flexlib");
webSocketClient.connect(); webSocketClient.connect();
} catch (final IOException | WebSocketException e) { } catch (final IOException | WebSocketException e) {
logger.warning("Failed to initialize FlexLib WebSocket client"); for (final FlexLibAdapter listener : eventListeners) {
listener.onError("Failed to initialize FlexLib WebSocket client");
}
} }
} }
} }
}; };
this.timer.schedule(this.timerTask, 0, 60_000);
} }
/** /**
@ -80,20 +71,27 @@ public final class FlexLib {
return this.token; return this.token;
} }
/** public FlexLib setToken(final String token) {
* Get logger used by FlexLib this.token = token;
* @return FlexLib logger return this;
*/
public Logger getLogger() {
return this.logger;
} }
/** /**
* Add new event listener * Add new event listener
* @param listener * @param listener
* @return FlexLib instance
*/ */
public void addListener(final FlexLibAdapter listener) { public FlexLib addListener(final FlexLibAdapter listener) {
this.eventListeners.add(listener); this.eventListeners.add(listener);
return this;
}
/**
* Start FlexLib
*/
public void start() {
this.timer.schedule(this.timerTask, 0, 60_000);
new FlexLibAdapterImpl(this);
} }
/** /**

View File

@ -51,4 +51,10 @@ public interface FlexLibAdapter {
*/ */
public default void onFriendDelete(final String player, final String targetPlayer) {} public default void onFriendDelete(final String player, final String targetPlayer) {}
/**
* Called when error happened
* @param message
*/
public default void onError(final String message) {}
} }

View File

@ -14,14 +14,4 @@ public final class FlexLibAdapterImpl implements FlexLibAdapter {
this.flexLib.updatePlayerFriends(player); this.flexLib.updatePlayerFriends(player);
} }
@Override
public void onWebSocketConnect() {
this.flexLib.getLogger().info("WebSocket connected");
}
@Override
public void onWebSocketDisconnect() {
this.flexLib.getLogger().info("WebSocket disconnected");
}
} }

View File

@ -35,7 +35,9 @@ public final class GraphQLApi {
try { try {
response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
} catch (final IOException e) { } catch (final IOException e) {
this.flexLib.getLogger().warning("Failed to send GraphQL query to FlexPlex server."); for (final FlexLibAdapter listener : this.flexLib.getEventListeners()) {
listener.onError("Failed to send GraphQL query to FlexPlex server.");
}
return Optional.empty(); return Optional.empty();
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
@ -46,7 +48,9 @@ public final class GraphQLApi {
try { try {
json = JsonParser.parseString(response.body()).getAsJsonObject(); json = JsonParser.parseString(response.body()).getAsJsonObject();
} catch (final JsonSyntaxException | IllegalStateException e) { } catch (final JsonSyntaxException | IllegalStateException e) {
this.flexLib.getLogger().warning("Received invalid json from FlexPlex GraphQL API"); for (final FlexLibAdapter listener : this.flexLib.getEventListeners()) {
listener.onError("Received invalid json from FlexPlex GraphQL API");
}
return Optional.empty(); return Optional.empty();
} }

View File

@ -13,14 +13,16 @@ public final class WebSocketClient {
private final FlexLib flexLib; private final FlexLib flexLib;
private final WebSocket webSocket; private final WebSocket webSocket;
protected WebSocketClient(final FlexLib flexLib, final String url, final String token) throws IOException { protected WebSocketClient(final FlexLib flexLib, final String url) throws IOException {
this.flexLib = flexLib; this.flexLib = flexLib;
this.webSocket = this.createWebSocket(url, token); this.webSocket = this.createWebSocket(url);
try { try {
this.webSocket.getConnectedSocket().setSoTimeout(0); this.webSocket.getConnectedSocket().setSoTimeout(0);
this.webSocket.getConnectedSocket().setKeepAlive(true); this.webSocket.getConnectedSocket().setKeepAlive(true);
} catch (final SocketException | WebSocketException e) { } catch (final SocketException | WebSocketException e) {
this.flexLib.getLogger().warning("Failed to set WebSocket settings"); for (final FlexLibAdapter listener : this.flexLib.getEventListeners()) {
listener.onError("Failed to set WebSocket settings");
}
} }
} }
@ -39,10 +41,10 @@ public final class WebSocketClient {
} }
} }
private WebSocket createWebSocket(final String url, final String token) throws IOException { private WebSocket createWebSocket(final String url) throws IOException {
return new WebSocketFactory() return new WebSocketFactory()
.createSocket(url) .createSocket(url)
.addHeader("Token", token) .addHeader("Token", this.flexLib.getToken())
.addListener(new WebSocketAdapter() { .addListener(new WebSocketAdapter() {
@Override @Override
public void onTextMessage(final WebSocket ws, final String message) { public void onTextMessage(final WebSocket ws, final String message) {
@ -98,7 +100,9 @@ public final class WebSocketClient {
} }
break; break;
default: default:
flexLib.getLogger().warning("Received invalid WebSocket message from FlexPlex. Message: " + args[0]); for (final FlexLibAdapter listener : flexLib.getEventListeners()) {
listener.onError("Received invalid WebSocket message from FlexPlex. Message: " + args[0]);
}
break; break;
} }
} }