X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Freactor%2Fengine%2FEngine.java;h=6f4376b7569f6088c95d1003907f4b47215dba26;hb=13a4fe6bece23b3dd561de657cf9bb7ea307e2b6;hp=7c50081629ac459b45938fdecbf051be74aef037;hpb=fb6252bcb9e3689b3320fb6a0ff2805fd2465e2a;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/reactor/engine/Engine.java b/src/main/java/net/pterodactylus/reactor/engine/Engine.java index 7c50081..6f4376b 100644 --- a/src/main/java/net/pterodactylus/reactor/engine/Engine.java +++ b/src/main/java/net/pterodactylus/reactor/engine/Engine.java @@ -47,12 +47,22 @@ public class Engine extends AbstractExecutionThreadService { private static final Logger logger = Logger.getLogger(Engine.class); /** The state manager. */ - private final StateManager stateManager = new StateManager("states"); + private final StateManager stateManager; /** All defined reactions. */ /* synchronize on itself. */ private final Map reactions = new HashMap(); + /** + * Creates a new engine. + * + * @param stateManager + * The state manager + */ + public Engine(StateManager stateManager) { + this.stateManager = stateManager; + } + // // ACCESSORS // @@ -64,15 +74,9 @@ public class Engine extends AbstractExecutionThreadService { * The name of the reaction * @param reaction * The reaction to add to this engine - * @throws IllegalStateException - * if the engine already contains a {@link Reaction} with the - * given name */ public void addReaction(String name, Reaction reaction) { synchronized (reactions) { - if (reactions.containsKey(name)) { - throw new IllegalStateException(String.format("Engine already contains a Reaction named “%s!”", name)); - } reactions.put(name, reaction); reactions.notifyAll(); } @@ -124,17 +128,17 @@ public class Engine extends AbstractExecutionThreadService { Reaction nextReaction; synchronized (reactions) { for (Entry reactionEntry : reactions.entrySet()) { - net.pterodactylus.reactor.State state = stateManager.loadState(reactionEntry.getKey()); + net.pterodactylus.reactor.State state = stateManager.loadLastState(reactionEntry.getKey()); long stateTime = (state != null) ? state.time() : 0; nextReactions.put(stateTime + reactionEntry.getValue().updateInterval(), Pair.of(reactionEntry.getKey(), reactionEntry.getValue())); } reactionName = nextReactions.get(nextReactions.firstKey()).getLeft(); nextReaction = nextReactions.get(nextReactions.firstKey()).getRight(); } - logger.debug(String.format("Next Reaction: %s.", nextReaction)); + logger.debug(String.format("Next Reaction: %s.", reactionName)); /* wait until the next reaction has to run. */ - net.pterodactylus.reactor.State lastState = stateManager.loadState(reactionName); + net.pterodactylus.reactor.State lastState = stateManager.loadLastState(reactionName); long lastStateTime = (lastState != null) ? lastState.time() : 0; int lastStateFailCount = (lastState != null) ? lastState.failCount() : 0; long waitTime = (lastStateTime + nextReaction.updateInterval()) - System.currentTimeMillis(); @@ -142,7 +146,7 @@ public class Engine extends AbstractExecutionThreadService { if (waitTime > 0) { synchronized (reactions) { try { - logger.debug(String.format("Waiting for %d milliseconds.", waitTime)); + logger.info(String.format("Waiting until %tc.", lastStateTime + nextReaction.updateInterval())); reactions.wait(waitTime); } catch (InterruptedException ie1) { /* we’re looping! */ @@ -183,21 +187,22 @@ public class Engine extends AbstractExecutionThreadService { if (!state.success()) { state.setFailCount(lastStateFailCount + 1); } + net.pterodactylus.reactor.State lastSuccessfulState = stateManager.loadLastSuccessfulState(reactionName); stateManager.saveState(reactionName, state); /* only run trigger if we have collected two successful states. */ Trigger trigger = nextReaction.trigger(); boolean triggerHit = false; - if ((lastState != null) && lastState.success() && state.success()) { + if ((lastSuccessfulState != null) && lastSuccessfulState.success() && state.success()) { logger.debug("Checking Trigger for changes..."); - triggerHit = trigger.triggers(state, lastState); + triggerHit = trigger.triggers(state, lastSuccessfulState); } /* run action if trigger was hit. */ logger.debug(String.format("Trigger was hit: %s.", triggerHit)); if (triggerHit) { logger.info("Executing Action..."); - nextReaction.action().execute(trigger.output()); + nextReaction.action().execute(trigger.output(nextReaction)); } }