Remove logger and change initialization
This commit is contained in:
parent
5d6b95a161
commit
3500a2a2fe
2
pom.xml
2
pom.xml
@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>fi.flexplex</groupId>
|
||||
<artifactId>lib</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
<name>FlexLib</name>
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
|
@ -7,18 +7,16 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.neovisionaries.ws.client.WebSocketException;
|
||||
|
||||
public final class FlexLib {
|
||||
|
||||
private final FlexLib instance;
|
||||
private final String token;
|
||||
private final Logger logger;
|
||||
private String token = "";
|
||||
private final Timer timer;
|
||||
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 WebSocketClient webSocketClient;
|
||||
private final HashMap<String, PlayerFriends> playerFriends = new HashMap<>();
|
||||
@ -36,17 +34,9 @@ public final class FlexLib {
|
||||
|
||||
/**
|
||||
* Instantiates FlexLib API
|
||||
* @param token token for FlexPlex API
|
||||
*/
|
||||
public FlexLib(final String token, final Logger logger) {
|
||||
logger.info("Initializing FlexLib");
|
||||
public FlexLib() {
|
||||
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.timerTask = new TimerTask() {
|
||||
@Override
|
||||
@ -61,15 +51,16 @@ public final class FlexLib {
|
||||
}
|
||||
if (webSocketClient == null) {
|
||||
try {
|
||||
webSocketClient = new WebSocketClient(instance, "wss://api.flexplex.fi/flexlib", token);
|
||||
webSocketClient = new WebSocketClient(instance, "wss://api.flexplex.fi/flexlib");
|
||||
webSocketClient.connect();
|
||||
} 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get logger used by FlexLib
|
||||
* @return FlexLib logger
|
||||
*/
|
||||
public Logger getLogger() {
|
||||
return this.logger;
|
||||
public FlexLib setToken(final String token) {
|
||||
this.token = token;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new event listener
|
||||
* @param listener
|
||||
* @return FlexLib instance
|
||||
*/
|
||||
public void addListener(final FlexLibAdapter listener) {
|
||||
public FlexLib addListener(final FlexLibAdapter listener) {
|
||||
this.eventListeners.add(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start FlexLib
|
||||
*/
|
||||
public void start() {
|
||||
this.timer.schedule(this.timerTask, 0, 60_000);
|
||||
new FlexLibAdapterImpl(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,4 +51,10 @@ public interface FlexLibAdapter {
|
||||
*/
|
||||
public default void onFriendDelete(final String player, final String targetPlayer) {}
|
||||
|
||||
/**
|
||||
* Called when error happened
|
||||
* @param message
|
||||
*/
|
||||
public default void onError(final String message) {}
|
||||
|
||||
}
|
||||
|
@ -14,14 +14,4 @@ public final class FlexLibAdapterImpl implements FlexLibAdapter {
|
||||
this.flexLib.updatePlayerFriends(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWebSocketConnect() {
|
||||
this.flexLib.getLogger().info("WebSocket connected");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWebSocketDisconnect() {
|
||||
this.flexLib.getLogger().info("WebSocket disconnected");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -35,7 +35,9 @@ public final class GraphQLApi {
|
||||
try {
|
||||
response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
} 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();
|
||||
} catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
@ -46,7 +48,9 @@ public final class GraphQLApi {
|
||||
try {
|
||||
json = JsonParser.parseString(response.body()).getAsJsonObject();
|
||||
} 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();
|
||||
}
|
||||
|
||||
|
@ -13,14 +13,16 @@ public final class WebSocketClient {
|
||||
private final FlexLib flexLib;
|
||||
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.webSocket = this.createWebSocket(url, token);
|
||||
this.webSocket = this.createWebSocket(url);
|
||||
try {
|
||||
this.webSocket.getConnectedSocket().setSoTimeout(0);
|
||||
this.webSocket.getConnectedSocket().setKeepAlive(true);
|
||||
} 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()
|
||||
.createSocket(url)
|
||||
.addHeader("Token", token)
|
||||
.addHeader("Token", this.flexLib.getToken())
|
||||
.addListener(new WebSocketAdapter() {
|
||||
@Override
|
||||
public void onTextMessage(final WebSocket ws, final String message) {
|
||||
@ -98,7 +100,9 @@ public final class WebSocketClient {
|
||||
}
|
||||
break;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user