From 5c99ffd69a3aff7473fa164243f82921bdaa085b Mon Sep 17 00:00:00 2001 From: Gravit Date: Thu, 18 Oct 2018 20:25:57 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D0=B0=20?= =?UTF-8?q?=D1=81=D0=BE=D0=B1=D1=8B=D1=82=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/utils/event/EventHandler.java | 6 ++ .../ru/gravit/utils/event/EventInterface.java | 7 ++ .../ru/gravit/utils/event/EventManager.java | 70 +++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 libLauncher/src/main/java/ru/gravit/utils/event/EventHandler.java create mode 100644 libLauncher/src/main/java/ru/gravit/utils/event/EventInterface.java create mode 100644 libLauncher/src/main/java/ru/gravit/utils/event/EventManager.java diff --git a/libLauncher/src/main/java/ru/gravit/utils/event/EventHandler.java b/libLauncher/src/main/java/ru/gravit/utils/event/EventHandler.java new file mode 100644 index 00000000..9ab3b021 --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/event/EventHandler.java @@ -0,0 +1,6 @@ +package ru.gravit.utils.event; + +@FunctionalInterface +public interface EventHandler { + void run(EventInterface event); +} \ No newline at end of file diff --git a/libLauncher/src/main/java/ru/gravit/utils/event/EventInterface.java b/libLauncher/src/main/java/ru/gravit/utils/event/EventInterface.java new file mode 100644 index 00000000..4bc39c73 --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/event/EventInterface.java @@ -0,0 +1,7 @@ +package ru.gravit.utils.event; + +import java.util.UUID; + +public interface EventInterface { + UUID getUUID(); +} diff --git a/libLauncher/src/main/java/ru/gravit/utils/event/EventManager.java b/libLauncher/src/main/java/ru/gravit/utils/event/EventManager.java new file mode 100644 index 00000000..46cff5f1 --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/event/EventManager.java @@ -0,0 +1,70 @@ +package ru.gravit.utils.event; + +import ru.gravit.utils.helper.LogHelper; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.UUID; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +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 func, UUID[] events) { + this.func = func; + this.events = events; + } + + EventHandler func; + UUID[] events; + } + public class QueueEntry + { + public QueueEntry(EventInterface event, UUID key) { + this.event = event; + this.key = key; + } + + EventInterface event; + UUID key; + } + public ArrayList handlers = new ArrayList<>(INITIAL_HANDLERS_SIZE); + public BlockingQueue queue = new LinkedBlockingQueue<>(QUEUE_MAX_SIZE); //Максимальный размер очереди + public int registerHandler(EventHandler func, UUID[] events) + { + Arrays.sort(events); + handlers.add(new Entry(func,events)); + return handlers.size(); + } + 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(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(); + } + } + } +}