More bug fixes

This commit is contained in:
2024-07-10 00:50:49 +02:00
parent 6cc8e23e07
commit c3d034448c
3 changed files with 112 additions and 43 deletions

View File

@@ -13,7 +13,10 @@ import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class EventManager extends DefaultMethodsOverrider { public class EventManager extends DefaultMethodsOverrider {
@@ -45,29 +48,48 @@ public class EventManager extends DefaultMethodsOverrider {
eventListeners.put(listenerClass, clazz); eventListeners.put(listenerClass, clazz);
} }
public final void unregisterListener(Class<? extends EventListener> listenerClass) { public synchronized final void unregisterListener(Class<? extends EventListener> listenerClass) {
if (!isListenerRegistered(listenerClass)) return; if (!isListenerRegistered(listenerClass)) return;
Object clazz = eventListeners.get(listenerClass); Object clazz = eventListeners.get(listenerClass);
synchronized (registeredListener) { synchronized (registeredListener) {
for (Class<? extends Event> eventClass : registeredListener.keySet()) { List<Class<? extends Event>> eventsToRemove = new ArrayList<>();
HashMap<EventPriority, HashMap<Object, Method>> priorityMap = registeredListener.get(eventClass);
for (Map.Entry<Class<? extends Event>, HashMap<EventPriority, HashMap<Object, Method>>> entry : registeredListener.entrySet()) {
Class<? extends Event> eventClass = entry.getKey();
HashMap<EventPriority, HashMap<Object, Method>> priorityMap = entry.getValue();
if (priorityMap != null) { if (priorityMap != null) {
synchronized (priorityMap) { synchronized (priorityMap) {
for (EventPriority priority : priorityMap.keySet()) { List<EventPriority> prioritiesToRemove = new ArrayList<>();
HashMap<Object, Method> listeners = priorityMap.get(priority);
for (Map.Entry<EventPriority, HashMap<Object, Method>> priorityEntry : priorityMap.entrySet()) {
EventPriority priority = priorityEntry.getKey();
HashMap<Object, Method> listeners = priorityEntry.getValue();
if (listeners != null) { if (listeners != null) {
listeners.remove(clazz); listeners.remove(clazz);
if (listeners.isEmpty()) priorityMap.remove(priority); if (listeners.isEmpty()) {
prioritiesToRemove.add(priority);
}
} }
} }
if (priorityMap.isEmpty()) registeredListener.remove(eventClass); for (EventPriority priority : prioritiesToRemove) {
priorityMap.remove(priority);
}
if (priorityMap.isEmpty()) {
eventsToRemove.add(eventClass);
}
} }
} }
} }
for (Class<? extends Event> eventClass : eventsToRemove) {
registeredListener.remove(eventClass);
}
} }
eventListeners.remove(listenerClass); eventListeners.remove(listenerClass);

View File

@@ -111,10 +111,10 @@ public class NetworkClient extends DefaultMethodsOverrider {
} }
public synchronized final void connect() throws IOException, InterruptedException { public synchronized final void connect() throws IOException, InterruptedException {
if (isConnected()) return;
if (debugLog) System.out.println("Connecting to server...");
try { try {
if (isConnected()) return;
if (debugLog) System.out.println("Connecting to server...");
socket = new Socket(host, port); socket = new Socket(host, port);
socket.setTcpNoDelay(false); socket.setTcpNoDelay(false);
@@ -132,7 +132,7 @@ public class NetworkClient extends DefaultMethodsOverrider {
if (debugLog) System.out.println("Connected to Server. Attempts: " + attempt); if (debugLog) System.out.println("Connected to Server. Attempts: " + attempt);
} catch (SocketException exception) { } catch (SocketException exception) {
if (isAutoReconnectEnabled()) reconnect(); if (isAutoReconnectEnabled()) reconnect();
else throw exception; else if (!receiveThread.isInterrupted()) throw exception;
} }
} }
@@ -189,9 +189,9 @@ public class NetworkClient extends DefaultMethodsOverrider {
} }
private final void receive() { private final void receive() {
if (!isConnected()) return;
try { try {
if (!isConnected()) return;
String command = ""; String command = "";
while (isConnected()) { while (isConnected()) {
@@ -232,20 +232,27 @@ public class NetworkClient extends DefaultMethodsOverrider {
} }
} catch (EOFException exception) { } catch (EOFException exception) {
attempt = 1; attempt = 1;
if (isAutoReconnectEnabled()) { if (isAutoReconnectEnabled()) reconnect();
if (maxAttempts == -1) reconnect(); else if (!receiveThread.isInterrupted()) {
else if (attempt <= maxAttempts) reconnect(); try {
else { stop();
eventManager.executeEvent(new C_StoppedEvent(this)); } catch (IOException exception1) {
exception.printStackTrace(); exception.printStackTrace();
exception1.printStackTrace();
} }
} else {
eventManager.executeEvent(new C_StoppedEvent(this));
exception.printStackTrace();
} }
} catch (IOException | ClassNotFoundException exception) { } catch (ClassNotFoundException exception) {
eventManager.executeEvent(new C_StoppedEvent(this));
exception.printStackTrace(); exception.printStackTrace();
} catch (IOException exception) {
eventManager.executeEvent(new C_StoppedEvent(this));
if (!receiveThread.isInterrupted()) {
try {
stop();
} catch (IOException exception1) {
exception.printStackTrace();
exception1.printStackTrace();
}
}
} }
} }
@@ -256,7 +263,8 @@ public class NetworkClient extends DefaultMethodsOverrider {
disconnect(); disconnect();
} catch (IOException exception) { } catch (IOException exception) {
if (maxAttempts > 0 && attempt > maxAttempts) { if (maxAttempts > 0 && attempt > maxAttempts) {
exception.printStackTrace(); eventManager.executeEvent(new C_StoppedEvent(this));
if (!receiveThread.isInterrupted()) exception.printStackTrace();
return; return;
} }
} }
@@ -270,14 +278,17 @@ public class NetworkClient extends DefaultMethodsOverrider {
} catch (InterruptedException | IOException exception) { } catch (InterruptedException | IOException exception) {
if (maxAttempts == -1) reconnect(); if (maxAttempts == -1) reconnect();
else if (attempt <= maxAttempts) reconnect(); else if (attempt <= maxAttempts) reconnect();
else exception.printStackTrace(); else {
eventManager.executeEvent(new C_StoppedEvent(this));
if (!receiveThread.isInterrupted()) exception.printStackTrace();
}
} }
} else { } else {
try { try {
stop(); stop();
} catch (IOException exception) { } catch (IOException exception) {
eventManager.executeEvent(new C_StoppedEvent(this)); eventManager.executeEvent(new C_StoppedEvent(this));
exception.printStackTrace(); if (!receiveThread.isInterrupted()) exception.printStackTrace();
} }
} }
} }

