Skip to content

Commit

Permalink
Create Module Event Bus
Browse files Browse the repository at this point in the history
  • Loading branch information
JaciBrunning committed Mar 10, 2015
1 parent 0279bb4 commit ad0be31
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ toast/log/
toast/groovy/
toast/preferences/
run/
libs/groovy-2.4.1.jar
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ repositories {
}

dependencies {
compile fileTree(dir: 'libs/', include: '*.jar')
deployerJars 'org.apache.maven.wagon:wagon-ssh:2.2'
}

Expand Down
2 changes: 1 addition & 1 deletion build.settings
Original file line number Diff line number Diff line change
@@ -1 +1 @@
toast.version=0.1.0a
toast.version=0.2.0
1 change: 1 addition & 0 deletions src/main/java/jaci/openrio/toast/core/Toast.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/jaci/openrio/toast/core/shared/ModuleEventBus.java
Original file line number Diff line number Diff line change
@@ -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<ModuleEventListener> 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();
}

}
Original file line number Diff line number Diff line change
@@ -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);

}

0 comments on commit ad0be31

Please sign in to comment.