[FIX] Удалён EventManager

This commit is contained in:
Gravit 2019-07-01 17:41:56 +07:00
parent 25ccda7ae2
commit 955020005a
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
17 changed files with 23 additions and 227 deletions

View file

@ -2,10 +2,8 @@
import java.util.UUID;
import pro.gravit.utils.event.EventInterface;
//Набор стандартных событий
public class ControlEvent implements EventInterface {
public class ControlEvent {
private static final UUID uuid = UUID.fromString("f1051a64-0cd0-4ed8-8430-d856a196e91f");
public enum ControlCommand {
@ -17,9 +15,4 @@ public ControlEvent(ControlCommand signal) {
}
public ControlCommand signal;
@Override
public UUID getUUID() {
return uuid;
}
}

View file

@ -1,17 +1,6 @@
package pro.gravit.launcher.events;
import java.util.UUID;
import pro.gravit.utils.event.EventInterface;
//Пустое событие
//Все обработчики обязаны его игнорировать
public final class PingEvent implements EventInterface {
private static final UUID uuid = UUID.fromString("7c8be7e7-82ce-4c99-84cd-ee8fcce1b509");
@Override
public UUID getUUID() {
return uuid;
}
public final class PingEvent {
}

View file

@ -1,20 +1,10 @@
package pro.gravit.launcher.events;
import java.util.UUID;
import pro.gravit.utils.event.EventInterface;
//Используется, что бы послать короткое сообщение, которое вмещается в int
public class SignalEvent implements EventInterface {
private static final UUID uuid = UUID.fromString("edc3afa1-2726-4da3-95c6-7e6994b981e1");
public class SignalEvent {
public int signal;
public SignalEvent(int signal) {
this.signal = signal;
}
@Override
public UUID getUUID() {
return uuid;
}
}

View file

@ -6,10 +6,8 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.profiles.PlayerProfile;
import pro.gravit.utils.event.EventInterface;
public class AuthRequestEvent extends RequestEvent implements EventInterface {
private static final UUID uuid = UUID.fromString("77e1bfd7-adf9-4f5d-87d6-a7dd068deb74");
public class AuthRequestEvent extends RequestEvent {
public AuthRequestEvent() {
}
@ -46,11 +44,6 @@ public AuthRequestEvent(ClientPermissions permissions, PlayerProfile playerProfi
this.session = session;
}
@Override
public UUID getUUID() {
return uuid;
}
@Override
public String getType() {
return "auth";

View file

@ -5,10 +5,8 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.profiles.PlayerProfile;
import pro.gravit.utils.event.EventInterface;
public class BatchProfileByUsernameRequestEvent extends RequestEvent implements EventInterface {
private static final UUID uuid = UUID.fromString("c1d6729e-be2c-48cc-b5ae-af8c012232c3");
public class BatchProfileByUsernameRequestEvent extends RequestEvent {
@LauncherNetworkAPI
public String error;
@LauncherNetworkAPI
@ -21,11 +19,6 @@ public BatchProfileByUsernameRequestEvent(PlayerProfile[] profiles) {
public BatchProfileByUsernameRequestEvent() {
}
@Override
public UUID getUUID() {
return uuid;
}
@Override
public String getType() {
return "batchProfileByUsername";

View file

@ -5,9 +5,9 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.profiles.PlayerProfile;
import pro.gravit.utils.event.EventInterface;
public class CheckServerRequestEvent extends RequestEvent implements EventInterface {
public class CheckServerRequestEvent extends RequestEvent {
private static final UUID _uuid = UUID.fromString("8801d07c-51ba-4059-b61d-fe1f1510b28a");
@LauncherNetworkAPI
public UUID uuid;
@ -21,11 +21,6 @@ public CheckServerRequestEvent(PlayerProfile playerProfile) {
public CheckServerRequestEvent() {
}
@Override
public UUID getUUID() {
return _uuid;
}
@Override
public String getType() {
return "checkServer";

View file

@ -4,9 +4,9 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.utils.event.EventInterface;
public class ErrorRequestEvent extends RequestEvent implements EventInterface {
public class ErrorRequestEvent extends RequestEvent {
public static UUID uuid = UUID.fromString("0af22bc7-aa01-4881-bdbb-dc62b3cdac96");
public ErrorRequestEvent(String error) {
@ -20,9 +20,4 @@ public ErrorRequestEvent(String error) {
public String getType() {
return "error";
}
@Override
public UUID getUUID() {
return null;
}
}

View file

@ -4,9 +4,9 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.utils.event.EventInterface;
public class JoinServerRequestEvent extends RequestEvent implements EventInterface {
public class JoinServerRequestEvent extends RequestEvent {
private static final UUID uuid = UUID.fromString("2a12e7b5-3f4a-4891-a2f9-ea141c8e1995");
public JoinServerRequestEvent(boolean allow) {
@ -16,11 +16,6 @@ public JoinServerRequestEvent(boolean allow) {
@LauncherNetworkAPI
public boolean allow;
@Override
public UUID getUUID() {
return uuid;
}
@Override
public String getType() {
return "joinServer";

View file

@ -4,9 +4,9 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.utils.event.EventInterface;
public class LauncherRequestEvent extends RequestEvent implements EventInterface {
public class LauncherRequestEvent extends RequestEvent {
private static final UUID uuid = UUID.fromString("d54cc12a-4f59-4f23-9b10-f527fdd2e38f");
@LauncherNetworkAPI
public String url;
@ -33,11 +33,6 @@ public LauncherRequestEvent(byte[] binary, byte[] digest) { //Legacy support con
this.digest = digest;
}
@Override
public UUID getUUID() {
return uuid;
}
@Override
public String getType() {
return "launcher";

View file

@ -5,9 +5,9 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.profiles.PlayerProfile;
import pro.gravit.utils.event.EventInterface;
public class ProfileByUUIDRequestEvent extends RequestEvent implements EventInterface {
public class ProfileByUUIDRequestEvent extends RequestEvent {
private static final UUID uuid = UUID.fromString("b9014cf3-4b95-4d38-8c5f-867f190a18a0");
@LauncherNetworkAPI
public String error;
@ -21,11 +21,6 @@ public ProfileByUUIDRequestEvent(PlayerProfile playerProfile) {
public ProfileByUUIDRequestEvent() {
}
@Override
public UUID getUUID() {
return uuid;
}
@Override
public String getType() {
return "profileByUUID";

View file

@ -5,9 +5,9 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.profiles.PlayerProfile;
import pro.gravit.utils.event.EventInterface;
public class ProfileByUsernameRequestEvent extends RequestEvent implements EventInterface {
public class ProfileByUsernameRequestEvent extends RequestEvent {
private static final UUID uuid = UUID.fromString("06204302-ff6b-4779-b97d-541e3bc39aa1");
@LauncherNetworkAPI
public String error;
@ -18,11 +18,6 @@ public ProfileByUsernameRequestEvent(PlayerProfile playerProfile) {
this.playerProfile = playerProfile;
}
@Override
public UUID getUUID() {
return uuid;
}
@Override
public String getType() {
return "profileByUsername";

View file

@ -6,9 +6,9 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.utils.event.EventInterface;
public class ProfilesRequestEvent extends RequestEvent implements EventInterface {
public class ProfilesRequestEvent extends RequestEvent {
private static final UUID uuid = UUID.fromString("2f26fbdf-598a-46dd-92fc-1699c0e173b1");
@LauncherNetworkAPI
public List<ClientProfile> profiles;
@ -20,11 +20,6 @@ public ProfilesRequestEvent(List<ClientProfile> profiles) {
public ProfilesRequestEvent() {
}
@Override
public UUID getUUID() {
return uuid;
}
@Override
public String getType() {
return "profiles";

View file

@ -5,9 +5,9 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.utils.event.EventInterface;
public class SetProfileRequestEvent extends RequestEvent implements EventInterface {
public class SetProfileRequestEvent extends RequestEvent {
private static final UUID uuid = UUID.fromString("08c0de9e-4364-4152-9066-8354a3a48541");
@LauncherNetworkAPI
public ClientProfile newProfile;
@ -20,9 +20,4 @@ public SetProfileRequestEvent(ClientProfile newProfile) {
public String getType() {
return "setProfile";
}
@Override
public UUID getUUID() {
return uuid;
}
}

View file

@ -5,9 +5,9 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.utils.event.EventInterface;
public class UpdateListRequestEvent extends RequestEvent implements EventInterface {
public class UpdateListRequestEvent extends RequestEvent {
private static final UUID uuid = UUID.fromString("5fa836ae-6b61-401c-96ac-d8396f07ec6b");
@LauncherNetworkAPI
public final HashSet<String> dirs;
@ -16,11 +16,6 @@ public UpdateListRequestEvent(HashSet<String> dirs) {
this.dirs = dirs;
}
@Override
public UUID getUUID() {
return uuid;
}
@Override
public String getType() {
return "updateList";

View file

@ -1,8 +0,0 @@
package pro.gravit.utils.event;
import java.util.UUID;
@FunctionalInterface
public interface EventHandler<T extends EventInterface> {
void run(UUID uuid, T event);
}

View file

@ -1,7 +0,0 @@
package pro.gravit.utils.event;
import java.util.UUID;
public interface EventInterface {
UUID getUUID();
}

View file

@ -1,102 +0,0 @@
package pro.gravit.utils.event;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import pro.gravit.utils.helper.CommonHelper;
import pro.gravit.utils.helper.LogHelper;
public class EventManager {
public static final int QUEUE_MAX_SIZE = 2048;
public static final int INITIAL_HANDLERS_SIZE = 16;
public class Entry {
public Entry(EventHandler<EventInterface> func, UUID[] events) {
this.func = func;
this.events = events;
}
EventHandler<EventInterface> func;
UUID[] events;
}
public class QueueEntry {
public QueueEntry(EventInterface event, UUID key) {
this.event = event;
this.key = key;
}
EventInterface event;
UUID key;
}
private EventExecutor executor;
private Thread executorThread;
private AtomicBoolean isStarted = new AtomicBoolean(false);
public synchronized void start() {
if (isStarted.get()) return;
executor = new EventExecutor();
isStarted.set(true);
executorThread = CommonHelper.newThread("EventExecutor", true, executor);
executorThread.start();
}
public synchronized void stop() {
if (!isStarted.get()) return;
executorThread.interrupt();
try {
executorThread.join();
} catch (InterruptedException ignored) {
}
}
public ArrayList<Entry> handlers = new ArrayList<>(INITIAL_HANDLERS_SIZE);
public BlockingQueue<QueueEntry> queue = new LinkedBlockingQueue<>(QUEUE_MAX_SIZE); //Максимальный размер очереди
public int registerHandler(EventHandler<EventInterface> func, UUID[] events) {
if (isStarted.get())
throw new IllegalThreadStateException("It is forbidden to add a handler during thread operation.");
Arrays.sort(events);
handlers.add(new Entry(func, events));
return handlers.size();
}
public void unregisterHandler(EventHandler<EventInterface> func) {
if (isStarted.get())
throw new IllegalThreadStateException("It is forbidden to remove a handler during thread operation.");
handlers.removeIf(e -> e.func.equals(func));
}
public void sendEvent(UUID key, EventInterface event, boolean blocking) {
if (blocking) process(key, event);
else queue.add(new QueueEntry(event, key));
}
public void process(UUID key, EventInterface event) {
for (Entry e : handlers) {
if (Arrays.binarySearch(e.events, key) >= 0) e.func.run(key, event);
}
}
public class EventExecutor implements Runnable {
public boolean enable = true;
@Override
public void run() {
while (enable && !Thread.interrupted()) {
try {
QueueEntry e = queue.take();
process(e.key, e.event);
} catch (InterruptedException e) {
LogHelper.error(e);
}
Thread.yield();
}
}
}
}