7103dc58a1b91a2a940785d0a0fd13fc3caa3bc8
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / engine / Engine.java
1 /*
2  * Rhynodge - Engine.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.rhynodge.engine;
19
20 import static java.lang.System.currentTimeMillis;
21 import static java.util.concurrent.TimeUnit.MILLISECONDS;
22
23 import java.util.Map;
24 import java.util.Optional;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.Future;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.concurrent.ScheduledFuture;
29 import java.util.concurrent.ScheduledThreadPoolExecutor;
30
31 import net.pterodactylus.rhynodge.Reaction;
32 import net.pterodactylus.rhynodge.actions.EmailAction;
33 import net.pterodactylus.rhynodge.states.StateManager;
34
35 /**
36  * Rhynodge main engine.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class Engine {
41
42         private final StateManager stateManager;
43         private final ScheduledExecutorService executorService;
44         private final Map<String, Future<?>> scheduledFutures = new ConcurrentHashMap<>();
45         private final EmailAction errorEmailAction;
46
47         public Engine(StateManager stateManager, EmailAction errorEmailAction) {
48                 this.stateManager = stateManager;
49                 this.errorEmailAction = errorEmailAction;
50                 executorService = new ScheduledThreadPoolExecutor(10);
51         }
52
53         //
54         // ACCESSORS
55         //
56
57         /**
58          * Adds the given reaction to this engine.
59          *
60          * @param name
61          *              The name of the reaction
62          * @param reaction
63          *              The reaction to add to this engine
64          */
65         public void addReaction(String name, Reaction reaction) {
66                 ReactionState reactionState = new ReactionState(stateManager, name);
67                 Optional<net.pterodactylus.rhynodge.State> lastState = reactionState.loadLastState();
68                 long lastExecutionTime = lastState.map(net.pterodactylus.rhynodge.State::time).orElse(0L);
69                 long nextExecutionTime = lastExecutionTime + reaction.updateInterval();
70                 ReactionRunner reactionRunner = new ReactionRunner(reaction, reactionState, errorEmailAction);
71                 ScheduledFuture<?> future = executorService.scheduleWithFixedDelay(reactionRunner, nextExecutionTime - currentTimeMillis(), reaction.updateInterval(), MILLISECONDS);
72                 scheduledFutures.put(name, future);
73         }
74
75         /**
76          * Removes the reaction with the given name.
77          *
78          * @param name
79          *              The name of the reaction to remove
80          */
81         public void removeReaction(String name) {
82                 if (!scheduledFutures.containsKey(name)) {
83                         return;
84                 }
85                 scheduledFutures.remove(name).cancel(true);
86         }
87
88 }