-
Notifications
You must be signed in to change notification settings - Fork 3
/
TestProgram.java
100 lines (84 loc) · 3.14 KB
/
TestProgram.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package de.vogel612.testclient_javabot;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import com.gmail.inverseconduit.AppContext;
import com.gmail.inverseconduit.bot.DefaultBot;
import com.gmail.inverseconduit.chat.ChatInterface;
import com.gmail.inverseconduit.commands.CommandHandle;
import com.gmail.inverseconduit.commands.sets.CoreBotCommands;
import de.vogel612.testclient_javabot.client.ClientGui;
import de.vogel612.testclient_javabot.core.TestingChatInterface;
/**
* Class responsible for making things know each other. This class is the core
* of the testclient. It's strongly oriented towards
* {@link com.gmail.inverseconduit.bot.Program JavaBot's Program}, and basically
* accomplishes the same tasks, just adapted to the needs of the testclient.
* This includes firing up the {@link DefaultBot actual Bot} in JavaBot and
* wiring it to the {@link TestingChatInterface}. Additionally the
* {@link ClientGui} is started here and the {@link CommandHandle Commands} are added to the bot.
*
* @author Vogel612<<a href="mailto:vogel612@gmx.de"
* >vogel612@gmx.de</a>>
*/
public class TestProgram {
private static final Logger LOGGER = Logger.getLogger(TestProgram.class.getName());
private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
private final DefaultBot bot;
private final ChatInterface chatInterface = new TestingChatInterface();
private final ClientGui gui;
public TestProgram(Stage stage) {
LOGGER.info("instantiating TestProgram class");
AppContext.INSTANCE.add(chatInterface);
bot = new DefaultBot(chatInterface);
gui = new ClientGui();
try {
gui.init(stage);
} catch (IOException e) {
throw new RuntimeException(e);
}
chatInterface.subscribe(bot);
chatInterface.subscribe(gui);
LOGGER.info("Basic component setup completed, beginning command glueing.");
new CoreBotCommands(chatInterface, bot).allCommands().forEach(bot::subscribe);
LOGGER.info("Glued Core Commands");
// Used to shut down the executor when the window is closed
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
dispose();
LOGGER.info("Junior Client - Bye Bye!");
}
});
}
public void startup() {
// fake the login??
scheduleQueryingThread();
bot.start();
gui.start();
LOGGER.info("Startup successfully completed");
}
private void scheduleQueryingThread() {
executor.scheduleAtFixedRate(() -> {
try {
chatInterface.queryMessages();
} catch(Exception e) {
Logger.getAnonymousLogger().severe("Exception in querying thread: " + e.getMessage());
e.printStackTrace();
}
}, 3, 1, TimeUnit.SECONDS);
Logger.getAnonymousLogger().info("querying thread started");
}
/**
* Shuts down the executor
*/
public void dispose() {
LOGGER.info("Shutting down Executor");
executor.shutdownNow();
}
}