diff --git a/.gitignore b/.gitignore index e48fa67..1a2da7a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ toast/log/ toast/groovy/ toast/preferences/ run/ +libs/groovy-2.4.1.jar diff --git a/build.gradle b/build.gradle index 9b0045d..797110b 100755 --- a/build.gradle +++ b/build.gradle @@ -58,6 +58,7 @@ repositories { } dependencies { + compile fileTree(dir: 'libs/', include: '*.jar') deployerJars 'org.apache.maven.wagon:wagon-ssh:2.2' } diff --git a/build.settings b/build.settings index 89e983a..2abe7ea 100644 --- a/build.settings +++ b/build.settings @@ -1 +1 @@ -toast.version=0.1.0a +toast.version=0.2.0 diff --git a/src/main/java/jaci/openrio/toast/core/Toast.java b/src/main/java/jaci/openrio/toast/core/Toast.java index b547643..1cad305 100644 --- a/src/main/java/jaci/openrio/toast/core/Toast.java +++ b/src/main/java/jaci/openrio/toast/core/Toast.java @@ -9,6 +9,7 @@ import jaci.openrio.toast.core.loader.groovy.GroovyLoader; import jaci.openrio.toast.core.loader.groovy.GroovyPreferences; import jaci.openrio.toast.core.monitoring.power.PDPMonitor; +import jaci.openrio.toast.core.shared.ModuleEventBus; import jaci.openrio.toast.lib.FRCHooks; import jaci.openrio.toast.lib.crash.CrashHandler; import jaci.openrio.toast.lib.log.Logger; diff --git a/src/main/java/jaci/openrio/toast/core/shared/ModuleEventBus.java b/src/main/java/jaci/openrio/toast/core/shared/ModuleEventBus.java new file mode 100644 index 0000000..7338c16 --- /dev/null +++ b/src/main/java/jaci/openrio/toast/core/shared/ModuleEventBus.java @@ -0,0 +1,52 @@ +package jaci.openrio.toast.core.shared; + +import java.util.ArrayList; + +/** + * The Module Event Bus is a bus that allows for Modules to raise events + * globally. This Event Bus allows for Modules to be optional dependencies, but + * still have methods invoked/listened for + * + * @author Jaci + */ +public class ModuleEventBus { + + static volatile ArrayList listeners = new ArrayList<>(); + + /** + * Register a {@link jaci.openrio.toast.core.shared.ModuleEventListener} to the + * bus. This is required for a class to be able to listen for events + */ + public static void registerListener(ModuleEventListener listener) { + listeners.add(listener); + } + + /** + * Raise an event to all listeners on the bus + * @param sender The sender of the event. This is usually your Module's name unless otherwise + * defined + * @param event_type The type of event to raise. This should be a unique identifier + * @param data The data passed to the event, or null if none are provided + */ + public static void raiseEvent(String sender, String event_type, Object... data) { + for (ModuleEventListener listener : listeners) + listener.onModuleEvent(sender, event_type, data); + } + + /** + * Raise an event to all listeners on the bus, in a new Thread + * @param sender The sender of the event. This is usually your Module's name unless otherwise + * defined + * @param event_type The type of event to raise. This should be a unique identifier + * @param data The data passed to the event, or null if none are provided + */ + public static void raiseConcurrentEvent(String sender, String event_type, Object... data) { + new Thread() { + public void run() { + for (ModuleEventListener listener : listeners) + listener.onModuleEvent(sender, event_type, data); + } + }.start(); + } + +} diff --git a/src/main/java/jaci/openrio/toast/core/shared/ModuleEventListener.java b/src/main/java/jaci/openrio/toast/core/shared/ModuleEventListener.java new file mode 100644 index 0000000..2260f7e --- /dev/null +++ b/src/main/java/jaci/openrio/toast/core/shared/ModuleEventListener.java @@ -0,0 +1,23 @@ +package jaci.openrio.toast.core.shared; + +/** + * Implement this on classes you wish to act as listeners on the {@link jaci.openrio.toast.core.shared.ModuleEventBus} + * + * These classes are simple listeners for the events broadcast to all modules. This allows for optional dependencies + * to trigger/listen for method invocations by other modules or Toast itself + * + * @author Jaci + */ +public interface ModuleEventListener { + + /** + * Invoked when an event is broadcast to this listener + * @param sender The sender of the event. This is usually the Name of a Module + * @param event_type The type of event that was raised. These are unique. See module + * documentation for more details + * @param data The data passed with the event. Keep in mind this can be null if no data + * was provided. See module documentation for more details + */ + public void onModuleEvent(String sender, String event_type, Object... data); + +}