View File

@@ -15,9 +15,7 @@ import me.finn.unlegitlibrary.network.system.server.events.server.S_StartedEvent
import me.finn.unlegitlibrary.network.system.server.events.server.S_StoppedEvent; import me.finn.unlegitlibrary.network.system.server.events.server.S_StoppedEvent;
import java.io.IOException; import java.io.IOException;
import java.net.BindException; import java.net.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -57,8 +55,8 @@ public class NetworkServer {
return packetHandler; return packetHandler;
} }
public final boolean isAutoRestart() { public final boolean isAutoRestartEnabled() {
return maxAttempts != 0; return maxAttempts != 0 && !incomingConnectionThread.isInterrupted();
} }
public final boolean isDebugLogEnabled() { public final boolean isDebugLogEnabled() {
@@ -83,10 +81,10 @@ public class NetworkServer {
} }
public synchronized final void start() throws IOException, InterruptedException { public synchronized final void start() throws IOException, InterruptedException {
if (isRunning()) return;
if (debugLog) System.out.println("Starting server...");
try { try {
if (isRunning()) return;
if (debugLog) System.out.println("Starting server...");
clientHandlers.clear(); clientHandlers.clear();
serverSocket = new ServerSocket(port); serverSocket = new ServerSocket(port);
@@ -97,13 +95,8 @@ public class NetworkServer {
if (debugLog) System.out.println("Server started on port " + port + ". Attempts: " + attempt); if (debugLog) System.out.println("Server started on port " + port + ". Attempts: " + attempt);
} catch (BindException exception) { } catch (BindException exception) {
if (isAutoRestart()) { if (isAutoRestartEnabled()) restart();
if (attempt > maxAttempts && maxAttempts != -1) throw exception; else if (!incomingConnectionThread.isInterrupted()) throw exception;
if (debugLog) System.out.println("Failed to start! Retrying... (Attempt: " + attempt++ + ")");
Thread.sleep(attemptDelayInSec * 1000L);
start();
} else throw exception;
} }
} }
@@ -111,7 +104,9 @@ public class NetworkServer {
if (!isRunning()) return; if (!isRunning()) return;
if (debugLog) System.out.println("Stopping server..."); if (debugLog) System.out.println("Stopping server...");
clientHandlers.forEach(clientHandler -> { List<ClientHandler> handlersToDisconnect = new ArrayList<>(clientHandlers);
handlersToDisconnect.forEach(clientHandler -> {
try { try {
clientHandler.disconnect(); clientHandler.disconnect();
} catch (IOException exception) { } catch (IOException exception) {
@@ -119,6 +114,7 @@ public class NetworkServer {
} }
}); });
handlersToDisconnect.clear();
clientHandlers.clear(); clientHandlers.clear();
serverSocket.close(); serverSocket.close();
@@ -135,9 +131,9 @@ public class NetworkServer {
} }
private final void incomingConnection() { private final void incomingConnection() {
if (!isRunning()) return;
try { try {
if (!isRunning()) return;
while (isRunning()) { while (isRunning()) {
Socket socket = serverSocket.accept(); Socket socket = serverSocket.accept();
if (socket == null) continue; if (socket == null) continue;
@@ -146,11 +142,51 @@ public class NetworkServer {
if (debugLog) System.out.println("New incoming connection..."); if (debugLog) System.out.println("New incoming connection...");
clientHandlers.add(new ClientHandler(this, socket, clientHandlers.size() + 1)); clientHandlers.add(new ClientHandler(this, socket, clientHandlers.size() + 1));
} }
} catch (SocketException exception) {
if (isAutoRestartEnabled()) restart();
else if (!incomingConnectionThread.isInterrupted()) exception.printStackTrace();
} catch (IOException exception) { } catch (IOException exception) {
exception.printStackTrace(); exception.printStackTrace();
} }
} }
private final void restart() {
if (isAutoRestartEnabled()) {
if (isRunning()) {
try {
stop();
} catch (IOException exception) {
if (maxAttempts > 0 && attempt > maxAttempts) {
eventManager.executeEvent(new S_StoppedEvent(this));
exception.printStackTrace();
return;
}
}
}
if (debugLog) System.out.println("Trying to restart... (Attempt: " + attempt++ + ")");
try {
Thread.sleep(attemptDelayInSec * 1000L);
start();
} catch (InterruptedException | IOException exception) {
if (maxAttempts == -1) restart();
else if (attempt <= maxAttempts) restart();
else {
eventManager.executeEvent(new S_StoppedEvent(this));
exception.printStackTrace();
}
}
} else {
try {
stop();
} catch (IOException exception) {
eventManager.executeEvent(new S_StoppedEvent(this));
exception.printStackTrace();
}
}
}
public final ClientHandler getClientHandlerByID(int id) { public final ClientHandler getClientHandlerByID(int id) {
for (ClientHandler clientHandler : clientHandlers) if (clientHandler.getClientID() == id) return clientHandler; for (ClientHandler clientHandler : clientHandlers) if (clientHandler.getClientID() == id) return clientHandler;
return null; return null;