mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Refactor and fixes
This commit is contained in:
@@ -86,4 +86,17 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>25</source>
|
||||
<target>25</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.toop.framework.eventbus;
|
||||
|
||||
import org.toop.framework.eventbus.events.EventType;
|
||||
import org.toop.framework.eventbus.events.EventWithUuid;
|
||||
import org.toop.framework.eventbus.events.IEvent;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
@@ -32,7 +32,7 @@ public class EventFlow {
|
||||
private String eventId = null;
|
||||
|
||||
/** The event instance created by this publisher. */
|
||||
private IEvent event = null;
|
||||
private EventType event = null;
|
||||
|
||||
/** The listener returned by GlobalEventBus subscription. Used for unsubscription. */
|
||||
private Object listener;
|
||||
@@ -43,13 +43,13 @@ public class EventFlow {
|
||||
/** Holds the results returned from the subscribed event, if any. */
|
||||
private Map<String, Object> result = null;
|
||||
|
||||
/** Empty constructor (event must be added via {@link #addPostEvent}). */
|
||||
/** Empty constructor (event must be added via {@link #addPostEvent(Class, Object...)}). */
|
||||
public EventFlow() {}
|
||||
|
||||
/**
|
||||
* Instantiate an event of the given class and store it in this publisher.
|
||||
*/
|
||||
public <T extends IEvent> EventFlow addPostEvent(Class<T> eventClass, Object... args) {
|
||||
public <T extends EventType> EventFlow addPostEvent(Class<T> eventClass, Object... args) {
|
||||
try {
|
||||
boolean isUuidEvent = EventWithUuid.class.isAssignableFrom(eventClass);
|
||||
|
||||
@@ -78,7 +78,7 @@ public class EventFlow {
|
||||
finalArgs = args;
|
||||
}
|
||||
|
||||
this.event = (IEvent) ctorHandle.invokeWithArguments(finalArgs);
|
||||
this.event = (EventType) ctorHandle.invokeWithArguments(finalArgs);
|
||||
return this;
|
||||
|
||||
} catch (Throwable e) {
|
||||
@@ -89,11 +89,11 @@ public class EventFlow {
|
||||
/**
|
||||
* Start listening for a response event type, chainable with perform().
|
||||
*/
|
||||
public <TT extends IEvent> ResponseBuilder<TT> onResponse(Class<TT> eventClass) {
|
||||
public <TT extends EventType> ResponseBuilder<TT> onResponse(Class<TT> eventClass) {
|
||||
return new ResponseBuilder<>(this, eventClass);
|
||||
}
|
||||
|
||||
public static class ResponseBuilder<R extends IEvent> {
|
||||
public static class ResponseBuilder<R extends EventType> {
|
||||
private final EventFlow parent;
|
||||
private final Class<R> responseClass;
|
||||
|
||||
@@ -153,17 +153,17 @@ public class EventFlow {
|
||||
}
|
||||
|
||||
// choose event type
|
||||
public <TT extends IEvent> EventSubscriberBuilder<TT> onEvent(Class<TT> eventClass) {
|
||||
public <TT extends EventType> EventSubscriberBuilder<TT> onEvent(Class<TT> eventClass) {
|
||||
return new EventSubscriberBuilder<>(this, eventClass);
|
||||
}
|
||||
|
||||
// One-liner shorthand
|
||||
public <TT extends IEvent> EventFlow listen(Class<TT> eventClass, Consumer<TT> action) {
|
||||
public <TT extends EventType> EventFlow listen(Class<TT> eventClass, Consumer<TT> action) {
|
||||
return this.onEvent(eventClass).perform(action);
|
||||
}
|
||||
|
||||
// Builder for chaining .onEvent(...).perform(...)
|
||||
public static class EventSubscriberBuilder<TT extends IEvent> {
|
||||
public static class EventSubscriberBuilder<TT extends EventType> {
|
||||
private final EventFlow publisher;
|
||||
private final Class<TT> eventClass;
|
||||
|
||||
@@ -211,7 +211,7 @@ public class EventFlow {
|
||||
return this.result;
|
||||
}
|
||||
|
||||
public IEvent getEvent() {
|
||||
public EventType getEvent() {
|
||||
return event;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.toop.framework.eventbus;
|
||||
|
||||
import org.toop.framework.eventbus.events.EventWithUuid;
|
||||
import org.toop.framework.eventbus.events.IEvent;
|
||||
import org.toop.framework.eventbus.events.EventType;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
@@ -20,7 +20,7 @@ import java.util.function.Consumer;
|
||||
*
|
||||
* <p><b>Performance note:</b> Directly using {@link GlobalEventBus} is possible,
|
||||
* but for safer type handling, automatic UUID management, and easier unsubscription,
|
||||
* it is recommended to use {@link EventPublisher} whenever possible.</p>
|
||||
* it is recommended to use {@link EventFlow} whenever possible.</p>
|
||||
*
|
||||
* <p>The bus maintains a fixed pool of worker threads that continuously process queued events.</p>
|
||||
*/
|
||||
@@ -30,10 +30,10 @@ public final class GlobalEventBus {
|
||||
private static final int WORKERS = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
/** Queue for asynchronous event processing. */
|
||||
private static final BlockingQueue<IEvent> EVENT_QUEUE = new LinkedBlockingQueue<>(WORKERS * 1024);
|
||||
private static final BlockingQueue<EventType> EVENT_QUEUE = new LinkedBlockingQueue<>(WORKERS * 1024);
|
||||
|
||||
/** Map of event class to type-specific listeners. */
|
||||
private static final Map<Class<?>, CopyOnWriteArrayList<Consumer<? super IEvent>>> LISTENERS = new ConcurrentHashMap<>();
|
||||
private static final Map<Class<?>, CopyOnWriteArrayList<Consumer<? super EventType>>> LISTENERS = new ConcurrentHashMap<>();
|
||||
|
||||
/** Map of event class to UUID-specific listeners. */
|
||||
private static final Map<Class<?>, ConcurrentHashMap<String, Consumer<? extends EventWithUuid>>> UUID_LISTENERS = new ConcurrentHashMap<>();
|
||||
@@ -59,7 +59,7 @@ public final class GlobalEventBus {
|
||||
private static void workerLoop() {
|
||||
try {
|
||||
while (true) {
|
||||
IEvent event = EVENT_QUEUE.take();
|
||||
EventType event = EVENT_QUEUE.take();
|
||||
dispatchEvent(event);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
@@ -75,8 +75,8 @@ public final class GlobalEventBus {
|
||||
* @param <T> the event type
|
||||
* @return the provided listener for possible unsubscription
|
||||
*/
|
||||
public static <T extends IEvent> Consumer<T> subscribe(Class<T> eventClass, Consumer<T> listener) {
|
||||
CopyOnWriteArrayList<Consumer<? super IEvent>> list =
|
||||
public static <T extends EventType> Consumer<T> subscribe(Class<T> eventClass, Consumer<T> listener) {
|
||||
CopyOnWriteArrayList<Consumer<? super EventType>> list =
|
||||
LISTENERS.computeIfAbsent(eventClass, k -> new CopyOnWriteArrayList<>());
|
||||
list.add(event -> listener.accept(eventClass.cast(event)));
|
||||
return listener;
|
||||
@@ -135,7 +135,7 @@ public final class GlobalEventBus {
|
||||
* @param event the event instance to post
|
||||
* @param <T> the event type
|
||||
*/
|
||||
public static <T extends IEvent> void post(T event) {
|
||||
public static <T extends EventType> void post(T event) {
|
||||
dispatchEvent(event);
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ public final class GlobalEventBus {
|
||||
* @param event the event instance to post
|
||||
* @param <T> the event type
|
||||
*/
|
||||
public static <T extends IEvent> void postAsync(T event) {
|
||||
public static <T extends EventType> void postAsync(T event) {
|
||||
if (!EVENT_QUEUE.offer(event)) {
|
||||
dispatchEvent(event);
|
||||
}
|
||||
@@ -154,19 +154,19 @@ public final class GlobalEventBus {
|
||||
|
||||
/** Dispatches an event to all type-specific, generic, and UUID-specific listeners. */
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void dispatchEvent(IEvent event) {
|
||||
private static void dispatchEvent(EventType event) {
|
||||
Class<?> clazz = event.getClass();
|
||||
|
||||
CopyOnWriteArrayList<Consumer<? super IEvent>> classListeners = LISTENERS.get(clazz);
|
||||
CopyOnWriteArrayList<Consumer<? super EventType>> classListeners = LISTENERS.get(clazz);
|
||||
if (classListeners != null) {
|
||||
for (Consumer<? super IEvent> listener : classListeners) {
|
||||
for (Consumer<? super EventType> listener : classListeners) {
|
||||
try { listener.accept(event); } catch (Throwable ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
CopyOnWriteArrayList<Consumer<? super IEvent>> genericListeners = LISTENERS.get(Object.class);
|
||||
CopyOnWriteArrayList<Consumer<? super EventType>> genericListeners = LISTENERS.get(Object.class);
|
||||
if (genericListeners != null) {
|
||||
for (Consumer<? super IEvent> listener : genericListeners) {
|
||||
for (Consumer<? super EventType> listener : genericListeners) {
|
||||
try { listener.accept(event); } catch (Throwable ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,12 +67,4 @@ public class Events {
|
||||
// Create a new instance
|
||||
return constructor.newInstance(args);
|
||||
}
|
||||
|
||||
public static class EventBusEvents {}
|
||||
|
||||
|
||||
|
||||
public static class TttEvents {}
|
||||
|
||||
public static class AiTttEvents {}
|
||||
}
|
||||
|
||||
@@ -17,13 +17,18 @@ public class NetworkingClientManager {
|
||||
private final Map<String, NetworkingClient> networkClients = new ConcurrentHashMap<>();
|
||||
|
||||
/** Starts a connection manager, to manage, connections. */
|
||||
public NetworkingClientManager() {
|
||||
new EventFlow().listen(NetworkEvents.StartClientRequest.class, this::handleStartClientRequest);
|
||||
new EventFlow().listen(NetworkEvents.StartClient.class, this::handleStartClient);
|
||||
new EventFlow().listen(NetworkEvents.SendCommand.class, this::handleCommand);
|
||||
new EventFlow().listen(NetworkEvents.CloseClient.class, this::handleCloseClient);
|
||||
new EventFlow().listen(NetworkEvents.RequestsAllClients.class, this::getAllConnections);
|
||||
new EventFlow().listen(NetworkEvents.ForceCloseAllClients.class, this::shutdownAll);
|
||||
public NetworkingClientManager() throws NetworkingInitializationException {
|
||||
try {
|
||||
new EventFlow().listen(NetworkEvents.StartClientRequest.class, this::handleStartClientRequest);
|
||||
new EventFlow().listen(NetworkEvents.StartClient.class, this::handleStartClient);
|
||||
new EventFlow().listen(NetworkEvents.SendCommand.class, this::handleCommand);
|
||||
new EventFlow().listen(NetworkEvents.CloseClient.class, this::handleCloseClient);
|
||||
new EventFlow().listen(NetworkEvents.RequestsAllClients.class, this::getAllConnections);
|
||||
new EventFlow().listen(NetworkEvents.ForceCloseAllClients.class, this::shutdownAll);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to initialize the client manager", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private String startClientRequest(Supplier<? extends NetworkingGameClientHandler> handlerFactory,
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.toop.framework.networking;
|
||||
|
||||
public class NetworkingInitializationException extends RuntimeException {
|
||||
public NetworkingInitializationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user